Avoid unnecessary reloads when navigating back,

this preserves the scroll position.
This commit is contained in:
tzugen 2021-06-21 13:41:39 +02:00
parent 36dccc845b
commit bc16b64be4
No known key found for this signature in database
GPG Key ID: 61E9C34BC10EC930
3 changed files with 17 additions and 5 deletions

View File

@ -49,8 +49,9 @@ class AlbumListFragment : GenericListFragment<MusicDirectory.Entry, AlbumRowAdap
if (args == null) throw IllegalArgumentException("Required arguments are missing") if (args == null) throw IllegalArgumentException("Required arguments are missing")
val refresh = args.getBoolean(Constants.INTENT_EXTRA_NAME_REFRESH) val refresh = args.getBoolean(Constants.INTENT_EXTRA_NAME_REFRESH)
val append = args.getBoolean(Constants.INTENT_EXTRA_NAME_APPEND)
return listModel.getAlbumList(refresh, refreshListView!!, args) return listModel.getAlbumList(refresh or append, refreshListView!!, args)
} }
/** /**

View File

@ -13,7 +13,8 @@ import org.moire.ultrasonic.util.Util
class AlbumListModel(application: Application) : GenericListModel(application) { class AlbumListModel(application: Application) : GenericListModel(application) {
val albumList: MutableLiveData<List<MusicDirectory.Entry>> = MutableLiveData() val albumList: MutableLiveData<List<MusicDirectory.Entry>> = MutableLiveData(listOf())
var lastType: String? = null
private var loadedUntil: Int = 0 private var loadedUntil: Int = 0
fun getAlbumList( fun getAlbumList(
@ -21,8 +22,14 @@ class AlbumListModel(application: Application) : GenericListModel(application) {
swipe: SwipeRefreshLayout, swipe: SwipeRefreshLayout,
args: Bundle args: Bundle
): LiveData<List<MusicDirectory.Entry>> { ): LiveData<List<MusicDirectory.Entry>> {
// Don't reload the data if navigating back to the view that was active before.
// This way, we keep the scroll position
val albumListType = args.getString(Constants.INTENT_EXTRA_NAME_ALBUM_LIST_TYPE)!!
backgroundLoadFromServer(refresh, swipe, args) if (refresh || albumList.value!!.isEmpty() || albumListType != lastType) {
lastType = albumListType
backgroundLoadFromServer(refresh, swipe, args)
}
return albumList return albumList
} }

View File

@ -30,13 +30,17 @@ import org.moire.ultrasonic.service.MusicService
* Provides ViewModel which contains the list of available Artists * Provides ViewModel which contains the list of available Artists
*/ */
class ArtistListModel(application: Application) : GenericListModel(application) { class ArtistListModel(application: Application) : GenericListModel(application) {
private val artists: MutableLiveData<List<Artist>> = MutableLiveData() private val artists: MutableLiveData<List<Artist>> = MutableLiveData(listOf())
/** /**
* Retrieves all available Artists in a LiveData * Retrieves all available Artists in a LiveData
*/ */
fun getItems(refresh: Boolean, swipe: SwipeRefreshLayout): LiveData<List<Artist>> { fun getItems(refresh: Boolean, swipe: SwipeRefreshLayout): LiveData<List<Artist>> {
backgroundLoadFromServer(refresh, swipe) // Don't reload the data if navigating back to the view that was active before.
// This way, we keep the scroll position
if (artists.value!!.isEmpty() || refresh) {
backgroundLoadFromServer(refresh, swipe)
}
return artists return artists
} }