2023-01-31 21:13:39 +05:30
|
|
|
package com.github.libretube.helpers
|
2022-06-26 01:51:22 +05:30
|
|
|
|
|
|
|
import android.content.Context
|
|
|
|
import android.content.SharedPreferences
|
2023-08-13 19:24:07 +05:30
|
|
|
import androidx.core.content.edit
|
2022-06-26 01:51:22 +05:30
|
|
|
import androidx.preference.PreferenceManager
|
2022-09-08 21:59:00 +05:30
|
|
|
import com.github.libretube.constants.PreferenceKeys
|
2022-11-28 00:21:30 +05:30
|
|
|
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
|
|
|
|
*/
|
2022-09-22 21:48:10 +05:30
|
|
|
lateinit var settings: SharedPreferences
|
2022-08-13 22:52:49 +05:30
|
|
|
|
|
|
|
/**
|
|
|
|
* For sensitive data (like token)
|
|
|
|
*/
|
|
|
|
private lateinit var authSettings: SharedPreferences
|
|
|
|
|
2023-08-22 22:18:27 +05:30
|
|
|
/**
|
|
|
|
* Possible chars to use for the SB User ID
|
|
|
|
*/
|
|
|
|
private const val USER_ID_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
|
|
|
|
|
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-08-13 22:52:49 +05:30
|
|
|
authSettings = getAuthenticationPreferences(context)
|
2022-07-17 20:49:55 +05:30
|
|
|
}
|
|
|
|
|
2022-11-23 22:06:52 +05:30
|
|
|
fun putString(key: String, value: String) {
|
2023-08-13 19:24:07 +05:30
|
|
|
settings.edit(commit = true) { putString(key, value) }
|
2022-08-23 19:21:00 +05:30
|
|
|
}
|
|
|
|
|
2022-11-23 22:06:52 +05:30
|
|
|
fun putBoolean(key: String, value: Boolean) {
|
2023-08-13 19:24:07 +05:30
|
|
|
settings.edit(commit = true) { putBoolean(key, value) }
|
2022-11-23 22:06:52 +05:30
|
|
|
}
|
|
|
|
|
2022-11-24 22:08:14 +05:30
|
|
|
fun putInt(key: String, value: Int) {
|
2023-08-13 19:24:07 +05:30
|
|
|
settings.edit(commit = true) { putInt(key, value) }
|
2022-11-26 11:42:46 +05:30
|
|
|
}
|
|
|
|
|
2022-11-28 00:21:30 +05:30
|
|
|
fun putLong(key: String, value: Long) {
|
2023-08-13 19:24:07 +05:30
|
|
|
settings.edit(commit = true) { putLong(key, value) }
|
2022-11-28 00:21:30 +05:30
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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 {
|
2023-03-28 22:01:14 +05:30
|
|
|
return runCatching {
|
|
|
|
settings.getInt(key, defValue)
|
|
|
|
}.getOrElse { settings.getLong(key, defValue.toLong()).toInt() }
|
2022-11-26 11:42:46 +05:30
|
|
|
}
|
|
|
|
|
2022-11-28 00:21:30 +05:30
|
|
|
fun getLong(key: String?, defValue: Long): Long {
|
|
|
|
return settings.getLong(key, defValue)
|
|
|
|
}
|
|
|
|
|
2023-06-01 01:13:50 +05:30
|
|
|
fun getStringSet(key: String?, defValue: Set<String>): Set<String> {
|
|
|
|
return settings.getStringSet(key, defValue).orEmpty()
|
|
|
|
}
|
|
|
|
|
2022-07-17 20:49:55 +05:30
|
|
|
fun clearPreferences() {
|
2023-08-13 19:24:07 +05:30
|
|
|
settings.edit { clear() }
|
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) {
|
2023-08-13 19:24:07 +05:30
|
|
|
authSettings.edit { putString(PreferenceKeys.TOKEN, newValue) }
|
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) {
|
2023-08-13 19:24:07 +05:30
|
|
|
authSettings.edit { putString(PreferenceKeys.USERNAME, newValue) }
|
2022-06-26 14:06:34 +05:30
|
|
|
}
|
|
|
|
|
2023-06-02 19:28:35 +05:30
|
|
|
fun setLastSeenVideoId(videoId: String) {
|
2023-08-13 19:24:07 +05:30
|
|
|
putString(PreferenceKeys.LAST_STREAM_VIDEO_ID, videoId)
|
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-11-28 00:21:30 +05:30
|
|
|
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) {
|
2023-08-13 19:24:07 +05:30
|
|
|
putString(PreferenceKeys.ERROR_LOG, log)
|
2022-08-01 14:52:08 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2023-08-13 19:24:07 +05:30
|
|
|
ignorableChannels.add(channelId)
|
|
|
|
}
|
|
|
|
settings.edit {
|
|
|
|
val channelsString = ignorableChannels.joinToString(",")
|
|
|
|
putString(PreferenceKeys.IGNORED_NOTIFICATION_CHANNELS, channelsString)
|
2022-12-19 21:28:34 +05:30
|
|
|
}
|
2022-11-06 15:17:18 +05:30
|
|
|
}
|
|
|
|
|
2023-08-22 22:18:27 +05:30
|
|
|
fun getSponsorBlockUserID(): String {
|
|
|
|
var uuid = getString(PreferenceKeys.SB_USER_ID, "")
|
|
|
|
if (uuid.isEmpty()) {
|
|
|
|
// generate a new user id to use for submitting SponsorBlock segments
|
|
|
|
uuid = (0 until 30).map { USER_ID_CHARS.random() }.joinToString("")
|
|
|
|
putString(PreferenceKeys.SB_USER_ID, uuid)
|
|
|
|
}
|
|
|
|
return uuid
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|