Skip to content

Commit

Permalink
feat: add group assets features
Browse files Browse the repository at this point in the history
  • Loading branch information
huhx committed Feb 25, 2024
1 parent daa0bac commit 026efe0
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.heightIn
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.statusBarsPadding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.itemsIndexed
Expand Down Expand Up @@ -45,6 +47,7 @@ import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.huhx.picker.R
Expand Down Expand Up @@ -87,16 +90,16 @@ internal fun AssetDisplayScreen(

Column {
AssetTab(tabs = tabs, pagerState = pagerState)
HorizontalPager(state = pagerState, userScrollEnabled = true) { page ->
HorizontalPager(state = pagerState, userScrollEnabled = false) { page ->
tabs[page].screen(viewModel)
}
}
}
}
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
@OptIn(ExperimentalMaterial3Api::class)
private fun DisplayTopAppBar(
directory: String,
selectedList: List<AssetInfo>,
Expand Down Expand Up @@ -186,24 +189,56 @@ private fun AssetTab(tabs: List<TabItem>, pagerState: PagerState) {

@Composable
private fun AssetContent(viewModel: AssetViewModel, requestType: RequestType) {
val assets = viewModel.getAssets(requestType)
val assets = viewModel.getGroupedAssets(requestType)
val gridCount = LocalAssetConfig.current.gridCount

LazyVerticalGrid(
modifier = Modifier.fillMaxSize(),
columns = GridCells.Fixed(gridCount),
contentPadding = PaddingValues(horizontal = 1.dp),
verticalArrangement = Arrangement.spacedBy(1.dp),
horizontalArrangement = Arrangement.spacedBy(1.dp),
userScrollEnabled = true
) {
itemsIndexed(assets, key = { _, it -> it.id }) { index, assetInfo ->
AssetImage(
assetInfo = assetInfo,
navigateToPreview = { viewModel.navigateToPreview(index, requestType) },
selectedList = viewModel.selectedList,
onLongClick = { selected -> viewModel.toggleSelect(selected, assetInfo) }
)
LazyColumn {
assets.forEach { (dateString, resources) ->
val allSelected = viewModel.isAllSelected(resources)
item {
Row(
modifier = Modifier
.fillMaxWidth()
.padding(start = 16.dp),
horizontalArrangement = Arrangement.SpaceBetween,
verticalAlignment = Alignment.CenterVertically
) {
Text(
text = dateString,
style = MaterialTheme.typography.bodyLarge.copy(fontWeight = FontWeight.Medium)
)

TextButton(onClick = {
if (allSelected) {
viewModel.unSelectAll(resources)
} else {
viewModel.selectAll(resources)
}
}) {
Text(text = if (allSelected) "取消全选" else "全选")
}
}
}

item {
LazyVerticalGrid(
modifier = Modifier.heightIn(0.dp, 600.dp),
columns = GridCells.Fixed(gridCount),
contentPadding = PaddingValues(horizontal = 1.dp),
verticalArrangement = Arrangement.spacedBy(1.dp),
horizontalArrangement = Arrangement.spacedBy(1.dp),
userScrollEnabled = false
) {
itemsIndexed(resources, key = { _, it -> it.id }) { index, assetInfo ->
AssetImage(
assetInfo = assetInfo,
navigateToPreview = { viewModel.navigateToPreview(index, requestType) },
selectedList = viewModel.selectedList,
onLongClick = { selected -> viewModel.toggleSelect(selected, assetInfo) }
)
}
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,27 @@ internal class AssetViewModel(
}
}


fun getGroupedAssets(requestType: RequestType): Map<String, List<AssetInfo>> {
val assetList = _directoryGroup.first { it.directory == directory }.assets

return assetList.filter {
when (requestType) {
RequestType.COMMON -> true
RequestType.IMAGE -> it.isImage()
RequestType.VIDEO -> it.isVideo()
}
}
.sortedByDescending { it.date }
.groupBy { it.dateString }
}

fun isAllSelected(assets: List<AssetInfo>): Boolean {
val selectedIds = selectedList.map { it.id }
val ids = assets.map { it.id }
return selectedIds.containsAll(ids)
}

fun navigateToPreview(index: Int, requestType: RequestType) {
navController.navigate(AssetRoute.preview(index, requestType))
}
Expand All @@ -93,4 +114,15 @@ internal class AssetViewModel(
fun getUri(): Uri? {
return assetPickerRepository.insertImage()
}

fun unSelectAll(resources: List<AssetInfo>) {
selectedList -= resources.toSet()
}

fun selectAll(resources: List<AssetInfo>) {
val selectedIds = selectedList.map { it.id }
val newSelectedList = resources.filterNot { selectedIds.contains(it.id) }

selectedList += newSelectedList
}
}

0 comments on commit 026efe0

Please sign in to comment.