From eb0fa67431ab96b6a448f0be4d1d8a03dbf4eb62 Mon Sep 17 00:00:00 2001 From: Cem Eren <2852343+omgcem@users.noreply.github.com> Date: Sat, 5 Mar 2022 00:10:20 +0100 Subject: [PATCH 1/3] Updated references to context_menu_track to correctly identify events Additionally updated event handler to correctly play, queue, pin and download songs now that events are triggering correctly. --- .../fragment/TrackCollectionFragment.kt | 99 +++++++++++++------ 1 file changed, 67 insertions(+), 32 deletions(-) diff --git a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/fragment/TrackCollectionFragment.kt b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/fragment/TrackCollectionFragment.kt index 50e3d6e7..dc40a8ea 100644 --- a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/fragment/TrackCollectionFragment.kt +++ b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/fragment/TrackCollectionFragment.kt @@ -46,6 +46,7 @@ import org.moire.ultrasonic.util.Constants import org.moire.ultrasonic.util.EntryByDiscAndTrackComparator import org.moire.ultrasonic.util.Settings import org.moire.ultrasonic.util.Util +import org.moire.ultrasonic.util.Util.toast /** * Displays a group of tracks, eg. the songs of an album, of a playlist etc. @@ -619,56 +620,80 @@ open class TrackCollectionFragment : MultiListFragment() { menuItem: MenuItem, item: MusicDirectory.Child ): Boolean { - val entryId = item.id + val songs = getClickedSong(item) when (menuItem.itemId) { - R.id.menu_play_now -> { - downloadHandler.downloadRecursively( - this, entryId, save = false, append = false, - autoPlay = true, shuffle = false, background = false, - playNext = false, unpin = false, isArtist = false + R.id.song_menu_play_now -> { + downloadHandler.download( + fragment = this, + append = false, + save = false, + autoPlay = true, + playNext = false, + shuffle = false, + songs = songs ) } - R.id.menu_play_next -> { - downloadHandler.downloadRecursively( - this, entryId, save = false, append = false, - autoPlay = false, shuffle = false, background = false, - playNext = true, unpin = false, isArtist = false + R.id.song_menu_play_next -> { + downloadHandler.download( + fragment = this, + append = true, + save = false, + autoPlay = false, + playNext = true, + shuffle = false, + songs = songs ) } - R.id.menu_play_last -> { - downloadHandler.downloadRecursively( - this, entryId, save = false, append = true, - autoPlay = false, shuffle = false, background = false, - playNext = false, unpin = false, isArtist = false + R.id.song_menu_play_last -> { + downloadHandler.download( + fragment = this, + append = true, + save = false, + autoPlay = false, + playNext = false, + shuffle = false, + songs = songs ) } - R.id.menu_pin -> { - downloadHandler.downloadRecursively( - this, entryId, save = true, append = true, - autoPlay = false, shuffle = false, background = false, - playNext = false, unpin = false, isArtist = false + R.id.song_menu_pin -> { + toast( + context, + resources.getQuantityString( + R.plurals.select_album_n_songs_pinned, + songs.size, + songs.size + ) ) + downloadBackground(true, songs) } - R.id.menu_unpin -> { - downloadHandler.downloadRecursively( - this, entryId, save = false, append = false, - autoPlay = false, shuffle = false, background = false, - playNext = false, unpin = true, isArtist = false + R.id.song_menu_unpin -> { + toast( + context, + resources.getQuantityString( + R.plurals.select_album_n_songs_unpinned, + songs.size, + songs.size + ) ) + mediaPlayerController.unpin(songs) } - R.id.menu_download -> { - downloadHandler.downloadRecursively( - this, entryId, save = false, append = false, - autoPlay = false, shuffle = false, background = true, - playNext = false, unpin = false, isArtist = false + R.id.song_menu_download -> { + toast( + context, + resources.getQuantityString( + R.plurals.select_album_n_songs_downloaded, + songs.size, + songs.size + ) ) + downloadBackground(false, songs) } R.id.select_album_play_all -> { // TODO: Why is this being handled here?! playAll() } - R.id.menu_item_share -> { + R.id.song_menu_share -> { if (item is MusicDirectory.Entry) { shareHandler.createShare( this, listOf(item), refreshListView, @@ -683,6 +708,16 @@ open class TrackCollectionFragment : MultiListFragment() { return true } + internal fun getClickedSong(item: MusicDirectory.Child): List { + //This can probably be done better + return viewAdapter.getCurrentList().mapNotNull { + if (it is MusicDirectory.Entry && (it.id == item.id)) + it + else + null + } + } + override fun onItemClick(item: MusicDirectory.Child) { when { item.isDirectory -> { From 0961f56a7d24a8f1903200700abc4e6147a2fc5c Mon Sep 17 00:00:00 2001 From: Cem Eren <2852343+omgcem@users.noreply.github.com> Date: Sun, 6 Mar 2022 00:19:25 +0100 Subject: [PATCH 2/3] Corrected style violation --- .../org/moire/ultrasonic/fragment/TrackCollectionFragment.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/fragment/TrackCollectionFragment.kt b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/fragment/TrackCollectionFragment.kt index dc40a8ea..b8fa5b72 100644 --- a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/fragment/TrackCollectionFragment.kt +++ b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/fragment/TrackCollectionFragment.kt @@ -709,7 +709,7 @@ open class TrackCollectionFragment : MultiListFragment() { } internal fun getClickedSong(item: MusicDirectory.Child): List { - //This can probably be done better + // This can probably be done better return viewAdapter.getCurrentList().mapNotNull { if (it is MusicDirectory.Entry && (it.id == item.id)) it From 273ac8f9b8a82acdeb0a6b55eeba0a9adc9ed630 Mon Sep 17 00:00:00 2001 From: Cem Eren <2852343+omgcem@users.noreply.github.com> Date: Fri, 11 Mar 2022 19:35:18 +0100 Subject: [PATCH 3/3] Reused existing functions for track actions Updated existing functions with additional parameter so they can be used by context menu but added default value function so that existing calls on buttons can remain without parameters being passed in. --- .../fragment/TrackCollectionFragment.kt | 67 ++++--------------- 1 file changed, 14 insertions(+), 53 deletions(-) diff --git a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/fragment/TrackCollectionFragment.kt b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/fragment/TrackCollectionFragment.kt index b8fa5b72..bbc9a963 100644 --- a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/fragment/TrackCollectionFragment.kt +++ b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/fragment/TrackCollectionFragment.kt @@ -247,9 +247,10 @@ open class TrackCollectionFragment : MultiListFragment() { super.onDestroyView() } - private fun playNow(append: Boolean) { - val selectedSongs = getSelectedSongs() - + private fun playNow( + append: Boolean, + selectedSongs: List = getSelectedSongs() + ) { if (selectedSongs.isNotEmpty()) { downloadHandler.download( this, append, false, !append, playNext = false, @@ -375,7 +376,10 @@ open class TrackCollectionFragment : MultiListFragment() { downloadBackground(save, songs) } - private fun downloadBackground(save: Boolean, songs: List) { + private fun downloadBackground( + save: Boolean, + songs: List + ) { val onValid = Runnable { networkAndStorageChecker.warnIfNetworkOrStorageUnavailable() mediaPlayerController.downloadBackground(songs, save) @@ -399,9 +403,7 @@ open class TrackCollectionFragment : MultiListFragment() { onValid.run() } - internal fun delete() { - val songs = getSelectedSongs() - + internal fun delete(songs: List = getSelectedSongs()) { Util.toast( context, resources.getQuantityString( @@ -412,8 +414,7 @@ open class TrackCollectionFragment : MultiListFragment() { mediaPlayerController.delete(songs) } - internal fun unpin() { - val songs = getSelectedSongs() + internal fun unpin(songs: List = getSelectedSongs()) { Util.toast( context, resources.getQuantityString( @@ -624,19 +625,11 @@ open class TrackCollectionFragment : MultiListFragment() { when (menuItem.itemId) { R.id.song_menu_play_now -> { - downloadHandler.download( - fragment = this, - append = false, - save = false, - autoPlay = true, - playNext = false, - shuffle = false, - songs = songs - ) + playNow(false, songs) } R.id.song_menu_play_next -> { downloadHandler.download( - fragment = this, + fragment = this@TrackCollectionFragment, append = true, save = false, autoPlay = false, @@ -646,47 +639,15 @@ open class TrackCollectionFragment : MultiListFragment() { ) } R.id.song_menu_play_last -> { - downloadHandler.download( - fragment = this, - append = true, - save = false, - autoPlay = false, - playNext = false, - shuffle = false, - songs = songs - ) + playNow(true, songs) } R.id.song_menu_pin -> { - toast( - context, - resources.getQuantityString( - R.plurals.select_album_n_songs_pinned, - songs.size, - songs.size - ) - ) downloadBackground(true, songs) } R.id.song_menu_unpin -> { - toast( - context, - resources.getQuantityString( - R.plurals.select_album_n_songs_unpinned, - songs.size, - songs.size - ) - ) - mediaPlayerController.unpin(songs) + unpin(songs) } R.id.song_menu_download -> { - toast( - context, - resources.getQuantityString( - R.plurals.select_album_n_songs_downloaded, - songs.size, - songs.size - ) - ) downloadBackground(false, songs) } R.id.select_album_play_all -> {