Skip to content

Commit

Permalink
1.1.0: Added support for tags
Browse files Browse the repository at this point in the history
  • Loading branch information
Inderjeet Singh committed Jul 12, 2018
1 parent 3a0a655 commit d4731c0
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 12 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.0.9"`
In your app build.gradle, add: `compile "com.github.PeelTechnologies:android-typesafe-prefs:1.1.0"`

# User Guide
PrefsKey can take arbitrarily complex Java object that Gson can serialize/deserialize. For example, `PrefsKey<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.0.9</version>
<version>1.1.0</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down
24 changes: 16 additions & 8 deletions src/main/java/com/peel/prefs/Prefs.java
Original file line number Diff line number Diff line change
Expand Up @@ -220,10 +220,14 @@ private <T> void putInternal(String name, Type type, T value, boolean cacheable)
*/
public <T> void remove(PrefsKey<T> key) {
String keyName = key.getName();
cache.remove(keyName);
boolean wasPresent = cache.get(keyName) != null;
SharedPreferences prefs = getPrefs();
prefs.edit().remove(keyName).apply();
for (EventListener listener : listeners) listener.onRemove(key);
wasPresent = wasPresent || prefs.contains(keyName);
if (wasPresent) {
cache.remove(keyName);
prefs.edit().remove(keyName).apply();
for (EventListener listener : listeners) listener.onRemove(key);
}
}

/**
Expand All @@ -232,12 +236,16 @@ public <T> void remove(PrefsKey<T> key) {
* @param key the key that was previously bound as an instance. If the key was not bound previously, nothing is done
*/
public <T> void remove(String keyName, Class<T> keyClass) {
cache.remove(keyName);
boolean wasPresent = cache.get(keyName) != null;
SharedPreferences prefs = getPrefs();
prefs.edit().remove(keyName).apply();
if (!listeners.isEmpty()) {
PrefsKey<T> key = new PrefsKey<>(keyName, keyClass, false);
for (EventListener listener : listeners) listener.onRemove(key);
wasPresent = wasPresent || prefs.contains(keyName);
if (wasPresent) {
cache.remove(keyName);
prefs.edit().remove(keyName).apply();
if (!listeners.isEmpty()) {
PrefsKey<T> key = new PrefsKey<>(keyName, keyClass, false);
for (EventListener listener : listeners) listener.onRemove(key);
}
}
}

Expand Down
26 changes: 24 additions & 2 deletions src/main/java/com/peel/prefs/PrefsKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public class PrefsKey<T> {
private final String name;
private final Type type;
private final boolean cacheableInMemory;
private final String[] tags;

/**
* @param name Ensure that this name is Unique.
Expand All @@ -40,10 +41,15 @@ public PrefsKey(String name, Class<T> clazz) {
this(name, clazz, true);
}

public PrefsKey(String name, Class<T> clazz, boolean cacheableInMemory) {
public PrefsKey(String name, Class<T> clazz, String... tags) {
this(name, clazz, true, tags);
}

public PrefsKey(String name, Class<T> clazz, boolean cacheableInMemory, String... tags) {
this.name = name;
this.type = clazz;
this.cacheableInMemory = cacheableInMemory;
this.tags = tags;
}

/**
Expand All @@ -53,16 +59,22 @@ public PrefsKey(String name, Class<T> clazz, boolean cacheableInMemory) {
public PrefsKey(String name, TypeToken<T> type) {
this(name, type, true);
}

public PrefsKey(String name, TypeToken<T> type, String... tags) {
this(name, type, true, tags);
}

/**
* @param name Ensure that this name is unique across the preference file
* @param type the type for this key
* @param cacheableInMemory Whether this key can be stored in an in-memory cache
* for faster access. If false, the key is loaded form disk on every access
*/
public PrefsKey(String name, TypeToken<T> type, boolean cacheableInMemory) {
public PrefsKey(String name, TypeToken<T> type, boolean cacheableInMemory, String... tags) {
this.name = name;
this.type = type.getType();
this.cacheableInMemory = cacheableInMemory;
this.tags = tags;
}

public String getName() {
Expand All @@ -77,6 +89,16 @@ public boolean isCacheableInMemory() {
return cacheableInMemory;
}

public boolean containsTag(String tagName) {
if (tags == null) return false;
for (String tag : tags) {
if (tag.equals(tagName)) {
return true;
}
}
return false;
}

@Override
public int hashCode() {
return name.hashCode();
Expand Down

0 comments on commit d4731c0

Please sign in to comment.