fix: ordering of search history
This commit is contained in:
parent
7b8af6ea8a
commit
0d365d55c5
4 changed files with 95 additions and 31 deletions
|
@ -10,52 +10,70 @@ import ani.dantotsu.R
|
|||
import ani.dantotsu.connections.anilist.AnilistSearch.SearchType
|
||||
import ani.dantotsu.databinding.ItemSearchHistoryBinding
|
||||
import ani.dantotsu.settings.saving.PrefManager
|
||||
import ani.dantotsu.settings.saving.PrefManager.asLiveStringSet
|
||||
import ani.dantotsu.settings.saving.PrefManager.asLiveClass
|
||||
import ani.dantotsu.settings.saving.PrefName
|
||||
import ani.dantotsu.settings.saving.SharedPreferenceStringSetLiveData
|
||||
import ani.dantotsu.settings.saving.SharedPreferenceClassLiveData
|
||||
import java.io.Serializable
|
||||
|
||||
data class SearchHistory(val search: String, val time: Long) : Serializable {
|
||||
companion object {
|
||||
private const val serialVersionUID = 1L
|
||||
}
|
||||
}
|
||||
|
||||
class SearchHistoryAdapter(type: SearchType, 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 searchHistoryLiveData: SharedPreferenceClassLiveData<List<SearchHistory>>? = null
|
||||
private var searchHistory: MutableList<SearchHistory>? = null
|
||||
private var historyType: PrefName = when (type) {
|
||||
SearchType.ANIME -> PrefName.AnimeSearchHistory
|
||||
SearchType.MANGA -> PrefName.MangaSearchHistory
|
||||
SearchType.CHARACTER -> PrefName.CharacterSearchHistory
|
||||
SearchType.STAFF -> PrefName.StaffSearchHistory
|
||||
SearchType.STUDIO -> PrefName.StudioSearchHistory
|
||||
SearchType.USER -> PrefName.UserSearchHistory
|
||||
SearchType.ANIME -> PrefName.SortedAnimeSH
|
||||
SearchType.MANGA -> PrefName.SortedMangaSH
|
||||
SearchType.CHARACTER -> PrefName.SortedCharacterSH
|
||||
SearchType.STAFF -> PrefName.SortedStaffSH
|
||||
SearchType.STUDIO -> PrefName.SortedStudioSH
|
||||
SearchType.USER -> PrefName.SortedUserSH
|
||||
}
|
||||
|
||||
private fun MutableList<SearchHistory>?.sorted(): List<String>? =
|
||||
this?.sortedByDescending { it.time }?.map { it.search }
|
||||
|
||||
init {
|
||||
searchHistoryLiveData =
|
||||
PrefManager.getLiveVal(historyType, mutableSetOf<String>()).asLiveStringSet()
|
||||
searchHistoryLiveData?.observeForever {
|
||||
searchHistory = it.toMutableSet()
|
||||
submitList(searchHistory?.toList())
|
||||
PrefManager.getLiveVal(historyType, mutableListOf<SearchHistory>()).asLiveClass()
|
||||
searchHistoryLiveData?.observeForever { data ->
|
||||
searchHistory = data.toMutableList()
|
||||
submitList(searchHistory?.sorted())
|
||||
}
|
||||
}
|
||||
|
||||
fun remove(item: String) {
|
||||
searchHistory?.remove(item)
|
||||
searchHistory?.let { list ->
|
||||
list.removeAll { it.search == item }
|
||||
}
|
||||
PrefManager.setVal(historyType, searchHistory)
|
||||
submitList(searchHistory?.toList())
|
||||
submitList(searchHistory?.sorted())
|
||||
}
|
||||
|
||||
fun add(item: String) {
|
||||
if (searchHistory?.contains(item) == true || item.isBlank()) return
|
||||
val maxSize = 25
|
||||
if (searchHistory?.any { it.search == item } == true || item.isBlank()) return
|
||||
if (PrefManager.getVal(PrefName.Incognito)) return
|
||||
searchHistory?.add(item)
|
||||
submitList(searchHistory?.toList())
|
||||
searchHistory?.add(SearchHistory(item, System.currentTimeMillis()))
|
||||
if ((searchHistory?.size ?: 0) > maxSize) {
|
||||
searchHistory?.removeAt(
|
||||
searchHistory?.sorted()?.lastIndex ?: 0
|
||||
)
|
||||
}
|
||||
submitList(searchHistory?.sorted())
|
||||
PrefManager.setVal(historyType, searchHistory)
|
||||
}
|
||||
|
||||
fun clearHistory() {
|
||||
searchHistory?.clear()
|
||||
PrefManager.setVal(historyType, searchHistory)
|
||||
submitList(searchHistory?.toList())
|
||||
submitList(searchHistory?.sorted())
|
||||
}
|
||||
|
||||
override fun onCreateViewHolder(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue