Skip to content

Commit

Permalink
preftils + support for default param + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
HubbleCommand committed Sep 21, 2024
1 parent 8290363 commit 487ea41
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@ class InstrumentedTest {
assertEquals(target, set)
}

@Test
fun testDefaults() {
with (preferences.edit()) {
remove(PreferencesTest.NUMBER.key)
apply()
}
assertEquals(PreferencesTest.NUMBER.default, preferences.get(PreferencesTest.NUMBER))
assertEquals(-3, preferences.get(PreferencesTest.NUMBER, -3))
}

@Test
fun testIncompatibleType() {
val invalidPreferenceType = Preference("invalid", 20u)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ public static void setup(){
preferences = InstrumentationRegistry.getInstrumentation().getTargetContext().getSharedPreferences("prefs", Context.MODE_PRIVATE);
editor = preferences.edit();

clear();
}

public static void clear() {
editor.remove(BasicPreferences.NUMBER.getKey());
editor.apply();
editor.commit();
Expand All @@ -46,6 +50,13 @@ public void testJavaBasic() throws IOException, ClassNotFoundException {
PreferenceUtils.put(editor, BasicPreferences.NUMBER, 10);
}

@Test
public void testJavaDefaults() throws IOException, ClassNotFoundException {
clear();
assertEquals(BasicPreferences.NUMBER.getDefault(), PreferenceUtils.get(preferences, BasicPreferences.NUMBER));
assertEquals(-3, PreferenceUtils.get(preferences, BasicPreferences.NUMBER, -3).intValue());
}

public static class SerializableType implements Serializable {
SerializableType(int integer, String string) {
this.integer = integer;
Expand Down
23 changes: 16 additions & 7 deletions preftils/src/main/java/com/hubble/preftils/PreferenceUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,22 @@ public void putValue(SharedPreferences.Editor editor, @NotNull T value) throws I
}
}

/**
* Utility wrapper that uses the Preference's defined default value
* @see PreferenceUtils#get(SharedPreferences, Preference, T)
*/
static <T extends Serializable> T get(SharedPreferences preferences, Preference<T> preference) throws IOException, ClassNotFoundException {
return get(preferences, preference, preference.getDefault());
}

/**
* Retrieves the value of a preference in a type-safe way
* <p>
* Propagates errors thrown by ObjectInputStream.readObject() if decoding is impossible
*
* @param preferences instance of SharedPreferences to read from
* @param preference the preference to read
* @param defaultValue the default value to return if no value was found for the key
* @return the value retrieved in SharedPreferences, or the default value of the [preference] parameter
*
* @throws ClassNotFoundException Class of a serialized object cannot be
Expand All @@ -68,22 +77,22 @@ public void putValue(SharedPreferences.Editor editor, @NotNull T value) throws I
* @see ObjectInputStream#readObject()
*/
@SuppressWarnings("unchecked")
static <T extends Serializable> T get(SharedPreferences preferences, Preference<T> preference) throws IOException, ClassNotFoundException {
static <T extends Serializable> T get(SharedPreferences preferences, Preference<T> preference, T defaultValue) throws IOException, ClassNotFoundException {
if (preference.getDefault() instanceof Integer) {
return (T) (Integer) preferences.getInt(preference.getKey(), (Integer) preference.getDefault());
return (T) (Integer) preferences.getInt(preference.getKey(), (Integer) defaultValue);
} else if (preference.getDefault() instanceof Long) {
return (T) (Long) preferences.getLong(preference.getKey(), (Long) preference.getDefault());
return (T) (Long) preferences.getLong(preference.getKey(), (Long) defaultValue);
} else if (preference.getDefault() instanceof Float) {
return (T) (Float) preferences.getFloat(preference.getKey(), (Float) preference.getDefault());
return (T) (Float) preferences.getFloat(preference.getKey(), (Float) defaultValue);
} else if (preference.getDefault() instanceof Boolean) {
return (T) (Boolean) preferences.getBoolean(preference.getKey(), (Boolean) preference.getDefault());
return (T) (Boolean) preferences.getBoolean(preference.getKey(), (Boolean) defaultValue);
} else if (preference.getDefault() instanceof String) {
return (T) preferences.getString(preference.getKey(), (String) preference.getDefault());
return (T) preferences.getString(preference.getKey(), (String) defaultValue);
} else {
String string = preferences.getString(preference.getKey(), null);

if (string == null) {
return preference.getDefault();
return defaultValue;
}

ByteArrayInputStream inStream = new ByteArrayInputStream(string.getBytes());
Expand Down
14 changes: 7 additions & 7 deletions preftils/src/main/java/com/hubble/preftils/PreferenceUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@ data class Preference<T: Any>(val key: String, val default: T)
* @throws SerializationException in case of any decoding-specific error
* @throws IllegalArgumentException if the decoded input is not a valid instance of [T]
*/
inline fun <reified T: Any> SharedPreferences.get(preference: Preference<T>): T {
inline fun <reified T: Any> SharedPreferences.get(preference: Preference<T>, default: T = preference.default): T {
return when (preference.default) {
//Only Preferences type not supported is StringSet
is String -> this.getString(preference.key, preference.default) as T
is Int -> this.getInt(preference.key, preference.default) as T
is Long -> this.getLong(preference.key, preference.default) as T
is Boolean -> this.getBoolean(preference.key, preference.default) as T
is Float -> this.getFloat(preference.key, preference.default) as T
is String -> this.getString(preference.key, default as String) as T
is Int -> this.getInt(preference.key, default as Int) as T
is Long -> this.getLong(preference.key, default as Long) as T
is Boolean -> this.getBoolean(preference.key, default as Boolean) as T
is Float -> this.getFloat(preference.key, default as Float) as T
else -> {
if (preference.default::class.java.isAnnotationPresent(Serializable::class.java)) {
val str = this.getString(preference.key, null) ?: return preference.default
val str = this.getString(preference.key, null) ?: return default
return Json.decodeFromString<T>(str)
}

Expand Down

0 comments on commit 487ea41

Please sign in to comment.