Merge branch 'fixAppend' into 'develop'

Fix EndlessScrolling in genre listings

Closes #1223

See merge request ultrasonic/ultrasonic!1050
This commit is contained in:
birdbird 2023-06-14 14:26:14 +00:00
commit ae7a200144
2 changed files with 14 additions and 9 deletions

View File

@ -91,7 +91,6 @@ open class TrackCollectionFragment(
private val rxBusSubscription: CompositeDisposable = CompositeDisposable() private val rxBusSubscription: CompositeDisposable = CompositeDisposable()
private var sortOrder = initialOrder private var sortOrder = initialOrder
private var offset: Int? = null
/** /**
* The id of the main layout * The id of the main layout
@ -188,13 +187,12 @@ open class TrackCollectionFragment(
private fun loadMoreTracks() { private fun loadMoreTracks() {
if (displayRandom() || navArgs.genreName != null) { if (displayRandom() || navArgs.genreName != null) {
offset = navArgs.offset + navArgs.size getLiveData(append = true)
getLiveData(refresh = true, append = true)
} }
} }
internal open fun handleRefresh() { internal open fun handleRefresh() {
getLiveData(true) getLiveData(refresh = true)
} }
internal open fun setupButtons(view: View) { internal open fun setupButtons(view: View) {
@ -553,7 +551,7 @@ open class TrackCollectionFragment(
val getVideos = navArgs.getVideos val getVideos = navArgs.getVideos
val getRandomTracks = displayRandom() val getRandomTracks = displayRandom()
val size = if (navArgs.size < 0) Settings.maxSongs else navArgs.size val size = if (navArgs.size < 0) Settings.maxSongs else navArgs.size
val offset = offset ?: navArgs.offset val offset = navArgs.offset
val refresh2 = navArgs.refresh || refresh val refresh2 = navArgs.refresh || refresh
listModel.viewModelScope.launch(handler) { listModel.viewModelScope.launch(handler) {
@ -685,7 +683,7 @@ open class TrackCollectionFragment(
override fun setOrderType(newOrder: SortOrder) { override fun setOrderType(newOrder: SortOrder) {
sortOrder = newOrder sortOrder = newOrder
getLiveData(true) getLiveData(refresh = true)
} }
override var viewCapabilities: ViewCapabilities = ViewCapabilities( override var viewCapabilities: ViewCapabilities = ViewCapabilities(

View File

@ -27,6 +27,7 @@ import org.moire.ultrasonic.util.Util
class TrackCollectionModel(application: Application) : GenericListModel(application) { class TrackCollectionModel(application: Application) : GenericListModel(application) {
val currentList: MutableLiveData<List<MusicDirectory.Child>> = MutableLiveData() val currentList: MutableLiveData<List<MusicDirectory.Child>> = MutableLiveData()
private var loadedUntil: Int = 0
/* /*
* Especially when dealing with indexes, this method can return Albums, Entries or a mix of both! * Especially when dealing with indexes, this method can return Albums, Entries or a mix of both!
@ -37,7 +38,6 @@ class TrackCollectionModel(application: Application) : GenericListModel(applicat
name: String? name: String?
) { ) {
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
val service = MusicServiceFactory.getMusicService() val service = MusicServiceFactory.getMusicService()
val musicDirectory = service.getMusicDirectory(id, name, refresh) val musicDirectory = service.getMusicDirectory(id, name, refresh)
currentListIsSortable = true currentListIsSortable = true
@ -57,11 +57,19 @@ class TrackCollectionModel(application: Application) : GenericListModel(applicat
} }
suspend fun getSongsForGenre(genre: String, count: Int, offset: Int, append: Boolean) { suspend fun getSongsForGenre(genre: String, count: Int, offset: Int, append: Boolean) {
// Handle the logic for endless scrolling:
// If appending the existing list, set the offset from where to load
var newOffset = offset
if (append) newOffset += (count + loadedUntil)
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
val service = MusicServiceFactory.getMusicService() val service = MusicServiceFactory.getMusicService()
val musicDirectory = service.getSongsByGenre(genre, count, offset) val musicDirectory = service.getSongsByGenre(genre, count, newOffset)
currentListIsSortable = false currentListIsSortable = false
updateList(musicDirectory, append) updateList(musicDirectory, append)
// Update current offset
loadedUntil = newOffset
} }
} }
@ -96,7 +104,6 @@ class TrackCollectionModel(application: Application) : GenericListModel(applicat
} }
suspend fun getRandom(size: Int, append: Boolean) { suspend fun getRandom(size: Int, append: Boolean) {
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
val service = MusicServiceFactory.getMusicService() val service = MusicServiceFactory.getMusicService()
val musicDirectory = service.getRandomSongs(size) val musicDirectory = service.getRandomSongs(size)