2021-11-23 20:38:26 +01:00
|
|
|
package org.moire.ultrasonic.model
|
2021-05-12 13:28:33 +02:00
|
|
|
|
|
|
|
import android.app.Application
|
|
|
|
import android.os.Bundle
|
|
|
|
import androidx.lifecycle.LiveData
|
|
|
|
import androidx.lifecycle.MutableLiveData
|
|
|
|
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
|
|
|
|
import org.moire.ultrasonic.api.subsonic.models.AlbumListType
|
2022-03-27 14:57:07 +02:00
|
|
|
import org.moire.ultrasonic.domain.Album
|
2021-05-12 13:28:33 +02:00
|
|
|
import org.moire.ultrasonic.service.MusicService
|
|
|
|
import org.moire.ultrasonic.util.Constants
|
2021-09-24 18:20:53 +02:00
|
|
|
import org.moire.ultrasonic.util.Settings
|
2021-05-12 13:28:33 +02:00
|
|
|
|
|
|
|
class AlbumListModel(application: Application) : GenericListModel(application) {
|
|
|
|
|
2022-03-27 14:57:07 +02:00
|
|
|
val list: MutableLiveData<List<Album>> = MutableLiveData()
|
2021-06-21 13:41:39 +02:00
|
|
|
var lastType: String? = null
|
2021-05-12 13:28:33 +02:00
|
|
|
private var loadedUntil: Int = 0
|
|
|
|
|
|
|
|
fun getAlbumList(
|
|
|
|
refresh: Boolean,
|
|
|
|
swipe: SwipeRefreshLayout,
|
|
|
|
args: Bundle
|
2022-03-27 14:57:07 +02:00
|
|
|
): LiveData<List<Album>> {
|
2021-06-21 13:41:39 +02:00
|
|
|
// Don't reload the data if navigating back to the view that was active before.
|
|
|
|
// This way, we keep the scroll position
|
2021-11-30 21:21:50 +01:00
|
|
|
val albumListType = args.getString(Constants.INTENT_ALBUM_LIST_TYPE)!!
|
2021-05-12 13:28:33 +02:00
|
|
|
|
2021-11-26 19:01:14 +01:00
|
|
|
if (refresh || list.value?.isEmpty() != false || albumListType != lastType) {
|
2021-06-21 13:41:39 +02:00
|
|
|
lastType = albumListType
|
|
|
|
backgroundLoadFromServer(refresh, swipe, args)
|
|
|
|
}
|
2021-11-23 20:38:26 +01:00
|
|
|
return list
|
|
|
|
}
|
|
|
|
|
2021-11-30 00:46:48 +01:00
|
|
|
private fun getAlbumsOfArtist(
|
|
|
|
musicService: MusicService,
|
|
|
|
refresh: Boolean,
|
|
|
|
id: String,
|
|
|
|
name: String?
|
|
|
|
) {
|
2022-04-18 07:12:31 +02:00
|
|
|
list.postValue(musicService.getAlbumsOfArtist(id, name, refresh))
|
2021-05-12 13:28:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun load(
|
|
|
|
isOffline: Boolean,
|
|
|
|
useId3Tags: Boolean,
|
|
|
|
musicService: MusicService,
|
|
|
|
refresh: Boolean,
|
|
|
|
args: Bundle
|
|
|
|
) {
|
2021-05-16 20:00:28 +02:00
|
|
|
super.load(isOffline, useId3Tags, musicService, refresh, args)
|
2021-05-12 13:28:33 +02:00
|
|
|
|
2021-11-30 21:21:50 +01:00
|
|
|
val albumListType = args.getString(Constants.INTENT_ALBUM_LIST_TYPE)!!
|
|
|
|
val size = args.getInt(Constants.INTENT_ALBUM_LIST_SIZE, 0)
|
|
|
|
var offset = args.getInt(Constants.INTENT_ALBUM_LIST_OFFSET, 0)
|
|
|
|
val append = args.getBoolean(Constants.INTENT_APPEND, false)
|
2021-05-12 13:28:33 +02:00
|
|
|
|
2022-03-27 14:57:07 +02:00
|
|
|
val musicDirectory: List<Album>
|
2021-05-16 20:00:28 +02:00
|
|
|
val musicFolderId = if (showSelectFolderHeader(args)) {
|
|
|
|
activeServerProvider.getActiveServer().musicFolderId
|
|
|
|
} else {
|
|
|
|
null
|
|
|
|
}
|
2021-05-12 13:28:33 +02:00
|
|
|
|
2021-12-05 21:07:08 +01:00
|
|
|
// If we are refreshing the random list, we want to avoid items moving across the screen,
|
|
|
|
// by clearing the list first
|
2021-12-21 23:44:18 +01:00
|
|
|
if (refresh && !append && albumListType == "random") {
|
2021-12-05 21:07:08 +01:00
|
|
|
list.postValue(listOf())
|
|
|
|
}
|
|
|
|
|
2021-05-12 13:28:33 +02:00
|
|
|
// Handle the logic for endless scrolling:
|
|
|
|
// If appending the existing list, set the offset from where to load
|
|
|
|
if (append) offset += (size + loadedUntil)
|
|
|
|
|
2021-11-23 20:38:26 +01:00
|
|
|
if (albumListType == Constants.ALBUMS_OF_ARTIST) {
|
|
|
|
return getAlbumsOfArtist(
|
|
|
|
musicService,
|
|
|
|
refresh,
|
2021-11-30 21:21:50 +01:00
|
|
|
args.getString(Constants.INTENT_ID, ""),
|
|
|
|
args.getString(Constants.INTENT_NAME, "")
|
2021-11-23 20:38:26 +01:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-05-12 13:28:33 +02:00
|
|
|
if (useId3Tags) {
|
2021-11-30 00:46:48 +01:00
|
|
|
musicDirectory =
|
|
|
|
musicService.getAlbumList2(
|
|
|
|
albumListType, size,
|
|
|
|
offset, musicFolderId
|
|
|
|
)
|
2021-05-12 13:28:33 +02:00
|
|
|
} else {
|
|
|
|
musicDirectory = musicService.getAlbumList(
|
|
|
|
albumListType, size,
|
|
|
|
offset, musicFolderId
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-05-16 20:00:28 +02:00
|
|
|
currentListIsSortable = isCollectionSortable(albumListType)
|
2021-05-12 13:28:33 +02:00
|
|
|
|
2021-11-23 20:38:26 +01:00
|
|
|
if (append && list.value != null) {
|
2022-03-27 14:57:07 +02:00
|
|
|
val newList = ArrayList<Album>()
|
2021-11-30 00:46:48 +01:00
|
|
|
newList.addAll(list.value!!)
|
|
|
|
newList.addAll(musicDirectory)
|
2021-12-05 21:07:08 +01:00
|
|
|
list.postValue(newList)
|
2021-05-12 13:28:33 +02:00
|
|
|
} else {
|
2021-11-30 00:46:48 +01:00
|
|
|
list.postValue(musicDirectory)
|
2021-05-12 13:28:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
loadedUntil = offset
|
|
|
|
}
|
|
|
|
|
2021-05-16 20:00:28 +02:00
|
|
|
override fun showSelectFolderHeader(args: Bundle?): Boolean {
|
|
|
|
if (args == null) return false
|
|
|
|
|
2021-11-30 21:21:50 +01:00
|
|
|
val albumListType = args.getString(Constants.INTENT_ALBUM_LIST_TYPE)!!
|
2021-05-16 20:00:28 +02:00
|
|
|
|
2021-05-12 13:28:33 +02:00
|
|
|
val isAlphabetical = (albumListType == AlbumListType.SORTED_BY_NAME.toString()) ||
|
|
|
|
(albumListType == AlbumListType.SORTED_BY_ARTIST.toString())
|
|
|
|
|
2021-09-24 18:20:53 +02:00
|
|
|
return !isOffline() && !Settings.shouldUseId3Tags && isAlphabetical
|
2021-05-12 13:28:33 +02:00
|
|
|
}
|
|
|
|
|
2021-05-16 20:00:28 +02:00
|
|
|
private fun isCollectionSortable(albumListType: String): Boolean {
|
2021-05-12 13:28:33 +02:00
|
|
|
return albumListType != "newest" && albumListType != "random" &&
|
|
|
|
albumListType != "highest" && albumListType != "recent" &&
|
|
|
|
albumListType != "frequent"
|
|
|
|
}
|
|
|
|
}
|