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"
|
|
|
|
|
2022-07-17 20:49:55 +05:30
|
|
|
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)
|
2022-07-18 01:36:43 +05:30
|
|
|
editor = settings.edit()
|
2022-07-17 20:49:55 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
fun setString(key: String?, value: String?) {
|
2022-06-26 01:51:22 +05:30
|
|
|
editor.putString(key, value)
|
|
|
|
editor.apply()
|
|
|
|
}
|
|
|
|
|
2022-07-17 20:49:55 +05:30
|
|
|
fun setInt(key: String?, value: Int) {
|
2022-06-26 01:51:22 +05:30
|
|
|
editor.putInt(key, value)
|
|
|
|
editor.apply()
|
|
|
|
}
|
|
|
|
|
2022-07-17 20:49:55 +05:30
|
|
|
fun setLong(key: String?, value: Long) {
|
2022-06-26 01:51:22 +05:30
|
|
|
editor.putLong(key, value)
|
|
|
|
editor.apply()
|
|
|
|
}
|
|
|
|
|
2022-07-17 20:49:55 +05:30
|
|
|
fun setBoolean(key: String?, value: Boolean) {
|
2022-06-26 01:51:22 +05:30
|
|
|
editor.putBoolean(key, value)
|
|
|
|
editor.apply()
|
|
|
|
}
|
|
|
|
|
2022-07-26 23:24:29 +05:30
|
|
|
fun getString(key: String?, defValue: String?): String {
|
|
|
|
return settings.getString(key, defValue)!!
|
2022-06-26 01:51:22 +05:30
|
|
|
}
|
|
|
|
|
2022-07-17 20:49:55 +05:30
|
|
|
fun getInt(key: String?, defValue: Int): Int {
|
2022-06-26 01:51:22 +05:30
|
|
|
return settings.getInt(key, defValue)
|
|
|
|
}
|
|
|
|
|
2022-07-17 20:49:55 +05:30
|
|
|
fun getLong(key: String?, defValue: Long): Long {
|
2022-06-26 01:51:22 +05:30
|
|
|
return settings.getLong(key, defValue)
|
|
|
|
}
|
|
|
|
|
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-07-17 20:49:55 +05:30
|
|
|
fun clearPreferences() {
|
2022-06-26 23:04:54 +05:30
|
|
|
editor.clear().apply()
|
2022-06-26 01:51:22 +05:30
|
|
|
}
|
|
|
|
|
2022-07-17 20:49:55 +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
|
|
|
}
|
|
|
|
|
2022-07-17 20:49:55 +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", "")!!
|
|
|
|
}
|
|
|
|
|
2022-07-17 20:49:55 +05:30
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-07-17 20:49:55 +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", "")!!
|
|
|
|
}
|
|
|
|
|
2022-07-17 20:49:55 +05:30
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-07-17 20:49:55 +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
|
|
|
|
2022-07-17 20:49:55 +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
|
|
|
}
|
|
|
|
|
2022-07-17 20:49:55 +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) {
|
2022-07-26 23:24:29 +05:30
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2022-07-17 20:49:55 +05:30
|
|
|
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
|
|
|
|
)
|
|
|
|
|
2022-07-17 20:49:55 +05:30
|
|
|
val watchHistory = getWatchHistory()
|
2022-07-01 23:17:43 +05:30
|
|
|
|
|
|
|
// delete entries that have the same videoId
|
2022-07-02 22:34:19 +05:30
|
|
|
var indexToRemove: Int? = null
|
2022-07-01 23:17:43 +05:30
|
|
|
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 23:17:43 +05:30
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2022-07-17 20:49:55 +05:30
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-17 20:49:55 +05:30
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2022-07-17 20:49:55 +05:30
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2022-07-17 20:49:55 +05:30
|
|
|
fun getWatchPositions(): ArrayList<WatchPosition> {
|
2022-07-22 02:41:22 +05:30
|
|
|
val mapper = ObjectMapper()
|
2022-07-17 20:49:55 +05:30
|
|
|
|
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-17 20:49:55 +05:30
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|