diff --git a/app/src/main/java/com/anthonyla/paperize/feature/wallpaper/presentation/PaperizeApp.kt b/app/src/main/java/com/anthonyla/paperize/feature/wallpaper/presentation/PaperizeApp.kt index 8ff9d57..3e41c80 100644 --- a/app/src/main/java/com/anthonyla/paperize/feature/wallpaper/presentation/PaperizeApp.kt +++ b/app/src/main/java/com/anthonyla/paperize/feature/wallpaper/presentation/PaperizeApp.kt @@ -17,9 +17,11 @@ import androidx.compose.ui.unit.Dp import androidx.core.content.ContextCompat import androidx.hilt.navigation.compose.hiltViewModel import androidx.lifecycle.compose.collectAsStateWithLifecycle +import androidx.navigation.NavType import androidx.navigation.compose.NavHost import androidx.navigation.compose.composable import androidx.navigation.compose.rememberNavController +import androidx.navigation.navArgument import androidx.navigation.toRoute import com.anthonyla.paperize.feature.wallpaper.domain.model.SelectedAlbum import com.anthonyla.paperize.feature.wallpaper.domain.model.Wallpaper @@ -43,10 +45,10 @@ import com.anthonyla.paperize.feature.wallpaper.presentation.wallpaper_screen.Wa import com.anthonyla.paperize.feature.wallpaper.presentation.wallpaper_view_screen.WallpaperViewScreen import com.anthonyla.paperize.feature.wallpaper.util.navigation.AddEdit import com.anthonyla.paperize.feature.wallpaper.util.navigation.AlbumView -import com.anthonyla.paperize.feature.wallpaper.util.navigation.FolderView import com.anthonyla.paperize.feature.wallpaper.util.navigation.Home import com.anthonyla.paperize.feature.wallpaper.util.navigation.Licenses import com.anthonyla.paperize.feature.wallpaper.util.navigation.NavConstants.INITIAL_OFFSET +import com.anthonyla.paperize.feature.wallpaper.util.navigation.NavScreens import com.anthonyla.paperize.feature.wallpaper.util.navigation.Notification import com.anthonyla.paperize.feature.wallpaper.util.navigation.Privacy import com.anthonyla.paperize.feature.wallpaper.util.navigation.Settings @@ -56,11 +58,15 @@ import com.anthonyla.paperize.feature.wallpaper.util.navigation.sharedXTransitio import com.anthonyla.paperize.feature.wallpaper.util.navigation.sharedXTransitionOut import com.anthonyla.paperize.feature.wallpaper.wallpaper_alarmmanager.WallpaperAlarmItem import com.anthonyla.paperize.feature.wallpaper.wallpaper_alarmmanager.WallpaperAlarmSchedulerImpl +import com.google.gson.Gson import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Job import kotlinx.coroutines.delay import kotlinx.coroutines.launch +import kotlinx.coroutines.runBlocking import kotlinx.coroutines.withContext +import java.net.URLEncoder +import java.nio.charset.StandardCharsets @Composable fun PaperizeApp( @@ -558,7 +564,14 @@ fun PaperizeApp( navController.navigate(WallpaperView(it)) }, onShowFolderView = { folderName, wallpapers -> - navController.navigate(FolderView(folderName, wallpapers)) + /*navController.navigate( + com.anthonyla.paperize.feature.wallpaper.util.navigation.FolderView( + folderName, + wallpapers + ) + )*/ + val encodedWallpapers = runBlocking { encodeUri(uri = Gson().toJson(wallpapers)) } + navController.navigate("${NavScreens.FolderView.route}/$folderName/$encodedWallpapers") }, animate = settingsState.value.animate ) @@ -593,7 +606,7 @@ fun PaperizeApp( animate = settingsState.value.animate ) } - composable( + /*composable( enterTransition = { if (settingsState.value.animate) { sharedXTransitionIn(initial = { (it * INITIAL_OFFSET).toInt() }) @@ -627,6 +640,52 @@ fun PaperizeApp( animate = settingsState.value.animate ) } else { navController.navigateUp() } + }*/ + composable( + route = NavScreens.FolderView.route.plus("/{folderName}/{wallpapers}"), + arguments = listOf( + navArgument("folderName") { type = NavType.StringType }, + navArgument("wallpapers") { type = NavType.StringType } + ), + enterTransition = { + if (settingsState.value.animate) { + sharedXTransitionIn(initial = { (it * INITIAL_OFFSET).toInt() }) + } else { null } + }, + exitTransition = { + if (settingsState.value.animate) { + sharedXTransitionOut(target = { -(it * INITIAL_OFFSET).toInt() }) + } else { null } + }, + popEnterTransition = { + if (settingsState.value.animate) { + sharedXTransitionIn(initial = { -(it * INITIAL_OFFSET).toInt() }) + } else { null } + }, + popExitTransition = { + if (settingsState.value.animate) { + sharedXTransitionOut(target = { -(it * INITIAL_OFFSET).toInt() }) + } else { null } + } + ) { backStackEntry -> + val folderName = backStackEntry.arguments?.getString("folderName") + val wallpapers = Gson().fromJson( + backStackEntry.arguments?.getString("wallpapers"), + Array::class.java + ).toList() + if (wallpapers.isNotEmpty()) { + FolderViewScreen( + folderName = folderName, + wallpapers = wallpapers, + onBackClick = { navController.navigateUp() }, + onShowWallpaperView = { + navController.navigate(WallpaperView(it)) + }, + animate = settingsState.value.animate + ) + } else { + navController.navigateUp() + } } // Navigate to the album view screen to view folders and wallpapers in an album composable ( @@ -662,7 +721,14 @@ fun PaperizeApp( navController.navigate(WallpaperView(it)) }, onShowFolderView = { folderName, wallpapers -> - navController.navigate(FolderView(folderName, wallpapers)) + /*navController.navigate( + com.anthonyla.paperize.feature.wallpaper.util.navigation.FolderView( + folderName, + wallpapers + ) + )*/ + val encodedWallpapers = runBlocking { encodeUri(uri = Gson().toJson(wallpapers)) } + navController.navigate("${NavScreens.FolderView.route}/$folderName/$encodedWallpapers") }, onDeleteAlbum = { navController.navigateUp() @@ -793,4 +859,10 @@ fun PaperizeApp( ) } } -} \ No newline at end of file +} + +// Encode an URI so it can be passed with navigation +suspend fun encodeUri(uri: String): String = + withContext(Dispatchers.IO) { + URLEncoder.encode(uri, StandardCharsets.UTF_8.toString()) + } \ No newline at end of file diff --git a/app/src/main/java/com/anthonyla/paperize/feature/wallpaper/util/navigation/NavScreens.kt b/app/src/main/java/com/anthonyla/paperize/feature/wallpaper/util/navigation/NavScreens.kt index f26cd9f..d9d31cf 100644 --- a/app/src/main/java/com/anthonyla/paperize/feature/wallpaper/util/navigation/NavScreens.kt +++ b/app/src/main/java/com/anthonyla/paperize/feature/wallpaper/util/navigation/NavScreens.kt @@ -57,8 +57,11 @@ data class AddEdit(val wallpaper: String) @Serializable data class WallpaperView(val wallpaper: String) -/** +/** Reverted back to passing JSON due to bug in navigation compose causing crash * Data class for FolderView screen - */ @Serializable data class FolderView(val folderName: String?, val wallpapers: List) + */ +sealed class NavScreens(val route: String) { + data object FolderView : NavScreens("folder_view_screen") +} \ No newline at end of file