LibreTube/app/src/main/java/com/github/libretube/preferences/PreferenceHelper.kt

94 lines
2.6 KiB
Kotlin
Raw Normal View History

2022-07-02 21:53:24 +05:30
package com.github.libretube.preferences
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-06-26 01:51:22 +05:30
object PreferenceHelper {
2022-08-13 22:52:49 +05:30
/**
* for normal preferences
*/
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()
/**
* set the context that is being used to access the shared preferences
*/
fun setContext(context: Context) {
2022-08-14 13:29:05 +05:30
settings = getDefaultSharedPreferences(context)
editor = settings.edit()
2022-08-13 22:52:49 +05:30
authSettings = getAuthenticationPreferences(context)
authEditor = authSettings.edit()
}
fun getString(key: String?, defValue: String?): String {
return settings.getString(key, defValue)!!
2022-06-26 01:51:22 +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)
}
fun clearPreferences() {
2022-06-26 23:04:54 +05:30
editor.clear().apply()
2022-06-26 01:51:22 +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
}
fun getToken(): String {
2022-08-13 22:52:49 +05:30
return authSettings.getString(PreferenceKeys.TOKEN, "")!!
2022-06-26 14:25:05 +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
}
fun getUsername(): String {
2022-08-13 22:52:49 +05:30
return authSettings.getString(PreferenceKeys.USERNAME, "")!!
2022-06-26 14:25:05 +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-07-29 12:34:00 +05:30
editor.putString(PreferenceKeys.LAST_STREAM_VIDEO_ID, videoId)
2022-07-28 18:01:35 +05:30
}
fun getLatestVideoId(): String {
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
}