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

159 lines
4.7 KiB
Kotlin
Raw Normal View History

package com.github.libretube.helpers
2022-06-26 01:51:22 +05:30
import android.content.Context
import android.content.SharedPreferences
import androidx.preference.PreferenceManager
2023-02-21 17:51:49 +05:30
import com.github.libretube.constants.PIPED_API_URL
2022-09-08 21:59:00 +05:30
import com.github.libretube.constants.PreferenceKeys
import java.time.Instant
2022-06-26 01:51:22 +05:30
object PreferenceHelper {
2022-08-13 22:52:49 +05:30
/**
* for normal preferences
*/
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
/**
* 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)
editor = settings.edit()
2022-08-13 22:52:49 +05:30
authSettings = getAuthenticationPreferences(context)
authEditor = authSettings.edit()
2023-02-21 17:51:49 +05:30
if (getString(PreferenceKeys.FETCH_INSTANCE, "").isBlank()) {
putString(PreferenceKeys.FETCH_INSTANCE, PIPED_API_URL)
}
}
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
}
fun putBoolean(key: String, value: Boolean) {
editor.putBoolean(key, value).commit()
}
2022-11-24 22:08:14 +05:30
fun putInt(key: String, value: Int) {
editor.putInt(key, value).commit()
}
fun putFloat(key: String, value: Float) {
editor.putFloat(key, value).commit()
}
fun putLong(key: String, value: Long) {
editor.putLong(key, value).commit()
}
2022-11-21 18:42:46 +05:30
fun getString(key: String?, defValue: String): String {
return settings.getString(key, defValue) ?: 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 {
2023-03-28 22:01:14 +05:30
return runCatching {
settings.getInt(key, defValue)
}.getOrElse { settings.getLong(key, defValue.toLong()).toInt() }
}
fun getLong(key: String?, defValue: Long): Long {
return settings.getLong(key, defValue)
}
fun getFloat(key: String?, defValue: Float): Float {
return settings.getFloat(key, defValue)
2022-08-15 14:17:31 +05:30
}
fun getStringSet(key: String?, defValue: Set<String>): Set<String> {
return settings.getStringSet(key, defValue).orEmpty()
}
fun clearPreferences() {
2022-06-26 23:04:54 +05:30
editor.clear().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-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, "")
}
fun updateLastFeedWatchedTime() {
putLong(PreferenceKeys.LAST_WATCHED_FEED_TIME, Instant.now().epochSecond)
}
fun getLastCheckedFeedTime(): Long {
return getLong(PreferenceKeys.LAST_WATCHED_FEED_TIME, 0)
}
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-11-06 15:17:18 +05:30
fun getIgnorableNotificationChannels(): List<String> {
return getString(PreferenceKeys.IGNORED_NOTIFICATION_CHANNELS, "").split(",")
}
fun isChannelNotificationIgnorable(channelId: String): Boolean {
return getIgnorableNotificationChannels().any { it == channelId }
}
fun toggleIgnorableNotificationChannel(channelId: String) {
val ignorableChannels = getIgnorableNotificationChannels().toMutableList()
2022-12-19 21:28:34 +05:30
if (ignorableChannels.contains(channelId)) {
ignorableChannels.remove(channelId)
} else {
ignorableChannels.add(
channelId,
2022-12-19 21:28:34 +05:30
)
}
2022-11-06 15:17:18 +05:30
editor.putString(
PreferenceKeys.IGNORED_NOTIFICATION_CHANNELS,
ignorableChannels.joinToString(","),
2022-11-06 15:56:24 +05:30
).apply()
2022-11-06 15:17:18 +05:30
}
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
}