mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 09:08:52 -06:00
Move some methods to StringUtils
This commit is contained in:
@@ -21,9 +21,9 @@ package org.isoron.uhabits.core.preferences;
|
||||
|
||||
import android.support.annotation.*;
|
||||
|
||||
import org.apache.commons.lang3.*;
|
||||
import org.isoron.uhabits.core.models.*;
|
||||
import org.isoron.uhabits.core.ui.*;
|
||||
import org.isoron.uhabits.core.utils.*;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
@@ -146,6 +146,11 @@ public class Preferences
|
||||
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()
|
||||
{
|
||||
return storage.getString("pref_sync_address", DEFAULT_SYNC_SERVER);
|
||||
@@ -173,6 +178,12 @@ public class Preferences
|
||||
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()
|
||||
{
|
||||
return storage.getInt("pref_theme", ThemeSwitcher.THEME_LIGHT);
|
||||
@@ -218,11 +229,21 @@ public class Preferences
|
||||
return storage.getBoolean("pref_feature_numerical_habits", false);
|
||||
}
|
||||
|
||||
public void setNumericalHabitsFeatureEnabled(boolean enabled)
|
||||
{
|
||||
storage.putBoolean("pref_feature_numerical_habits", enabled);
|
||||
}
|
||||
|
||||
public boolean isPureBlackEnabled()
|
||||
{
|
||||
return storage.getBoolean("pref_pure_black", false);
|
||||
}
|
||||
|
||||
public void setPureBlackEnabled(boolean enabled)
|
||||
{
|
||||
storage.putBoolean("pref_pure_black", enabled);
|
||||
}
|
||||
|
||||
public boolean isShortToggleEnabled()
|
||||
{
|
||||
return storage.getBoolean("pref_short_toggle", false);
|
||||
@@ -238,6 +259,12 @@ public class Preferences
|
||||
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)
|
||||
{
|
||||
listeners.remove(listener);
|
||||
@@ -253,11 +280,6 @@ public class Preferences
|
||||
storage.putInt("pref_default_habit_palette_color", color);
|
||||
}
|
||||
|
||||
public void setLastAppVersion(int version)
|
||||
{
|
||||
storage.putInt("last_version", version);
|
||||
}
|
||||
|
||||
public void setNotificationsSticky(boolean sticky)
|
||||
{
|
||||
storage.putBoolean("pref_sticky_notifications", sticky);
|
||||
@@ -270,19 +292,6 @@ public class Preferences
|
||||
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()
|
||||
{
|
||||
return storage.getBoolean("pref_sticky_notifications", false);
|
||||
@@ -301,45 +310,42 @@ public class Preferences
|
||||
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)
|
||||
{
|
||||
storage.putInt("last_hint_number", number);
|
||||
storage.putLong("last_hint_timestamp", timestamp.getUnixTime());
|
||||
}
|
||||
|
||||
public void setSyncKey(String key)
|
||||
{
|
||||
storage.putString("pref_sync_key", key);
|
||||
for(Listener l : listeners) l.onSyncFeatureChanged();
|
||||
}
|
||||
|
||||
public void setPureBlackEnabled(boolean enabled)
|
||||
{
|
||||
storage.putBoolean("pref_pure_black", enabled);
|
||||
}
|
||||
|
||||
public int getLastAppVersion()
|
||||
{
|
||||
return storage.getInt("last_version", 0);
|
||||
}
|
||||
|
||||
public void setSnoozeInterval(int interval)
|
||||
public void setLastAppVersion(int version)
|
||||
{
|
||||
storage.putString("pref_snooze_interval", String.valueOf(interval));
|
||||
}
|
||||
|
||||
public void setNumericalHabitsFeatureEnabled(boolean enabled)
|
||||
{
|
||||
storage.putBoolean("pref_feature_numerical_habits", enabled);
|
||||
storage.putInt("last_version", version);
|
||||
}
|
||||
|
||||
public interface Listener
|
||||
{
|
||||
default void onCheckmarkSequenceChanged() {}
|
||||
default void onCheckmarkSequenceChanged()
|
||||
{
|
||||
}
|
||||
|
||||
default void onNotificationsChanged() {}
|
||||
default void onNotificationsChanged()
|
||||
{
|
||||
}
|
||||
|
||||
default void onSyncFeatureChanged() {}
|
||||
default void onSyncFeatureChanged()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
public interface Storage
|
||||
@@ -366,16 +372,14 @@ public class Preferences
|
||||
|
||||
void remove(String key);
|
||||
|
||||
default void putLongArray(String key, Long[] values) {
|
||||
putString(key, StringUtils.join(values, ','));
|
||||
default void putLongArray(String key, long[] values)
|
||||
{
|
||||
putString(key, StringUtils.joinLongs(values));
|
||||
}
|
||||
|
||||
default Long[] getLongArray(String key) {
|
||||
String strNumbers = getString(key, "");
|
||||
String parts[] = StringUtils.split(strNumbers, ',');
|
||||
Long numbers[] = new Long[parts.length];
|
||||
for (int i = 0; i < parts.length; i++) numbers[i] = Long.valueOf(parts[i]);
|
||||
return numbers;
|
||||
default long[] getLongArray(String key)
|
||||
{
|
||||
return StringUtils.splitLongs(getString(key, ""));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,23 +28,19 @@ import javax.inject.Inject;
|
||||
public class WidgetPreferences {
|
||||
private Preferences.Storage storage;
|
||||
|
||||
public static final long STACK_WIDGET_HABITS = -1;
|
||||
public static final long HABIT_NOT_FOUND = -2;
|
||||
|
||||
@Inject
|
||||
public WidgetPreferences(Preferences.Storage storage) {
|
||||
this.storage = storage;
|
||||
}
|
||||
|
||||
public void addWidget(int widgetId, long habitId) {
|
||||
storage.putLong(getHabitIdKey(widgetId), habitId);
|
||||
public void addWidget(int widgetId, long habitIds[]) {
|
||||
storage.putLongArray(getHabitIdKey(widgetId), habitIds);
|
||||
}
|
||||
|
||||
public long getHabitIdFromWidgetId(int widgetId) {
|
||||
Long habitId = storage.getLong(getHabitIdKey(widgetId), HABIT_NOT_FOUND);
|
||||
if (habitId == HABIT_NOT_FOUND) throw new HabitNotFoundException();
|
||||
|
||||
return habitId;
|
||||
public long[] getHabitIdsFromWidgetId(int widgetId) {
|
||||
long habitIds[] = storage.getLongArray(getHabitIdKey(widgetId));
|
||||
if (habitIds.length == 0) throw new HabitNotFoundException();
|
||||
return habitIds;
|
||||
}
|
||||
|
||||
public void removeWidget(int id) {
|
||||
|
||||
@@ -50,4 +50,18 @@ public class StringUtils
|
||||
|
||||
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
|
||||
public void testLongArray() throws Exception
|
||||
{
|
||||
Long[] expected1 = new Long[]{1L, 2L, 3L, 5L};
|
||||
Long[] expected2 = new Long[]{1L};
|
||||
Long[] expected3 = new Long[]{};
|
||||
Long[] expected4 = new Long[]{};
|
||||
long[] expected1 = new long[]{1L, 2L, 3L, 5L};
|
||||
long[] expected2 = new long[]{1L};
|
||||
long[] expected3 = new long[]{};
|
||||
long[] expected4 = new long[]{};
|
||||
|
||||
storage.putLongArray("key1", expected1);
|
||||
storage.putLongArray("key2", expected2);
|
||||
storage.putLongArray("key3", expected3);
|
||||
|
||||
Long[] actual1 = storage.getLongArray("key1");
|
||||
Long[] actual2 = storage.getLongArray("key2");
|
||||
Long[] actual3 = storage.getLongArray("key3");
|
||||
Long[] actual4 = storage.getLongArray("invalidKey");
|
||||
long[] actual1 = storage.getLongArray("key1");
|
||||
long[] actual2 = storage.getLongArray("key2");
|
||||
long[] actual3 = storage.getLongArray("key3");
|
||||
long[] actual4 = storage.getLongArray("invalidKey");
|
||||
|
||||
assertTrue(Arrays.equals(actual1, expected1));
|
||||
assertTrue(Arrays.equals(actual2, expected2));
|
||||
|
||||
Reference in New Issue
Block a user