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

219 lines
6.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-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-07-02 22:34:19 +05:30
import com.github.libretube.obj.WatchPosition
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"
private lateinit var prefContext: Context
private lateinit var settings: SharedPreferences
private lateinit var editor: SharedPreferences.Editor
/**
* set the context that is being used to access the shared preferences
*/
fun setContext(context: Context) {
prefContext = context
settings = getDefaultSharedPreferences(prefContext)
editor = settings.edit()
}
fun setString(key: String?, value: String?) {
2022-06-26 01:51:22 +05:30
editor.putString(key, value)
editor.apply()
}
fun setInt(key: String?, value: Int) {
2022-06-26 01:51:22 +05:30
editor.putInt(key, value)
editor.apply()
}
fun setLong(key: String?, value: Long) {
2022-06-26 01:51:22 +05:30
editor.putLong(key, value)
editor.apply()
}
fun setBoolean(key: String?, value: Boolean) {
2022-06-26 01:51:22 +05:30
editor.putBoolean(key, value)
editor.apply()
}
fun getString(key: String?, defValue: String?): String? {
2022-06-26 01:51:22 +05:30
return settings.getString(key, defValue)
}
fun getInt(key: String?, defValue: Int): Int {
2022-06-26 01:51:22 +05:30
return settings.getInt(key, defValue)
}
fun getLong(key: String?, defValue: Long): Long {
2022-06-26 01:51:22 +05:30
return settings.getLong(key, defValue)
}
fun getBoolean(key: String?, defValue: Boolean): Boolean {
2022-06-26 01:51:22 +05:30
return settings.getBoolean(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 {
val sharedPref = prefContext.getSharedPreferences("token", Context.MODE_PRIVATE)
2022-06-26 14:25:05 +05:30
return sharedPref?.getString("token", "")!!
}
fun setToken(newValue: String) {
val editor = prefContext.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(): String {
val sharedPref = prefContext.getSharedPreferences("username", Context.MODE_PRIVATE)
2022-06-26 14:25:05 +05:30
return sharedPref.getString("username", "")!!
}
fun setUsername(newValue: String) {
val editor = prefContext.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
}
fun saveCustomInstance(customInstance: CustomInstance) {
2022-06-26 15:41:10 +05:30
val gson = Gson()
val customInstancesList = getCustomInstances()
2022-06-26 15:41:10 +05:30
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(): ArrayList<CustomInstance> {
2022-06-26 15:41:10 +05:30
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()
}
}
fun getHistory(): List<String> {
2022-06-26 15:50:06 +05:30
return try {
val set: Set<String> = settings.getStringSet("search_history", HashSet())!!
set.toList()
} catch (e: Exception) {
emptyList()
}
}
fun saveHistory(historyList: List<String>) {
2022-06-26 15:50:06 +05:30
val set: Set<String> = HashSet(historyList)
editor.putStringSet("search_history", set).apply()
}
fun addToWatchHistory(videoId: String, streams: Streams) {
2022-07-01 21:34:25 +05:30
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()
// delete entries that have the same videoId
2022-07-02 22:34:19 +05:30
var indexToRemove: Int? = null
watchHistory.forEachIndexed { index, item ->
if (item.videoId == videoId) indexToRemove = index
}
2022-07-02 22:34:19 +05:30
if (indexToRemove != null) 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(): ArrayList<WatchHistoryItem> {
2022-07-01 21:34:25 +05:30
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()
}
}
fun saveWatchPosition(videoId: String, position: Long) {
val watchPositions = getWatchPositions()
2022-07-02 22:34:19 +05:30
val watchPositionItem = WatchPosition(videoId, position)
var indexToRemove: Int? = null
watchPositions.forEachIndexed { index, item ->
if (item.videoId == videoId) indexToRemove = index
}
if (indexToRemove != null) watchPositions.removeAt(indexToRemove!!)
watchPositions += watchPositionItem
val gson = Gson()
val json = gson.toJson(watchPositions)
editor.putString("watch_positions", json).commit()
}
fun removeWatchPosition(videoId: String) {
val watchPositions = getWatchPositions()
2022-07-02 22:34:19 +05:30
var indexToRemove: Int? = null
watchPositions.forEachIndexed { index, item ->
if (item.videoId == videoId) indexToRemove = index
}
if (indexToRemove != null) watchPositions.removeAt(indexToRemove!!)
val gson = Gson()
val json = gson.toJson(watchPositions)
editor.putString("watch_positions", json).commit()
}
fun getWatchPositions(): ArrayList<WatchPosition> {
2022-07-02 22:34:19 +05:30
val gson = Gson()
2022-07-02 22:34:19 +05:30
val json: String = settings.getString("watch_positions", "")!!
val type: Type = object : TypeToken<List<WatchPosition?>?>() {}.type
2022-07-02 22:34:19 +05:30
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)
}
}