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:
OxygenCobalt 2020-09-24 15:29:32 -06:00
parent ac3b3e7903
commit 35e186d8a9
8 changed files with 56 additions and 75 deletions

View file

@ -21,8 +21,8 @@ import org.oxycblt.auxio.theme.toColor
class MainFragment : Fragment() {
private val shownFragments = listOf(0, 1)
private val libraryFragment: LibraryFragment by lazy { LibraryFragment() }
private val songsFragment: SongsFragment by lazy { SongsFragment() }
private lateinit var libraryFragment: LibraryFragment
private lateinit var songsFragment: SongsFragment
private val tabIcons = listOf(
R.drawable.ic_library,
@ -81,10 +81,21 @@ class MainFragment : Fragment() {
private fun fragmentAt(position: Int): Fragment {
return when (position) {
0 -> libraryFragment
1 -> songsFragment
0 -> {
if (!::libraryFragment.isInitialized) {
libraryFragment = LibraryFragment()
}
else -> libraryFragment
libraryFragment
}
else -> {
if (!::songsFragment.isInitialized) {
songsFragment = SongsFragment()
}
songsFragment
}
}
}

View file

@ -38,16 +38,18 @@ class AlbumDetailFragment : Fragment() {
}!!
}
binding.lifecycleOwner = this
binding.detailModel = detailModel
binding.album = detailModel.currentAlbum
binding.songRecycler.adapter = DetailSongAdapter(
val songAdapter = DetailSongAdapter(
detailModel.currentAlbum!!.songs,
ClickListener {
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.setHasFixedSize(true)

View file

@ -14,7 +14,6 @@ import org.oxycblt.auxio.detail.adapters.DetailAlbumAdapter
import org.oxycblt.auxio.music.MusicViewModel
import org.oxycblt.auxio.music.models.Album
import org.oxycblt.auxio.recycler.ClickListener
import org.oxycblt.auxio.recycler.SortMode
import org.oxycblt.auxio.theme.applyDivider
class ArtistDetailFragment : Fragment() {
@ -39,36 +38,19 @@ class ArtistDetailFragment : Fragment() {
}
val albumAdapter = DetailAlbumAdapter(
detailModel.currentArtist!!.albums,
ClickListener {
navToAlbum(it)
}
)
binding.lifecycleOwner = this
binding.detailModel = detailModel
binding.artist = detailModel.currentArtist!!
binding.albumRecycler.adapter = albumAdapter
binding.albumRecycler.applyDivider()
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.")
return binding.root

View file

@ -5,7 +5,6 @@ import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import org.oxycblt.auxio.music.models.Album
import org.oxycblt.auxio.music.models.Artist
import org.oxycblt.auxio.recycler.SortMode
class DetailViewModel : ViewModel() {
var isAlreadyNavigating = false
@ -13,9 +12,6 @@ class DetailViewModel : ViewModel() {
private val mNavToParentArtist = MutableLiveData<Boolean>()
val navToParentArtist: LiveData<Boolean> get() = mNavToParentArtist
private val mArtistSortMode = MutableLiveData(SortMode.NUMERIC_DOWN)
val artistSortMode: LiveData<SortMode> get() = mArtistSortMode
var currentArtist: Artist? = null
var currentAlbum: Album? = null
@ -26,15 +22,4 @@ class DetailViewModel : ViewModel() {
fun doneWithNavToParent() {
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
}
}
}

View file

@ -2,16 +2,17 @@ package org.oxycblt.auxio.detail.adapters
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import org.oxycblt.auxio.databinding.ItemAlbumBinding
import org.oxycblt.auxio.music.models.Album
import org.oxycblt.auxio.recycler.ClickListener
import org.oxycblt.auxio.recycler.DiffCallback
class DetailAlbumAdapter(
private val data: List<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 {
return ViewHolder(
@ -20,7 +21,7 @@ class DetailAlbumAdapter(
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.bind(getItem(position))
holder.bind(data[position])
}
// Generic ViewHolder for an album

View file

@ -31,8 +31,10 @@ class MusicSorter(
for (album in albums) {
// Find all songs that match the current album ID to prevent any bugs w/comparing names.
// This cant be done with artists/genres sadly.
val albumSongs = songs.filter { it.albumId == album.id }
// Also sort them by their track number.
val albumSongs = songs.filter {
it.albumId == album.id
}.sortedBy { it.track }
// Then add them to the album
for (song in albumSongs) {
@ -72,8 +74,10 @@ class MusicSorter(
val unknownAlbums = albums.toMutableList()
for (artist in artists) {
// Find all albums that match the current artist name
val artistAlbums = albums.filter { it.artistName == 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
}.sortedByDescending { it.year }
// Then add them to the artist, along with refreshing the amount of albums
for (album in artistAlbums) {

View file

@ -3,12 +3,13 @@ package org.oxycblt.auxio.recycler
import androidx.recyclerview.widget.DiffUtil
import org.oxycblt.auxio.R
import org.oxycblt.auxio.music.models.Album
import org.oxycblt.auxio.music.models.Song
// RecyclerView click listener
class ClickListener<T>(val onClick: (T) -> Unit)
// Diff callback
class DiffCallback : DiffUtil.ItemCallback<Album>() {
// Diff callback for albums
class AlbumDiffCallback : DiffUtil.ItemCallback<Album>() {
override fun areContentsTheSame(oldItem: Album, newItem: Album): Boolean {
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
enum class SortMode(val iconRes: Int) {
// Icons for each mode are assigned to the enums themselves
@ -42,5 +54,10 @@ enum class SortMode(val iconRes: Int) {
String.CASE_INSENSITIVE_ORDER
) { it.name },
)
val songSortComparators = mapOf<SortMode, Comparator<Song>>(
NUMERIC_DOWN to compareBy { it.track },
NUMERIC_UP to compareByDescending { it.track }
)
}
}

View file

@ -8,10 +8,6 @@
<variable
name="artist"
type="org.oxycblt.auxio.music.models.Artist" />
<variable
name="detailModel"
type="org.oxycblt.auxio.detail.DetailViewModel" />
</data>
<LinearLayout
@ -110,23 +106,6 @@
app:layout_constraintStart_toStartOf="parent"
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
android:id="@+id/album_recycler"
android:layout_width="match_parent"