Skip to content

Commit

Permalink
Refactored folder view
Browse files Browse the repository at this point in the history
  • Loading branch information
Anthonyy232 committed Nov 14, 2024
1 parent f2fc197 commit 47fa756
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<Job?>(null) }
val scope = rememberCoroutineScope()

Expand Down Expand Up @@ -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)
}
},
Expand All @@ -600,17 +601,19 @@ fun PaperizeApp(

// Navigate to the folder view screen to view wallpapers in a folder
animatedScreen<FolderView>(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
Expand Down Expand Up @@ -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)
}
},
Expand Down
Original file line number Diff line number Diff line change
@@ -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()
}
Original file line number Diff line number Diff line change
@@ -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,
)
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>("")
var wallpapers = mutableStateOf<List<String>>(emptyList())
private val _state = MutableStateFlow<FolderState>(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()
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,23 @@ 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
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
import my.nanihadesuka.compose.ScrollbarSettings

@Composable
fun FolderViewScreen(
folderName: String?,
wallpapers: List<String>,
folder: Folder,
onBackClick: () -> Unit,
onShowWallpaperView: (String) -> Unit,
animate: Boolean
Expand All @@ -36,7 +37,7 @@ fun FolderViewScreen(
Scaffold(
topBar = {
FolderViewTopBar(
title = folderName ?: "",
title = folder.folderName ?: "",
onBackClick = onBackClick
)
},
Expand All @@ -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
)
)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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() }
Expand Down

0 comments on commit 47fa756

Please sign in to comment.