Correctly display the folder header also in album view

Also fix a number of smaller issues
This commit is contained in:
tzugen 2021-05-16 20:00:28 +02:00
parent 72c03cc500
commit 2cf80707f7
No known key found for this signature in database
GPG Key ID: 61E9C34BC10EC930
8 changed files with 43 additions and 46 deletions

View File

@ -9,7 +9,6 @@ import androidx.recyclerview.widget.RecyclerView
import org.koin.core.component.KoinApiExtension
import org.moire.ultrasonic.R
import org.moire.ultrasonic.domain.MusicDirectory
import org.moire.ultrasonic.domain.MusicFolder
import org.moire.ultrasonic.util.Constants
/**
@ -45,11 +44,6 @@ class AlbumListFragment : GenericListFragment<MusicDirectory.Entry, AlbumRowAdap
*/
override val itemClickTarget: Int = R.id.trackCollectionFragment
/**
* Whether to show the folder selector
*/
override var folderHeaderEnabled = false
/**
* The central function to pass a query to the model and return a LiveData object
*/
@ -103,8 +97,4 @@ class AlbumListFragment : GenericListFragment<MusicDirectory.Entry, AlbumRowAdap
bundle.putString(Constants.INTENT_EXTRA_NAME_PARENT_ID, item.parent)
findNavController().navigate(itemClickTarget, bundle)
}
override val musicFolderObserver = { _: List<MusicFolder> ->
// Do nothing
}
}

View File

