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.koin.core.component.KoinApiExtension
import org.moire.ultrasonic.R import org.moire.ultrasonic.R
import org.moire.ultrasonic.domain.MusicDirectory import org.moire.ultrasonic.domain.MusicDirectory
import org.moire.ultrasonic.domain.MusicFolder
import org.moire.ultrasonic.util.Constants import org.moire.ultrasonic.util.Constants
/** /**
@ -45,11 +44,6 @@ class AlbumListFragment : GenericListFragment<MusicDirectory.Entry, AlbumRowAdap
*/ */
override val itemClickTarget: Int = R.id.trackCollectionFragment 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 * 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) bundle.putString(Constants.INTENT_EXTRA_NAME_PARENT_ID, item.parent)
findNavController().navigate(itemClickTarget, bundle) 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, refresh: Boolean,
args: Bundle args: Bundle
) { ) {
val musicDirectory: MusicDirectory super.load(isOffline, useId3Tags, musicService, refresh, args)
val musicFolderId = if (showSelectFolderHeader) {
activeServerProvider.getActiveServer().musicFolderId
} else {
null
}
val albumListType = args.getString(Constants.INTENT_EXTRA_NAME_ALBUM_LIST_TYPE)!! val albumListType = args.getString(Constants.INTENT_EXTRA_NAME_ALBUM_LIST_TYPE)!!
val size = args.getInt(Constants.INTENT_EXTRA_NAME_ALBUM_LIST_SIZE, 0) val size = args.getInt(Constants.INTENT_EXTRA_NAME_ALBUM_LIST_SIZE, 0)
var offset = args.getInt(Constants.INTENT_EXTRA_NAME_ALBUM_LIST_OFFSET, 0) var offset = args.getInt(Constants.INTENT_EXTRA_NAME_ALBUM_LIST_OFFSET, 0)
val append = args.getBoolean(Constants.INTENT_EXTRA_NAME_APPEND, false) 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: // Handle the logic for endless scrolling:
// If appending the existing list, set the offset from where to load // 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) { if (append && albumList.value != null) {
val list = ArrayList<MusicDirectory.Entry>() val list = ArrayList<MusicDirectory.Entry>()
@ -79,14 +79,18 @@ class AlbumListModel(application: Application) : GenericListModel(application) {
loadedUntil = offset 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()) || val isAlphabetical = (albumListType == AlbumListType.SORTED_BY_NAME.toString()) ||
(albumListType == AlbumListType.SORTED_BY_ARTIST.toString()) (albumListType == AlbumListType.SORTED_BY_ARTIST.toString())
return !isOffline() && !Util.getShouldUseId3Tags() && isAlphabetical return !isOffline() && !Util.getShouldUseId3Tags() && isAlphabetical
} }
private fun sortableCollection(albumListType: String): Boolean { private fun isCollectionSortable(albumListType: String): Boolean {
return albumListType != "newest" && albumListType != "random" && return albumListType != "newest" && albumListType != "random" &&
albumListType != "highest" && albumListType != "recent" && albumListType != "highest" && albumListType != "recent" &&
albumListType != "frequent" albumListType != "frequent"

View File

@ -1,5 +1,5 @@
/* /*
* ArtistRowAdapter.kt * AlbumRowAdapter.kt
* Copyright (C) 2009-2021 Ultrasonic developers * Copyright (C) 2009-2021 Ultrasonic developers
* *
* Distributed under terms of the GNU GPLv3 license. * Distributed under terms of the GNU GPLv3 license.
@ -21,7 +21,7 @@ import org.moire.ultrasonic.util.ImageLoader
import org.moire.ultrasonic.view.SelectMusicFolderView 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( class AlbumRowAdapter(
albumList: List<MusicDirectory.Entry>, albumList: List<MusicDirectory.Entry>,

View File

@ -6,7 +6,6 @@ import androidx.lifecycle.LiveData
import org.koin.core.component.KoinApiExtension import org.koin.core.component.KoinApiExtension
import org.moire.ultrasonic.R import org.moire.ultrasonic.R
import org.moire.ultrasonic.domain.Artist import org.moire.ultrasonic.domain.Artist
import org.moire.ultrasonic.domain.MusicFolder
import org.moire.ultrasonic.util.Constants import org.moire.ultrasonic.util.Constants
/** /**
@ -61,12 +60,4 @@ class ArtistListFragment : GenericListFragment<Artist, ArtistRowAdapter>() {
imageLoaderProvider.getImageLoader() 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, refresh: Boolean,
args: Bundle args: Bundle
) { ) {
if (!isOffline && !useId3Tags) { super.load(isOffline, useId3Tags, musicService, refresh, args)
musicFolders.postValue(
musicService.getMusicFolders(refresh)
)
}
val musicFolderId = activeServer.musicFolderId 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 * 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 * Whether to show the folder selector
*/ */
internal open var folderHeaderEnabled: Boolean = true
fun showFolderHeader(): Boolean { fun showFolderHeader(): Boolean {
return folderHeaderEnabled && listModel.isOffline() && !Util.getShouldUseId3Tags() return listModel.showSelectFolderHeader(arguments) &&
!listModel.isOffline() && !Util.getShouldUseId3Tags()
} }
fun setTitle(title: String?) { 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 * An abstract Model, which can be extended to retrieve a list of items from the API
*/ */
@KoinApiExtension @KoinApiExtension
abstract class GenericListModel(application: Application) : open class GenericListModel(application: Application) :
AndroidViewModel(application), KoinComponent { AndroidViewModel(application), KoinComponent {
val activeServerProvider: ActiveServerProvider by inject() val activeServerProvider: ActiveServerProvider by inject()
@ -42,7 +42,11 @@ abstract class GenericListModel(application: Application) :
var currentListIsSortable = true var currentListIsSortable = true
var showHeader = 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() 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 * This is the central function you need to implement if you want to extend this class
*/ */
abstract fun load( open fun load(
isOffline: Boolean, isOffline: Boolean,
useId3Tags: Boolean, useId3Tags: Boolean,
musicService: MusicService, musicService: MusicService,
refresh: Boolean, refresh: Boolean,
args: Bundle 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 * Retrieves the available Music Folders in a LiveData

View File

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