detail: drop equality checks

Most of the list creation operations are O(1), and the stateflow
equality makes it not matter anyway.
This commit is contained in:
Alexander Capehart 2023-05-17 11:25:30 -06:00
parent 9c7e1d9fc2
commit 6cfb50a10f
No known key found for this signature in database
GPG key ID: 37DBE3621FE9AD47
7 changed files with 11 additions and 42 deletions

View file

@ -46,8 +46,6 @@ import org.oxycblt.auxio.util.*
* [ViewModel] that manages the Song, Album, Artist, and Genre detail views. Keeps track of the * [ViewModel] that manages the Song, Album, Artist, and Genre detail views. Keeps track of the
* current item they are showing, sub-data to display, and configuration. * current item they are showing, sub-data to display, and configuration.
* *
* FIXME: Need to do direct item comparison in equality checks, or reset on navigation.
*
* @author Alexander Capehart (OxygenCobalt) * @author Alexander Capehart (OxygenCobalt)
*/ */
@HiltViewModel @HiltViewModel
@ -163,9 +161,7 @@ constructor(
var playlistSongSort: Sort var playlistSongSort: Sort
get() = musicSettings.playlistSongSort get() = musicSettings.playlistSongSort
set(value) { set(value) {
logD(value)
musicSettings.playlistSongSort = value musicSettings.playlistSongSort = value
logD(musicSettings.playlistSongSort)
// Refresh the playlist list to reflect the new sort. // Refresh the playlist list to reflect the new sort.
currentPlaylist.value?.let { refreshPlaylistList(it, true) } currentPlaylist.value?.let { refreshPlaylistList(it, true) }
} }
@ -234,10 +230,6 @@ constructor(
* @param uid The UID of the [Song] to load. Must be valid. * @param uid The UID of the [Song] to load. Must be valid.
*/ */
fun setSongUid(uid: Music.UID) { fun setSongUid(uid: Music.UID) {
if (_currentSong.value?.uid == uid) {
// Nothing to do.
return
}
logD("Opening Song [uid: $uid]") logD("Opening Song [uid: $uid]")
_currentSong.value = musicRepository.deviceLibrary?.findSong(uid)?.also(::refreshAudioInfo) _currentSong.value = musicRepository.deviceLibrary?.findSong(uid)?.also(::refreshAudioInfo)
} }
@ -249,10 +241,6 @@ constructor(
* @param uid The [Music.UID] of the [Album] to update [currentAlbum] to. Must be valid. * @param uid The [Music.UID] of the [Album] to update [currentAlbum] to. Must be valid.
*/ */
fun setAlbumUid(uid: Music.UID) { fun setAlbumUid(uid: Music.UID) {
if (_currentAlbum.value?.uid == uid) {
// Nothing to do.
return
}
logD("Opening Album [uid: $uid]") logD("Opening Album [uid: $uid]")
_currentAlbum.value = _currentAlbum.value =
musicRepository.deviceLibrary?.findAlbum(uid)?.also(::refreshAlbumList) musicRepository.deviceLibrary?.findAlbum(uid)?.also(::refreshAlbumList)
@ -265,10 +253,6 @@ constructor(
* @param uid The [Music.UID] of the [Artist] to update [currentArtist] to. Must be valid. * @param uid The [Music.UID] of the [Artist] to update [currentArtist] to. Must be valid.
*/ */
fun setArtistUid(uid: Music.UID) { fun setArtistUid(uid: Music.UID) {
if (_currentArtist.value?.uid == uid) {
// Nothing to do.
return
}
logD("Opening Artist [uid: $uid]") logD("Opening Artist [uid: $uid]")
_currentArtist.value = _currentArtist.value =
musicRepository.deviceLibrary?.findArtist(uid)?.also(::refreshArtistList) musicRepository.deviceLibrary?.findArtist(uid)?.also(::refreshArtistList)
@ -281,10 +265,6 @@ constructor(
* @param uid The [Music.UID] of the [Genre] to update [currentGenre] to. Must be valid. * @param uid The [Music.UID] of the [Genre] to update [currentGenre] to. Must be valid.
*/ */
fun setGenreUid(uid: Music.UID) { fun setGenreUid(uid: Music.UID) {
if (_currentGenre.value?.uid == uid) {
// Nothing to do.
return
}
logD("Opening Genre [uid: $uid]") logD("Opening Genre [uid: $uid]")
_currentGenre.value = _currentGenre.value =
musicRepository.deviceLibrary?.findGenre(uid)?.also(::refreshGenreList) musicRepository.deviceLibrary?.findGenre(uid)?.also(::refreshGenreList)
@ -297,10 +277,6 @@ constructor(
* @param uid The [Music.UID] of the [Playlist] to update [currentPlaylist] to. Must be valid. * @param uid The [Music.UID] of the [Playlist] to update [currentPlaylist] to. Must be valid.
*/ */
fun setPlaylistUid(uid: Music.UID) { fun setPlaylistUid(uid: Music.UID) {
if (_currentPlaylist.value?.uid == uid) {
// Nothing to do.
return
}
logD("Opening Playlist [uid: $uid]") logD("Opening Playlist [uid: $uid]")
_currentPlaylist.value = _currentPlaylist.value =
musicRepository.userLibrary?.findPlaylist(uid)?.also(::refreshPlaylistList) musicRepository.userLibrary?.findPlaylist(uid)?.also(::refreshPlaylistList)

View file

@ -170,7 +170,7 @@ private class DeviceLibraryImpl(rawSongs: List<RawSong>, settings: MusicSettings
*/ */
private fun buildSongs(rawSongs: List<RawSong>, settings: MusicSettings) = private fun buildSongs(rawSongs: List<RawSong>, settings: MusicSettings) =
Sort(Sort.Mode.ByName, Sort.Direction.ASCENDING) Sort(Sort.Mode.ByName, Sort.Direction.ASCENDING)
.songs(rawSongs.map { SongImpl(it, settings) }.distinct()) .songs(rawSongs.map { SongImpl(it, settings) }.distinctBy { it.uid })
/** /**
* Build a list of [Album]s from the given [Song]s. * Build a list of [Album]s from the given [Song]s.

View file

@ -34,6 +34,9 @@ import org.oxycblt.auxio.util.toUuidOrNull
import org.oxycblt.auxio.util.unlikelyToBeNull import org.oxycblt.auxio.util.unlikelyToBeNull
import org.oxycblt.auxio.util.update import org.oxycblt.auxio.util.update
// TODO: Entirely rework music equality such that it's not completely UID-focused and actually
// takes metadata into account
/** /**
* Library-backed implementation of [Song]. * Library-backed implementation of [Song].
* *

View file

@ -82,7 +82,6 @@ class AddToPlaylistDialog :
override fun onClick(item: PlaylistChoice, viewHolder: RecyclerView.ViewHolder) { override fun onClick(item: PlaylistChoice, viewHolder: RecyclerView.ViewHolder) {
musicModel.addToPlaylist(pickerModel.currentPendingSongs.value ?: return, item.playlist) musicModel.addToPlaylist(pickerModel.currentPendingSongs.value ?: return, item.playlist)
pickerModel.confirmPlaylistAddition()
requireContext().showToast(R.string.lng_playlist_added) requireContext().showToast(R.string.lng_playlist_added)
findNavController().navigateUp() findNavController().navigateUp()
} }

View file

@ -60,7 +60,7 @@ class NewPlaylistDialog : ViewBindingDialogFragment<DialogPlaylistNameBinding>()
} }
// TODO: Navigate to playlist if there are songs in it // TODO: Navigate to playlist if there are songs in it
musicModel.createPlaylist(name, pendingPlaylist.songs) musicModel.createPlaylist(name, pendingPlaylist.songs)
pickerModel.confirmPlaylistCreation() pickerModel.dropPendingAddition()
requireContext().showToast(R.string.lng_playlist_created) requireContext().showToast(R.string.lng_playlist_created)
} }
.setNegativeButton(R.string.lbl_cancel, null) .setNegativeButton(R.string.lbl_cancel, null)

View file

@ -105,10 +105,6 @@ class PlaylistPickerViewModel @Inject constructor(private val musicRepository: M
* @param songUids The [Music.UID]s of songs to be present in the playlist. * @param songUids The [Music.UID]s of songs to be present in the playlist.
*/ */
fun setPendingPlaylist(context: Context, songUids: Array<Music.UID>) { fun setPendingPlaylist(context: Context, songUids: Array<Music.UID>) {
if (currentPendingPlaylist.value?.songs?.map { it.uid } == songUids) {
// Nothing to do.
return
}
val deviceLibrary = musicRepository.deviceLibrary ?: return val deviceLibrary = musicRepository.deviceLibrary ?: return
val songs = songUids.mapNotNull(deviceLibrary::findSong) val songs = songUids.mapNotNull(deviceLibrary::findSong)
@ -146,15 +142,6 @@ class PlaylistPickerViewModel @Inject constructor(private val musicRepository: M
} }
} }
/** Confirm the playlist creation process as completed. */
fun confirmPlaylistCreation() {
// Confirm any playlist additions if needed, as the creation process may have been started
// by it and is still waiting on a result.
confirmPlaylistAddition()
_currentPendingPlaylist.value = null
_chosenName.value = ChosenName.Empty
}
/** /**
* Update the current [Song]s that to show playlist add choices for. Will do nothing if already * Update the current [Song]s that to show playlist add choices for. Will do nothing if already
* equal. * equal.
@ -169,8 +156,8 @@ class PlaylistPickerViewModel @Inject constructor(private val musicRepository: M
refreshPlaylistChoices(songs) refreshPlaylistChoices(songs)
} }
/** Mark the addition process as complete. */ /** Drop any pending songs to add since a playlist has already been found for them. */
fun confirmPlaylistAddition() { fun dropPendingAddition() {
_currentPendingSongs.value = null _currentPendingSongs.value = null
} }

View file

@ -62,6 +62,10 @@ open class FakeMusicRepository : MusicRepository {
throw NotImplementedError() throw NotImplementedError()
} }
override fun deletePlaylist(playlist: Playlist) {
throw NotImplementedError()
}
override fun addToPlaylist(songs: List<Song>, playlist: Playlist) { override fun addToPlaylist(songs: List<Song>, playlist: Playlist) {
throw NotImplementedError() throw NotImplementedError()
} }