Move some methods to StringUtils

pull/428/head
Alinson S. Xavier 8 years ago
parent 1ad06bcc15
commit 2904f3e2f8

@ -21,9 +21,9 @@ package org.isoron.uhabits.core.preferences;
import android.support.annotation.*; import android.support.annotation.*;
import org.apache.commons.lang3.*;
import org.isoron.uhabits.core.models.*; import org.isoron.uhabits.core.models.*;
import org.isoron.uhabits.core.ui.*; import org.isoron.uhabits.core.ui.*;
import org.isoron.uhabits.core.utils.*;
import java.util.*; import java.util.*;
@ -146,6 +146,11 @@ public class Preferences
return Long.parseLong(storage.getString("pref_snooze_interval", "15")); return Long.parseLong(storage.getString("pref_snooze_interval", "15"));
} }
public void setSnoozeInterval(int interval)
{
storage.putString("pref_snooze_interval", String.valueOf(interval));
}
public String getSyncAddress() public String getSyncAddress()
{ {
return storage.getString("pref_sync_address", DEFAULT_SYNC_SERVER); return storage.getString("pref_sync_address", DEFAULT_SYNC_SERVER);
@ -173,6 +178,12 @@ public class Preferences
return storage.getString("pref_sync_key", ""); return storage.getString("pref_sync_key", "");
} }
public void setSyncKey(String key)
{
storage.putString("pref_sync_key", key);
for (Listener l : listeners) l.onSyncFeatureChanged();
}
public int getTheme() public int getTheme()
{ {
return storage.getInt("pref_theme", ThemeSwitcher.THEME_LIGHT); return storage.getInt("pref_theme", ThemeSwitcher.THEME_LIGHT);
@ -218,11 +229,21 @@ public class Preferences
return storage.getBoolean("pref_feature_numerical_habits", false); return storage.getBoolean("pref_feature_numerical_habits", false);
} }
public void setNumericalHabitsFeatureEnabled(boolean enabled)
{
storage.putBoolean("pref_feature_numerical_habits", enabled);
}
public boolean isPureBlackEnabled() public boolean isPureBlackEnabled()
{ {
return storage.getBoolean("pref_pure_black", false); return storage.getBoolean("pref_pure_black", false);
} }
public void setPureBlackEnabled(boolean enabled)
{
storage.putBoolean("pref_pure_black", enabled);
}
public boolean isShortToggleEnabled() public boolean isShortToggleEnabled()
{ {
return storage.getBoolean("pref_short_toggle", false); return storage.getBoolean("pref_short_toggle", false);
@ -238,6 +259,12 @@ public class Preferences
return storage.getBoolean("pref_feature_sync", false); return storage.getBoolean("pref_feature_sync", false);
} }
public void setSyncEnabled(boolean isEnabled)
{
storage.putBoolean("pref_feature_sync", isEnabled);
for (Listener l : listeners) l.onSyncFeatureChanged();
}
public void removeListener(Listener listener) public void removeListener(Listener listener)
{ {
listeners.remove(listener); listeners.remove(listener);
@ -253,11 +280,6 @@ public class Preferences
storage.putInt("pref_default_habit_palette_color", color); storage.putInt("pref_default_habit_palette_color", color);
} }
public void setLastAppVersion(int version)
{
storage.putInt("last_version", version);
}
public void setNotificationsSticky(boolean sticky) public void setNotificationsSticky(boolean sticky)
{ {
storage.putBoolean("pref_sticky_notifications", sticky); storage.putBoolean("pref_sticky_notifications", sticky);
@ -270,19 +292,6 @@ public class Preferences
for (Listener l : listeners) l.onNotificationsChanged(); for (Listener l : listeners) l.onNotificationsChanged();
} }
public void setCheckmarkSequenceReversed(boolean reverse)
{
shouldReverseCheckmarks = reverse;
storage.putBoolean("pref_checkmark_reverse_order", reverse);
for (Listener l : listeners) l.onCheckmarkSequenceChanged();
}
public void setSyncEnabled(boolean isEnabled)
{
storage.putBoolean("pref_feature_sync", isEnabled);
for(Listener l : listeners) l.onSyncFeatureChanged();
}
public boolean shouldMakeNotificationsSticky() public boolean shouldMakeNotificationsSticky()
{ {
return storage.getBoolean("pref_sticky_notifications", false); return storage.getBoolean("pref_sticky_notifications", false);
@ -301,45 +310,42 @@ public class Preferences
return shouldReverseCheckmarks; return shouldReverseCheckmarks;
} }
public void setCheckmarkSequenceReversed(boolean reverse)
{
shouldReverseCheckmarks = reverse;
storage.putBoolean("pref_checkmark_reverse_order", reverse);
for (Listener l : listeners) l.onCheckmarkSequenceChanged();
}
public void updateLastHint(int number, Timestamp timestamp) public void updateLastHint(int number, Timestamp timestamp)
{ {
storage.putInt("last_hint_number", number); storage.putInt("last_hint_number", number);
storage.putLong("last_hint_timestamp", timestamp.getUnixTime()); storage.putLong("last_hint_timestamp", timestamp.getUnixTime());
} }
public void setSyncKey(String key) public int getLastAppVersion()
{ {
storage.putString("pref_sync_key", key); return storage.getInt("last_version", 0);
for(Listener l : listeners) l.onSyncFeatureChanged();
} }
public void setPureBlackEnabled(boolean enabled) public void setLastAppVersion(int version)
{ {
storage.putBoolean("pref_pure_black", enabled); storage.putInt("last_version", version);
} }
public int getLastAppVersion() public interface Listener
{ {
return storage.getInt("last_version", 0); default void onCheckmarkSequenceChanged()
}
public void setSnoozeInterval(int interval)
{ {
storage.putString("pref_snooze_interval", String.valueOf(interval));
} }
public void setNumericalHabitsFeatureEnabled(boolean enabled) default void onNotificationsChanged()
{ {
storage.putBoolean("pref_feature_numerical_habits", enabled);
} }
public interface Listener default void onSyncFeatureChanged()
{ {
default void onCheckmarkSequenceChanged() {} }
default void onNotificationsChanged() {}
default void onSyncFeatureChanged() {}
} }
public interface Storage public interface Storage
@ -366,16 +372,14 @@ public class Preferences
void remove(String key); void remove(String key);
default void putLongArray(String key, Long[] values) { default void putLongArray(String key, long[] values)
putString(key, StringUtils.join(values, ',')); {
putString(key, StringUtils.joinLongs(values));
} }
default Long[] getLongArray(String key) { default long[] getLongArray(String key)
String strNumbers = getString(key, ""); {
String parts[] = StringUtils.split(strNumbers, ','); return StringUtils.splitLongs(getString(key, ""));
Long numbers[] = new Long[parts.length];
for (int i = 0; i < parts.length; i++) numbers[i] = Long.valueOf(parts[i]);
return numbers;
} }
} }
} }

