2022-09-08 21:59:00 +05:30
|
|
|
package com.github.libretube.util
|
2022-06-26 01:51:22 +05:30
|
|
|
|
|
|
|
import android.content.Context
|
|
|
|
import android.content.SharedPreferences
|
|
|
|
import androidx.preference.PreferenceManager
|
2022-07-22 02:41:22 +05:30
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper
|
2022-09-08 21:59:00 +05:30
|
|
|
import com.github.libretube.constants.PreferenceKeys
|
2022-06-26 01:51:22 +05:30
|
|
|
|
|
|
|
object PreferenceHelper {
|
2022-08-13 22:52:49 +05:30
|
|
|
/**
|
|
|
|
* for normal preferences
|
|
|
|
*/
|
2022-07-17 20:49:55 +05:30
|
|
|
private lateinit var settings: SharedPreferences
|
|
|
|
private lateinit var editor: SharedPreferences.Editor
|
2022-08-13 22:52:49 +05:30
|
|
|
|
|
|
|
/**
|
|
|
|
* For sensitive data (like token)
|
|
|
|
*/
|
|
|
|
private lateinit var authSettings: SharedPreferences
|
|
|
|
private lateinit var authEditor: SharedPreferences.Editor
|
|
|
|
|
2022-07-29 12:34:00 +05:30
|
|
|
private val mapper = ObjectMapper()
|
2022-07-17 20:49:55 +05:30
|
|
|
|
|
|
|
/**
|
|
|
|
* set the context that is being used to access the shared preferences
|
|
|
|
*/
|
2022-08-26 12:12:13 +05:30
|
|
|
fun initialize(context: Context) {
|
2022-08-14 13:29:05 +05:30
|
|
|
settings = getDefaultSharedPreferences(context)
|
2022-07-18 01:36:43 +05:30
|
|
|
editor = settings.edit()
|
2022-08-13 22:52:49 +05:30
|
|
|
|
|
|
|
authSettings = getAuthenticationPreferences(context)
|
|
|
|
authEditor = authSettings.edit()
|
2022-07-17 20:49:55 +05:30
|
|
|
}
|
|
|
|
|
2022-08-23 19:21:00 +05:30
|
|
|
fun putString(key: String?, value: String) {
|
2022-08-26 02:35:04 +05:30
|
|
|
editor.putString(key, value).commit()
|
2022-08-23 19:21:00 +05:30
|
|
|
}
|
|
|
|
|
2022-07-26 23:24:29 +05:30
|
|
|
fun getString(key: String?, defValue: String?): String {
|
|
|
|
return settings.getString(key, defValue)!!
|
2022-06-26 01:51:22 +05:30
|
|
|
}
|
|
|
|
|
2022-07-17 20:49:55 +05:30
|
|
|
fun getBoolean(key: String?, defValue: Boolean): Boolean {
|
2022-06-26 01:51:22 +05:30
|
|
|
return settings.getBoolean(key, defValue)
|
|
|
|
}
|
|
|
|
|
2022-08-15 14:17:31 +05:30
|
|
|
fun getInt(key: String?, defValue: Int): Int {
|
|
|
|
return settings.getInt(key, defValue)
|
|
|
|
}
|
|
|
|
|
2022-07-17 20:49:55 +05:30
|
|
|
fun clearPreferences() {
|
2022-06-26 23:04:54 +05:30
|
|
|
editor.clear().apply()
|
2022-06-26 01:51:22 +05:30
|
|
|
}
|
|
|
|
|
2022-07-17 20:49:55 +05:30
|
|
|
fun removePreference(value: String?) {
|
2022-06-26 23:04:54 +05:30
|
|
|
editor.remove(value).apply()
|
2022-06-26 01:51:22 +05:30
|
|
|
}
|
|
|
|
|
2022-07-17 20:49:55 +05:30
|
|
|
fun getToken(): String {
|
2022-08-13 22:52:49 +05:30
|
|
|
return authSettings.getString(PreferenceKeys.TOKEN, "")!!
|
2022-06-26 14:25:05 +05:30
|
|
|
}
|
|
|
|
|
2022-07-17 20:49:55 +05:30
|
|
|
fun setToken(newValue: String) {
|
2022-08-13 22:52:49 +05:30
|
|
|
authEditor.putString(PreferenceKeys.TOKEN, newValue).apply()
|
2022-06-26 14:25:05 +05:30
|
|
|
}
|
|
|
|
|
2022-07-17 20:49:55 +05:30
|
|
|
fun getUsername(): String {
|
2022-08-13 22:52:49 +05:30
|
|
|
return authSettings.getString(PreferenceKeys.USERNAME, "")!!
|
2022-06-26 14:25:05 +05:30
|
|
|
}
|
|
|
|
|
2022-07-17 20:49:55 +05:30
|
|
|
fun setUsername(newValue: String) {
|
2022-08-13 22:52:49 +05:30
|
|
|
authEditor.putString(PreferenceKeys.USERNAME, newValue).apply()
|
2022-06-26 14:06:34 +05:30
|
|
|
}
|
|
|
|
|
2022-07-28 18:01:35 +05:30
|
|
|
fun setLatestVideoId(videoId: String) {
|
2022-08-18 17:02:09 +05:30
|
|
|
editor.putString(PreferenceKeys.LAST_STREAM_VIDEO_ID, videoId).commit()
|
2022-07-28 18:01:35 +05:30
|
|
|
}
|
|
|
|
|
2022-09-12 17:52:55 +05:30
|
|
|
fun getLastSeenVideoId(): String {
|
2022-07-28 18:01:35 +05:30
|
|
|
return getString(PreferenceKeys.LAST_STREAM_VIDEO_ID, "")
|
|
|
|
}
|
|
|
|
|
2022-08-01 14:52:08 +05:30
|
|
|
fun saveErrorLog(log: String) {
|
|
|
|
editor.putString(PreferenceKeys.ERROR_LOG, log).commit()
|
|
|
|
}
|
|
|
|
|
|
|
|
fun getErrorLog(): String {
|
|
|
|
return getString(PreferenceKeys.ERROR_LOG, "")
|
|
|
|
}
|
|
|
|
|
2022-06-26 01:51:22 +05:30
|
|
|
private fun getDefaultSharedPreferences(context: Context): SharedPreferences {
|
|
|
|
return PreferenceManager.getDefaultSharedPreferences(context)
|
|
|
|
}
|
2022-08-13 22:52:49 +05:30
|
|
|
|
|
|
|
private fun getAuthenticationPreferences(context: Context): SharedPreferences {
|
|
|
|
return context.getSharedPreferences(PreferenceKeys.AUTH_PREF_FILE, Context.MODE_PRIVATE)
|
|
|
|
}
|
2022-06-26 01:51:22 +05:30
|
|
|
}
|