diff --git a/app/src/main/java/ani/dantotsu/Functions.kt b/app/src/main/java/ani/dantotsu/Functions.kt index c06b5356..86fa6ef8 100644 --- a/app/src/main/java/ani/dantotsu/Functions.kt +++ b/app/src/main/java/ani/dantotsu/Functions.kt @@ -105,45 +105,6 @@ fun logger(e: Any?, print: Boolean = true) { println(e) } -fun saveData(fileName: String, data: Any?, context: Context? = null) { - tryWith { - val a = context ?: currContext() - if (a != null) { - val fos: FileOutputStream = a.openFileOutput(fileName, Context.MODE_PRIVATE) - val os = ObjectOutputStream(fos) - os.writeObject(data) - os.close() - fos.close() - } - } -} - -@Suppress("UNCHECKED_CAST") -fun loadData(fileName: String, context: Context? = null, toast: Boolean = true): T? { - val a = context ?: currContext() - try { - if (a?.fileList() != null) - if (fileName in a.fileList()) { - val fileIS: FileInputStream = a.openFileInput(fileName) - val objIS = ObjectInputStream(fileIS) - val data = objIS.readObject() as T - objIS.close() - fileIS.close() - return data - } - } catch (e: Exception) { - if (toast) snackString(a?.getString(R.string.error_loading_data, fileName)) - //try to delete the file - try { - a?.deleteFile(fileName) - } catch (e: Exception) { - FirebaseCrashlytics.getInstance().log("Failed to delete file $fileName") - FirebaseCrashlytics.getInstance().recordException(e) - } - e.printStackTrace() - } - return null -} fun initActivity(a: Activity) { val window = a.window diff --git a/app/src/main/java/ani/dantotsu/settings/saving/PrefWrapper.kt b/app/src/main/java/ani/dantotsu/settings/saving/PrefWrapper.kt index 9f2daf2d..35f6d32d 100644 --- a/app/src/main/java/ani/dantotsu/settings/saving/PrefWrapper.kt +++ b/app/src/main/java/ani/dantotsu/settings/saving/PrefWrapper.kt @@ -143,6 +143,49 @@ object PrefWrapper { } } + @Suppress("UNCHECKED_CAST") + fun setCustomVal(key: String, value: T) { + //for custom force irrelevant + with(irrelevantPreferences!!.edit()) { + when (value) { + is Boolean -> putBoolean(key, value as Boolean) + is Int -> putInt(key, value as Int) + is Float -> putFloat(key, value as Float) + is Long -> putLong(key, value as Long) + is String -> putString(key, value as String) + is Set<*> -> putStringSet(key, value as Set) + else -> throw IllegalArgumentException("Type not supported") + } + apply() + } + } + + @Suppress("UNCHECKED_CAST") + fun getCustomVal(key: String, default: T): T { + //for custom force irrelevant + return try { + when (default) { + is Boolean -> irrelevantPreferences!!.getBoolean(key, default) as T + is Int -> irrelevantPreferences!!.getInt(key, default) as T + is Float -> irrelevantPreferences!!.getFloat(key, default) as T + is Long -> irrelevantPreferences!!.getLong(key, default) as T + is String -> irrelevantPreferences!!.getString(key, default) as T + is Set<*> -> irrelevantPreferences!!.getStringSet(key, default as Set) as T + else -> throw IllegalArgumentException("Type not supported") + } + } catch (e: Exception) { + default + } + } + + fun removeCustomVal(key: String) { + //for custom force irrelevant + with(irrelevantPreferences!!.edit()) { + remove(key) + apply() + } + } + fun SharedPreferenceLiveData<*>.asLiveBool(): SharedPreferenceBooleanLiveData = this as? SharedPreferenceBooleanLiveData ?: throw ClassCastException("Cannot cast to SharedPreferenceLiveData")