remove old setting saving
This commit is contained in:
parent
965adddf8d
commit
4d682f014d
2 changed files with 43 additions and 39 deletions
|
@ -105,45 +105,6 @@ fun logger(e: Any?, print: Boolean = true) {
|
||||||
println(e)
|
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 <T> 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) {
|
fun initActivity(a: Activity) {
|
||||||
val window = a.window
|
val window = a.window
|
||||||
|
|
|
@ -143,6 +143,49 @@ object PrefWrapper {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Suppress("UNCHECKED_CAST")
|
||||||
|
fun <T> 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<String>)
|
||||||
|
else -> throw IllegalArgumentException("Type not supported")
|
||||||
|
}
|
||||||
|
apply()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Suppress("UNCHECKED_CAST")
|
||||||
|
fun <T> 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<String>) 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 =
|
fun SharedPreferenceLiveData<*>.asLiveBool(): SharedPreferenceBooleanLiveData =
|
||||||
this as? SharedPreferenceBooleanLiveData
|
this as? SharedPreferenceBooleanLiveData
|
||||||
?: throw ClassCastException("Cannot cast to SharedPreferenceLiveData<Boolean>")
|
?: throw ClassCastException("Cannot cast to SharedPreferenceLiveData<Boolean>")
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue