Cleanup code

Cleanup a bunch of redundant, unused, and bad code.
This commit is contained in:
OxygenCobalt
2020-11-21 16:50:16 -07:00
parent 13b80585d2
commit d8a40fe219
32 changed files with 66 additions and 109 deletions
@@ -59,9 +59,9 @@ class MainFragment : Fragment() {
)
val navController = (
childFragmentManager.findFragmentById(R.id.explore_nav_host)
as NavHostFragment?
)?.findNavController()
childFragmentManager.findFragmentById(R.id.explore_nav_host)
as NavHostFragment?
)?.findNavController()
// --- UI SETUP ---
@@ -90,7 +90,7 @@ class AlbumDetailFragment : DetailFragment() {
}
// Don't enable the sort button if there's only one song [or less]
if (detailModel.currentAlbum.value!!.numSongs < 2) {
if (detailModel.currentAlbum.value!!.songs.size < 2) {
binding.albumSortButton.disable(requireContext())
}
@@ -86,7 +86,7 @@ class ArtistDetailFragment : DetailFragment() {
}
// Disable the sort button if there is only one album [Or less]
if (detailModel.currentArtist.value!!.numAlbums < 2) {
if (detailModel.currentArtist.value!!.albums.size < 2) {
binding.artistSortButton.disable(requireContext())
}
@@ -86,7 +86,7 @@ class GenreDetailFragment : DetailFragment() {
}
// Disable the sort button if there is only one artist [Or less]
if (detailModel.currentGenre.value!!.numArtists < 2) {
if (detailModel.currentGenre.value!!.artists.size < 2) {
binding.genreSortButton.disable(requireContext())
}
@@ -22,7 +22,7 @@ class LibraryViewModel : ViewModel() {
val searchHasFocus: Boolean get() = mSearchHasFocus
// TODO: Move these to prefs when they're added
private val mShowMode = MutableLiveData(ShowMode.SHOW_GENRES)
private val mShowMode = MutableLiveData(ShowMode.SHOW_ARTISTS)
val showMode: LiveData<ShowMode> get() = mShowMode
private val mSortMode = MutableLiveData(SortMode.ALPHA_DOWN)
@@ -45,7 +45,6 @@ data class Song(
* @property year The year this album was released. 0 if there is none in the metadata.
* @property artist The Album's parent [Artist]. use this instead of [artistId]
* @property songs The Album's child [Song]s.
* @property numSongs The amount of songs in an Album.
* @property totalDuration The combined duration of all of the album's child songs, formatted.
* @author OxygenCobalt
*/
@@ -59,7 +58,6 @@ data class Album(
lateinit var artist: Artist
val songs = mutableListOf<Song>()
val numSongs: Int get() = songs.size
val totalDuration: String by lazy {
var seconds: Long = 0
songs.forEach {
@@ -80,15 +78,7 @@ data class Artist(
val albums = mutableListOf<Album>()
val genres = mutableListOf<Genre>()
val numAlbums: Int get() = albums.size
val numSongs: Int by lazy {
var num = 0
albums.forEach {
num += it.numSongs
}
num
}
val songs: MutableList<Song> by lazy {
val songs: List<Song> by lazy {
val songs = mutableListOf<Song>()
albums.forEach {
songs.addAll(it.songs)
@@ -109,22 +99,14 @@ data class Genre(
) : BaseModel() {
val artists = mutableListOf<Artist>()
val numArtists: Int get() = artists.size
val numAlbums: Int by lazy {
var num = 0
val albums: List<Album> by lazy {
val albums = mutableListOf<Album>()
artists.forEach {
num += it.numAlbums
albums.addAll(it.albums)
}
num
albums
}
val numSongs: Int by lazy {
var num = 0
artists.forEach {
num += it.numSongs
}
num
}
val songs: MutableList<Song> by lazy {
val songs: List<Song> by lazy {
val songs = mutableListOf<Song>()
artists.forEach {
songs.addAll(it.songs)
@@ -92,10 +92,10 @@ fun Int.toYear(context: Context): String {
@BindingAdapter("genreCounts")
fun TextView.bindGenreCounts(genre: Genre) {
val artists = context.resources.getQuantityString(
R.plurals.format_artist_count, genre.numArtists, genre.numArtists
R.plurals.format_artist_count, genre.artists.size, genre.artists.size
)
val albums = context.resources.getQuantityString(
R.plurals.format_album_count, genre.numAlbums, genre.numAlbums
R.plurals.format_album_count, genre.albums.size, genre.albums.size
)
text = context.getString(R.string.format_double_counts, artists, albums)
@@ -116,10 +116,10 @@ fun TextView.bindArtistGenre(artist: Artist) {
@BindingAdapter("artistCounts")
fun TextView.bindArtistCounts(artist: Artist) {
val albums = context.resources.getQuantityString(
R.plurals.format_album_count, artist.numAlbums, artist.numAlbums
R.plurals.format_album_count, artist.albums.size, artist.albums.size
)
val songs = context.resources.getQuantityString(
R.plurals.format_song_count, artist.numSongs, artist.numSongs
R.plurals.format_song_count, artist.songs.size, artist.songs.size
)
text = context.getString(R.string.format_double_counts, albums, songs)
@@ -133,7 +133,7 @@ fun TextView.bindAllAlbumDetails(album: Album) {
album.year.toYear(context),
context.resources.getQuantityString(
R.plurals.format_song_count,
album.numSongs, album.numSongs
album.songs.size, album.songs.size
),
album.totalDuration
)
@@ -147,7 +147,7 @@ fun TextView.bindAlbumInfo(album: Album) {
album.artist.name,
context.resources.getQuantityString(
R.plurals.format_song_count,
album.numSongs, album.numSongs
album.songs.size, album.songs.size
)
)
}
@@ -3,7 +3,6 @@ package org.oxycblt.auxio.music.coil
import android.content.Context
import android.graphics.Bitmap
import android.net.Uri
import android.util.Log
import android.widget.ImageView
import androidx.core.graphics.drawable.toBitmap
import androidx.databinding.BindingAdapter
@@ -16,7 +15,7 @@ import org.oxycblt.auxio.music.Genre
import org.oxycblt.auxio.music.Song
// Get a bitmap for a song, onDone will be called when the bitmap is loaded.
// Don't use this on UI elements, thats what the BindingAdapters are for.
// Don't use this on UI elements, that's what the BindingAdapters are for.
fun getBitmap(song: Song, context: Context, onDone: (Bitmap) -> Unit) {
Coil.enqueue(
ImageRequest.Builder(context)
@@ -57,7 +56,7 @@ fun ImageView.bindArtistImage(artist: Artist) {
val request: ImageRequest
// If there is more than one album, then create a mosaic of them.
if (artist.numAlbums >= 4) {
if (artist.albums.size >= 4) {
val uris = mutableListOf<Uri>()
for (i in 0..3) {
@@ -93,11 +92,9 @@ fun ImageView.bindArtistImage(artist: Artist) {
fun ImageView.bindGenreImage(genre: Genre) {
val request: ImageRequest
if (genre.numArtists >= 4) {
if (genre.artists.size >= 4) {
val uris = mutableListOf<Uri>()
Log.d(this::class.simpleName, genre.numAlbums.toString())
// Get the Nth cover from each artist, if possible.
for (i in 0..3) {
val artist = genre.artists[i]
@@ -21,7 +21,7 @@ enum class MusicLoaderResponse {
// Class that loads music from the FileSystem.
// TODO: Add custom artist images from the filesystem
// TODO: Move genre loading of songs [Loads would take longer though]
// TODO: Move genre loading to songs [Loads would take longer though]
class MusicLoader(
private val resolver: ContentResolver,
@@ -219,7 +219,7 @@ class MusicLoader(
}
albums = albums.distinctBy {
it.name to it.artistId to it.year to it.numSongs
it.name to it.artistId to it.year
}.toMutableList()
Log.d(
@@ -57,8 +57,6 @@ fun NotificationManager.createMediaNotification(
PendingIntent.FLAG_UPDATE_CURRENT
)
// TODO: Things that probably aren't possible but would be nice
// - Playing intent takes you to PlaybackFragment instead of MainFragment
return NotificationCompat.Builder(context, NotificationUtils.CHANNEL_ID)
.setSmallIcon(R.drawable.ic_song)
.setStyle(
@@ -124,7 +122,7 @@ fun NotificationCompat.Builder.updateMode(context: Context) {
// If playing from all songs, set the subtext as that, otherwise the currently played parent.
if (playbackManager.mode == PlaybackMode.ALL_SONGS) {
setSubText(context.getString(R.string.title_all_songs))
setSubText(context.getString(R.string.label_all_songs))
} else {
setSubText(playbackManager.parent!!.name)
}
@@ -158,7 +158,7 @@ class PlaybackFragment : Fragment(), SeekBar.OnSeekBarChangeListener {
}
}
// Updates for the current duration TextView/Seekbar
// Updates for the current duration TextView/SeekBar
playbackModel.formattedPosition.observe(viewLifecycleOwner) {
binding.playbackDurationCurrent.text = it
}
@@ -80,20 +80,21 @@ class QueueAdapter(
fun removeItem(adapterIndex: Int) {
data.removeAt(adapterIndex)
notifyItemRemoved(adapterIndex)
// Check for two things:
// If the data from the next queue is now entirely empty [Signified by a header at the end]
// Or if the data from the last queue is now entirely empty [Signified by there being 2 headers with no items in between]
if (data[data.lastIndex] is Header) {
val lastIndex = data.lastIndex
// TODO: Do notifyItemRangeRemoved instead of notifyItemRemoved
data.removeAt(lastIndex)
notifyItemRemoved(lastIndex)
notifyItemRangeRemoved(lastIndex, 2)
} else if (data.lastIndex >= 1 && data[0] is Header && data[1] is Header) {
data.removeAt(0)
notifyItemRemoved(0)
notifyItemRangeRemoved(0, 2)
} else {
notifyItemRemoved(adapterIndex)
}
}
@@ -85,7 +85,7 @@ class QueueFragment : Fragment() {
name = getString(
R.string.format_next_from,
if (playbackModel.mode.value == PlaybackMode.ALL_SONGS)
getString(R.string.title_all_songs)
getString(R.string.label_all_songs)
else
playbackModel.parent.value!!.name
)
@@ -133,7 +133,7 @@ class PlaybackStateManager private constructor() {
PlaybackMode.IN_ARTIST -> {
mParent = song.album.artist
mQueue = song.album.artist.songs
mQueue = song.album.artist.songs.toMutableList()
}
PlaybackMode.IN_ALBUM -> {
@@ -425,7 +425,7 @@ class PlaybackStateManager private constructor() {
}
// --- PERSISTENCE FUNCTIONS ---
// TODO: Implement a fast queue save function that can be enabled in settings
// TODO: Implement a fast queue save function
suspend fun saveStateToDatabase(context: Context) {
Log.d(this::class.simpleName, "Saving state to DB.")
@@ -557,15 +557,15 @@ class PlaybackStateManager private constructor() {
// Traverse albums and then album songs instead of just the songs, as its faster.
musicStore.albums.find { it.id == item.albumId }
?.songs?.find { it.id == item.songId }?.let {
mQueue.add(it)
}
mQueue.add(it)
}
}
userQueueItems.forEach { item ->
musicStore.albums.find { it.id == item.albumId }
?.songs?.find { it.id == item.songId }?.let {
mUserQueue.add(it)
}
mUserQueue.add(it)
}
}
forceQueueUpdate()
@@ -6,7 +6,7 @@ import org.oxycblt.auxio.music.BaseModel
// Base Diff callback
class DiffCallback<T : BaseModel> : DiffUtil.ItemCallback<T>() {
override fun areContentsTheSame(oldItem: T, newItem: T): Boolean {
return oldItem == newItem
return oldItem.hashCode() == newItem.hashCode()
}
override fun areItemsTheSame(oldItem: T, newItem: T): Boolean {
@@ -43,7 +43,7 @@ fun ImageButton.disable(context: Context) {
}
fun String.createToast(context: Context) {
Toast.makeText(context, this, Toast.LENGTH_SHORT).show()
Toast.makeText(context.applicationContext, this, Toast.LENGTH_SHORT).show()
}
// Apply a custom vertical divider