@ -35,19 +35,19 @@ class AlbumListModel(application: Application) : GenericListModel(application) {
refresh: Boolean,
args: Bundle
) {
val musicDirectory: MusicDirectory
val musicFolderId = if (showSelectFolderHeader) {
activeServerProvider.getActiveServer().musicFolderId
} else {
null
}
super.load(isOffline, useId3Tags, musicService, refresh, args)
val albumListType = args.getString(Constants.INTENT_EXTRA_NAME_ALBUM_LIST_TYPE)!!
val size = args.getInt(Constants.INTENT_EXTRA_NAME_ALBUM_LIST_SIZE, 0)
var offset = args.getInt(Constants.INTENT_EXTRA_NAME_ALBUM_LIST_OFFSET, 0)
val append = args.getBoolean(Constants.INTENT_EXTRA_NAME_APPEND, false)
showHeader = showHeader(albumListType)
val musicDirectory: MusicDirectory
val musicFolderId = if (showSelectFolderHeader(args)) {
activeServerProvider.getActiveServer().musicFolderId
} else {
null
}
// Handle the logic for endless scrolling:
// If appending the existing list, set the offset from where to load
@ -65,7 +65,7 @@ class AlbumListModel(application: Application) : GenericListModel(application) {
)
}
currentListIsSortable = sortableCollection(albumListType)
currentListIsSortable = isCollectionSortable(albumListType)
if (append && albumList.value != null) {
val list = ArrayList<MusicDirectory.Entry>()
@ -79,14 +79,18 @@ class AlbumListModel(application: Application) : GenericListModel(application) {
loadedUntil = offset
}
private fun showHeader(albumListType: String): Boolean {
override fun showSelectFolderHeader(args: Bundle?): Boolean {
if (args == null) return false
val albumListType = args.getString(Constants.INTENT_EXTRA_NAME_ALBUM_LIST_TYPE)!!
val isAlphabetical = (albumListType == AlbumListType.SORTED_BY_NAME.toString()) ||
(albumListType == AlbumListType.SORTED_BY_ARTIST.toString())
return !isOffline() && !Util.getShouldUseId3Tags() && isAlphabetical
}
private fun sortableCollection(albumListType: String): Boolean {
private fun isCollectionSortable(albumListType: String): Boolean {
return albumListType != "newest" && albumListType != "random" &&
albumListType != "highest" && albumListType != "recent" &&
albumListType != "frequent"

View File

@ -1,5 +1,5 @@
/*
* ArtistRowAdapter.kt
* AlbumRowAdapter.kt
* Copyright (C) 2009-2021 Ultrasonic developers
*
* Distributed under terms of the GNU GPLv3 license.
@ -21,7 +21,7 @@ import org.moire.ultrasonic.util.ImageLoader
import org.moire.ultrasonic.view.SelectMusicFolderView
/**
* Creates a Row in a RecyclerView which contains the details of an Artist
* Creates a Row in a RecyclerView which contains the details of an Album
*/
class AlbumRowAdapter(
albumList: List<MusicDirectory.Entry>,

View File

@ -6,7 +6,6 @@ import androidx.lifecycle.LiveData
import org.koin.core.component.KoinApiExtension
import org.moire.ultrasonic.R
import org.moire.ultrasonic.domain.Artist
import org.moire.ultrasonic.domain.MusicFolder
import org.moire.ultrasonic.util.Constants
/**
@ -61,12 +60,4 @@ class ArtistListFragment : GenericListFragment<Artist, ArtistRowAdapter>() {
imageLoaderProvider.getImageLoader()
)
}
override val musicFolderObserver = { changedFolders: List<MusicFolder> ->
viewAdapter.notifyDataSetChanged()
selectFolderHeader!!.setData(
activeServerProvider.getActiveServer().musicFolderId,
changedFolders
)
}
}

View File

@ -49,11 +49,7 @@ class ArtistListModel(application: Application) : GenericListModel(application)
refresh: Boolean,
args: Bundle
) {
if (!isOffline && !useId3Tags) {
musicFolders.postValue(
musicService.getMusicFolders(refresh)
)
}
super.load(isOffline, useId3Tags, musicService, refresh, args)
val musicFolderId = activeServer.musicFolderId

View File

@ -88,15 +88,21 @@ abstract class GenericListFragment<T : GenericEntry, TA : GenericRowAdapter<T>>
/**
* The observer to be called if the available music folders have changed
*/
abstract val musicFolderObserver: (List<MusicFolder>) -> Unit
val musicFolderObserver = { changedFolders: List<MusicFolder> ->
viewAdapter.notifyDataSetChanged()
selectFolderHeader?.setData(
activeServerProvider.getActiveServer().musicFolderId,
changedFolders
)
Unit
}
/**
* Whether to show the folder selector
*/
internal open var folderHeaderEnabled: Boolean = true
fun showFolderHeader(): Boolean {
return folderHeaderEnabled && listModel.isOffline() && !Util.getShouldUseId3Tags()
return listModel.showSelectFolderHeader(arguments) &&
!listModel.isOffline() && !Util.getShouldUseId3Tags()
}
fun setTitle(title: String?) {

View File

@ -29,7 +29,7 @@ import org.moire.ultrasonic.util.Util
* An abstract Model, which can be extended to retrieve a list of items from the API
*/
@KoinApiExtension
abstract class GenericListModel(application: Application) :
open class GenericListModel(application: Application) :
AndroidViewModel(application), KoinComponent {
val activeServerProvider: ActiveServerProvider by inject()
@ -42,7 +42,11 @@ abstract class GenericListModel(application: Application) :
var currentListIsSortable = true
var showHeader = true
var showSelectFolderHeader = false
@Suppress("UNUSED_PARAMETER")
open fun showSelectFolderHeader(args: Bundle?): Boolean {
return true
}
internal val musicFolders: MutableLiveData<List<MusicFolder>> = MutableLiveData()
@ -96,13 +100,20 @@ abstract class GenericListModel(application: Application) :
/**
* This is the central function you need to implement if you want to extend this class
*/
abstract fun load(
open fun load(
isOffline: Boolean,
useId3Tags: Boolean,
musicService: MusicService,
refresh: Boolean,
args: Bundle
)
) {
// Update the list of available folders if enabled
if (showSelectFolderHeader(args) && !isOffline && !useId3Tags) {
musicFolders.postValue(
musicService.getMusicFolders(refresh)
)
}
}
/**
* Retrieves the available Music Folders in a LiveData

View File

@ -232,7 +232,6 @@ class TrackCollectionFragment : Fragment() {
}
val handler = CoroutineExceptionHandler { _, exception ->
println("CoroutineExceptionHandler got $exception")
Handler(Looper.getMainLooper()).post {
context?.let { CommunicationErrorHandler.handleError(exception, it) }
}
@ -727,7 +726,7 @@ class TrackCollectionFragment : Fragment() {
}
}
} else {
if (model.showSelectFolderHeader) {
if (model.showSelectFolderHeader(arguments)) {
if (albumListView!!.headerViewsCount == 0) {
albumListView!!.addHeaderView(selectFolderHeader!!.itemView, null, false)
}