package com.github.libretube.preferences import android.content.Context import android.content.SharedPreferences import androidx.preference.PreferenceManager import com.fasterxml.jackson.core.type.TypeReference import com.fasterxml.jackson.databind.ObjectMapper object PreferenceHelper { private val TAG = "PreferenceHelper" private lateinit var prefContext: Context /** * for normal preferences */ private lateinit var settings: SharedPreferences private lateinit var editor: SharedPreferences.Editor /** * For sensitive data (like token) */ private lateinit var authSettings: SharedPreferences private lateinit var authEditor: SharedPreferences.Editor private val mapper = ObjectMapper() /** * set the context that is being used to access the shared preferences */ fun setContext(context: Context) { prefContext = context settings = getDefaultSharedPreferences(prefContext) editor = settings.edit() authSettings = getAuthenticationPreferences(context) authEditor = authSettings.edit() } fun getString(key: String?, defValue: String?): String { return settings.getString(key, defValue)!! } fun getBoolean(key: String?, defValue: Boolean): Boolean { return settings.getBoolean(key, defValue) } fun clearPreferences() { editor.clear().apply() } fun removePreference(value: String?) { editor.remove(value).apply() } fun getToken(): String { return authSettings.getString(PreferenceKeys.TOKEN, "")!! } fun setToken(newValue: String) { authEditor.putString(PreferenceKeys.TOKEN, newValue).apply() } fun getUsername(): String { return authSettings.getString(PreferenceKeys.USERNAME, "")!! } fun setUsername(newValue: String) { authEditor.putString(PreferenceKeys.USERNAME, newValue).apply() } fun setLatestVideoId(videoId: String) { editor.putString(PreferenceKeys.LAST_STREAM_VIDEO_ID, videoId) } fun getLatestVideoId(): String { return getString(PreferenceKeys.LAST_STREAM_VIDEO_ID, "") } fun saveErrorLog(log: String) { editor.putString(PreferenceKeys.ERROR_LOG, log).commit() } fun getErrorLog(): String { return getString(PreferenceKeys.ERROR_LOG, "") } fun getLocalSubscriptions(): List { val json = settings.getString(PreferenceKeys.LOCAL_SUBSCRIPTIONS, "") return try { val type = object : TypeReference>() {} mapper.readValue(json, type) } catch (e: Exception) { listOf() } } fun setLocalSubscriptions(channels: List) { val json = mapper.writeValueAsString(channels) editor.putString(PreferenceKeys.LOCAL_SUBSCRIPTIONS, json).commit() } private fun getDefaultSharedPreferences(context: Context): SharedPreferences { return PreferenceManager.getDefaultSharedPreferences(context) } private fun getAuthenticationPreferences(context: Context): SharedPreferences { return context.getSharedPreferences(PreferenceKeys.AUTH_PREF_FILE, Context.MODE_PRIVATE) } }