Remove sorting modes
Remove the sorting modes from the detail fragments. They are somewhat useless there and would be better suited for LibraryFragment.
This commit is contained in:
parent
ac3b3e7903
commit
35e186d8a9
8 changed files with 56 additions and 75 deletions
|
@ -21,8 +21,8 @@ import org.oxycblt.auxio.theme.toColor
|
||||||
class MainFragment : Fragment() {
|
class MainFragment : Fragment() {
|
||||||
private val shownFragments = listOf(0, 1)
|
private val shownFragments = listOf(0, 1)
|
||||||
|
|
||||||
private val libraryFragment: LibraryFragment by lazy { LibraryFragment() }
|
private lateinit var libraryFragment: LibraryFragment
|
||||||
private val songsFragment: SongsFragment by lazy { SongsFragment() }
|
private lateinit var songsFragment: SongsFragment
|
||||||
|
|
||||||
private val tabIcons = listOf(
|
private val tabIcons = listOf(
|
||||||
R.drawable.ic_library,
|
R.drawable.ic_library,
|
||||||
|
@ -81,10 +81,21 @@ class MainFragment : Fragment() {
|
||||||
|
|
||||||
private fun fragmentAt(position: Int): Fragment {
|
private fun fragmentAt(position: Int): Fragment {
|
||||||
return when (position) {
|
return when (position) {
|
||||||
0 -> libraryFragment
|
0 -> {
|
||||||
1 -> songsFragment
|
if (!::libraryFragment.isInitialized) {
|
||||||
|
libraryFragment = LibraryFragment()
|
||||||
|
}
|
||||||
|
|
||||||
else -> libraryFragment
|
libraryFragment
|
||||||
|
}
|
||||||
|
|
||||||
|
else -> {
|
||||||
|
if (!::songsFragment.isInitialized) {
|
||||||
|
songsFragment = SongsFragment()
|
||||||
|
}
|
||||||
|
|
||||||
|
songsFragment
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -38,16 +38,18 @@ class AlbumDetailFragment : Fragment() {
|
||||||
}!!
|
}!!
|
||||||
}
|
}
|
||||||
|
|
||||||
binding.lifecycleOwner = this
|
val songAdapter = DetailSongAdapter(
|
||||||
binding.detailModel = detailModel
|
|
||||||
binding.album = detailModel.currentAlbum
|
|
||||||
|
|
||||||
binding.songRecycler.adapter = DetailSongAdapter(
|
|
||||||
detailModel.currentAlbum!!.songs,
|
detailModel.currentAlbum!!.songs,
|
||||||
ClickListener {
|
ClickListener {
|
||||||
Log.d(this::class.simpleName, it.name)
|
Log.d(this::class.simpleName, it.name)
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
binding.lifecycleOwner = this
|
||||||
|
binding.detailModel = detailModel
|
||||||
|
binding.album = detailModel.currentAlbum
|
||||||
|
|
||||||
|
binding.songRecycler.adapter = songAdapter
|
||||||
binding.songRecycler.applyDivider()
|
binding.songRecycler.applyDivider()
|
||||||
binding.songRecycler.setHasFixedSize(true)
|
binding.songRecycler.setHasFixedSize(true)
|
||||||
|
|
||||||
|
|
|
@ -14,7 +14,6 @@ import org.oxycblt.auxio.detail.adapters.DetailAlbumAdapter
|
||||||
import org.oxycblt.auxio.music.MusicViewModel
|
import org.oxycblt.auxio.music.MusicViewModel
|
||||||
import org.oxycblt.auxio.music.models.Album
|
import org.oxycblt.auxio.music.models.Album
|
||||||
import org.oxycblt.auxio.recycler.ClickListener
|
import org.oxycblt.auxio.recycler.ClickListener
|
||||||
import org.oxycblt.auxio.recycler.SortMode
|
|
||||||
import org.oxycblt.auxio.theme.applyDivider
|
import org.oxycblt.auxio.theme.applyDivider
|
||||||
|
|
||||||
class ArtistDetailFragment : Fragment() {
|
class ArtistDetailFragment : Fragment() {
|
||||||
|
@ -39,36 +38,19 @@ class ArtistDetailFragment : Fragment() {
|
||||||
}
|
}
|
||||||
|
|
||||||
val albumAdapter = DetailAlbumAdapter(
|
val albumAdapter = DetailAlbumAdapter(
|
||||||
|
detailModel.currentArtist!!.albums,
|
||||||
ClickListener {
|
ClickListener {
|
||||||
navToAlbum(it)
|
navToAlbum(it)
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
binding.lifecycleOwner = this
|
binding.lifecycleOwner = this
|
||||||
binding.detailModel = detailModel
|
|
||||||
binding.artist = detailModel.currentArtist!!
|
binding.artist = detailModel.currentArtist!!
|
||||||
|
|
||||||
binding.albumRecycler.adapter = albumAdapter
|
binding.albumRecycler.adapter = albumAdapter
|
||||||
binding.albumRecycler.applyDivider()
|
binding.albumRecycler.applyDivider()
|
||||||
binding.albumRecycler.setHasFixedSize(true)
|
binding.albumRecycler.setHasFixedSize(true)
|
||||||
|
|
||||||
detailModel.artistSortMode.observe(viewLifecycleOwner) { mode ->
|
|
||||||
// Update the current sort icon
|
|
||||||
binding.sortButton.setImageResource(mode.iconRes)
|
|
||||||
|
|
||||||
// Then update the sort mode of the album adapter.
|
|
||||||
albumAdapter.submitList(
|
|
||||||
detailModel.currentArtist!!.albums.sortedWith(
|
|
||||||
SortMode.albumSortComparators.getOrDefault(
|
|
||||||
mode,
|
|
||||||
|
|
||||||
// If any invalid value is given, just default to the normal sort order.
|
|
||||||
compareByDescending { it.year }
|
|
||||||
)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
Log.d(this::class.simpleName, "Fragment created.")
|
Log.d(this::class.simpleName, "Fragment created.")
|
||||||
|
|
||||||
return binding.root
|
return binding.root
|
||||||
|
|
|
@ -5,7 +5,6 @@ import androidx.lifecycle.MutableLiveData
|
||||||
import androidx.lifecycle.ViewModel
|
import androidx.lifecycle.ViewModel
|
||||||
import org.oxycblt.auxio.music.models.Album
|
import org.oxycblt.auxio.music.models.Album
|
||||||
import org.oxycblt.auxio.music.models.Artist
|
import org.oxycblt.auxio.music.models.Artist
|
||||||
import org.oxycblt.auxio.recycler.SortMode
|
|
||||||
|
|
||||||
class DetailViewModel : ViewModel() {
|
class DetailViewModel : ViewModel() {
|
||||||
var isAlreadyNavigating = false
|
var isAlreadyNavigating = false
|
||||||
|
@ -13,9 +12,6 @@ class DetailViewModel : ViewModel() {
|
||||||
private val mNavToParentArtist = MutableLiveData<Boolean>()
|
private val mNavToParentArtist = MutableLiveData<Boolean>()
|
||||||
val navToParentArtist: LiveData<Boolean> get() = mNavToParentArtist
|
val navToParentArtist: LiveData<Boolean> get() = mNavToParentArtist
|
||||||
|
|
||||||
private val mArtistSortMode = MutableLiveData(SortMode.NUMERIC_DOWN)
|
|
||||||
val artistSortMode: LiveData<SortMode> get() = mArtistSortMode
|
|
||||||
|
|
||||||
var currentArtist: Artist? = null
|
var currentArtist: Artist? = null
|
||||||
var currentAlbum: Album? = null
|
var currentAlbum: Album? = null
|
||||||
|
|
||||||
|
@ -26,15 +22,4 @@ class DetailViewModel : ViewModel() {
|
||||||
fun doneWithNavToParent() {
|
fun doneWithNavToParent() {
|
||||||
mNavToParentArtist.value = false
|
mNavToParentArtist.value = false
|
||||||
}
|
}
|
||||||
|
|
||||||
fun incrementArtistSortMode() {
|
|
||||||
mArtistSortMode.value = when (mArtistSortMode.value) {
|
|
||||||
SortMode.NUMERIC_DOWN -> SortMode.NUMERIC_UP
|
|
||||||
SortMode.NUMERIC_UP -> SortMode.ALPHA_DOWN
|
|
||||||
SortMode.ALPHA_DOWN -> SortMode.ALPHA_UP
|
|
||||||
SortMode.ALPHA_UP -> SortMode.NUMERIC_DOWN
|
|
||||||
|
|
||||||
else -> SortMode.NUMERIC_DOWN
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,16 +2,17 @@ package org.oxycblt.auxio.detail.adapters
|
||||||
|
|
||||||
import android.view.LayoutInflater
|
import android.view.LayoutInflater
|
||||||
import android.view.ViewGroup
|
import android.view.ViewGroup
|
||||||
import androidx.recyclerview.widget.ListAdapter
|
|
||||||
import androidx.recyclerview.widget.RecyclerView
|
import androidx.recyclerview.widget.RecyclerView
|
||||||
import org.oxycblt.auxio.databinding.ItemAlbumBinding
|
import org.oxycblt.auxio.databinding.ItemAlbumBinding
|
||||||
import org.oxycblt.auxio.music.models.Album
|
import org.oxycblt.auxio.music.models.Album
|
||||||
import org.oxycblt.auxio.recycler.ClickListener
|
import org.oxycblt.auxio.recycler.ClickListener
|
||||||
import org.oxycblt.auxio.recycler.DiffCallback
|
|
||||||
|
|
||||||
class DetailAlbumAdapter(
|
class DetailAlbumAdapter(
|
||||||
|
private val data: List<Album>,
|
||||||
private val listener: ClickListener<Album>
|
private val listener: ClickListener<Album>
|
||||||
) : ListAdapter<Album, DetailAlbumAdapter.ViewHolder>(DiffCallback()) {
|
) : RecyclerView.Adapter<DetailAlbumAdapter.ViewHolder>() {
|
||||||
|
|
||||||
|
override fun getItemCount(): Int = data.size
|
||||||
|
|
||||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
|
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
|
||||||
return ViewHolder(
|
return ViewHolder(
|
||||||
|
@ -20,7 +21,7 @@ class DetailAlbumAdapter(
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
|
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
|
||||||
holder.bind(getItem(position))
|
holder.bind(data[position])
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generic ViewHolder for an album
|
// Generic ViewHolder for an album
|
||||||
|
|
|
@ -31,8 +31,10 @@ class MusicSorter(
|
||||||
|
|
||||||
for (album in albums) {
|
for (album in albums) {
|
||||||
// Find all songs that match the current album ID to prevent any bugs w/comparing names.
|
// Find all songs that match the current album ID to prevent any bugs w/comparing names.
|
||||||
// This cant be done with artists/genres sadly.
|
// Also sort them by their track number.
|
||||||
val albumSongs = songs.filter { it.albumId == album.id }
|
val albumSongs = songs.filter {
|
||||||
|
it.albumId == album.id
|
||||||
|
}.sortedBy { it.track }
|
||||||
|
|
||||||
// Then add them to the album
|
// Then add them to the album
|
||||||
for (song in albumSongs) {
|
for (song in albumSongs) {
|
||||||
|
@ -72,8 +74,10 @@ class MusicSorter(
|
||||||
val unknownAlbums = albums.toMutableList()
|
val unknownAlbums = albums.toMutableList()
|
||||||
|
|
||||||
for (artist in artists) {
|
for (artist in artists) {
|
||||||
// Find all albums that match the current artist name
|
// Find all albums that match the current artist name, and then sort them by year
|
||||||
val artistAlbums = albums.filter { it.artistName == artist.name }
|
val artistAlbums = albums.filter {
|
||||||
|
it.artistName == artist.name
|
||||||
|
}.sortedByDescending { it.year }
|
||||||
|
|
||||||
// Then add them to the artist, along with refreshing the amount of albums
|
// Then add them to the artist, along with refreshing the amount of albums
|
||||||
for (album in artistAlbums) {
|
for (album in artistAlbums) {
|
||||||
|
|
|
@ -3,12 +3,13 @@ package org.oxycblt.auxio.recycler
|
||||||
import androidx.recyclerview.widget.DiffUtil
|
import androidx.recyclerview.widget.DiffUtil
|
||||||
import org.oxycblt.auxio.R
|
import org.oxycblt.auxio.R
|
||||||
import org.oxycblt.auxio.music.models.Album
|
import org.oxycblt.auxio.music.models.Album
|
||||||
|
import org.oxycblt.auxio.music.models.Song
|
||||||
|
|
||||||
// RecyclerView click listener
|
// RecyclerView click listener
|
||||||
class ClickListener<T>(val onClick: (T) -> Unit)
|
class ClickListener<T>(val onClick: (T) -> Unit)
|
||||||
|
|
||||||
// Diff callback
|
// Diff callback for albums
|
||||||
class DiffCallback : DiffUtil.ItemCallback<Album>() {
|
class AlbumDiffCallback : DiffUtil.ItemCallback<Album>() {
|
||||||
override fun areContentsTheSame(oldItem: Album, newItem: Album): Boolean {
|
override fun areContentsTheSame(oldItem: Album, newItem: Album): Boolean {
|
||||||
return oldItem.id == newItem.id
|
return oldItem.id == newItem.id
|
||||||
}
|
}
|
||||||
|
@ -18,6 +19,17 @@ class DiffCallback : DiffUtil.ItemCallback<Album>() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Diff callback for songs
|
||||||
|
class SongDiffCallback : DiffUtil.ItemCallback<Song>() {
|
||||||
|
override fun areContentsTheSame(oldItem: Song, newItem: Song): Boolean {
|
||||||
|
return oldItem.id == newItem.id
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun areItemsTheSame(oldItem: Song, newItem: Song): Boolean {
|
||||||
|
return oldItem == newItem
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Sorting modes
|
// Sorting modes
|
||||||
enum class SortMode(val iconRes: Int) {
|
enum class SortMode(val iconRes: Int) {
|
||||||
// Icons for each mode are assigned to the enums themselves
|
// Icons for each mode are assigned to the enums themselves
|
||||||
|
@ -42,5 +54,10 @@ enum class SortMode(val iconRes: Int) {
|
||||||
String.CASE_INSENSITIVE_ORDER
|
String.CASE_INSENSITIVE_ORDER
|
||||||
) { it.name },
|
) { it.name },
|
||||||
)
|
)
|
||||||
|
|
||||||
|
val songSortComparators = mapOf<SortMode, Comparator<Song>>(
|
||||||
|
NUMERIC_DOWN to compareBy { it.track },
|
||||||
|
NUMERIC_UP to compareByDescending { it.track }
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,10 +8,6 @@
|
||||||
<variable
|
<variable
|
||||||
name="artist"
|
name="artist"
|
||||||
type="org.oxycblt.auxio.music.models.Artist" />
|
type="org.oxycblt.auxio.music.models.Artist" />
|
||||||
|
|
||||||
<variable
|
|
||||||
name="detailModel"
|
|
||||||
type="org.oxycblt.auxio.detail.DetailViewModel" />
|
|
||||||
</data>
|
</data>
|
||||||
|
|
||||||
<LinearLayout
|
<LinearLayout
|
||||||
|
@ -110,23 +106,6 @@
|
||||||
app:layout_constraintStart_toStartOf="parent"
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
app:layout_constraintTop_toBottomOf="@+id/artist_counts" />
|
app:layout_constraintTop_toBottomOf="@+id/artist_counts" />
|
||||||
|
|
||||||
<ImageButton
|
|
||||||
android:id="@+id/sort_button"
|
|
||||||
android:layout_width="wrap_content"
|
|
||||||
android:layout_height="0dp"
|
|
||||||
android:layout_marginTop="@dimen/margin_medium"
|
|
||||||
android:background="@drawable/header_dividers"
|
|
||||||
android:contentDescription="@string/description_sort_button"
|
|
||||||
android:paddingStart="@dimen/padding_medium"
|
|
||||||
android:paddingTop="@dimen/padding_small"
|
|
||||||
android:paddingEnd="@dimen/margin_medium"
|
|
||||||
android:paddingBottom="@dimen/padding_small"
|
|
||||||
android:onClick="@{() -> detailModel.incrementArtistSortMode()}"
|
|
||||||
tools:src="@drawable/ic_sort_numeric_down"
|
|
||||||
app:layout_constraintBottom_toTopOf="@+id/album_recycler"
|
|
||||||
app:layout_constraintEnd_toEndOf="parent"
|
|
||||||
app:layout_constraintTop_toBottomOf="@+id/artist_counts" />
|
|
||||||
|
|
||||||
<androidx.recyclerview.widget.RecyclerView
|
<androidx.recyclerview.widget.RecyclerView
|
||||||
android:id="@+id/album_recycler"
|
android:id="@+id/album_recycler"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
|
|
Loading…
Reference in a new issue