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

253 lines
7.5 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-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-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 {
return settings.getString(key, defValue)!!
2022-06-26 01:51:22 +05:30
}
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-07-22 02:41:22 +05:30
val mapper = ObjectMapper()
2022-06-26 15:41:10 +05:30
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-07-22 02:41:22 +05:30
val mapper = ObjectMapper()
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-24 15:26:53 +05:30
val set: Set<String> = settings.getStringSet("search_history", LinkedHashSet())!!
2022-06-26 15:50:06 +05:30
set.toList()
} 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>) {
val set: Set<String> = LinkedHashSet(historyList)
2022-06-26 15:50:06 +05:30
editor.putStringSet("search_history", set).apply()
}
fun addToWatchHistory(videoId: String, streams: Streams) {
2022-07-22 02:41:22 +05:30
val mapper = ObjectMapper()
2022-07-01 21:34:25 +05:30
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
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()
}
fun getWatchHistory(): ArrayList<WatchHistoryItem> {
2022-07-22 02:41:22 +05:30
val mapper = ObjectMapper()
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 mapper = ObjectMapper()
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 mapper = ObjectMapper()
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-22 02:41:22 +05:30
val mapper = ObjectMapper()
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-06-26 01:51:22 +05:30
private fun getDefaultSharedPreferences(context: Context): SharedPreferences {
return PreferenceManager.getDefaultSharedPreferences(context)
}
}