From 7d6a92b5a8471c64f8d90736c39808d1e5f86079 Mon Sep 17 00:00:00 2001 From: qwerty287 Date: Wed, 4 May 2022 19:02:56 +0200 Subject: [PATCH] TinyDB cleanups (#1122) * never save a string from settings, fetch this from the list * remove some unused methods which are * not native (saving `double` values) or * we should not use (saving `List`s or `Object`s * fix some Javadoc comments Co-authored-by: qwerty287 Reviewed-on: https://codeberg.org/gitnex/GitNex/pulls/1122 Reviewed-by: M M Arif Co-authored-by: qwerty287 Co-committed-by: qwerty287 --- .../activities/CreateLabelActivity.java | 3 - .../SettingsAppearanceActivity.java | 4 +- .../java/org/mian/gitnex/helpers/TinyDB.java | 231 +----------------- 3 files changed, 5 insertions(+), 233 deletions(-) diff --git a/app/src/main/java/org/mian/gitnex/activities/CreateLabelActivity.java b/app/src/main/java/org/mian/gitnex/activities/CreateLabelActivity.java index 47b7ef73..8c9f31a1 100644 --- a/app/src/main/java/org/mian/gitnex/activities/CreateLabelActivity.java +++ b/app/src/main/java/org/mian/gitnex/activities/CreateLabelActivity.java @@ -241,7 +241,6 @@ public class CreateLabelActivity extends BaseActivity { else { enableProcessButton(); - tinyDB.putString("labelColor", ""); Toasty.error(ctx, getString(R.string.genericError)); } } @@ -296,8 +295,6 @@ public class CreateLabelActivity extends BaseActivity { else { enableProcessButton(); - tinyDB.putString("labelColor", ""); - tinyDB.putString("labelColorDefault", ""); Toasty.error(ctx, getString(R.string.genericError)); } } diff --git a/app/src/main/java/org/mian/gitnex/activities/SettingsAppearanceActivity.java b/app/src/main/java/org/mian/gitnex/activities/SettingsAppearanceActivity.java index 57f8de0f..cc0747b7 100644 --- a/app/src/main/java/org/mian/gitnex/activities/SettingsAppearanceActivity.java +++ b/app/src/main/java/org/mian/gitnex/activities/SettingsAppearanceActivity.java @@ -80,7 +80,7 @@ public class SettingsAppearanceActivity extends BaseActivity { activitySettingsAppearanceBinding.darkThemeSelectedTime.setText(ctx.getResources().getString(R.string.settingsThemeTimeSelectedHint, darkHour, darkMinute)); activitySettingsAppearanceBinding.tvDateTimeSelected.setText(timeList[timeSelectedChoice]); - activitySettingsAppearanceBinding.customFontSelected.setText(tinyDB.getString("customFontStr", "Manrope")); + activitySettingsAppearanceBinding.customFontSelected.setText(customFontList[customFontSelectedChoice]); activitySettingsAppearanceBinding.themeSelected.setText(themeList[themeSelectedChoice]); if(themeList[themeSelectedChoice].startsWith("Auto")) { @@ -159,7 +159,6 @@ public class SettingsAppearanceActivity extends BaseActivity { customFontSelectedChoice = i; activitySettingsAppearanceBinding.customFontSelected.setText(customFontList[i]); - tinyDB.putString("customFontStr", customFontList[i]); tinyDB.putInt("customFontId", i); AppUtil.typeface = null; // reset typeface FontsOverride.setDefaultFont(this); @@ -187,7 +186,6 @@ public class SettingsAppearanceActivity extends BaseActivity { timeSelectedChoice = i; activitySettingsAppearanceBinding.tvDateTimeSelected.setText(timeList[i]); - tinyDB.putString("timeStr", timeList[i]); tinyDB.putInt("timeId", i); switch(i) { diff --git a/app/src/main/java/org/mian/gitnex/helpers/TinyDB.java b/app/src/main/java/org/mian/gitnex/helpers/TinyDB.java index 92441fc8..8d01a05a 100644 --- a/app/src/main/java/org/mian/gitnex/helpers/TinyDB.java +++ b/app/src/main/java/org/mian/gitnex/helpers/TinyDB.java @@ -69,7 +69,6 @@ public class TinyDB { try { fileCreated = imageFile.createNewFile(); - } catch (IOException e) { e.printStackTrace(); } @@ -81,18 +80,14 @@ public class TinyDB { } catch (Exception e) { e.printStackTrace(); - bitmapCompressed = false; - } finally { if (out != null) { try { out.flush(); out.close(); streamClosed = true; - } catch (IOException e) { e.printStackTrace(); - streamClosed = false; } } } @@ -105,7 +100,6 @@ public class TinyDB { /** * Get int value from SharedPreferences at 'key'. If key not found, return 'defaultValue' * @param key SharedPreferences key - * @param defaultValue int value returned if key was not found * @return int value at 'key' or 'defaultValue' if key not found */ public int getInt(String key) { @@ -116,22 +110,6 @@ public class TinyDB { return preferences.getInt(key, defaultValue); } - /** - * Get parsed ArrayList of Integers from SharedPreferences at 'key' - * @param key SharedPreferences key - * @return ArrayList of Integers - */ - public ArrayList getListInt(String key) { - String[] myList = TextUtils.split(preferences.getString(key, ""), "‚‗‚"); - ArrayList arrayToList = new ArrayList(Arrays.asList(myList)); - ArrayList newList = new ArrayList(); - - for (String item : arrayToList) - newList.add(Integer.parseInt(item)); - - return newList; - } - /** * Get long value from SharedPreferences at 'key'. If key not found, return 'defaultValue' * @param key SharedPreferences key @@ -145,7 +123,6 @@ public class TinyDB { /** * Get float value from SharedPreferences at 'key'. If key not found, return 'defaultValue' * @param key SharedPreferences key - * @param defaultValue float value returned if key was not found * @return float value at 'key' or 'defaultValue' if key not found */ public float getFloat(String key) { @@ -156,55 +133,6 @@ public class TinyDB { return preferences.getFloat(key, defaultValue); } - /** - * Get double value from SharedPreferences at 'key'. If exception thrown, return 'defaultValue' - * @param key SharedPreferences key - * @param defaultValue double value returned if exception is thrown - * @return double value at 'key' or 'defaultValue' if exception is thrown - */ - public double getDouble(String key, double defaultValue) { - String number = getString(key); - - try { - return Double.parseDouble(number); - - } catch (NumberFormatException e) { - return defaultValue; - } - } - - /** - * Get parsed ArrayList of Double from SharedPreferences at 'key' - * @param key SharedPreferences key - * @return ArrayList of Double - */ - public ArrayList getListDouble(String key) { - String[] myList = TextUtils.split(preferences.getString(key, ""), "‚‗‚"); - ArrayList arrayToList = new ArrayList(Arrays.asList(myList)); - ArrayList newList = new ArrayList(); - - for (String item : arrayToList) - newList.add(Double.parseDouble(item)); - - return newList; - } - - /** - * Get parsed ArrayList of Integers from SharedPreferences at 'key' - * @param key SharedPreferences key - * @return ArrayList of Longs - */ - public ArrayList getListLong(String key) { - String[] myList = TextUtils.split(preferences.getString(key, ""), "‚‗‚"); - ArrayList arrayToList = new ArrayList(Arrays.asList(myList)); - ArrayList newList = new ArrayList(); - - for (String item : arrayToList) - newList.add(Long.parseLong(item)); - - return newList; - } - /** * Get String value from SharedPreferences at 'key'. If key not found, return "" * @param key SharedPreferences key @@ -218,19 +146,9 @@ public class TinyDB { return preferences.getString(key, defaultValue); } - /** - * Get parsed ArrayList of String from SharedPreferences at 'key' - * @param key SharedPreferences key - * @return ArrayList of String - */ - public ArrayList getListString(String key) { - return new ArrayList(Arrays.asList(TextUtils.split(preferences.getString(key, ""), "‚‗‚"))); - } - /** * Get boolean value from SharedPreferences at 'key'. If key not found, return 'defaultValue' * @param key SharedPreferences key - * @param defaultValue boolean value returned if key was not found * @return boolean value at 'key' or 'defaultValue' if key not found */ public boolean getBoolean(String key) { @@ -241,52 +159,6 @@ public class TinyDB { return preferences.getBoolean(key, defaultValue); } - /** - * Get parsed ArrayList of Boolean from SharedPreferences at 'key' - * @param key SharedPreferences key - * @return ArrayList of Boolean - */ - public ArrayList getListBoolean(String key) { - ArrayList myList = getListString(key); - ArrayList newList = new ArrayList(); - - for (String item : myList) { - if (item.equals("true")) { - newList.add(true); - } else { - newList.add(false); - } - } - - return newList; - } - - -// public ArrayList getListObject(String key, Class mClass){ -// Gson gson = new Gson(); -// -// ArrayList objStrings = getListString(key); -// ArrayList objects = new ArrayList(); -// -// for(String jObjString : objStrings){ -// Object value = gson.fromJson(jObjString, mClass); -// objects.add(value); -// } -// return objects; -// } - - - -// public T getObject(String key, Class classOfT){ -// -// String json = getString(key); -// Object value = new Gson().fromJson(json, classOfT); -// if (value == null) -// throw new NullPointerException(); -// return (T)value; -// } - - // Put methods /** @@ -299,17 +171,6 @@ public class TinyDB { preferences.edit().putInt(key, value).apply(); } - /** - * Put ArrayList of Integer into SharedPreferences with 'key' and save - * @param key SharedPreferences key - * @param intList ArrayList of Integer to be added - */ - public void putListInt(String key, ArrayList intList) { - checkForNullKey(key); - Integer[] myIntList = intList.toArray(new Integer[0]); - preferences.edit().putString(key, TextUtils.join("‚‗‚", myIntList)).apply(); - } - /** * Put long value into SharedPreferences with 'key' and save * @param key SharedPreferences key @@ -320,17 +181,6 @@ public class TinyDB { preferences.edit().putLong(key, value).apply(); } - /** - * Put ArrayList of Long into SharedPreferences with 'key' and save - * @param key SharedPreferences key - * @param longList ArrayList of Long to be added - */ - public void putListLong(String key, ArrayList longList) { - checkForNullKey(key); - Long[] myLongList = longList.toArray(new Long[0]); - preferences.edit().putString(key, TextUtils.join("‚‗‚", myLongList)).apply(); - } - /** * Put float value into SharedPreferences with 'key' and save * @param key SharedPreferences key @@ -341,27 +191,6 @@ public class TinyDB { preferences.edit().putFloat(key, value).apply(); } - /** - * Put double value into SharedPreferences with 'key' and save - * @param key SharedPreferences key - * @param value double value to be added - */ - public void putDouble(String key, double value) { - checkForNullKey(key); - putString(key, String.valueOf(value)); - } - - /** - * Put ArrayList of Double into SharedPreferences with 'key' and save - * @param key SharedPreferences key - * @param doubleList ArrayList of Double to be added - */ - public void putListDouble(String key, ArrayList doubleList) { - checkForNullKey(key); - Double[] myDoubleList = doubleList.toArray(new Double[doubleList.size()]); - preferences.edit().putString(key, TextUtils.join("‚‗‚", myDoubleList)).apply(); - } - /** * Put String value into SharedPreferences with 'key' and save * @param key SharedPreferences key @@ -372,17 +201,6 @@ public class TinyDB { preferences.edit().putString(key, value).apply(); } - /** - * Put ArrayList of String into SharedPreferences with 'key' and save - * @param key SharedPreferences key - * @param stringList ArrayList of String to be added - */ - public void putListString(String key, ArrayList stringList) { - checkForNullKey(key); - String[] myStringList = stringList.toArray(new String[stringList.size()]); - preferences.edit().putString(key, TextUtils.join("‚‗‚", myStringList)).apply(); - } - /** * Put boolean value into SharedPreferences with 'key' and save * @param key SharedPreferences key @@ -393,47 +211,6 @@ public class TinyDB { preferences.edit().putBoolean(key, value).apply(); } - /** - * Put ArrayList of Boolean into SharedPreferences with 'key' and save - * @param key SharedPreferences key - * @param boolList ArrayList of Boolean to be added - */ - public void putListBoolean(String key, ArrayList boolList) { - checkForNullKey(key); - ArrayList newList = new ArrayList(); - - for (Boolean item : boolList) { - if (item) { - newList.add("true"); - } else { - newList.add("false"); - } - } - - putListString(key, newList); - } - - /** - * 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 putListObject(String key, ArrayList objArray){ -// checkForNullKey(key); -// Gson gson = new Gson(); -// ArrayList objStrings = new ArrayList(); -// for(Object obj : objArray){ -// objStrings.add(gson.toJson(obj)); -// } -// putListString(key, objStrings); -// } - /** * Remove SharedPreferences item with 'key' * @param key SharedPreferences key @@ -509,18 +286,18 @@ public class TinyDB { } /** * null keys would corrupt the shared pref file and make them unreadable this is a preventive measure - * @param the pref key + * @param key the pref key */ - public void checkForNullKey(String key){ + public void checkForNullKey(String key) { if (key == null){ throw new NullPointerException(); } } /** * null keys would corrupt the shared pref file and make them unreadable this is a preventive measure - * @param the pref key + * @param value the pref key */ - public void checkForNullValue(String value){ + public void checkForNullValue(String value) { if (value == null){ throw new NullPointerException(); }