-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from fs/add-default-storage-abstraction
Add default storage implementation
- Loading branch information
Showing
5 changed files
with
209 additions
and
40 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
25 changes: 0 additions & 25 deletions
25
app/src/main/java/com/flatstack/android/utils/HomeAsUp.java
This file was deleted.
Oops, something went wrong.
40 changes: 40 additions & 0 deletions
40
app/src/main/java/com/flatstack/android/utils/storage/IStorage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.flatstack.android.utils.storage; | ||
|
||
import android.support.annotation.NonNull; | ||
import android.support.annotation.Nullable; | ||
|
||
import java.lang.reflect.Type; | ||
import java.util.List; | ||
|
||
/** | ||
* Created by ereminilya on 8/1/17. | ||
*/ | ||
|
||
public interface IStorage { | ||
|
||
@Nullable <T> T get(@NonNull String key, @NonNull Type type); | ||
|
||
<T> void put(@NonNull String key, @NonNull T item); | ||
|
||
void putString(@NonNull final String key, @NonNull String str); | ||
|
||
@Nullable String getString(@NonNull String key); | ||
|
||
void putLong(@NonNull String key, long number); | ||
|
||
long getLong(@NonNull String key, long defaultValue); | ||
|
||
void putInt(@NonNull String key, int number); | ||
|
||
int getInt(@NonNull String key, int defaultValue); | ||
|
||
void putBoolean(@NonNull String key, boolean value); | ||
|
||
boolean getBoolean(@NonNull String key, boolean defaultValue); | ||
|
||
void remove(@NonNull String key); | ||
|
||
<T> void putCollection(@NonNull String key, @NonNull List<T> items); | ||
|
||
@Nullable <T> List<T> getCollection(@NonNull String key, @NonNull Type type); | ||
} |
83 changes: 83 additions & 0 deletions
83
app/src/main/java/com/flatstack/android/utils/storage/Storage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package com.flatstack.android.utils.storage; | ||
|
||
import android.content.SharedPreferences; | ||
import android.support.annotation.NonNull; | ||
import android.support.annotation.Nullable; | ||
|
||
import com.google.gson.Gson; | ||
|
||
import java.lang.reflect.Type; | ||
import java.util.List; | ||
|
||
/** | ||
* Created by ereminilya on 8/1/17. | ||
*/ | ||
|
||
public class Storage implements IStorage { | ||
|
||
private final SharedPreferences sp; | ||
private final Gson gson; | ||
|
||
public Storage(SharedPreferences sp, Gson gson) { | ||
this.sp = sp; | ||
this.gson = gson; | ||
} | ||
|
||
@Override | ||
@Nullable public <T> T get(@NonNull String key, @NonNull Type type) { | ||
String json = sp.getString(key, ""); | ||
if ("".equals(json)) return null; | ||
else { | ||
return gson.fromJson(json, type); | ||
} | ||
} | ||
|
||
@Override public void put(@NonNull String key, @NonNull Object items) { | ||
sp.edit().putString(key, gson.toJson(items)).apply(); | ||
} | ||
|
||
@Override public void putString(@NonNull String key, @NonNull String str) { | ||
sp.edit().putString(key, str).apply(); | ||
} | ||
|
||
@Nullable @Override public String getString(@NonNull String key) { | ||
return sp.getString(key, null); | ||
} | ||
|
||
@Override public void putLong(@NonNull String key, long number) { | ||
sp.edit().putLong(key, number).apply(); | ||
} | ||
|
||
@Override public long getLong(@NonNull String key, long defaultValue) { | ||
return sp.getLong(key, defaultValue); | ||
} | ||
|
||
@Override public void putInt(@NonNull String key, int number) { | ||
sp.edit().putInt(key, number).apply(); | ||
} | ||
|
||
@Override public int getInt(@NonNull String key, int defaultValue) { | ||
return sp.getInt(key, defaultValue); | ||
} | ||
|
||
@Override public void putBoolean(@NonNull String key, boolean value) { | ||
sp.edit().putBoolean(key, value).apply(); | ||
} | ||
|
||
@Override public boolean getBoolean(@NonNull String key, boolean defaultValue) { | ||
return sp.getBoolean(key, defaultValue); | ||
} | ||
|
||
@Override public void remove(@NonNull String key) { | ||
sp.edit().remove(key).apply(); | ||
} | ||
|
||
public <T> void putCollection(@NonNull String key, @NonNull List<T> items) { | ||
put(key, items); | ||
} | ||
|
||
@Override public <T> List<T> getCollection(@NonNull String key, @NonNull Type type) { | ||
return get(key, type); | ||
} | ||
|
||
} |
71 changes: 71 additions & 0 deletions
71
app/src/test/java/com/flatstack/android/utils/storage/RuntimeStorage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package com.flatstack.android.utils.storage; | ||
|
||
import android.support.annotation.NonNull; | ||
import android.support.annotation.Nullable; | ||
|
||
import java.lang.reflect.Type; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class RuntimeStorage implements IStorage { | ||
|
||
private Map<String, Object> map = new HashMap<>(); | ||
|
||
@Override public <T> void put(@NonNull String key, @NonNull T item) { | ||
map.put(key, item); | ||
} | ||
|
||
@Nullable @Override | ||
public <T> T get(@NonNull String key, @NonNull Type clazz) { | ||
return (T) map.get(key); | ||
} | ||
|
||
@Override public void putLong(@NonNull String key, long number) { | ||
map.put(key, Long.valueOf(number)); | ||
} | ||
|
||
@Override public long getLong(@NonNull String key, long defaultValue) { | ||
Object o = map.get(key); | ||
return o != null ? (long) o : defaultValue; | ||
} | ||
|
||
@Override public void putInt(@NonNull String key, int number) { | ||
map.put(key, Integer.valueOf(number)); | ||
} | ||
|
||
@Override public int getInt(@NonNull String key, int defaultValue) { | ||
Object o = map.get(key); | ||
return o != null ? (int) o : defaultValue; | ||
} | ||
|
||
@Override public void putBoolean(@NonNull String key, boolean value) { | ||
map.put(key, Boolean.valueOf(value)); | ||
} | ||
|
||
@Override public boolean getBoolean(@NonNull String key, boolean defaultValue) { | ||
Object o = map.get(key); | ||
return o != null ? (boolean) o : defaultValue; | ||
} | ||
|
||
@Override public void putString(@NonNull String key, @NonNull String str) { | ||
map.put(key, str); | ||
} | ||
|
||
@Override public String getString(@NonNull String key) { | ||
Object o = map.get(key); | ||
return (o != null) ? (String) o : null; | ||
} | ||
|
||
@Override public void remove(@NonNull String key) { | ||
map.remove(key); | ||
} | ||
|
||
@Override public <T> void putCollection(@NonNull String key, @NonNull List<T> items) { | ||
map.put(key, items); | ||
} | ||
|
||
@Override public <T> List<T> getCollection(@NonNull String key, @NonNull Type type) { | ||
return (List<T>) map.get(key); | ||
} | ||
} |