From 47fa7563c0f1abaee960771443e9d5f74837591f Mon Sep 17 00:00:00 2001 From: Anthony La Date: Wed, 13 Nov 2024 22:06:59 -0800 Subject: [PATCH] Refactored folder view --- .../wallpaper/presentation/PaperizeApp.kt | 32 +++++----- .../folder_view_screen/FolderEvent.kt | 11 ++++ .../folder_view_screen/FolderState.kt | 8 +++ .../folder_view_screen/FolderViewModel.kt | 28 ++++++++- .../folder_view_screen/FolderViewScreen.kt | 60 +++++++------------ .../home_screen/components/HomeTopBar.kt | 3 +- .../sort_view_screen/SortViewScreen.kt | 23 ++++++- .../WallpaperViewScreen.kt | 12 +--- 8 files changed, 108 insertions(+), 69 deletions(-) create mode 100644 app/src/main/java/com/anthonyla/paperize/feature/wallpaper/presentation/folder_view_screen/FolderEvent.kt create mode 100644 app/src/main/java/com/anthonyla/paperize/feature/wallpaper/presentation/folder_view_screen/FolderState.kt 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 a5910efd..95a47b36 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 @@ -28,6 +28,7 @@ import com.anthonyla.paperize.feature.wallpaper.presentation.add_album_screen.Ad import com.anthonyla.paperize.feature.wallpaper.presentation.album.AlbumsEvent import com.anthonyla.paperize.feature.wallpaper.presentation.album.AlbumsViewModel import com.anthonyla.paperize.feature.wallpaper.presentation.album_view_screen.AlbumViewScreen +import com.anthonyla.paperize.feature.wallpaper.presentation.folder_view_screen.FolderEvent import com.anthonyla.paperize.feature.wallpaper.presentation.folder_view_screen.FolderViewModel import com.anthonyla.paperize.feature.wallpaper.presentation.folder_view_screen.FolderViewScreen import com.anthonyla.paperize.feature.wallpaper.presentation.home_screen.HomeScreen @@ -82,6 +83,7 @@ fun PaperizeApp( val selectedState = wallpaperScreenViewModel.state.collectAsStateWithLifecycle() val settingsState = settingsViewModel.state.collectAsStateWithLifecycle() val sortState = sortViewModel.state.collectAsStateWithLifecycle() + val folderState = folderViewModel.state.collectAsStateWithLifecycle() var job by remember { mutableStateOf(null) } val scope = rememberCoroutineScope() @@ -573,8 +575,7 @@ fun PaperizeApp( }, onShowFolderView = { folder -> if (folder.wallpapers.isNotEmpty()) { - folderViewModel.folderName.value = folder.folderName ?: "" - folderViewModel.wallpapers.value = folder.wallpapers.map { it.wallpaperUri } + folderViewModel.onEvent(FolderEvent.LoadFolderView(folder)) navController.navigate(FolderView) } }, @@ -600,17 +601,19 @@ fun PaperizeApp( // Navigate to the folder view screen to view wallpapers in a folder animatedScreen(animate = settingsState.value.themeSettings.animate) { - val folderName = folderViewModel.folderName.value - val wallpapers = folderViewModel.wallpapers.value - FolderViewScreen( - folderName = folderName, - wallpapers = wallpapers, - onBackClick = { navController.navigateUp() }, - onShowWallpaperView = { - navController.navigate(WallpaperView(it)) - }, - animate = settingsState.value.themeSettings.animate - ) + if (folderState.value.folder == null || folderState.value.folder!!.wallpapers.isEmpty()) { + navController.navigateUp() + } + else { + FolderViewScreen( + folder = folderState.value.folder!!, + onBackClick = { navController.navigateUp() }, + onShowWallpaperView = { + navController.navigate(WallpaperView(it)) + }, + animate = settingsState.value.themeSettings.animate + ) + } } // Navigate to sort view screen to sort wallpapers and folders @@ -652,8 +655,7 @@ fun PaperizeApp( }, onShowFolderView = { folder -> if (folder.wallpapers.isNotEmpty()) { - folderViewModel.folderName.value = folder.folderName ?: "" - folderViewModel.wallpapers.value = folder.wallpapers.map { it.wallpaperUri } + folderViewModel.onEvent(FolderEvent.LoadFolderView(folder)) navController.navigate(FolderView) } }, diff --git a/app/src/main/java/com/anthonyla/paperize/feature/wallpaper/presentation/folder_view_screen/FolderEvent.kt b/app/src/main/java/com/anthonyla/paperize/feature/wallpaper/presentation/folder_view_screen/FolderEvent.kt new file mode 100644 index 00000000..dd28a5d2 --- /dev/null +++ b/app/src/main/java/com/anthonyla/paperize/feature/wallpaper/presentation/folder_view_screen/FolderEvent.kt @@ -0,0 +1,11 @@ +package com.anthonyla.paperize.feature.wallpaper.presentation.folder_view_screen + +import com.anthonyla.paperize.feature.wallpaper.domain.model.Folder + +sealed class FolderEvent { + data object Reset: FolderEvent() + + data class LoadFolderView( + val folder: Folder? = null + ): FolderEvent() +} \ No newline at end of file diff --git a/app/src/main/java/com/anthonyla/paperize/feature/wallpaper/presentation/folder_view_screen/FolderState.kt b/app/src/main/java/com/anthonyla/paperize/feature/wallpaper/presentation/folder_view_screen/FolderState.kt new file mode 100644 index 00000000..82c3a705 --- /dev/null +++ b/app/src/main/java/com/anthonyla/paperize/feature/wallpaper/presentation/folder_view_screen/FolderState.kt @@ -0,0 +1,8 @@ +package com.anthonyla.paperize.feature.wallpaper.presentation.folder_view_screen + +import com.anthonyla.paperize.feature.wallpaper.domain.model.Folder +import com.anthonyla.paperize.feature.wallpaper.domain.model.Wallpaper + +data class FolderState( + val folder: Folder? = null, +) diff --git a/app/src/main/java/com/anthonyla/paperize/feature/wallpaper/presentation/folder_view_screen/FolderViewModel.kt b/app/src/main/java/com/anthonyla/paperize/feature/wallpaper/presentation/folder_view_screen/FolderViewModel.kt index 8678f1e7..e0d8dbc6 100644 --- a/app/src/main/java/com/anthonyla/paperize/feature/wallpaper/presentation/folder_view_screen/FolderViewModel.kt +++ b/app/src/main/java/com/anthonyla/paperize/feature/wallpaper/presentation/folder_view_screen/FolderViewModel.kt @@ -2,12 +2,36 @@ package com.anthonyla.paperize.feature.wallpaper.presentation.folder_view_screen import androidx.compose.runtime.mutableStateOf import androidx.lifecycle.ViewModel +import androidx.lifecycle.viewModelScope +import com.anthonyla.paperize.feature.wallpaper.domain.model.Folder +import com.anthonyla.paperize.feature.wallpaper.presentation.sort_view_screen.SortEvent +import com.anthonyla.paperize.feature.wallpaper.presentation.sort_view_screen.SortState +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.SharingStarted +import kotlinx.coroutines.flow.stateIn import javax.inject.Inject /** * ViewModel for the folder view screen to hold the folder name and the list of wallpapers */ class FolderViewModel @Inject constructor (): ViewModel() { - var folderName = mutableStateOf("") - var wallpapers = mutableStateOf>(emptyList()) + private val _state = MutableStateFlow(FolderState()) + val state = _state.stateIn( + scope = viewModelScope, + started = SharingStarted.WhileSubscribed(5000), + initialValue = FolderState() + ) + + fun onEvent(event: FolderEvent) { + when (event) { + is FolderEvent.LoadFolderView -> { + _state.value = state.value.copy( + folder = event.folder + ) + } + is FolderEvent.Reset -> { + _state.value = FolderState() + } + } + } } \ No newline at end of file diff --git a/app/src/main/java/com/anthonyla/paperize/feature/wallpaper/presentation/folder_view_screen/FolderViewScreen.kt b/app/src/main/java/com/anthonyla/paperize/feature/wallpaper/presentation/folder_view_screen/FolderViewScreen.kt index df43c61e..36fda6fc 100644 --- a/app/src/main/java/com/anthonyla/paperize/feature/wallpaper/presentation/folder_view_screen/FolderViewScreen.kt +++ b/app/src/main/java/com/anthonyla/paperize/feature/wallpaper/presentation/folder_view_screen/FolderViewScreen.kt @@ -10,6 +10,7 @@ import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.size import androidx.compose.foundation.lazy.grid.GridCells import androidx.compose.foundation.lazy.grid.LazyVerticalGrid +import androidx.compose.foundation.lazy.grid.items import androidx.compose.foundation.lazy.grid.rememberLazyGridState import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.material3.MaterialTheme @@ -17,6 +18,7 @@ import androidx.compose.material3.Scaffold import androidx.compose.runtime.Composable import androidx.compose.ui.Modifier import androidx.compose.ui.unit.dp +import com.anthonyla.paperize.feature.wallpaper.domain.model.Folder import com.anthonyla.paperize.feature.wallpaper.presentation.album.components.WallpaperItem import com.anthonyla.paperize.feature.wallpaper.presentation.folder_view_screen.components.FolderViewTopBar import my.nanihadesuka.compose.LazyVerticalGridScrollbar @@ -24,8 +26,7 @@ import my.nanihadesuka.compose.ScrollbarSettings @Composable fun FolderViewScreen( - folderName: String?, - wallpapers: List, + folder: Folder, onBackClick: () -> Unit, onShowWallpaperView: (String) -> Unit, animate: Boolean @@ -36,7 +37,7 @@ fun FolderViewScreen( Scaffold( topBar = { FolderViewTopBar( - title = folderName ?: "", + title = folder.folderName ?: "", onBackClick = onBackClick ) }, @@ -60,44 +61,29 @@ fun FolderViewScreen( contentPadding = PaddingValues(4.dp, 4.dp), horizontalArrangement = Arrangement.Start, ) { - items(count = wallpapers.size, key = { index -> wallpapers[index] }) { index -> - if (animate) { - WallpaperItem( - wallpaperUri = wallpapers[index], - itemSelected = false, - selectionMode = false, - allowHapticFeedback = false, - onItemSelection = {}, - onWallpaperViewClick = { - onShowWallpaperView(wallpapers[index]) - }, - modifier = Modifier - .padding(4.dp) - .size(150.dp, 350.dp) - .animateItem( + items(items = folder.wallpapers, key = { wallpaper -> wallpaper.wallpaperUri }) { wallpaper -> + WallpaperItem( + wallpaperUri = wallpaper.wallpaperUri, + itemSelected = false, + selectionMode = false, + allowHapticFeedback = false, + onItemSelection = {}, + onWallpaperViewClick = { + onShowWallpaperView(wallpaper.wallpaperUri) + }, + modifier = Modifier + .padding(4.dp) + .size(150.dp, 350.dp) + .then( + if (animate) Modifier.animateItem( placementSpec = tween( durationMillis = 800, delayMillis = 0, easing = FastOutSlowInEasing - ), - ) - ) - } - else { - WallpaperItem( - wallpaperUri = wallpapers[index], - itemSelected = false, - selectionMode = false, - allowHapticFeedback = false, - onItemSelection = {}, - onWallpaperViewClick = { - onShowWallpaperView(wallpapers[index]) - }, - modifier = Modifier - .padding(4.dp) - .size(150.dp, 350.dp) - ) - } + ) + ) else Modifier + ) + ) } } } diff --git a/app/src/main/java/com/anthonyla/paperize/feature/wallpaper/presentation/home_screen/components/HomeTopBar.kt b/app/src/main/java/com/anthonyla/paperize/feature/wallpaper/presentation/home_screen/components/HomeTopBar.kt index 36e562d5..bef6189d 100644 --- a/app/src/main/java/com/anthonyla/paperize/feature/wallpaper/presentation/home_screen/components/HomeTopBar.kt +++ b/app/src/main/java/com/anthonyla/paperize/feature/wallpaper/presentation/home_screen/components/HomeTopBar.kt @@ -91,8 +91,7 @@ fun HomeTopBar ( } } else { IconButton( - onClick = { onSettingsClick() }, - modifier = Modifier.testTag("paperize:home_to_settings_button"), + onClick = { onSettingsClick() } ) { Icon( imageVector = Icons.Outlined.Settings, diff --git a/app/src/main/java/com/anthonyla/paperize/feature/wallpaper/presentation/sort_view_screen/SortViewScreen.kt b/app/src/main/java/com/anthonyla/paperize/feature/wallpaper/presentation/sort_view_screen/SortViewScreen.kt index 9392528f..9f09b253 100644 --- a/app/src/main/java/com/anthonyla/paperize/feature/wallpaper/presentation/sort_view_screen/SortViewScreen.kt +++ b/app/src/main/java/com/anthonyla/paperize/feature/wallpaper/presentation/sort_view_screen/SortViewScreen.kt @@ -1,5 +1,6 @@ package com.anthonyla.paperize.feature.wallpaper.presentation.sort_view_screen +import android.R.attr.onClick import android.os.Build import android.view.HapticFeedbackConstants import androidx.compose.animation.AnimatedContent @@ -159,6 +160,12 @@ fun SortViewScreen( }, interactionSource = interactionSource, ), + colors = CardColors( + containerColor = MaterialTheme.colorScheme.secondaryContainer, + contentColor = MaterialTheme.colorScheme.onSecondaryContainer, + disabledContainerColor = MaterialTheme.colorScheme.secondaryContainer, + disabledContentColor = MaterialTheme.colorScheme.onSecondaryContainer, + ), onClick = { expandedFolderId.value = folder.folderUri } ) { Row( @@ -236,7 +243,13 @@ fun SortViewScreen( }, interactionSource = interactionSource, ), - onClick = {} + onClick = {}, + colors = CardColors( + containerColor = MaterialTheme.colorScheme.secondaryContainer, + contentColor = MaterialTheme.colorScheme.onSecondaryContainer, + disabledContainerColor = MaterialTheme.colorScheme.secondaryContainer, + disabledContentColor = MaterialTheme.colorScheme.onSecondaryContainer, + ), ) { Row( verticalAlignment = Alignment.CenterVertically, @@ -336,7 +349,13 @@ fun SortViewScreen( }, interactionSource = interactionSource, ), - onClick = {} + onClick = {}, + colors = CardColors( + containerColor = MaterialTheme.colorScheme.secondaryContainer, + contentColor = MaterialTheme.colorScheme.onSecondaryContainer, + disabledContainerColor = MaterialTheme.colorScheme.secondaryContainer, + disabledContentColor = MaterialTheme.colorScheme.onSecondaryContainer, + ), ) { Row( verticalAlignment = Alignment.CenterVertically, diff --git a/app/src/main/java/com/anthonyla/paperize/feature/wallpaper/presentation/wallpaper_view_screen/WallpaperViewScreen.kt b/app/src/main/java/com/anthonyla/paperize/feature/wallpaper/presentation/wallpaper_view_screen/WallpaperViewScreen.kt index b34bbb9f..9f206344 100644 --- a/app/src/main/java/com/anthonyla/paperize/feature/wallpaper/presentation/wallpaper_view_screen/WallpaperViewScreen.kt +++ b/app/src/main/java/com/anthonyla/paperize/feature/wallpaper/presentation/wallpaper_view_screen/WallpaperViewScreen.kt @@ -29,6 +29,7 @@ import androidx.compose.ui.res.stringResource import androidx.compose.ui.unit.dp import androidx.core.net.toUri import com.anthonyla.paperize.R +import com.anthonyla.paperize.core.isValidUri import com.skydoves.landscapist.ImageOptions import com.skydoves.landscapist.glide.GlideImage import net.engawapg.lib.zoomable.rememberZoomState @@ -44,17 +45,6 @@ fun WallpaperViewScreen( onBackClick: () -> Unit, animate : Boolean ) { - fun isValidUri(context: Context, uriString: String?): Boolean { - val uri = uriString?.toUri() - return try { - uri?.let { - val inputStream = context.contentResolver.openInputStream(it) - inputStream?.close() - } - true - } catch (e: Exception) { false } - } - val showUri = isValidUri(LocalContext.current, wallpaperUri) val zoomState = rememberZoomState() BackHandler { onBackClick() }