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

179 lines
6.2 KiB
Kotlin
Raw Normal View History

2022-06-26 01:51:22 +05:30
package com.github.libretube.util
import android.content.Context
import android.content.SharedPreferences
import androidx.preference.PreferenceManager
2022-06-26 15:41:10 +05:30
import com.github.libretube.obj.CustomInstance
2022-07-01 21:34:25 +05:30
import com.github.libretube.obj.Streams
import com.github.libretube.obj.WatchHistoryItem
2022-06-26 15:41:10 +05:30
import com.google.common.reflect.TypeToken
import com.google.gson.Gson
import java.lang.reflect.Type
2022-06-26 01:51:22 +05:30
object PreferenceHelper {
2022-07-01 21:34:25 +05:30
private val TAG = "PreferenceHelper"
2022-06-26 01:51:22 +05:30
fun setString(context: Context, key: String?, value: String?) {
val editor = getDefaultSharedPreferencesEditor(context)
editor.putString(key, value)
editor.apply()
}
fun setInt(context: Context, key: String?, value: Int) {
val editor = getDefaultSharedPreferencesEditor(context)
editor.putInt(key, value)
editor.apply()
}
fun setLong(context: Context, key: String?, value: Long) {
val editor = getDefaultSharedPreferencesEditor(context)
editor.putLong(key, value)
editor.apply()
}
fun setBoolean(context: Context, key: String?, value: Boolean) {
val editor = getDefaultSharedPreferencesEditor(context)
editor.putBoolean(key, value)
editor.apply()
}
fun getString(context: Context, key: String?, defValue: String?): String? {
val settings: SharedPreferences = getDefaultSharedPreferences(context)
return settings.getString(key, defValue)
}
fun getInt(context: Context, key: String?, defValue: Int): Int {
val settings: SharedPreferences = getDefaultSharedPreferences(context)
return settings.getInt(key, defValue)
}
fun getLong(context: Context, key: String?, defValue: Long): Long {
val settings: SharedPreferences = getDefaultSharedPreferences(context)
return settings.getLong(key, defValue)
}
fun getBoolean(context: Context, key: String?, defValue: Boolean): Boolean {
val settings: SharedPreferences = getDefaultSharedPreferences(context)
return settings.getBoolean(key, defValue)
}
fun clearPreferences(context: Context) {
val editor = getDefaultSharedPreferencesEditor(context)
2022-06-26 23:04:54 +05:30
editor.clear().apply()
2022-06-26 01:51:22 +05:30
}
fun removePreference(context: Context, value: String?) {
val editor = getDefaultSharedPreferencesEditor(context)
2022-06-26 23:04:54 +05:30
editor.remove(value).apply()
2022-06-26 01:51:22 +05:30
}
2022-06-26 14:25:05 +05:30
fun getToken(context: Context): String {
val sharedPref = context.getSharedPreferences("token", Context.MODE_PRIVATE)
return sharedPref?.getString("token", "")!!
}
fun setToken(context: Context, newValue: String) {
val editor = context.getSharedPreferences("token", Context.MODE_PRIVATE).edit()
2022-06-26 22:51:54 +05:30
editor.putString("token", newValue).apply()
2022-06-26 14:25:05 +05:30
}
fun getUsername(context: Context): String {
val sharedPref = context.getSharedPreferences("username", Context.MODE_PRIVATE)
return sharedPref.getString("username", "")!!
}
fun setUsername(context: Context, newValue: String) {
val editor = context.getSharedPreferences("username", Context.MODE_PRIVATE).edit()
2022-06-26 22:51:54 +05:30
editor.putString("username", newValue).apply()
2022-06-26 14:06:34 +05:30
}
2022-06-26 15:41:10 +05:30
fun saveCustomInstance(context: Context, customInstance: CustomInstance) {
val editor = getDefaultSharedPreferencesEditor(context)
val gson = Gson()
val customInstancesList = getCustomInstances(context)
customInstancesList += customInstance
val json = gson.toJson(customInstancesList)
2022-06-26 22:51:54 +05:30
editor.putString("customInstances", json).apply()
2022-06-26 15:41:10 +05:30
}
fun getCustomInstances(context: Context): ArrayList<CustomInstance> {
val settings = getDefaultSharedPreferences(context)
val gson = Gson()
val json: String = settings.getString("customInstances", "")!!
val type: Type = object : TypeToken<List<CustomInstance?>?>() {}.type
return try {
gson.fromJson(json, type)
} catch (e: Exception) {
arrayListOf()
}
}
2022-06-26 15:50:06 +05:30
fun getHistory(context: Context): List<String> {
return try {
val settings = getDefaultSharedPreferences(context)
val set: Set<String> = settings.getStringSet("search_history", HashSet())!!
set.toList()
} catch (e: Exception) {
emptyList()
}
}
fun saveHistory(context: Context, historyList: List<String>) {
val editor = getDefaultSharedPreferencesEditor(context)
val set: Set<String> = HashSet(historyList)
editor.putStringSet("search_history", set).apply()
}
2022-07-01 21:34:25 +05:30
fun addToWatchHistory(context: Context, videoId: String, streams: Streams) {
val editor = getDefaultSharedPreferencesEditor(context)
val gson = Gson()
val watchHistoryItem = WatchHistoryItem(
videoId,
streams.title,
streams.uploadDate,
streams.uploader,
streams.uploaderUrl?.replace("/channel/", ""),
2022-07-01 22:20:51 +05:30
streams.uploaderAvatar,
2022-07-01 21:34:25 +05:30
streams.thumbnailUrl,
streams.duration
)
val watchHistory = getWatchHistory(context)
// delete entries that have the same videoId
var indexToRemove = Int.MAX_VALUE
watchHistory.forEachIndexed { index, item ->
if (item.videoId == videoId) indexToRemove = index
}
if (indexToRemove != Int.MAX_VALUE) watchHistory.removeAt(indexToRemove)
2022-07-01 21:34:25 +05:30
watchHistory += watchHistoryItem
val json = gson.toJson(watchHistory)
editor.putString("watch_history", json).apply()
}
fun getWatchHistory(context: Context): ArrayList<WatchHistoryItem> {
val settings = getDefaultSharedPreferences(context)
val gson = Gson()
val json: String = settings.getString("watch_history", "")!!
val type: Type = object : TypeToken<List<WatchHistoryItem?>?>() {}.type
return try {
gson.fromJson(json, type)
} catch (e: Exception) {
arrayListOf()
}
}
2022-06-26 01:51:22 +05:30
private fun getDefaultSharedPreferences(context: Context): SharedPreferences {
return PreferenceManager.getDefaultSharedPreferences(context)
}
2022-06-26 15:50:56 +05:30
2022-06-26 01:51:22 +05:30
private fun getDefaultSharedPreferencesEditor(context: Context): SharedPreferences.Editor {
return getDefaultSharedPreferences(context).edit()
}
}