Skip to content

Commit

Permalink
Updated TinyDB
Browse files Browse the repository at this point in the history
  • Loading branch information
d3rt0xx authored Aug 15, 2022
1 parent 9bc6329 commit 3685dd9
Showing 1 changed file with 48 additions and 56 deletions.
104 changes: 48 additions & 56 deletions app/src/main/java/app/d3rt0xx/flyffdroid/TinyDB.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,26 @@ public TinyDB(Context appContext) {
context = appContext;
}

/**
* Check if external storage is writable or not
*
* @return true if writable, false otherwise
*/
public static boolean isExternalStorageWritable() {
return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState());
}

/**
* Check if external storage is readable or not
*
* @return true if readable, false otherwise
*/
public static boolean isExternalStorageReadable() {
String state = Environment.getExternalStorageState();

return Environment.MEDIA_MOUNTED.equals(state) ||
Environment.MEDIA_MOUNTED_READ_ONLY.equals(state);
}

/**
* Decodes the Bitmap from 'path' and returns it
Expand All @@ -73,7 +93,6 @@ public Bitmap getImage(String path) {
return bitmapFromPath;
}


/**
* Returns the String path of the last saved image
*
Expand All @@ -83,7 +102,6 @@ public String getSavedImagePath() {
return lastImagePath;
}


/**
* Saves 'theBitmap' into folder 'theFolder' with the name 'theImageName'
*
Expand All @@ -107,7 +125,6 @@ public String putImage(String theFolder, String theImageName, Bitmap theBitmap)
return mFullPath;
}


/**
* Saves 'theBitmap' into 'fullPath'
*
Expand All @@ -119,6 +136,8 @@ public boolean putImageWithFullPath(String fullPath, Bitmap theBitmap) {
return !(fullPath == null || theBitmap == null) && saveBitmap(fullPath, theBitmap);
}

// Getters

/**
* Creates the path for the image with name 'imageName' in DEFAULT_APP.. directory
*
Expand Down Expand Up @@ -192,8 +211,6 @@ private boolean saveBitmap(String fullPath, Bitmap bitmap) {
return (fileCreated && bitmapCompressed && streamClosed);
}

// Getters

/**
* Get int value from SharedPreferences at 'key'. If key not found, return 0
*
Expand Down Expand Up @@ -344,32 +361,30 @@ public ArrayList<Boolean> getListBoolean(String key) {
}


public ArrayList<Object> getListObject(String key, Class<?> mClass){
Gson gson = new Gson();
// Put methods

ArrayList<String> objStrings = getListString(key);
ArrayList<Object> objects = new ArrayList<Object>();
public ArrayList<Object> getListObject(String key, Class<?> mClass) {
Gson gson = new Gson();

for(String jObjString : objStrings){
Object value = gson.fromJson(jObjString, mClass);
objects.add(value);
}
return objects;
}
ArrayList<String> objStrings = getListString(key);
ArrayList<Object> objects = new ArrayList<Object>();

for (String jObjString : objStrings) {
Object value = gson.fromJson(jObjString, mClass);
objects.add(value);
}
return objects;
}

public <T> T getObject(String key, Class<T> classOfT){
public <T> T getObject(String key, Class<T> classOfT) {

String json = getString(key);
Object value = new Gson().fromJson(json, classOfT);
if (value == null)
throw new NullPointerException();
return (T)value;
return (T) value;
}


// Put methods

/**
* Put int value into SharedPreferences with 'key' and save
*
Expand Down Expand Up @@ -508,23 +523,24 @@ public void putListBoolean(String key, ArrayList<Boolean> boolList) {

/**
* Put ObJect any type into SharedPrefrences with 'key' and save
*
* @param key SharedPreferences key
* @param obj is the Object you want to put
*/
public void putObject(String key, Object obj){
checkForNullKey(key);
Gson gson = new Gson();
putString(key, gson.toJson(obj));
public void putObject(String key, Object obj) {
checkForNullKey(key);
Gson gson = new Gson();
putString(key, gson.toJson(obj));
}

public void putListObject(String key, ArrayList<Object> objArray){
checkForNullKey(key);
Gson gson = new Gson();
ArrayList<String> objStrings = new ArrayList<String>();
for(Object obj : objArray){
objStrings.add(gson.toJson(obj));
}
putListString(key, objStrings);
public void putListObject(String key, ArrayList<Object> objArray) {
checkForNullKey(key);
Gson gson = new Gson();
ArrayList<String> objStrings = new ArrayList<String>();
for (Object obj : objArray) {
objStrings.add(gson.toJson(obj));
}
putListString(key, objStrings);
}

/**
Expand All @@ -546,7 +562,6 @@ public boolean deleteImage(String path) {
return new File(path).delete();
}


/**
* Clear SharedPreferences (remove everything)
*/
Expand All @@ -563,7 +578,6 @@ public void clear() {
return preferences.getAll();
}


/**
* Register SharedPreferences change listener
*
Expand All @@ -586,28 +600,6 @@ public void unregisterOnSharedPreferenceChangeListener(
preferences.unregisterOnSharedPreferenceChangeListener(listener);
}


/**
* Check if external storage is writable or not
*
* @return true if writable, false otherwise
*/
public static boolean isExternalStorageWritable() {
return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState());
}

/**
* Check if external storage is readable or not
*
* @return true if readable, false otherwise
*/
public static boolean isExternalStorageReadable() {
String state = Environment.getExternalStorageState();

return Environment.MEDIA_MOUNTED.equals(state) ||
Environment.MEDIA_MOUNTED_READ_ONLY.equals(state);
}

/**
* Checks if an object exists in Memory
*
Expand Down

0 comments on commit 3685dd9

Please sign in to comment.