From fcd84cde437ef44d58d81db5c05b6f9b882ed0b1 Mon Sep 17 00:00:00 2001 From: vfsfitvnm Date: Sun, 24 Jul 2022 23:04:33 +0200 Subject: [PATCH] Revamp compose-routing --- .../ui/components/themed/MediaItemMenu.kt | 11 +- .../vimusic/ui/screens/AlbumScreen.kt | 19 +-- .../vimusic/ui/screens/ArtistScreen.kt | 19 +-- .../ui/screens/BuiltInPlaylistScreen.kt | 19 +-- .../vimusic/ui/screens/HomeScreen.kt | 13 +- .../vimusic/ui/screens/IntentUriScreen.kt | 14 +- .../vimusic/ui/screens/LocalPlaylistScreen.kt | 19 +-- .../vimusic/ui/screens/PlaylistScreen.kt | 19 +-- .../it/vfsfitvnm/vimusic/ui/screens/Routes.kt | 121 +++++------------- .../vimusic/ui/screens/SearchResultScreen.kt | 17 +-- .../vimusic/ui/screens/SearchScreen.kt | 21 +-- .../vimusic/ui/screens/SettingsScreen.kt | 21 +-- .../ui/screens/settings/AboutScreen.kt | 20 +-- .../settings/AppearanceSettingsScreen.kt | 19 +-- .../settings/BackupAndRestoreScreen.kt | 19 +-- .../screens/settings/CacheSettingsScreen.kt | 19 +-- .../screens/settings/OtherSettingsScreen.kt | 19 +-- .../screens/settings/PlayerSettingsScreen.kt | 19 +-- .../vimusic/ui/screens/settings/Routes.kt | 47 ------- .../main/kotlin/it/vfsfitvnm/route/Route.kt | 67 +++------- .../kotlin/it/vfsfitvnm/route/RouteHandler.kt | 19 ++- .../it/vfsfitvnm/route/RouteHandlerScope.kt | 25 ++-- .../kotlin/it/vfsfitvnm/route/Transitions.kt | 10 +- 23 files changed, 122 insertions(+), 474 deletions(-) delete mode 100644 app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/settings/Routes.kt diff --git a/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/components/themed/MediaItemMenu.kt b/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/components/themed/MediaItemMenu.kt index 81a67a4..7fed6c3 100644 --- a/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/components/themed/MediaItemMenu.kt +++ b/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/components/themed/MediaItemMenu.kt @@ -43,9 +43,9 @@ import it.vfsfitvnm.vimusic.transaction import it.vfsfitvnm.vimusic.ui.components.ChunkyButton import it.vfsfitvnm.vimusic.ui.components.LocalMenuState import it.vfsfitvnm.vimusic.ui.components.Pager -import it.vfsfitvnm.vimusic.ui.screens.rememberAlbumRoute -import it.vfsfitvnm.vimusic.ui.screens.rememberArtistRoute -import it.vfsfitvnm.vimusic.ui.screens.rememberCreatePlaylistRoute +import it.vfsfitvnm.vimusic.ui.screens.albumRoute +import it.vfsfitvnm.vimusic.ui.screens.artistRoute +import it.vfsfitvnm.vimusic.ui.screens.viewPlaylistsRoute import it.vfsfitvnm.vimusic.ui.styling.LocalAppearance import it.vfsfitvnm.vimusic.utils.addNext import it.vfsfitvnm.vimusic.utils.asMediaItem @@ -235,9 +235,6 @@ fun BaseMediaItemMenu( ) { val context = LocalContext.current - val albumRoute = rememberAlbumRoute() - val artistRoute = rememberArtistRoute() - MediaItemMenu( mediaItem = mediaItem, onDismiss = onDismiss, @@ -299,8 +296,6 @@ fun MediaItemMenu( Database.playlistPreviews(PlaylistSortBy.DateAdded, SortOrder.Descending) }.collectAsState(initial = emptyList(), context = Dispatchers.IO) - val viewPlaylistsRoute = rememberCreatePlaylistRoute() - Menu(modifier = modifier) { RouteHandler( transitionSpec = { diff --git a/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/AlbumScreen.kt b/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/AlbumScreen.kt index 6858d8d..020a36e 100644 --- a/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/AlbumScreen.kt +++ b/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/AlbumScreen.kt @@ -81,9 +81,7 @@ import kotlinx.coroutines.runBlocking @ExperimentalAnimationApi @Composable -fun AlbumScreen( - browseId: String -) { +fun AlbumScreen(browseId: String) { val lazyListState = rememberLazyListState() val albumResult by remember(browseId) { @@ -99,21 +97,8 @@ fun AlbumScreen( Database.albumSongs(browseId) }.collectAsState(initial = emptyList(), context = Dispatchers.IO) - val albumRoute = rememberAlbumRoute() - val artistRoute = rememberArtistRoute() - RouteHandler(listenToGlobalEmitter = true) { - albumRoute { browseId -> - AlbumScreen( - browseId = browseId ?: error("browseId cannot be null") - ) - } - - artistRoute { browseId -> - ArtistScreen( - browseId = browseId ?: error("browseId cannot be null") - ) - } + globalRoutes() host { val context = LocalContext.current diff --git a/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/ArtistScreen.kt b/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/ArtistScreen.kt index 9058f89..82fa682 100644 --- a/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/ArtistScreen.kt +++ b/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/ArtistScreen.kt @@ -63,26 +63,11 @@ import kotlinx.coroutines.runBlocking @ExperimentalAnimationApi @Composable -fun ArtistScreen( - browseId: String, -) { +fun ArtistScreen(browseId: String) { val lazyListState = rememberLazyListState() - val albumRoute = rememberAlbumRoute() - val artistRoute = rememberArtistRoute() - RouteHandler(listenToGlobalEmitter = true) { - albumRoute { browseId -> - AlbumScreen( - browseId = browseId ?: error("browseId cannot be null") - ) - } - - artistRoute { browseId -> - ArtistScreen( - browseId = browseId ?: error("browseId cannot be null") - ) - } + globalRoutes() host { val binder = LocalPlayerServiceBinder.current diff --git a/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/BuiltInPlaylistScreen.kt b/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/BuiltInPlaylistScreen.kt index d974371..4a09ab5 100644 --- a/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/BuiltInPlaylistScreen.kt +++ b/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/BuiltInPlaylistScreen.kt @@ -54,26 +54,11 @@ import kotlinx.coroutines.flow.map @ExperimentalAnimationApi @Composable -fun BuiltInPlaylistScreen( - builtInPlaylist: BuiltInPlaylist, -) { +fun BuiltInPlaylistScreen(builtInPlaylist: BuiltInPlaylist) { val lazyListState = rememberLazyListState() - val albumRoute = rememberAlbumRoute() - val artistRoute = rememberArtistRoute() - RouteHandler(listenToGlobalEmitter = true) { - albumRoute { browseId -> - AlbumScreen( - browseId = browseId ?: error("browseId cannot be null") - ) - } - - artistRoute { browseId -> - ArtistScreen( - browseId = browseId ?: error("browseId cannot be null") - ) - } + globalRoutes() host { val menuState = LocalMenuState.current diff --git a/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/HomeScreen.kt b/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/HomeScreen.kt index 80b4b9b..0fb3f8c 100644 --- a/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/HomeScreen.kt +++ b/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/HomeScreen.kt @@ -108,15 +108,6 @@ fun HomeScreen() { val lazyListState = rememberLazyListState() val lazyHorizontalGridState = rememberLazyGridState() - val intentUriRoute = rememberIntentUriRoute() - val settingsRoute = rememberSettingsRoute() - val playlistRoute = rememberLocalPlaylistRoute() - val builtInPlaylistRoute = rememberBuiltInPlaylistRoute() - val searchRoute = rememberSearchRoute() - val searchResultRoute = rememberSearchResultRoute() - val albumRoute = rememberAlbumRoute() - val artistRoute = rememberArtistRoute() - var playlistSortBy by rememberPreference(playlistSortByKey, PlaylistSortBy.DateAdded) var playlistSortOrder by rememberPreference(playlistSortOrderKey, SortOrder.Descending) var playlistGridExpanded by rememberPreference(playlistGridExpandedKey, false) @@ -137,7 +128,7 @@ fun HomeScreen() { SettingsScreen() } - playlistRoute { playlistId -> + localPlaylistRoute { playlistId -> LocalPlaylistScreen( playlistId = playlistId ?: error("playlistId cannot be null") ) @@ -520,7 +511,7 @@ fun HomeScreen() { .clickable( indication = rememberRipple(bounded = true), interactionSource = remember { MutableInteractionSource() }, - onClick = { playlistRoute(playlistPreview.playlist.id) } + onClick = { localPlaylistRoute(playlistPreview.playlist.id) } ) ) } diff --git a/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/IntentUriScreen.kt b/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/IntentUriScreen.kt index bcb4bbf..3d51e79 100644 --- a/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/IntentUriScreen.kt +++ b/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/IntentUriScreen.kt @@ -54,8 +54,6 @@ import kotlinx.coroutines.withContext @ExperimentalAnimationApi @Composable fun IntentUriScreen(uri: Uri) { - val albumRoute = rememberAlbumRoute() - val artistRoute = rememberArtistRoute() val lazyListState = rememberLazyListState() @@ -98,17 +96,7 @@ fun IntentUriScreen(uri: Uri) { } RouteHandler(listenToGlobalEmitter = true) { - albumRoute { browseId -> - AlbumScreen( - browseId = browseId ?: error("browseId cannot be null") - ) - } - - artistRoute { browseId -> - ArtistScreen( - browseId = browseId ?: error("browseId cannot be null") - ) - } + globalRoutes() host { val menuState = LocalMenuState.current diff --git a/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/LocalPlaylistScreen.kt b/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/LocalPlaylistScreen.kt index abd6d1c..c658b50 100644 --- a/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/LocalPlaylistScreen.kt +++ b/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/LocalPlaylistScreen.kt @@ -65,30 +65,15 @@ import kotlinx.coroutines.flow.map @ExperimentalAnimationApi @Composable -fun LocalPlaylistScreen( - playlistId: Long, -) { +fun LocalPlaylistScreen(playlistId: Long) { val playlistWithSongs by remember(playlistId) { Database.playlistWithSongs(playlistId).map { it ?: PlaylistWithSongs.NotFound } }.collectAsState(initial = PlaylistWithSongs.Empty, context = Dispatchers.IO) val lazyListState = rememberLazyListState() - val albumRoute = rememberAlbumRoute() - val artistRoute = rememberArtistRoute() - RouteHandler(listenToGlobalEmitter = true) { - albumRoute { browseId -> - AlbumScreen( - browseId = browseId ?: error("browseId cannot be null") - ) - } - - artistRoute { browseId -> - ArtistScreen( - browseId = browseId ?: error("browseId cannot be null") - ) - } + globalRoutes() host { val hapticFeedback = LocalHapticFeedback.current diff --git a/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/PlaylistScreen.kt b/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/PlaylistScreen.kt index 4d063ee..e99554e 100644 --- a/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/PlaylistScreen.kt +++ b/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/PlaylistScreen.kt @@ -75,26 +75,11 @@ import kotlinx.coroutines.withContext @ExperimentalAnimationApi @Composable -fun PlaylistScreen( - browseId: String, -) { +fun PlaylistScreen(browseId: String) { val lazyListState = rememberLazyListState() - val albumRoute = rememberAlbumRoute() - val artistRoute = rememberArtistRoute() - RouteHandler(listenToGlobalEmitter = true) { - albumRoute { browseId -> - AlbumScreen( - browseId = browseId ?: error("browseId cannot be null") - ) - } - - artistRoute { browseId -> - ArtistScreen( - browseId = browseId ?: error("browseId cannot be null") - ) - } + globalRoutes() host { val context = LocalContext.current diff --git a/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/Routes.kt b/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/Routes.kt index 7c8c0f0..338bba5 100644 --- a/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/Routes.kt +++ b/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/Routes.kt @@ -1,104 +1,45 @@ package it.vfsfitvnm.vimusic.ui.screens +import android.annotation.SuppressLint import android.net.Uri +import androidx.compose.animation.ExperimentalAnimationApi import androidx.compose.runtime.Composable -import androidx.compose.runtime.mutableStateOf -import androidx.compose.runtime.remember -import androidx.compose.runtime.saveable.rememberSaveable import it.vfsfitvnm.route.Route0 import it.vfsfitvnm.route.Route1 +import it.vfsfitvnm.route.RouteHandlerScope import it.vfsfitvnm.vimusic.enums.BuiltInPlaylist -@Composable -fun rememberIntentUriRoute(): Route1 { - val uri = rememberSaveable { - mutableStateOf(null) - } - return remember { - Route1("IntentUriRoute", uri) - } -} +val aboutRoute = Route0("aboutRoute") +val albumRoute = Route1("albumRoute") +val appearanceSettingsRoute = Route0("appearanceSettingsRoute") +val artistRoute = Route1("artistRoute") +val backupAndRestoreRoute = Route0("backupAndRestoreRoute") +val builtInPlaylistRoute = Route1("builtInPlaylistRoute") +val cacheSettingsRoute = Route0("cacheSettingsRoute") +val intentUriRoute = Route1("intentUriRoute") +val localPlaylistRoute = Route1("localPlaylistRoute") +val otherSettingsRoute = Route0("otherSettingsRoute") +val playerSettingsRoute = Route0("playerSettingsRoute") +val playlistRoute = Route1("playlistRoute") +val searchResultRoute = Route1("searchResultRoute") +val searchRoute = Route1("searchRoute") +val settingsRoute = Route0("settingsRoute") +val viewPlaylistsRoute = Route0("createPlaylistRoute") +@SuppressLint("ComposableNaming") +@Suppress("NOTHING_TO_INLINE") +@ExperimentalAnimationApi @Composable -fun rememberPlaylistRoute(): Route1 { - val browseId = rememberSaveable { - mutableStateOf(null) +inline fun RouteHandlerScope.globalRoutes() { + albumRoute { browseId -> + AlbumScreen( + browseId = browseId ?: error("browseId cannot be null") + ) } - return remember { - Route1("PlaylistRoute", browseId) - } -} -@Composable -fun rememberAlbumRoute(): Route1 { - val browseId = rememberSaveable { - mutableStateOf(null) - } - return remember { - Route1("AlbumRoute", browseId) - } -} - -@Composable -fun rememberArtistRoute(): Route1 { - val browseId = rememberSaveable { - mutableStateOf(null) - } - return remember { - Route1("ArtistRoute", browseId) - } -} - -@Composable -fun rememberLocalPlaylistRoute(): Route1 { - val playlistId = rememberSaveable { - mutableStateOf(null) - } - return remember { - Route1("LocalPlaylistRoute", playlistId) - } -} - -@Composable -fun rememberBuiltInPlaylistRoute(): Route1 { - val playlistType = rememberSaveable { - mutableStateOf(BuiltInPlaylist.Favorites) - } - return remember { - Route1("BuiltInPlaylistRoute", playlistType) - } -} - -@Composable -fun rememberSearchRoute(): Route1 { - val initialTextInput = remember { - mutableStateOf("") - } - return remember { - Route1("SearchRoute", initialTextInput) - } -} - -@Composable -fun rememberCreatePlaylistRoute(): Route0 { - return remember { - Route0("CreatePlaylistRoute") - } -} - -@Composable -fun rememberSearchResultRoute(): Route1 { - val searchQuery = rememberSaveable { - mutableStateOf("") - } - return remember { - Route1("SearchResultRoute", searchQuery) - } -} - -@Composable -fun rememberSettingsRoute(): Route0 { - return remember { - Route0("SettingsRoute") + artistRoute { browseId -> + ArtistScreen( + browseId = browseId ?: error("browseId cannot be null") + ) } } diff --git a/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/SearchResultScreen.kt b/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/SearchResultScreen.kt index 2d18b9d..b65b835 100644 --- a/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/SearchResultScreen.kt +++ b/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/SearchResultScreen.kt @@ -71,10 +71,7 @@ import kotlinx.coroutines.withContext @ExperimentalAnimationApi @Composable -fun SearchResultScreen( - query: String, - onSearchAgain: () -> Unit, -) { +fun SearchResultScreen(query: String, onSearchAgain: () -> Unit) { val (colorPalette, typography) = LocalAppearance.current val binder = LocalPlayerServiceBinder.current @@ -107,10 +104,6 @@ fun SearchResultScreen( val thumbnailSizePx = Dimensions.thumbnails.song.px - val albumRoute = rememberAlbumRoute() - val playlistRoute = rememberPlaylistRoute() - val artistRoute = rememberArtistRoute() - RouteHandler(listenToGlobalEmitter = true) { albumRoute { browseId -> AlbumScreen( @@ -118,14 +111,14 @@ fun SearchResultScreen( ) } - playlistRoute { browseId -> - PlaylistScreen( + artistRoute { browseId -> + ArtistScreen( browseId = browseId ?: "browseId cannot be null" ) } - artistRoute { browseId -> - ArtistScreen( + playlistRoute { browseId -> + PlaylistScreen( browseId = browseId ?: "browseId cannot be null" ) } diff --git a/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/SearchScreen.kt b/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/SearchScreen.kt index c38bbb0..36544bd 100644 --- a/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/SearchScreen.kt +++ b/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/SearchScreen.kt @@ -69,26 +69,9 @@ import kotlinx.coroutines.withContext @ExperimentalAnimationApi @Composable -fun SearchScreen( - initialTextInput: String, - onSearch: (String) -> Unit, - onUri: (Uri) -> Unit, -) { - val albumRoute = rememberAlbumRoute() - val artistRoute = rememberArtistRoute() - +fun SearchScreen(initialTextInput: String, onSearch: (String) -> Unit, onUri: (Uri) -> Unit) { RouteHandler(listenToGlobalEmitter = true) { - albumRoute { browseId -> - AlbumScreen( - browseId = browseId ?: error("browseId cannot be null") - ) - } - - artistRoute { browseId -> - ArtistScreen( - browseId = browseId ?: error("browseId cannot be null") - ) - } + globalRoutes() host { val (colorPalette, typography) = LocalAppearance.current diff --git a/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/SettingsScreen.kt b/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/SettingsScreen.kt index df2eb28..36c3798 100644 --- a/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/SettingsScreen.kt +++ b/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/SettingsScreen.kt @@ -29,15 +29,6 @@ import it.vfsfitvnm.vimusic.utils.* @ExperimentalAnimationApi @Composable fun SettingsScreen() { - val albumRoute = rememberAlbumRoute() - val artistRoute = rememberArtistRoute() - val appearanceSettingsRoute = rememberAppearanceSettingsRoute() - val playerSettingsRoute = rememberPlayerSettingsRoute() - val backupAndRestoreRoute = rememberBackupAndRestoreRoute() - val cacheSettingsRoute = rememberCacheSettingsRoute() - val otherSettingsRoute = rememberOtherSettingsRoute() - val aboutRoute = rememberAboutRoute() - val scrollState = rememberScrollState() RouteHandler( @@ -53,17 +44,7 @@ fun SettingsScreen() { } } ) { - albumRoute { browseId -> - AlbumScreen( - browseId = browseId ?: error("browseId cannot be null") - ) - } - - artistRoute { browseId -> - ArtistScreen( - browseId = browseId ?: error("browseId cannot be null") - ) - } + globalRoutes() appearanceSettingsRoute { AppearanceSettingsScreen() diff --git a/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/settings/AboutScreen.kt b/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/settings/AboutScreen.kt index 04c05a4..d5b2737 100644 --- a/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/settings/AboutScreen.kt +++ b/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/settings/AboutScreen.kt @@ -27,10 +27,7 @@ import it.vfsfitvnm.route.RouteHandler import it.vfsfitvnm.vimusic.BuildConfig import it.vfsfitvnm.vimusic.R import it.vfsfitvnm.vimusic.ui.components.TopAppBar -import it.vfsfitvnm.vimusic.ui.screens.AlbumScreen -import it.vfsfitvnm.vimusic.ui.screens.ArtistScreen -import it.vfsfitvnm.vimusic.ui.screens.rememberAlbumRoute -import it.vfsfitvnm.vimusic.ui.screens.rememberArtistRoute +import it.vfsfitvnm.vimusic.ui.screens.globalRoutes import it.vfsfitvnm.vimusic.ui.styling.LocalAppearance import it.vfsfitvnm.vimusic.utils.bold import it.vfsfitvnm.vimusic.utils.secondary @@ -39,23 +36,10 @@ import it.vfsfitvnm.vimusic.utils.semiBold @ExperimentalAnimationApi @Composable fun AboutScreen() { - val albumRoute = rememberAlbumRoute() - val artistRoute = rememberArtistRoute() - val scrollState = rememberScrollState() RouteHandler(listenToGlobalEmitter = true) { - albumRoute { browseId -> - AlbumScreen( - browseId = browseId ?: error("browseId cannot be null") - ) - } - - artistRoute { browseId -> - ArtistScreen( - browseId = browseId ?: error("browseId cannot be null") - ) - } + globalRoutes() host { val (colorPalette, typography) = LocalAppearance.current diff --git a/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/settings/AppearanceSettingsScreen.kt b/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/settings/AppearanceSettingsScreen.kt index f60728e..8d92c22 100644 --- a/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/settings/AppearanceSettingsScreen.kt +++ b/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/settings/AppearanceSettingsScreen.kt @@ -25,12 +25,9 @@ import it.vfsfitvnm.vimusic.R import it.vfsfitvnm.vimusic.enums.ColorPaletteMode import it.vfsfitvnm.vimusic.enums.ThumbnailRoundness import it.vfsfitvnm.vimusic.ui.components.TopAppBar -import it.vfsfitvnm.vimusic.ui.screens.AlbumScreen -import it.vfsfitvnm.vimusic.ui.screens.ArtistScreen import it.vfsfitvnm.vimusic.ui.screens.EnumValueSelectorSettingsEntry import it.vfsfitvnm.vimusic.ui.screens.SettingsEntryGroupText -import it.vfsfitvnm.vimusic.ui.screens.rememberAlbumRoute -import it.vfsfitvnm.vimusic.ui.screens.rememberArtistRoute +import it.vfsfitvnm.vimusic.ui.screens.globalRoutes import it.vfsfitvnm.vimusic.ui.styling.LocalAppearance import it.vfsfitvnm.vimusic.utils.colorPaletteModeKey import it.vfsfitvnm.vimusic.utils.rememberPreference @@ -40,23 +37,11 @@ import it.vfsfitvnm.vimusic.utils.thumbnailRoundnessKey @ExperimentalAnimationApi @Composable fun AppearanceSettingsScreen() { - val albumRoute = rememberAlbumRoute() - val artistRoute = rememberArtistRoute() val scrollState = rememberScrollState() RouteHandler(listenToGlobalEmitter = true) { - albumRoute { browseId -> - AlbumScreen( - browseId = browseId ?: error("browseId cannot be null") - ) - } - - artistRoute { browseId -> - ArtistScreen( - browseId = browseId ?: error("browseId cannot be null") - ) - } + globalRoutes() host { val (colorPalette, typography) = LocalAppearance.current diff --git a/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/settings/BackupAndRestoreScreen.kt b/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/settings/BackupAndRestoreScreen.kt index fe244e8..df3d3d9 100644 --- a/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/settings/BackupAndRestoreScreen.kt +++ b/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/settings/BackupAndRestoreScreen.kt @@ -43,10 +43,7 @@ import it.vfsfitvnm.vimusic.service.PlayerService import it.vfsfitvnm.vimusic.ui.components.TopAppBar import it.vfsfitvnm.vimusic.ui.components.themed.ConfirmationDialog import it.vfsfitvnm.vimusic.ui.components.themed.TextCard -import it.vfsfitvnm.vimusic.ui.screens.AlbumScreen -import it.vfsfitvnm.vimusic.ui.screens.ArtistScreen -import it.vfsfitvnm.vimusic.ui.screens.rememberAlbumRoute -import it.vfsfitvnm.vimusic.ui.screens.rememberArtistRoute +import it.vfsfitvnm.vimusic.ui.screens.globalRoutes import it.vfsfitvnm.vimusic.ui.styling.LocalAppearance import it.vfsfitvnm.vimusic.utils.intent import it.vfsfitvnm.vimusic.utils.semiBold @@ -59,23 +56,11 @@ import kotlin.system.exitProcess @ExperimentalAnimationApi @Composable fun BackupAndRestoreScreen() { - val albumRoute = rememberAlbumRoute() - val artistRoute = rememberArtistRoute() val scrollState = rememberScrollState() RouteHandler(listenToGlobalEmitter = true) { - albumRoute { browseId -> - AlbumScreen( - browseId = browseId ?: error("browseId cannot be null") - ) - } - - artistRoute { browseId -> - ArtistScreen( - browseId = browseId ?: error("browseId cannot be null") - ) - } + globalRoutes() host { val (colorPalette, typography) = LocalAppearance.current diff --git a/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/settings/CacheSettingsScreen.kt b/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/settings/CacheSettingsScreen.kt index 42f284f..71bdc33 100644 --- a/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/settings/CacheSettingsScreen.kt +++ b/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/settings/CacheSettingsScreen.kt @@ -35,14 +35,11 @@ import it.vfsfitvnm.vimusic.enums.CoilDiskCacheMaxSize import it.vfsfitvnm.vimusic.enums.ExoPlayerDiskCacheMaxSize import it.vfsfitvnm.vimusic.ui.components.TopAppBar import it.vfsfitvnm.vimusic.ui.components.themed.TextCard -import it.vfsfitvnm.vimusic.ui.screens.AlbumScreen -import it.vfsfitvnm.vimusic.ui.screens.ArtistScreen import it.vfsfitvnm.vimusic.ui.screens.DisabledSettingsEntry import it.vfsfitvnm.vimusic.ui.screens.EnumValueSelectorSettingsEntry import it.vfsfitvnm.vimusic.ui.screens.SettingsEntry import it.vfsfitvnm.vimusic.ui.screens.SettingsEntryGroupText -import it.vfsfitvnm.vimusic.ui.screens.rememberAlbumRoute -import it.vfsfitvnm.vimusic.ui.screens.rememberArtistRoute +import it.vfsfitvnm.vimusic.ui.screens.globalRoutes import it.vfsfitvnm.vimusic.ui.styling.LocalAppearance import it.vfsfitvnm.vimusic.utils.coilDiskCacheMaxSizeKey import it.vfsfitvnm.vimusic.utils.exoPlayerDiskCacheMaxSizeKey @@ -55,23 +52,11 @@ import kotlinx.coroutines.launch @ExperimentalAnimationApi @Composable fun CacheSettingsScreen() { - val albumRoute = rememberAlbumRoute() - val artistRoute = rememberArtistRoute() val scrollState = rememberScrollState() RouteHandler(listenToGlobalEmitter = true) { - albumRoute { browseId -> - AlbumScreen( - browseId = browseId ?: error("browseId cannot be null") - ) - } - - artistRoute { browseId -> - ArtistScreen( - browseId = browseId ?: error("browseId cannot be null") - ) - } + globalRoutes() host { val context = LocalContext.current diff --git a/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/settings/OtherSettingsScreen.kt b/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/settings/OtherSettingsScreen.kt index 439eea0..7b2e822 100644 --- a/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/settings/OtherSettingsScreen.kt +++ b/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/settings/OtherSettingsScreen.kt @@ -35,13 +35,10 @@ import it.vfsfitvnm.route.RouteHandler import it.vfsfitvnm.vimusic.R import it.vfsfitvnm.vimusic.ui.components.TopAppBar import it.vfsfitvnm.vimusic.ui.components.themed.TextCard -import it.vfsfitvnm.vimusic.ui.screens.AlbumScreen -import it.vfsfitvnm.vimusic.ui.screens.ArtistScreen import it.vfsfitvnm.vimusic.ui.screens.SettingsEntry import it.vfsfitvnm.vimusic.ui.screens.SettingsEntryGroupText import it.vfsfitvnm.vimusic.ui.screens.SwitchSettingEntry -import it.vfsfitvnm.vimusic.ui.screens.rememberAlbumRoute -import it.vfsfitvnm.vimusic.ui.screens.rememberArtistRoute +import it.vfsfitvnm.vimusic.ui.screens.globalRoutes import it.vfsfitvnm.vimusic.ui.styling.LocalAppearance import it.vfsfitvnm.vimusic.utils.isIgnoringBatteryOptimizations import it.vfsfitvnm.vimusic.utils.isInvincibilityEnabledKey @@ -51,23 +48,11 @@ import it.vfsfitvnm.vimusic.utils.semiBold @ExperimentalAnimationApi @Composable fun OtherSettingsScreen() { - val albumRoute = rememberAlbumRoute() - val artistRoute = rememberArtistRoute() val scrollState = rememberScrollState() RouteHandler(listenToGlobalEmitter = true) { - albumRoute { browseId -> - AlbumScreen( - browseId = browseId ?: error("browseId cannot be null") - ) - } - - artistRoute { browseId -> - ArtistScreen( - browseId = browseId ?: error("browseId cannot be null") - ) - } + globalRoutes() host { val context = LocalContext.current diff --git a/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/settings/PlayerSettingsScreen.kt b/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/settings/PlayerSettingsScreen.kt index 96e76ee..f9d4788 100644 --- a/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/settings/PlayerSettingsScreen.kt +++ b/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/settings/PlayerSettingsScreen.kt @@ -30,13 +30,10 @@ import it.vfsfitvnm.route.RouteHandler import it.vfsfitvnm.vimusic.LocalPlayerServiceBinder import it.vfsfitvnm.vimusic.R import it.vfsfitvnm.vimusic.ui.components.TopAppBar -import it.vfsfitvnm.vimusic.ui.screens.AlbumScreen -import it.vfsfitvnm.vimusic.ui.screens.ArtistScreen import it.vfsfitvnm.vimusic.ui.screens.SettingsEntry import it.vfsfitvnm.vimusic.ui.screens.SettingsEntryGroupText import it.vfsfitvnm.vimusic.ui.screens.SwitchSettingEntry -import it.vfsfitvnm.vimusic.ui.screens.rememberAlbumRoute -import it.vfsfitvnm.vimusic.ui.screens.rememberArtistRoute +import it.vfsfitvnm.vimusic.ui.screens.globalRoutes import it.vfsfitvnm.vimusic.ui.styling.LocalAppearance import it.vfsfitvnm.vimusic.utils.persistentQueueKey import it.vfsfitvnm.vimusic.utils.rememberPreference @@ -47,23 +44,11 @@ import it.vfsfitvnm.vimusic.utils.volumeNormalizationKey @ExperimentalAnimationApi @Composable fun PlayerSettingsScreen() { - val albumRoute = rememberAlbumRoute() - val artistRoute = rememberArtistRoute() val scrollState = rememberScrollState() RouteHandler(listenToGlobalEmitter = true) { - albumRoute { browseId -> - AlbumScreen( - browseId = browseId ?: error("browseId cannot be null") - ) - } - - artistRoute { browseId -> - ArtistScreen( - browseId = browseId ?: error("browseId cannot be null") - ) - } + globalRoutes() host { val context = LocalContext.current diff --git a/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/settings/Routes.kt b/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/settings/Routes.kt deleted file mode 100644 index b3f313b..0000000 --- a/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/settings/Routes.kt +++ /dev/null @@ -1,47 +0,0 @@ -package it.vfsfitvnm.vimusic.ui.screens.settings - -import androidx.compose.runtime.Composable -import androidx.compose.runtime.remember -import it.vfsfitvnm.route.Route0 - -@Composable -fun rememberAppearanceSettingsRoute(): Route0 { - return remember { - Route0("AppearanceSettingsRoute") - } -} - -@Composable -fun rememberPlayerSettingsRoute(): Route0 { - return remember { - Route0("PlayerSettingsRoute") - } -} - -@Composable -fun rememberBackupAndRestoreRoute(): Route0 { - return remember { - Route0("BackupAndRestoreRoute") - } -} - -@Composable -fun rememberCacheSettingsRoute(): Route0 { - return remember { - Route0("CacheSettingsRoute") - } -} - -@Composable -fun rememberOtherSettingsRoute(): Route0 { - return remember { - Route0("OtherSettingsRoute") - } -} - -@Composable -fun rememberAboutRoute(): Route0 { - return remember { - Route0("AboutRoute") - } -} diff --git a/compose-routing/src/main/kotlin/it/vfsfitvnm/route/Route.kt b/compose-routing/src/main/kotlin/it/vfsfitvnm/route/Route.kt index bbbd4e1..9fe83b2 100644 --- a/compose-routing/src/main/kotlin/it/vfsfitvnm/route/Route.kt +++ b/compose-routing/src/main/kotlin/it/vfsfitvnm/route/Route.kt @@ -1,10 +1,15 @@ +@file:Suppress("UNCHECKED_CAST") + package it.vfsfitvnm.route -import androidx.compose.runtime.* +import androidx.compose.runtime.Composable +import androidx.compose.runtime.Immutable +import androidx.compose.runtime.MutableState +import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.saveable.SaverScope import androidx.compose.runtime.saveable.rememberSaveable -@Stable +@Immutable open class Route internal constructor(val tag: String) { override fun equals(other: Any?): Boolean { return when { @@ -19,7 +24,7 @@ open class Route internal constructor(val tag: String) { } object GlobalEmitter { - var listener: ((Route) -> Unit)? = null + var listener: ((Route, Array) -> Unit)? = null } object Saver : androidx.compose.runtime.saveable.Saver { @@ -35,10 +40,8 @@ fun rememberRoute(route: Route? = null): MutableState { } } -@Stable -class Route0( - tag: String -) : Route(tag) { +@Immutable +class Route0(tag: String) : Route(tag) { context(RouteHandlerScope) @Composable operator fun invoke(content: @Composable () -> Unit) { @@ -48,64 +51,36 @@ class Route0( } fun global() { - GlobalEmitter.listener?.invoke(this) + GlobalEmitter.listener?.invoke(this, emptyArray()) } } -@Stable -class Route1( - tag: String, - state0: MutableState -) : Route(tag) { - var p0 by state0 - +@Immutable +class Route1(tag: String) : Route(tag) { context(RouteHandlerScope) @Composable operator fun invoke(content: @Composable (P0) -> Unit) { if (this == route) { - if (route is Route1<*>) { - @Suppress("UNCHECKED_CAST") - (route as Route1).let { route -> - this.p0 = route.p0 - } - } - content(this.p0) + content(parameters[0] as P0) } } - fun global(p0: P0 = this.p0) { - this.p0 = p0 - GlobalEmitter.listener?.invoke(this) + fun global(p0: P0) { + GlobalEmitter.listener?.invoke(this, arrayOf(p0)) } } -@Stable -class Route2( - tag: String, - state0: MutableState, - state1: MutableState -) : Route(tag) { - var p0 by state0 - var p1 by state1 - +@Immutable +class Route2(tag: String) : Route(tag) { context(RouteHandlerScope) @Composable operator fun invoke(content: @Composable (P0, P1) -> Unit) { if (this == route) { - if (route is Route2<*, *>) { - @Suppress("UNCHECKED_CAST") - (route as Route2).let { route -> - this.p0 = route.p0 - this.p1 = route.p1 - } - } - content(this.p0, this.p1) + content(parameters[0] as P0, parameters[1] as P1) } } - fun global(p0: P0 = this.p0, p1: P1 = this.p1) { - this.p0 = p0 - this.p1 = p1 - GlobalEmitter.listener?.invoke(this) + fun global(p0: P0, p1: P1) { + GlobalEmitter.listener?.invoke(this, arrayOf(p0, p1)) } } diff --git a/compose-routing/src/main/kotlin/it/vfsfitvnm/route/RouteHandler.kt b/compose-routing/src/main/kotlin/it/vfsfitvnm/route/RouteHandler.kt index 2ff315d..d7b432c 100644 --- a/compose-routing/src/main/kotlin/it/vfsfitvnm/route/RouteHandler.kt +++ b/compose-routing/src/main/kotlin/it/vfsfitvnm/route/RouteHandler.kt @@ -7,7 +7,12 @@ import androidx.compose.animation.AnimatedContentScope import androidx.compose.animation.ContentTransform import androidx.compose.animation.ExperimentalAnimationApi import androidx.compose.animation.core.updateTransition -import androidx.compose.runtime.* +import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect +import androidx.compose.runtime.getValue +import androidx.compose.runtime.remember +import androidx.compose.runtime.saveable.rememberSaveable +import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier @ExperimentalAnimationApi @@ -45,9 +50,14 @@ fun RouteHandler( ) { val backDispatcher = LocalOnBackPressedDispatcherOwner.current?.onBackPressedDispatcher + val parameters = rememberSaveable { + arrayOfNulls(2) + } + val scope = remember(route) { RouteHandlerScope( route = route, + parameters = parameters, push = onRouteChanged, pop = { if (handleBackPress) backDispatcher?.onBackPressed() else onRouteChanged(null) } ) @@ -55,7 +65,10 @@ fun RouteHandler( if (listenToGlobalEmitter) { LaunchedEffect(route) { - Route.GlobalEmitter.listener = if (route == null) onRouteChanged else null + Route.GlobalEmitter.listener = if (route == null) ({ newRoute, newParameters -> + newParameters.forEachIndexed(parameters::set) + onRouteChanged(newRoute) + }) else null } } @@ -65,7 +78,7 @@ fun RouteHandler( updateTransition(targetState = scope, label = null).AnimatedContent( transitionSpec = transitionSpec, - contentKey = { it.route?.tag }, + contentKey = RouteHandlerScope::route, modifier = modifier, ) { it.content() diff --git a/compose-routing/src/main/kotlin/it/vfsfitvnm/route/RouteHandlerScope.kt b/compose-routing/src/main/kotlin/it/vfsfitvnm/route/RouteHandlerScope.kt index 549bc9b..05394e5 100644 --- a/compose-routing/src/main/kotlin/it/vfsfitvnm/route/RouteHandlerScope.kt +++ b/compose-routing/src/main/kotlin/it/vfsfitvnm/route/RouteHandlerScope.kt @@ -2,11 +2,12 @@ package it.vfsfitvnm.route import android.annotation.SuppressLint import androidx.compose.runtime.Composable -import androidx.compose.runtime.Immutable +import androidx.compose.runtime.Stable -@Immutable +@Stable class RouteHandlerScope( val route: Route?, + val parameters: Array, private val push: (Route?) -> Unit, val pop: () -> Unit, ) { @@ -18,23 +19,17 @@ class RouteHandlerScope( } } - operator fun Route0.invoke() { + operator fun Route.invoke() { push(this) } - operator fun Route1.invoke( - p0: P0 = this.p0 - ) { - this.p0 = p0 - push(this) + operator fun Route.invoke(p0: P0) { + parameters[0] = p0 + invoke() } - operator fun Route2.invoke( - p0: P0 = this.p0, - p1: P1 = this.p1 - ) { - this.p0 = p0 - this.p1 = p1 - push(this) + operator fun Route.invoke(p0: P0, p1: P1) { + parameters[1] = p1 + invoke(p0) } } diff --git a/compose-routing/src/main/kotlin/it/vfsfitvnm/route/Transitions.kt b/compose-routing/src/main/kotlin/it/vfsfitvnm/route/Transitions.kt index 647ef0b..b87cad0 100644 --- a/compose-routing/src/main/kotlin/it/vfsfitvnm/route/Transitions.kt +++ b/compose-routing/src/main/kotlin/it/vfsfitvnm/route/Transitions.kt @@ -1,8 +1,14 @@ package it.vfsfitvnm.route -import androidx.compose.animation.* +import androidx.compose.animation.AnimatedContentScope +import androidx.compose.animation.ContentTransform +import androidx.compose.animation.EnterTransition +import androidx.compose.animation.ExitTransition +import androidx.compose.animation.ExperimentalAnimationApi import androidx.compose.animation.core.tween - +import androidx.compose.animation.fadeIn +import androidx.compose.animation.fadeOut +import androidx.compose.animation.with @ExperimentalAnimationApi val AnimatedContentScope.leftSlide: ContentTransform