@ -28,23 +28,19 @@ import javax.inject.Inject;
public class WidgetPreferences { public class WidgetPreferences {
private Preferences.Storage storage; private Preferences.Storage storage;
public static final long STACK_WIDGET_HABITS = -1;
public static final long HABIT_NOT_FOUND = -2;
@Inject @Inject
public WidgetPreferences(Preferences.Storage storage) { public WidgetPreferences(Preferences.Storage storage) {
this.storage = storage; this.storage = storage;
} }
public void addWidget(int widgetId, long habitId) { public void addWidget(int widgetId, long habitIds[]) {
storage.putLong(getHabitIdKey(widgetId), habitId); storage.putLongArray(getHabitIdKey(widgetId), habitIds);
} }
public long getHabitIdFromWidgetId(int widgetId) { public long[] getHabitIdsFromWidgetId(int widgetId) {
Long habitId = storage.getLong(getHabitIdKey(widgetId), HABIT_NOT_FOUND); long habitIds[] = storage.getLongArray(getHabitIdKey(widgetId));
if (habitId == HABIT_NOT_FOUND) throw new HabitNotFoundException(); if (habitIds.length == 0) throw new HabitNotFoundException();
return habitIds;
return habitId;
} }
public void removeWidget(int id) { public void removeWidget(int id) {

@ -50,4 +50,18 @@ public class StringUtils
return toStringStyle; return toStringStyle;
} }
public static String joinLongs(long values[])
{
return org.apache.commons.lang3.StringUtils.join(values, ',');
}
public static long[] splitLongs(String str)
{
String parts[] = org.apache.commons.lang3.StringUtils.split(str, ',');
long numbers[] = new long[parts.length];
for (int i = 0; i < parts.length; i++) numbers[i] = Long.valueOf(parts[i]);
return numbers;
}
} }

@ -94,19 +94,19 @@ public class PropertiesStorageTest extends BaseUnitTest
@Test @Test
public void testLongArray() throws Exception public void testLongArray() throws Exception
{ {
Long[] expected1 = new Long[]{1L, 2L, 3L, 5L}; long[] expected1 = new long[]{1L, 2L, 3L, 5L};
Long[] expected2 = new Long[]{1L}; long[] expected2 = new long[]{1L};
Long[] expected3 = new Long[]{}; long[] expected3 = new long[]{};
Long[] expected4 = new Long[]{}; long[] expected4 = new long[]{};
storage.putLongArray("key1", expected1); storage.putLongArray("key1", expected1);
storage.putLongArray("key2", expected2); storage.putLongArray("key2", expected2);
storage.putLongArray("key3", expected3); storage.putLongArray("key3", expected3);
Long[] actual1 = storage.getLongArray("key1"); long[] actual1 = storage.getLongArray("key1");
Long[] actual2 = storage.getLongArray("key2"); long[] actual2 = storage.getLongArray("key2");
Long[] actual3 = storage.getLongArray("key3"); long[] actual3 = storage.getLongArray("key3");
Long[] actual4 = storage.getLongArray("invalidKey"); long[] actual4 = storage.getLongArray("invalidKey");
assertTrue(Arrays.equals(actual1, expected1)); assertTrue(Arrays.equals(actual1, expected1));
assertTrue(Arrays.equals(actual2, expected2)); assertTrue(Arrays.equals(actual2, expected2));

Loading…
Cancel
Save