From c97f9f7e3d1a845e41143e84bb96a1ecfd002d73 Mon Sep 17 00:00:00 2001 From: Anthonyy232 Date: Thu, 9 May 2024 12:53:37 -0700 Subject: [PATCH] Fixed crash due to importing folder caused by type-safe navigation compose bug Updated version number and version code for 1.0.1 --- app/build.gradle.kts | 4 +- .../wallpaper/presentation/PaperizeApp.kt | 98 ++++++++++++++++--- .../add_album_screen/AddAlbumViewModel.kt | 4 +- .../components/PaperizeListItem.kt | 2 +- .../wallpaper/util/navigation/NavScreens.kt | 9 +- 5 files changed, 92 insertions(+), 25 deletions(-) diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 38066263..56fd9082 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -17,8 +17,8 @@ android { applicationId = "com.anthonyla.paperize" minSdk = 26 targetSdk = 34 - versionCode = 4 - versionName = "1.0.0" + versionCode = 5 + versionName = "1.0.1" testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" vectorDrawables { 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 95874911..76e3845a 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 @@ -11,9 +11,11 @@ import androidx.compose.ui.platform.LocalContext 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 @@ -37,10 +39,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 @@ -49,6 +51,12 @@ import com.anthonyla.paperize.feature.wallpaper.util.navigation.WallpaperView import com.anthonyla.paperize.feature.wallpaper.util.navigation.sharedXTransitionIn import com.anthonyla.paperize.feature.wallpaper.util.navigation.sharedXTransitionOut import com.anthonyla.paperize.feature.wallpaper.wallpaperservice.WallpaperService +import com.google.gson.Gson +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.runBlocking +import kotlinx.coroutines.withContext +import java.net.URLEncoder +import java.nio.charset.StandardCharsets @Composable fun PaperizeApp( @@ -242,11 +250,18 @@ fun PaperizeApp( initialAlbumName = addEdit.wallpaper, onBackClick = { navController.navigateUp() }, onConfirmation = { navController.navigateUp() }, - onShowWallpaperView = { wallpaper -> - navController.navigate(WallpaperView(wallpaper)) + onShowWallpaperView = { + 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") } ) } @@ -273,7 +288,7 @@ fun PaperizeApp( ) } // Navigate to folder view screen to view wallpapers in a particular folder - composable ( + /*composable ( enterTransition = { sharedXTransitionIn(initial = { (it * INITIAL_OFFSET).toInt() }) }, @@ -288,15 +303,53 @@ fun PaperizeApp( }, ) { backStackEntry -> val folderView: FolderView = backStackEntry.toRoute() - FolderViewScreen( - folderName = folderView.folderName, - wallpapers = folderView.wallpapers, - onBackClick = { navController.navigateUp() }, - onShowWallpaperView = { - navController.navigate(WallpaperView(it)) - }, - animate = settingsState.value.animate - ) + if (folderView.wallpapers.isNotEmpty()) { + FolderViewScreen( + folderName = folderView.folderName, + wallpapers = folderView.wallpapers, + onBackClick = { navController.navigateUp() }, + onShowWallpaperView = { + navController.navigate(WallpaperView(it)) + }, + 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 = { + sharedXTransitionIn(initial = { (it * INITIAL_OFFSET).toInt() }) + }, + exitTransition = { + sharedXTransitionOut(target = { -(it * INITIAL_OFFSET).toInt() }) + }, + popEnterTransition = { + sharedXTransitionIn(initial = { -(it * INITIAL_OFFSET).toInt() }) + }, + popExitTransition = { + sharedXTransitionOut(target = { (it * INITIAL_OFFSET).toInt() }) + }, + ) { 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 ( @@ -323,7 +376,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 = { albumsViewModel.onEvent(AlbumsEvent.DeleteAlbumWithWallpapers(albumWithWallpaper)) @@ -430,4 +490,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/presentation/add_album_screen/AddAlbumViewModel.kt b/app/src/main/java/com/anthonyla/paperize/feature/wallpaper/presentation/add_album_screen/AddAlbumViewModel.kt index 9bb68df0..a3ac23a2 100644 --- a/app/src/main/java/com/anthonyla/paperize/feature/wallpaper/presentation/add_album_screen/AddAlbumViewModel.kt +++ b/app/src/main/java/com/anthonyla/paperize/feature/wallpaper/presentation/add_album_screen/AddAlbumViewModel.kt @@ -3,7 +3,6 @@ package com.anthonyla.paperize.feature.wallpaper.presentation.add_album_screen import android.app.Application import android.content.Context -import android.webkit.MimeTypeMap import androidx.core.net.toUri import androidx.lifecycle.AndroidViewModel import androidx.lifecycle.viewModelScope @@ -274,9 +273,8 @@ class AddAlbumViewModel @Inject constructor( if (file.isDirectory()) { files.addAll(listFilesRecursive(file, context)) } else { - val extension = MimeTypeMap.getFileExtensionFromUrl(file.uri.toString()) val allowedExtensions = listOf("jpg", "png", "heif", "webp") - if (extension in allowedExtensions) { + if (file.extension in allowedExtensions) { files.add(file.uri.toString()) } } diff --git a/app/src/main/java/com/anthonyla/paperize/feature/wallpaper/presentation/settings_screen/components/PaperizeListItem.kt b/app/src/main/java/com/anthonyla/paperize/feature/wallpaper/presentation/settings_screen/components/PaperizeListItem.kt index a7847d31..1e381e6a 100644 --- a/app/src/main/java/com/anthonyla/paperize/feature/wallpaper/presentation/settings_screen/components/PaperizeListItem.kt +++ b/app/src/main/java/com/anthonyla/paperize/feature/wallpaper/presentation/settings_screen/components/PaperizeListItem.kt @@ -63,7 +63,7 @@ fun PaperizeListItem(onGitHubClick: () -> Unit, onPlaystoreClick: () -> Unit, on fontSize = MaterialTheme.typography.titleLarge.fontSize ) Text( - text = "Version: 1.0.0", + text = "Version: 1.0.1", style = MaterialTheme.typography.bodySmall, fontSize = MaterialTheme.typography.bodyMedium.fontSize ) 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 cbed68cb..d9d31cfe 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) \ No newline at end of file +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