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

263 lines
8.3 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-30 19:45:52 +05:30
import com.fasterxml.jackson.core.type.TypeReference
2022-07-22 02:41:22 +05:30
import com.fasterxml.jackson.databind.ObjectMapper
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-07-29 12:30:13 +05:30
import com.github.libretube.util.toID
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
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) {
prefContext = context
settings = getDefaultSharedPreferences(prefContext)
editor = settings.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)
}
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) {
val customInstancesList = getCustomInstances()
2022-06-26 15:41:10 +05:30
customInstancesList += customInstance
2022-07-22 02:41:22 +05:30
val json = mapper.writeValueAsString(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 json: String = settings.getString("customInstances", "")!!
2022-07-22 02:41:22 +05:30
val type = mapper.typeFactory.constructCollectionType(
List::class.java,
CustomInstance::class.java
)
2022-06-26 15:41:10 +05:30
return try {
2022-07-22 02:41:22 +05:30
mapper.readValue(json, type)
2022-06-26 15:41:10 +05:30
} catch (e: Exception) {
arrayListOf()
}
}
2022-07-24 15:26:53 +05:30
fun getSearchHistory(): List<String> {
2022-06-26 15:50:06 +05:30
return try {
2022-07-30 19:45:52 +05:30
val json = settings.getString("search_history", "")!!
2022-07-31 18:06:39 +05:30
val type = object : TypeReference<List<String>>() {}
2022-07-30 19:45:52 +05:30
return mapper.readValue(json, type)
2022-06-26 15:50:06 +05:30
} catch (e: Exception) {
emptyList()
}
}
2022-07-24 15:26:53 +05:30
fun saveToSearchHistory(query: String) {
val historyList = getSearchHistory().toMutableList()
2022-07-24 15:26:53 +05:30
if ((historyList.contains(query))) {
// remove from history list if already contained
historyList -= query
}
// append new query to history
historyList.add(0, query)
if (historyList.size > 10) {
historyList.removeAt(historyList.size - 1)
}
updateSearchHistory(historyList)
}
fun removeFromSearchHistory(query: String) {
val historyList = getSearchHistory().toMutableList()
historyList -= query
updateSearchHistory(historyList)
}
private fun updateSearchHistory(historyList: List<String>) {
2022-07-30 19:45:52 +05:30
val json = mapper.writeValueAsString(historyList)
editor.putString("search_history", json).apply()
2022-06-26 15:50:06 +05:30
}
fun addToWatchHistory(videoId: String, streams: Streams) {
2022-07-29 12:17:37 +05:30
removeFromWatchHistory(videoId)
2022-07-01 21:34:25 +05:30
val watchHistoryItem = WatchHistoryItem(
videoId,
streams.title,
streams.uploadDate,
streams.uploader,
2022-07-29 12:30:13 +05:30
streams.uploaderUrl.toID(),
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()
2022-07-01 21:34:25 +05:30
watchHistory += watchHistoryItem
2022-08-09 18:19:34 +05:30
// remove oldest item when the watch history is longer than the pref
val maxWatchHistorySize = getString(PreferenceKeys.WATCH_HISTORY_SIZE, "unlimited")
if (maxWatchHistorySize != "unlimited" && watchHistory.size > maxWatchHistorySize.toInt()) {
watchHistory.removeAt(0)
}
2022-07-22 02:41:22 +05:30
val json = mapper.writeValueAsString(watchHistory)
2022-07-01 21:34:25 +05:30
editor.putString("watch_history", json).apply()
}
2022-07-29 12:17:37 +05:30
fun removeFromWatchHistory(videoId: String) {
val watchHistory = getWatchHistory()
var indexToRemove: Int? = null
watchHistory.forEachIndexed { index, item ->
if (item.videoId == videoId) indexToRemove = index
}
if (indexToRemove != null) {
watchHistory.removeAt(indexToRemove!!)
val json = mapper.writeValueAsString(watchHistory)
editor.putString("watch_history", json).commit()
}
}
fun getWatchHistory(): ArrayList<WatchHistoryItem> {
2022-07-01 21:34:25 +05:30
val json: String = settings.getString("watch_history", "")!!
2022-07-22 02:41:22 +05:30
val type = mapper.typeFactory.constructCollectionType(
List::class.java,
WatchHistoryItem::class.java
)
2022-07-01 21:34:25 +05:30
return try {
2022-07-22 02:41:22 +05:30
mapper.readValue(json, type)
2022-07-01 21:34:25 +05:30
} 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
2022-07-22 02:41:22 +05:30
val json = mapper.writeValueAsString(watchPositions)
2022-07-02 22:34:19 +05:30
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!!)
2022-07-22 02:41:22 +05:30
val json = mapper.writeValueAsString(watchPositions)
2022-07-02 22:34:19 +05:30
editor.putString("watch_positions", json).commit()
}
fun getWatchPositions(): ArrayList<WatchPosition> {
2022-07-02 22:34:19 +05:30
val json: String = settings.getString("watch_positions", "")!!
2022-07-22 02:41:22 +05:30
val type = mapper.typeFactory.constructCollectionType(
List::class.java,
WatchPosition::class.java
)
2022-07-02 22:34:19 +05:30
return try {
2022-07-22 02:41:22 +05:30
mapper.readValue(json, type)
2022-07-02 22:34:19 +05:30
} catch (e: Exception) {
arrayListOf()
}
}
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-08-03 17:11:45 +05:30
fun getLocalSubscriptions(): List<String> {
val json = settings.getString(PreferenceKeys.LOCAL_SUBSCRIPTIONS, "")
return try {
val type = object : TypeReference<List<String>>() {}
mapper.readValue(json, type)
} catch (e: Exception) {
listOf()
}
}
fun setLocalSubscriptions(channels: List<String>) {
val json = mapper.writeValueAsString(channels)
editor.putString(PreferenceKeys.LOCAL_SUBSCRIPTIONS, json).commit()
}
2022-06-26 01:51:22 +05:30
private fun getDefaultSharedPreferences(context: Context): SharedPreferences {
return PreferenceManager.getDefaultSharedPreferences(context)
}
}