Skip to content

Commit

Permalink
1.1.6: Added default prefs to namedPrefs if it has a prefsFileName
Browse files Browse the repository at this point in the history
  • Loading branch information
Inderjeet Singh committed Jul 30, 2018
1 parent f6d85dc commit 83b2395
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ repositories {
}
```

In your app build.gradle, add: `compile "com.github.PeelTechnologies:android-typesafe-prefs:1.1.5"`
In your app build.gradle, add: `compile "com.github.PeelTechnologies:android-typesafe-prefs:1.1.6"`

# User Guide
TypedKey can take arbitrarily complex Java object that Gson can serialize/deserialize. For example, `TypedKey<Customer>` may represent a class with nested fields for `Address`, name, phone numbers, etc.
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

<groupId>com.github.PeelTechnologies</groupId>
<artifactId>android-typesafe-prefs</artifactId>
<version>1.1.5</version>
<version>1.1.6</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/com/peel/prefs/SharedPrefs.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,16 @@ public synchronized static void init(Prefs defaultPrefs, Prefs... prefsList) {
Objects.requireNonNull(defaultPrefs);
SharedPrefs.defaultPrefs = defaultPrefs;
SharedPrefs.context = defaultPrefs.context();
String defaultPrefsFileName = defaultPrefs.getPrefsFileName(); // can be null
if (defaultPrefsFileName != null) {
namedPrefs.put(defaultPrefsFileName, defaultPrefs);
}
if (prefsList != null) {
String defaultPrefsFileName = defaultPrefs.getPrefsFileName(); // can be null
for (Prefs prefs : prefsList) {
String prefsFileName = prefs.getPrefsFileName();
if (prefsFileName == null) {
throw new IllegalArgumentException("Only defaultPrefs is allowed to have a null prefsFileName!");
}
if (prefsFileName.equals(defaultPrefsFileName)) {
throw new IllegalArgumentException(prefsFileName + " is already registered as the defaultPrefs!");
}
if (namedPrefs.containsKey(prefsFileName)) {
throw new IllegalArgumentException("Already added Prefs for " + prefsFileName + ". Duplicates not allowed.");
}
Expand Down Expand Up @@ -148,7 +148,8 @@ private static <T> Prefs prefs(TypedKey<T> key) {
return prefs(key.getPrefsFileName());
}

private static <T> Prefs prefs(String prefsFileName) {
// visible for testing only
static <T> Prefs prefs(String prefsFileName) {
Prefs prefs = prefsFileName == null ? defaultPrefs : namedPrefs.get(prefsFileName);
Objects.requireNonNull(prefs, prefsFileName + " not initialized before use!");
return prefs;
Expand Down
2 changes: 2 additions & 0 deletions src/test/java/com/peel/prefs/SharedPrefsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,12 @@ public void testInitWithNonNullDefaultPrefs() {
SharedPrefs.put(key1, "1");
assertEquals("1", SharedPrefs.get(key1));
assertEquals("1", SharedPrefs.get("key1", String.class)); // side effect, not really intended but works
assertEquals("1", SharedPrefs.prefs("my_props1").get(key1));

TypedKey<String> key2 = new TypedKey<>("key2", String.class, "my_props2", false);
SharedPrefs.put(key2, "2");
assertEquals("2", SharedPrefs.get(key2));

}

@Test
Expand Down

0 comments on commit 83b2395

Please sign in to comment.