diff --git a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/fragment/TrackCollectionFragment.kt b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/fragment/TrackCollectionFragment.kt index ecb7df54..cd71ee7c 100644 --- a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/fragment/TrackCollectionFragment.kt +++ b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/fragment/TrackCollectionFragment.kt @@ -91,7 +91,6 @@ open class TrackCollectionFragment( private val rxBusSubscription: CompositeDisposable = CompositeDisposable() private var sortOrder = initialOrder - private var offset: Int? = null /** * The id of the main layout @@ -188,13 +187,12 @@ open class TrackCollectionFragment( private fun loadMoreTracks() { if (displayRandom() || navArgs.genreName != null) { - offset = navArgs.offset + navArgs.size - getLiveData(refresh = true, append = true) + getLiveData(append = true) } } internal open fun handleRefresh() { - getLiveData(true) + getLiveData(refresh = true) } internal open fun setupButtons(view: View) { @@ -553,7 +551,7 @@ open class TrackCollectionFragment( val getVideos = navArgs.getVideos val getRandomTracks = displayRandom() 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 listModel.viewModelScope.launch(handler) { @@ -685,7 +683,7 @@ open class TrackCollectionFragment( override fun setOrderType(newOrder: SortOrder) { sortOrder = newOrder - getLiveData(true) + getLiveData(refresh = true) } override var viewCapabilities: ViewCapabilities = ViewCapabilities( diff --git a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/model/TrackCollectionModel.kt b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/model/TrackCollectionModel.kt index df0cb4aa..22a92869 100644 --- a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/model/TrackCollectionModel.kt +++ b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/model/TrackCollectionModel.kt @@ -27,6 +27,7 @@ import org.moire.ultrasonic.util.Util class TrackCollectionModel(application: Application) : GenericListModel(application) { val currentList: MutableLiveData> = MutableLiveData() + private var loadedUntil: Int = 0 /* * 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? ) { withContext(Dispatchers.IO) { - val service = MusicServiceFactory.getMusicService() val musicDirectory = service.getMusicDirectory(id, name, refresh) currentListIsSortable = true @@ -57,11 +57,19 @@ class TrackCollectionModel(application: Application) : GenericListModel(applicat } 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) { val service = MusicServiceFactory.getMusicService() - val musicDirectory = service.getSongsByGenre(genre, count, offset) + val musicDirectory = service.getSongsByGenre(genre, count, newOffset) currentListIsSortable = false 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) { - withContext(Dispatchers.IO) { val service = MusicServiceFactory.getMusicService() val musicDirectory = service.getRandomSongs(size)