Change show constants to dedicated enum
Move the ShowMode constants to a dedicated ShowMode enum in /recycler/.
This commit is contained in:
parent
7abb888108
commit
127f700700
11 changed files with 55 additions and 63 deletions
|
@ -33,6 +33,9 @@ TODOs surrounded with !s are things I tried to do, but failed for reasons includ
|
|||
/bugs/
|
||||
- Fix issue where fast navigations will cause the app to not display anything
|
||||
|
||||
/other/
|
||||
- Collapse show constants into ShowMode enum
|
||||
|
||||
To be added:
|
||||
/prefs/
|
||||
/playback/
|
|
@ -24,9 +24,7 @@ import org.oxycblt.auxio.music.Artist
|
|||
import org.oxycblt.auxio.music.BaseModel
|
||||
import org.oxycblt.auxio.music.Genre
|
||||
import org.oxycblt.auxio.music.MusicViewModel
|
||||
import org.oxycblt.auxio.theme.SHOW_ALBUMS
|
||||
import org.oxycblt.auxio.theme.SHOW_ARTISTS
|
||||
import org.oxycblt.auxio.theme.SHOW_GENRES
|
||||
import org.oxycblt.auxio.recycler.ShowMode
|
||||
import org.oxycblt.auxio.theme.applyColor
|
||||
import org.oxycblt.auxio.theme.applyDivider
|
||||
import org.oxycblt.auxio.theme.resolveAttr
|
||||
|
@ -70,8 +68,11 @@ class LibraryFragment : Fragment(), SearchView.OnQueryTextListener {
|
|||
|
||||
item.setOnActionExpandListener(object : MenuItem.OnActionExpandListener {
|
||||
override fun onMenuItemActionExpand(item: MenuItem): Boolean {
|
||||
// When opened, update the adapter to the SearchAdapter
|
||||
// And remove the sorting group
|
||||
// When opened, update the adapter to the SearchAdapter, and make the sorting
|
||||
// group invisible. The query is also reset, as if the Auxio process is
|
||||
// killed in the background while still on the search adapter, then the
|
||||
// search query will stick around if its opened again
|
||||
// TODO: Couldn't you just try to restore the search state on restart?
|
||||
binding.libraryRecycler.adapter = searchAdapter
|
||||
setGroupVisible(R.id.group_sorting, false)
|
||||
libraryModel.resetQuery()
|
||||
|
@ -80,11 +81,11 @@ class LibraryFragment : Fragment(), SearchView.OnQueryTextListener {
|
|||
}
|
||||
|
||||
override fun onMenuItemActionCollapse(item: MenuItem): Boolean {
|
||||
// When closed, switch back to LibraryAdapter, make the sorting
|
||||
// visible again, and reset the query so that the old results wont show
|
||||
// up if the search is opened again.
|
||||
// When closed, make the sorting icon visible again, change back to
|
||||
// LibraryAdapter, and reset the query.
|
||||
binding.libraryRecycler.adapter = libraryAdapter
|
||||
setGroupVisible(R.id.group_sorting, true)
|
||||
libraryModel.resetQuery()
|
||||
|
||||
return true
|
||||
}
|
||||
|
@ -118,9 +119,9 @@ class LibraryFragment : Fragment(), SearchView.OnQueryTextListener {
|
|||
// Update the adapter with the new data
|
||||
libraryAdapter.updateData(
|
||||
when (libraryModel.showMode.value) {
|
||||
SHOW_GENRES -> mode.getSortedGenreList(musicModel.genres.value!!)
|
||||
SHOW_ARTISTS -> mode.getSortedArtistList(musicModel.artists.value!!)
|
||||
SHOW_ALBUMS -> mode.getSortedAlbumList(musicModel.albums.value!!)
|
||||
ShowMode.SHOW_GENRES -> mode.getSortedGenreList(musicModel.genres.value!!)
|
||||
ShowMode.SHOW_ARTISTS -> mode.getSortedArtistList(musicModel.artists.value!!)
|
||||
ShowMode.SHOW_ALBUMS -> mode.getSortedAlbumList(musicModel.albums.value!!)
|
||||
|
||||
else -> mode.getSortedArtistList(musicModel.artists.value!!)
|
||||
}
|
||||
|
@ -162,18 +163,23 @@ class LibraryFragment : Fragment(), SearchView.OnQueryTextListener {
|
|||
}
|
||||
|
||||
private fun navToItem(baseModel: BaseModel) {
|
||||
Log.d(this::class.simpleName, "Navigating to the detail fragment for ${baseModel.name}")
|
||||
|
||||
if (!libraryModel.isNavigating) {
|
||||
libraryModel.updateNavigationStatus(true)
|
||||
|
||||
Log.d(this::class.simpleName, "Navigating to the detail fragment for ${baseModel.name}")
|
||||
|
||||
findNavController().navigate(
|
||||
when (baseModel) {
|
||||
is Genre -> MainFragmentDirections.actionShowGenre(baseModel.id)
|
||||
is Artist -> MainFragmentDirections.actionShowArtist(baseModel.id)
|
||||
is Album -> MainFragmentDirections.actionShowAlbum(baseModel.id, true)
|
||||
|
||||
else -> return
|
||||
// If given model wasn't valid, then reset the navigation status
|
||||
// and abort the navigation.
|
||||
else -> {
|
||||
libraryModel.updateNavigationStatus(false)
|
||||
return
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
|
|
|
@ -10,10 +10,8 @@ import org.oxycblt.auxio.R
|
|||
import org.oxycblt.auxio.music.BaseModel
|
||||
import org.oxycblt.auxio.music.Header
|
||||
import org.oxycblt.auxio.music.MusicViewModel
|
||||
import org.oxycblt.auxio.recycler.ShowMode
|
||||
import org.oxycblt.auxio.recycler.SortMode
|
||||
import org.oxycblt.auxio.theme.SHOW_ALBUMS
|
||||
import org.oxycblt.auxio.theme.SHOW_ARTISTS
|
||||
import org.oxycblt.auxio.theme.SHOW_SONGS
|
||||
|
||||
class LibraryViewModel : ViewModel() {
|
||||
private var mIsNavigating = false
|
||||
|
@ -23,8 +21,8 @@ class LibraryViewModel : ViewModel() {
|
|||
val searchHasFocus: Boolean get() = mSearchHasFocus
|
||||
|
||||
// TODO: Move these to prefs when they're added
|
||||
private val mShowMode = MutableLiveData(SHOW_ARTISTS)
|
||||
val showMode: LiveData<Int> get() = mShowMode
|
||||
private val mShowMode = MutableLiveData(ShowMode.SHOW_ARTISTS)
|
||||
val showMode: LiveData<ShowMode> get() = mShowMode
|
||||
|
||||
private val mSortMode = MutableLiveData(SortMode.ALPHA_DOWN)
|
||||
val sortMode: LiveData<SortMode> get() = mSortMode
|
||||
|
@ -62,21 +60,21 @@ class LibraryViewModel : ViewModel() {
|
|||
val artists = musicModel.artists.value!!.filter { it.name.contains(query, true) }
|
||||
|
||||
if (artists.isNotEmpty()) {
|
||||
combined.add(Header(id = SHOW_ARTISTS.toLong()))
|
||||
combined.add(Header(id = ShowMode.SHOW_ARTISTS.constant))
|
||||
combined.addAll(artists)
|
||||
}
|
||||
|
||||
val albums = musicModel.albums.value!!.filter { it.name.contains(query, true) }
|
||||
|
||||
if (albums.isNotEmpty()) {
|
||||
combined.add(Header(id = SHOW_ALBUMS.toLong()))
|
||||
combined.add(Header(id = ShowMode.SHOW_ALBUMS.constant))
|
||||
combined.addAll(albums)
|
||||
}
|
||||
|
||||
val songs = musicModel.songs.value!!.filter { it.name.contains(query, true) }
|
||||
|
||||
if (songs.isNotEmpty()) {
|
||||
combined.add(Header(id = SHOW_SONGS.toLong()))
|
||||
combined.add(Header(id = ShowMode.SHOW_SONGS.constant))
|
||||
combined.addAll(songs)
|
||||
}
|
||||
|
||||
|
|
|
@ -7,17 +7,15 @@ import org.oxycblt.auxio.music.Artist
|
|||
import org.oxycblt.auxio.music.BaseModel
|
||||
import org.oxycblt.auxio.music.Genre
|
||||
import org.oxycblt.auxio.recycler.ClickListener
|
||||
import org.oxycblt.auxio.recycler.ShowMode
|
||||
import org.oxycblt.auxio.recycler.viewholders.AlbumViewHolder
|
||||
import org.oxycblt.auxio.recycler.viewholders.ArtistViewHolder
|
||||
import org.oxycblt.auxio.recycler.viewholders.GenreViewHolder
|
||||
import org.oxycblt.auxio.theme.SHOW_ALBUMS
|
||||
import org.oxycblt.auxio.theme.SHOW_ARTISTS
|
||||
import org.oxycblt.auxio.theme.SHOW_GENRES
|
||||
|
||||
// A ListAdapter that can contain three different types of ViewHolders depending
|
||||
// the showmode given. It cannot display multiple types of viewholders *at once*.
|
||||
class LibraryAdapter(
|
||||
private val showMode: Int,
|
||||
private val showMode: ShowMode,
|
||||
private val doOnClick: (BaseModel) -> Unit
|
||||
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
|
||||
|
||||
|
@ -32,9 +30,9 @@ class LibraryAdapter(
|
|||
init {
|
||||
// Assign the data on startup depending on the type
|
||||
data = when (showMode) {
|
||||
SHOW_GENRES -> listOf<Genre>()
|
||||
SHOW_ALBUMS -> listOf<Album>()
|
||||
SHOW_ARTISTS -> listOf<Artist>()
|
||||
ShowMode.SHOW_GENRES -> listOf<Genre>()
|
||||
ShowMode.SHOW_ARTISTS -> listOf<Artist>()
|
||||
ShowMode.SHOW_ALBUMS -> listOf<Album>()
|
||||
|
||||
else -> listOf<Artist>()
|
||||
}
|
||||
|
@ -45,18 +43,18 @@ class LibraryAdapter(
|
|||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
|
||||
// Return a different View Holder depending on the show type
|
||||
return when (showMode) {
|
||||
SHOW_GENRES -> GenreViewHolder.from(parent.context, genreListener)
|
||||
SHOW_ARTISTS -> ArtistViewHolder.from(parent.context, artistListener)
|
||||
SHOW_ALBUMS -> AlbumViewHolder.from(parent.context, albumListener)
|
||||
ShowMode.SHOW_GENRES -> GenreViewHolder.from(parent.context, genreListener)
|
||||
ShowMode.SHOW_ARTISTS -> ArtistViewHolder.from(parent.context, artistListener)
|
||||
ShowMode.SHOW_ALBUMS -> AlbumViewHolder.from(parent.context, albumListener)
|
||||
else -> ArtistViewHolder.from(parent.context, artistListener)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
|
||||
when (showMode) {
|
||||
SHOW_GENRES -> (holder as GenreViewHolder).bind(data[position] as Genre)
|
||||
SHOW_ARTISTS -> (holder as ArtistViewHolder).bind(data[position] as Artist)
|
||||
SHOW_ALBUMS -> (holder as AlbumViewHolder).bind(data[position] as Album)
|
||||
ShowMode.SHOW_GENRES -> (holder as GenreViewHolder).bind(data[position] as Genre)
|
||||
ShowMode.SHOW_ARTISTS -> (holder as ArtistViewHolder).bind(data[position] as Artist)
|
||||
ShowMode.SHOW_ALBUMS -> (holder as AlbumViewHolder).bind(data[position] as Album)
|
||||
|
||||
else -> return
|
||||
}
|
||||
|
|
|
@ -23,7 +23,7 @@ class SearchAdapter(
|
|||
|
||||
// Create separate listeners for each type, as a BaseModel ClickListener cant be
|
||||
// casted to a ClickListener of a class that inherits BaseModel.
|
||||
// FIXME: Maybe theres a way for this to be improved?
|
||||
// FIXME: Maybe there's a way for this to be improved?
|
||||
private val genreListener = ClickListener<Genre> { doOnClick(it) }
|
||||
private val artistListener = ClickListener<Artist> { doOnClick(it) }
|
||||
private val albumListener = ClickListener<Album> { doOnClick(it) }
|
||||
|
|
|
@ -8,9 +8,7 @@ import android.text.format.DateUtils
|
|||
import android.widget.TextView
|
||||
import androidx.databinding.BindingAdapter
|
||||
import org.oxycblt.auxio.R
|
||||
import org.oxycblt.auxio.theme.SHOW_ALBUMS
|
||||
import org.oxycblt.auxio.theme.SHOW_ARTISTS
|
||||
import org.oxycblt.auxio.theme.SHOW_SONGS
|
||||
import org.oxycblt.auxio.recycler.ShowMode
|
||||
|
||||
// List of ID3 genres + Winamp extensions, each index corresponds to their int value.
|
||||
// There are a lot more int-genre extensions as far as Im aware, but this works for most cases.
|
||||
|
@ -143,6 +141,7 @@ fun TextView.bindAllAlbumDetails(album: Album) {
|
|||
)
|
||||
}
|
||||
|
||||
// Get *less* miscellaneous album information. Please don't confuse this with the above.
|
||||
@BindingAdapter("albumInfo")
|
||||
fun TextView.bindAlbumInfo(album: Album) {
|
||||
text = context.getString(
|
||||
|
@ -160,21 +159,15 @@ fun TextView.bindAlbumDate(album: Album) {
|
|||
text = album.year.toYear(context)
|
||||
}
|
||||
|
||||
@BindingAdapter("albumSongCount")
|
||||
// Format the amount of songs in an album
|
||||
fun TextView.bindAlbumSongs(album: Album) {
|
||||
text = context.resources.getQuantityString(
|
||||
R.plurals.format_song_count, album.numSongs, album.numSongs
|
||||
)
|
||||
}
|
||||
|
||||
// Bind the text used by the header item
|
||||
@BindingAdapter("headerText")
|
||||
fun TextView.bindHeaderText(header: Header) {
|
||||
text = context.getString(
|
||||
when (header.id.toInt()) {
|
||||
SHOW_ARTISTS -> R.string.label_artists
|
||||
SHOW_ALBUMS -> R.string.label_albums
|
||||
SHOW_SONGS -> R.string.label_songs
|
||||
when (header.id) {
|
||||
ShowMode.SHOW_GENRES.constant -> R.string.label_genres
|
||||
ShowMode.SHOW_ARTISTS.constant -> R.string.label_artists
|
||||
ShowMode.SHOW_ALBUMS.constant -> R.string.label_albums
|
||||
ShowMode.SHOW_SONGS.constant -> R.string.label_songs
|
||||
|
||||
else -> R.string.label_artists
|
||||
}
|
||||
|
|
5
app/src/main/java/org/oxycblt/auxio/recycler/ShowMode.kt
Normal file
5
app/src/main/java/org/oxycblt/auxio/recycler/ShowMode.kt
Normal file
|
@ -0,0 +1,5 @@
|
|||
package org.oxycblt.auxio.recycler
|
||||
|
||||
enum class ShowMode(val constant: Long) {
|
||||
SHOW_GENRES(10), SHOW_ARTISTS(11), SHOW_ALBUMS(12), SHOW_SONGS(13);
|
||||
}
|
|
@ -15,7 +15,6 @@ import org.oxycblt.auxio.music.Song
|
|||
import org.oxycblt.auxio.recycler.ClickListener
|
||||
|
||||
// Basic ViewHolders for each music model.
|
||||
// FIXME: Mode these type signaturs to something sensible.
|
||||
|
||||
class GenreViewHolder private constructor(
|
||||
listener: ClickListener<Genre>,
|
||||
|
|
|
@ -1,12 +1,9 @@
|
|||
package org.oxycblt.auxio.songs
|
||||
|
||||
import android.view.LayoutInflater
|
||||
import android.view.ViewGroup
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import org.oxycblt.auxio.databinding.ItemSongBinding
|
||||
import org.oxycblt.auxio.music.Song
|
||||
import org.oxycblt.auxio.recycler.ClickListener
|
||||
import org.oxycblt.auxio.recycler.viewholders.BaseViewHolder
|
||||
import org.oxycblt.auxio.recycler.viewholders.SongViewHolder
|
||||
|
||||
class SongAdapter(
|
||||
|
|
|
@ -1,8 +0,0 @@
|
|||
package org.oxycblt.auxio.theme
|
||||
|
||||
// Preference Constants
|
||||
// TODO: Move these to dedicated enum class.
|
||||
const val SHOW_ARTISTS = 0
|
||||
const val SHOW_ALBUMS = 1
|
||||
const val SHOW_GENRES = 2
|
||||
const val SHOW_SONGS = 3
|
|
@ -14,6 +14,7 @@
|
|||
<!-- Label Namespace | Static Labels -->
|
||||
<string name="label_retry">Retry</string>
|
||||
<string name="label_grant">Grant</string>
|
||||
<string name="label_genres">Genres</string>
|
||||
<string name="label_artists">Artists</string>
|
||||
<string name="label_albums">Albums</string>
|
||||
<string name="label_songs">Songs</string>
|
||||
|
|
Loading…
Reference in a new issue