Fix scrolling to top when managing plugins

This commit is contained in:
Blatzar 2022-08-14 19:53:21 +02:00
parent 18f1fba047
commit a43534417b
3 changed files with 17 additions and 10 deletions

View File

@ -90,9 +90,11 @@ class PluginsFragment : Fragment() {
pluginViewModel.handlePluginAction(activity, url, it, isLocal) pluginViewModel.handlePluginAction(activity, url, it, isLocal)
} }
observe(pluginViewModel.filteredPlugins) { observe(pluginViewModel.filteredPlugins) { (scrollToTop, list) ->
(plugin_recycler_view?.adapter as? PluginAdapter?)?.updateList(it) (plugin_recycler_view?.adapter as? PluginAdapter?)?.updateList(list)
plugin_recycler_view?.scrollToPosition(0)
if (scrollToTop)
plugin_recycler_view?.scrollToPosition(0)
} }
if (isLocal) { if (isLocal) {

View File

@ -22,14 +22,19 @@ import kotlinx.coroutines.launch
import me.xdrop.fuzzywuzzy.FuzzySearch import me.xdrop.fuzzywuzzy.FuzzySearch
typealias Plugin = Pair<String, SitePlugin> typealias Plugin = Pair<String, SitePlugin>
/**
* The boolean signifies if the plugin list should be scrolled to the top, used for searching.
* */
typealias PluginViewDataUpdate = Pair<Boolean, List<PluginViewData>>
class PluginsViewModel : ViewModel() { class PluginsViewModel : ViewModel() {
/** plugins is an unaltered list of plugins */ /** plugins is an unaltered list of plugins */
private var plugins: List<PluginViewData> = emptyList() private var plugins: List<PluginViewData> = emptyList()
/** filteredPlugins is a subset of plugins following the current search query and tv type selection */ /** filteredPlugins is a subset of plugins following the current search query and tv type selection */
private var _filteredPlugins = MutableLiveData<List<PluginViewData>>() private var _filteredPlugins = MutableLiveData<PluginViewDataUpdate>()
var filteredPlugins: LiveData<List<PluginViewData>> = _filteredPlugins var filteredPlugins: LiveData<PluginViewDataUpdate> = _filteredPlugins
val tvTypes = mutableListOf<String>() val tvTypes = mutableListOf<String>()
private var currentQuery: String? = null private var currentQuery: String? = null
@ -169,7 +174,7 @@ class PluginsViewModel : ViewModel() {
} }
this.plugins = list this.plugins = list
_filteredPlugins.postValue(list.filterTvTypes().sortByQuery(currentQuery)) _filteredPlugins.postValue(false to list.filterTvTypes().sortByQuery(currentQuery))
} }
// Perhaps can be optimized? // Perhaps can be optimized?
@ -191,7 +196,7 @@ class PluginsViewModel : ViewModel() {
} }
fun updateFilteredPlugins() { fun updateFilteredPlugins() {
_filteredPlugins.postValue(plugins.filterTvTypes().sortByQuery(currentQuery)) _filteredPlugins.postValue(false to plugins.filterTvTypes().sortByQuery(currentQuery))
} }
fun updatePluginList(repositoryUrl: String) = viewModelScope.launch { fun updatePluginList(repositoryUrl: String) = viewModelScope.launch {
@ -201,7 +206,7 @@ class PluginsViewModel : ViewModel() {
fun search(query: String?) { fun search(query: String?) {
currentQuery = query currentQuery = query
_filteredPlugins.postValue(filteredPlugins.value?.sortByQuery(query)) _filteredPlugins.postValue(true to (filteredPlugins.value?.second?.sortByQuery(query) ?: emptyList()))
} }
/** /**
@ -217,6 +222,6 @@ class PluginsViewModel : ViewModel() {
} }
plugins = downloadedPlugins plugins = downloadedPlugins
_filteredPlugins.postValue(downloadedPlugins.filterTvTypes().sortByQuery(currentQuery)) _filteredPlugins.postValue(false to downloadedPlugins.filterTvTypes().sortByQuery(currentQuery))
} }
} }

View File

@ -11,7 +11,7 @@
<com.google.android.material.appbar.AppBarLayout <com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:background="@android:color/transparent"> android:background="?attr/primaryGrayBackground">
<com.google.android.material.appbar.MaterialToolbar <com.google.android.material.appbar.MaterialToolbar
android:id="@+id/settings_toolbar" android:id="@+id/settings_toolbar"