Skip to content

Commit

Permalink
fix: the issue that can select exceeded resources
Browse files Browse the repository at this point in the history
  • Loading branch information
huhx committed Mar 4, 2024
1 parent e3c43f9 commit e8371f0
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 11 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,5 @@ dependencies {

androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
debugImplementation 'androidx.compose.ui:ui-tooling:1.6.1'
debugImplementation 'androidx.compose.ui:ui-tooling:1.6.2'
}
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ buildscript {
kotlin_version = '1.9.22'

accompanist_version = '0.34.0'
coil_version = '2.5.0'
coil_version = '2.6.0'
exoplayer_version = '2.19.1'
}
}// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
id 'com.android.application' version '8.2.2' apply false
id 'com.android.library' version '8.2.2' apply false
id 'com.android.application' version '8.3.0' apply false
id 'com.android.library' version '8.3.0' apply false
id 'org.jetbrains.kotlin.android' version "$kotlin_version" apply false
id "org.jlleitschuh.gradle.ktlint" version "11.6.0"
}
Expand Down
2 changes: 1 addition & 1 deletion compose_image_picker/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ dependencies {
implementation "io.coil-kt:coil-video:$coil_version"
implementation "io.coil-kt:coil-gif:$coil_version"

def media3_version = "1.3.0-beta01"
def media3_version = '1.3.0-rc01'
implementation "androidx.media3:media3-exoplayer:$media3_version"
implementation "androidx.media3:media3-ui:$media3_version"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.huhx.picker.view

import android.net.Uri
import android.widget.Toast
import androidx.activity.compose.BackHandler
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.contract.ActivityResultContracts
Expand Down Expand Up @@ -46,8 +47,10 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.FilterQuality
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
Expand All @@ -66,7 +69,7 @@ internal fun AssetDisplayScreen(
onClose: (List<AssetInfo>) -> Unit,
) {
BackHandler {
if (viewModel.selectedList.isNotEmpty()) {
if (viewModel.selectedList.isNotEmpty()) {
viewModel.clear()
} else {
onClose(viewModel.selectedList)
Expand Down Expand Up @@ -193,7 +196,22 @@ private fun AssetTab(tabs: List<TabItem>, pagerState: PagerState) {
@Composable
private fun AssetContent(viewModel: AssetViewModel, requestType: RequestType) {
val assets = viewModel.getGroupedAssets(requestType)
val context = LocalContext.current
val gridCount = LocalAssetConfig.current.gridCount
val maxAssets = LocalAssetConfig.current.maxAssets
val errorMessage = stringResource(R.string.message_selected_exceed, maxAssets)

if (assets.isEmpty()) {
return Box(
modifier = Modifier.fillMaxSize(),
contentAlignment = Alignment.Center
) {
Text(
text = "对应的资源为空",
textAlign = TextAlign.Center
)
}
}

LazyColumn {
assets.forEach { (dateString, resources) ->
Expand All @@ -215,7 +233,9 @@ private fun AssetContent(viewModel: AssetViewModel, requestType: RequestType) {
if (allSelected) {
viewModel.unSelectAll(resources)
} else {
viewModel.selectAll(resources)
if (viewModel.selectAll(resources, maxAssets)) {
Toast.makeText(context, errorMessage, Toast.LENGTH_SHORT).show()
}
}
}) {
Text(
Expand Down Expand Up @@ -258,6 +278,9 @@ private fun AssetImage(
onLongClick: (Boolean) -> Unit,
) {
val selected = selectedList.any { it.id == assetInfo.id }
val context = LocalContext.current
val maxAssets = LocalAssetConfig.current.maxAssets
val errorMessage = stringResource(R.string.message_selected_exceed, maxAssets)

Box(
modifier = modifier.fillMaxSize(),
Expand All @@ -270,7 +293,14 @@ private fun AssetImage(
resourceType = assetInfo.resourceType,
durationString = assetInfo.formatDuration(),
navigateToPreview = navigateToPreview,
onLongClick = { onLongClick(!selected) }
onLongClick = {
val selectResult = !selected
if (!selectResult || selectedList.size < maxAssets) {
onLongClick(selectResult)
} else {
Toast.makeText(context, errorMessage, Toast.LENGTH_SHORT).show()
}
}
)
AssetImageIndicator(assetInfo = assetInfo, selected = selected, assetSelected = selectedList)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,11 @@ internal class AssetViewModel(
selectedList -= resources.toSet()
}

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

selectedList += newSelectedList
selectedList += newSelectedList.subList(0, minOf(maxAssets - selectedIds.size, newSelectedList.size))
return maxAssets - selectedIds.size < newSelectedList.size
}
}
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Sat Jul 23 19:28:55 CST 2022
distributionBase=GRADLE_USER_HOME
distributionUrl=https\://services.gradle.org/distributions/gradle-8.2-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-bin.zip
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME

0 comments on commit e8371f0

Please sign in to comment.