From 5b3f17dee741afca243ff5e1a68bdb6d720e7327 Mon Sep 17 00:00:00 2001 From: mattcarter11 <38189440+mattcarter11@users.noreply.github.com> Date: Fri, 3 Jan 2025 16:48:25 +0100 Subject: [PATCH] refactor: move select logic to parent --- .../dd3boh/outertune/ui/component/Items.kt | 36 +++++++------------ .../outertune/ui/screens/AlbumScreen.kt | 11 ++++-- .../outertune/ui/screens/HistoryScreen.kt | 11 ++++-- .../dd3boh/outertune/ui/screens/HomeScreen.kt | 18 +++++----- .../outertune/ui/screens/SetupWizzard.kt | 7 ++-- .../outertune/ui/screens/StatsScreen.kt | 4 +-- .../ui/screens/artist/ArtistItemsScreen.kt | 1 + .../ui/screens/artist/ArtistScreen.kt | 4 +-- .../ui/screens/artist/ArtistSongsScreen.kt | 11 ++++-- .../screens/library/LibraryFoldersScreen.kt | 15 +++++--- .../ui/screens/library/LibrarySongsScreen.kt | 11 ++++-- .../ui/screens/playlist/AutoPlaylistScreen.kt | 11 ++++-- .../screens/playlist/LocalPlaylistScreen.kt | 11 ++++-- .../ui/screens/search/LocalSearchScreen.kt | 4 +-- 14 files changed, 96 insertions(+), 59 deletions(-) diff --git a/app/src/main/java/com/dd3boh/outertune/ui/component/Items.kt b/app/src/main/java/com/dd3boh/outertune/ui/component/Items.kt index ac497d270..e84e384c3 100644 --- a/app/src/main/java/com/dd3boh/outertune/ui/component/Items.kt +++ b/app/src/main/java/com/dd3boh/outertune/ui/component/Items.kt @@ -355,9 +355,9 @@ fun GridItem( fun SongListItem( song: Song, onPlay: () -> Unit, - onSelectModeActivation : () -> Unit, - inSelectMode : Boolean, - selectionIds: MutableList?, + onSelectedChange: (Boolean) -> Unit, + inSelectMode: Boolean?, + isSelected: Boolean, navController: NavController, modifier: Modifier = Modifier, enableSwipeToQueue: Boolean = true, @@ -366,8 +366,8 @@ fun SongListItem( showInLibraryIcon: Boolean = true, showDownloadIcon: Boolean = true, showLocalIcon: Boolean = true, - playlistSong : PlaylistSong? = null, - playlistBrowseId : String? = null, + playlistSong: PlaylistSong? = null, + playlistBrowseId: String? = null, showDragHandle: Boolean = false, dragHandleModifier: Modifier? = null, disableShowMenu: Boolean = false, @@ -385,15 +385,6 @@ fun SongListItem( val snackbarHostState = remember { SnackbarHostState() } - val isSelected = selectionIds?.contains(song.id) == true - val onCheckedChange: (Boolean) -> Unit = { - if (it) { - selectionIds?.add(song.id) - } else { - selectionIds?.remove(song.id) - } - } - val listItem: @Composable () -> Unit = { ListItem( title = song.song.title, @@ -429,10 +420,10 @@ fun SongListItem( }, trailingContent = { if (available) { - if (inSelectMode) { + if (inSelectMode == true) { Checkbox( checked = isSelected, - onCheckedChange = onCheckedChange + onCheckedChange = onSelectedChange ) } else { IconButton( @@ -471,14 +462,14 @@ fun SongListItem( } } }, - isSelected = inSelectMode && isSelected, + isSelected = inSelectMode == true && isSelected, isActive = isActive, available = available, modifier = modifier.combinedClickable( onClick = { if (available) { - if (inSelectMode) { - onCheckedChange(!isSelected) + if (inSelectMode == true) { + onSelectedChange(!isSelected) } else if (song.id == mediaMetadata?.id) { playerConnection.player.togglePlayPause() } else { @@ -488,7 +479,7 @@ fun SongListItem( }, onLongClick = { if (available) { - if (selectionIds == null) { + if (inSelectMode == null){ menuState.show { SongMenu( originalSong = song, @@ -497,10 +488,9 @@ fun SongListItem( ) } } - if (!inSelectMode) { + else if (!inSelectMode) { haptic.performHapticFeedback(HapticFeedbackType.LongPress) - onSelectModeActivation() - onCheckedChange(true) + onSelectedChange(true) } } } diff --git a/app/src/main/java/com/dd3boh/outertune/ui/screens/AlbumScreen.kt b/app/src/main/java/com/dd3boh/outertune/ui/screens/AlbumScreen.kt index b7cb4bf81..156f21f84 100644 --- a/app/src/main/java/com/dd3boh/outertune/ui/screens/AlbumScreen.kt +++ b/app/src/main/java/com/dd3boh/outertune/ui/screens/AlbumScreen.kt @@ -432,9 +432,16 @@ fun AlbumScreen( ) ) }, - onSelectModeActivation = { inSelectMode = true }, + onSelectedChange = { + inSelectMode = true + if (it) { + selection.add(song.id) + } else { + selection.remove(song.id) + } + }, inSelectMode = inSelectMode, - selectionIds = selection, + isSelected = selection.contains(song.id), navController = navController, modifier = Modifier.fillMaxWidth().animateItem() ) diff --git a/app/src/main/java/com/dd3boh/outertune/ui/screens/HistoryScreen.kt b/app/src/main/java/com/dd3boh/outertune/ui/screens/HistoryScreen.kt index f041a343f..71f1e5577 100644 --- a/app/src/main/java/com/dd3boh/outertune/ui/screens/HistoryScreen.kt +++ b/app/src/main/java/com/dd3boh/outertune/ui/screens/HistoryScreen.kt @@ -423,9 +423,16 @@ fun HistoryScreen( ) ) }, - onSelectModeActivation = { inSelectMode = true }, + onSelectedChange = { + inSelectMode = true + if (it) { + selection.add(event.event.id) + } else { + selection.remove(event.event.id) + } + }, inSelectMode = inSelectMode, - selectionIds = selection, + isSelected = selection.contains(event.event.id), navController = navController, modifier = Modifier.fillMaxWidth().animateItem() ) diff --git a/app/src/main/java/com/dd3boh/outertune/ui/screens/HomeScreen.kt b/app/src/main/java/com/dd3boh/outertune/ui/screens/HomeScreen.kt index 732a2e7d6..18cd43093 100644 --- a/app/src/main/java/com/dd3boh/outertune/ui/screens/HomeScreen.kt +++ b/app/src/main/java/com/dd3boh/outertune/ui/screens/HomeScreen.kt @@ -475,9 +475,9 @@ fun HomeScreen( onPlay = { playerConnection.playQueue(YouTubeQueue.radio(song!!.toMediaMetadata())) }, - onSelectModeActivation = {}, - inSelectMode = false, - selectionIds = null, + onSelectedChange = {}, + inSelectMode = null, + isSelected = false, navController = navController, modifier = Modifier.width(horizontalLazyGridItemWidth) ) @@ -509,9 +509,9 @@ fun HomeScreen( onPlay = { playerConnection.playQueue(YouTubeQueue.radio(song!!.toMediaMetadata())) }, - onSelectModeActivation = {}, - inSelectMode = false, - selectionIds = null, + onSelectedChange = {}, + inSelectMode = null, + isSelected = false, navController = navController, modifier = Modifier.width(horizontalLazyGridItemWidth) ) @@ -642,9 +642,9 @@ fun HomeScreen( onPlay = { playerConnection.playQueue(YouTubeQueue.radio(song!!.toMediaMetadata())) }, - onSelectModeActivation = {}, - inSelectMode = false, - selectionIds = null, + onSelectedChange = {}, + inSelectMode = null, + isSelected = false, navController = navController, modifier = Modifier.width(horizontalLazyGridItemWidth) ) diff --git a/app/src/main/java/com/dd3boh/outertune/ui/screens/SetupWizzard.kt b/app/src/main/java/com/dd3boh/outertune/ui/screens/SetupWizzard.kt index 676d9f27b..df49aec5a 100644 --- a/app/src/main/java/com/dd3boh/outertune/ui/screens/SetupWizzard.kt +++ b/app/src/main/java/com/dd3boh/outertune/ui/screens/SetupWizzard.kt @@ -37,7 +37,6 @@ import androidx.compose.material.icons.rounded.Backup import androidx.compose.material.icons.rounded.Contrast import androidx.compose.material.icons.rounded.DarkMode import androidx.compose.material.icons.rounded.Lyrics -import androidx.compose.material.icons.rounded.MoreVert import androidx.compose.material.icons.rounded.MusicVideo import androidx.compose.material.icons.rounded.NotInterested import androidx.compose.material.icons.rounded.Palette @@ -596,9 +595,9 @@ fun SetupWizard( SongListItem( song = song, onPlay = {}, - inSelectMode = false, - selectionIds = mutableListOf(), - onSelectModeActivation = {}, + onSelectedChange = {}, + inSelectMode = null, + isSelected = false, navController = navController, enableSwipeToQueue = false, disableShowMenu = true diff --git a/app/src/main/java/com/dd3boh/outertune/ui/screens/StatsScreen.kt b/app/src/main/java/com/dd3boh/outertune/ui/screens/StatsScreen.kt index 202848b6e..348bd48c1 100644 --- a/app/src/main/java/com/dd3boh/outertune/ui/screens/StatsScreen.kt +++ b/app/src/main/java/com/dd3boh/outertune/ui/screens/StatsScreen.kt @@ -98,9 +98,9 @@ fun StatsScreen( YouTubeQueue.radio(song.toMediaMetadata()) ) }, - onSelectModeActivation = {}, + onSelectedChange = {}, inSelectMode = false, - selectionIds = null, + isSelected = false, navController = navController, modifier = Modifier.fillMaxWidth().animateItem() ) diff --git a/app/src/main/java/com/dd3boh/outertune/ui/screens/artist/ArtistItemsScreen.kt b/app/src/main/java/com/dd3boh/outertune/ui/screens/artist/ArtistItemsScreen.kt index a9f5612f8..9755b0917 100644 --- a/app/src/main/java/com/dd3boh/outertune/ui/screens/artist/ArtistItemsScreen.kt +++ b/app/src/main/java/com/dd3boh/outertune/ui/screens/artist/ArtistItemsScreen.kt @@ -7,6 +7,7 @@ import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.asPaddingValues import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.windowInsetsPadding import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.grid.GridCells diff --git a/app/src/main/java/com/dd3boh/outertune/ui/screens/artist/ArtistScreen.kt b/app/src/main/java/com/dd3boh/outertune/ui/screens/artist/ArtistScreen.kt index 4c39c2ad4..48a25f879 100644 --- a/app/src/main/java/com/dd3boh/outertune/ui/screens/artist/ArtistScreen.kt +++ b/app/src/main/java/com/dd3boh/outertune/ui/screens/artist/ArtistScreen.kt @@ -311,9 +311,9 @@ fun ArtistScreen( ) ) }, - onSelectModeActivation = { }, + onSelectedChange = { }, inSelectMode = false, - selectionIds = null, + isSelected = false, navController = navController, modifier = Modifier.fillMaxWidth().animateItem() ) diff --git a/app/src/main/java/com/dd3boh/outertune/ui/screens/artist/ArtistSongsScreen.kt b/app/src/main/java/com/dd3boh/outertune/ui/screens/artist/ArtistSongsScreen.kt index cfc6bad3c..464d17231 100644 --- a/app/src/main/java/com/dd3boh/outertune/ui/screens/artist/ArtistSongsScreen.kt +++ b/app/src/main/java/com/dd3boh/outertune/ui/screens/artist/ArtistSongsScreen.kt @@ -187,9 +187,16 @@ fun ArtistSongsScreen( } } }, - onSelectModeActivation = { inSelectMode = true }, + onSelectedChange = { + inSelectMode = true + if (it) { + selection.add(song.id) + } else { + selection.remove(song.id) + } + }, inSelectMode = inSelectMode, - selectionIds = selection, + isSelected = selection.contains(song.id), navController = navController, modifier = Modifier.fillMaxWidth().animateItem() ) diff --git a/app/src/main/java/com/dd3boh/outertune/ui/screens/library/LibraryFoldersScreen.kt b/app/src/main/java/com/dd3boh/outertune/ui/screens/library/LibraryFoldersScreen.kt index d420c1f70..b95c1bf9b 100644 --- a/app/src/main/java/com/dd3boh/outertune/ui/screens/library/LibraryFoldersScreen.kt +++ b/app/src/main/java/com/dd3boh/outertune/ui/screens/library/LibraryFoldersScreen.kt @@ -67,8 +67,6 @@ import com.dd3boh.outertune.ui.component.SelectHeader import com.dd3boh.outertune.ui.component.SongFolderItem import com.dd3boh.outertune.ui.component.SongListItem import com.dd3boh.outertune.ui.component.SortHeader -import com.dd3boh.outertune.ui.component.SwipeToQueueBox -import com.dd3boh.outertune.ui.menu.SongMenu import com.dd3boh.outertune.utils.numberToAlpha import com.dd3boh.outertune.utils.rememberEnumPreference import com.dd3boh.outertune.utils.rememberPreference @@ -83,7 +81,7 @@ import java.util.Stack fun LibraryFoldersScreen( navController: NavController, viewModel: LibrarySongsViewModel = hiltViewModel(), - filterContent: @Composable() (() -> Unit)? = null + filterContent: @Composable (() -> Unit)? = null ) { val menuState = LocalMenuState.current val database = LocalDatabase.current @@ -325,9 +323,16 @@ fun LibraryFoldersScreen( ) ) }, - onSelectModeActivation = { inSelectMode = true }, + onSelectedChange = { + inSelectMode = true + if (it) { + selection.add(song.id) + } else { + selection.remove(song.id) + } + }, inSelectMode = inSelectMode, - selectionIds = selection, + isSelected = selection.contains(song.id), navController = navController, modifier = Modifier.fillMaxWidth().animateItem() ) diff --git a/app/src/main/java/com/dd3boh/outertune/ui/screens/library/LibrarySongsScreen.kt b/app/src/main/java/com/dd3boh/outertune/ui/screens/library/LibrarySongsScreen.kt index ddfba1e03..46fea936b 100644 --- a/app/src/main/java/com/dd3boh/outertune/ui/screens/library/LibrarySongsScreen.kt +++ b/app/src/main/java/com/dd3boh/outertune/ui/screens/library/LibrarySongsScreen.kt @@ -267,9 +267,16 @@ fun LibrarySongsScreen( ) ) }, - onSelectModeActivation = { inSelectMode = true }, + onSelectedChange = { + inSelectMode = true + if (it) { + selection.add(song.id) + } else { + selection.remove(song.id) + } + }, inSelectMode = inSelectMode, - selectionIds = selection, + isSelected = selection.contains(song.id), navController = navController, modifier = Modifier.fillMaxWidth().animateItem() ) diff --git a/app/src/main/java/com/dd3boh/outertune/ui/screens/playlist/AutoPlaylistScreen.kt b/app/src/main/java/com/dd3boh/outertune/ui/screens/playlist/AutoPlaylistScreen.kt index 54ac3307b..52228007e 100644 --- a/app/src/main/java/com/dd3boh/outertune/ui/screens/playlist/AutoPlaylistScreen.kt +++ b/app/src/main/java/com/dd3boh/outertune/ui/screens/playlist/AutoPlaylistScreen.kt @@ -535,9 +535,16 @@ fun AutoPlaylistScreen( }, showLikedIcon = playlistType != PlaylistType.LIKE, showDownloadIcon = playlistType != PlaylistType.DOWNLOAD, - onSelectModeActivation = { inSelectMode = true }, + onSelectedChange = { + inSelectMode = true + if (it) { + selection.add(song.id) + } else { + selection.remove(song.id) + } + }, inSelectMode = inSelectMode, - selectionIds = selection, + isSelected = selection.contains(song.id), navController = navController, modifier = Modifier.fillMaxWidth().background(MaterialTheme.colorScheme.background), ) diff --git a/app/src/main/java/com/dd3boh/outertune/ui/screens/playlist/LocalPlaylistScreen.kt b/app/src/main/java/com/dd3boh/outertune/ui/screens/playlist/LocalPlaylistScreen.kt index 795fab53a..5de513327 100644 --- a/app/src/main/java/com/dd3boh/outertune/ui/screens/playlist/LocalPlaylistScreen.kt +++ b/app/src/main/java/com/dd3boh/outertune/ui/screens/playlist/LocalPlaylistScreen.kt @@ -470,9 +470,16 @@ fun LocalPlaylistScreen( }, showDragHandle = sortType == PlaylistSongSortType.CUSTOM && !locked && editable, dragHandleModifier = Modifier.draggableHandle(), - onSelectModeActivation = { inSelectMode = true }, + onSelectedChange = { + inSelectMode = true + if (it) { + selection.add(song.song.id) + } else { + selection.remove(song.song.id) + } + }, inSelectMode = inSelectMode, - selectionIds = selection, + isSelected = selection.contains(song.song.id), navController = navController, modifier = Modifier.fillMaxWidth().background(MaterialTheme.colorScheme.background), playlistSong = song, diff --git a/app/src/main/java/com/dd3boh/outertune/ui/screens/search/LocalSearchScreen.kt b/app/src/main/java/com/dd3boh/outertune/ui/screens/search/LocalSearchScreen.kt index f8d648cc5..7e9fe1efb 100644 --- a/app/src/main/java/com/dd3boh/outertune/ui/screens/search/LocalSearchScreen.kt +++ b/app/src/main/java/com/dd3boh/outertune/ui/screens/search/LocalSearchScreen.kt @@ -160,9 +160,9 @@ fun LocalSearchScreen( startIndex = songs.indexOfFirst { it.id == item.id } )) }, - onSelectModeActivation = { }, + onSelectedChange = { }, inSelectMode = false, - selectionIds = null, + isSelected = false, navController = navController, modifier = Modifier.animateItem() )