backend preference wrapper

This commit is contained in:
rebelonion 2024-01-30 00:39:12 -06:00
parent eb5b83564f
commit 8020b32541
66 changed files with 482 additions and 458 deletions

View file

@ -1,6 +1,5 @@
package ani.dantotsu.media
import android.content.SharedPreferences
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
@ -9,24 +8,25 @@ import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import ani.dantotsu.R
import ani.dantotsu.databinding.ItemSearchHistoryBinding
import ani.dantotsu.others.SharedPreferenceStringSetLiveData
import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get
import ani.dantotsu.settings.saving.PrefName
import ani.dantotsu.settings.saving.PrefWrapper
import ani.dantotsu.settings.saving.PrefWrapper.asLiveStringSet
import ani.dantotsu.settings.saving.SharedPreferenceStringSetLiveData
import java.util.Locale
class SearchHistoryAdapter(private val type: String, private val searchClicked: (String) -> Unit) : ListAdapter<String, SearchHistoryAdapter.SearchHistoryViewHolder>(
DIFF_CALLBACK_INSTALLED
) {
private var searchHistoryLiveData: SharedPreferenceStringSetLiveData? = null
private var searchHistory: MutableSet<String>? = null
private var sharedPreferences: SharedPreferences? = null
private var historyType: PrefName = when (type.lowercase(Locale.ROOT)) {
"anime" -> PrefName.AnimeSearchHistory
"manga" -> PrefName.MangaSearchHistory
else -> throw IllegalArgumentException("Invalid type")
}
init {
sharedPreferences = Injekt.get<SharedPreferences>()
searchHistoryLiveData = SharedPreferenceStringSetLiveData(
sharedPreferences!!,
"searchHistory_$type",
mutableSetOf()
)
searchHistoryLiveData = PrefWrapper.getLiveVal(historyType, mutableSetOf<String>()).asLiveStringSet()
searchHistoryLiveData?.observeForever {
searchHistory = it.toMutableSet()
submitList(searchHistory?.reversed())
@ -35,14 +35,14 @@ class SearchHistoryAdapter(private val type: String, private val searchClicked:
fun remove(item: String) {
searchHistory?.remove(item)
sharedPreferences?.edit()?.putStringSet("searchHistory_$type", searchHistory)?.apply()
PrefWrapper.setVal(historyType, searchHistory)
}
fun add(item: String) {
if (searchHistory?.contains(item) == true || item.isBlank()) return
if (sharedPreferences?.getBoolean("incognito", false) == true) return
if (PrefWrapper.getVal(PrefName.Incognito, false)) return
searchHistory?.add(item)
sharedPreferences?.edit()?.putStringSet("searchHistory_$type", searchHistory)?.apply()
PrefWrapper.setVal(historyType, searchHistory)
}
override fun onCreateViewHolder(