Skip to content

Commit

Permalink
Fixed crash due to importing folder caused by type-safe navigation co…
Browse files Browse the repository at this point in the history
…mpose bug

Updated version number and version code for 1.0.1
  • Loading branch information
Anthonyy232 committed May 9, 2024
1 parent 4429355 commit c97f9f7
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 25 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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(
Expand Down Expand Up @@ -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")
}
)
}
Expand All @@ -273,7 +288,7 @@ fun PaperizeApp(
)
}
// Navigate to folder view screen to view wallpapers in a particular folder
composable<FolderView> (
/*composable<FolderView> (
enterTransition = {
sharedXTransitionIn(initial = { (it * INITIAL_OFFSET).toInt() })
},
Expand All @@ -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<String>::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<AlbumView> (
Expand All @@ -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))
Expand Down Expand Up @@ -430,4 +490,10 @@ fun PaperizeApp(
)
}
}
}
}

// 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())
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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())
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>)
data class FolderView(val folderName: String?, val wallpapers: List<String>)
*/
sealed class NavScreens(val route: String) {
data object FolderView : NavScreens("folder_view_screen")
}

0 comments on commit c97f9f7

Please sign in to comment.