Skip to content

Commit e8371f0

Browse files
committed
fix: the issue that can select exceeded resources
1 parent e3c43f9 commit e8371f0

File tree

6 files changed

+42
-11
lines changed

6 files changed

+42
-11
lines changed

app/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,5 +62,5 @@ dependencies {
6262

6363
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
6464
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
65-
debugImplementation 'androidx.compose.ui:ui-tooling:1.6.1'
65+
debugImplementation 'androidx.compose.ui:ui-tooling:1.6.2'
6666
}

build.gradle

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ buildscript {
66
kotlin_version = '1.9.22'
77

88
accompanist_version = '0.34.0'
9-
coil_version = '2.5.0'
9+
coil_version = '2.6.0'
1010
exoplayer_version = '2.19.1'
1111
}
1212
}// Top-level build file where you can add configuration options common to all sub-projects/modules.
1313
plugins {
14-
id 'com.android.application' version '8.2.2' apply false
15-
id 'com.android.library' version '8.2.2' apply false
14+
id 'com.android.application' version '8.3.0' apply false
15+
id 'com.android.library' version '8.3.0' apply false
1616
id 'org.jetbrains.kotlin.android' version "$kotlin_version" apply false
1717
id "org.jlleitschuh.gradle.ktlint" version "11.6.0"
1818
}

compose_image_picker/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ dependencies {
114114
implementation "io.coil-kt:coil-video:$coil_version"
115115
implementation "io.coil-kt:coil-gif:$coil_version"
116116

117-
def media3_version = "1.3.0-beta01"
117+
def media3_version = '1.3.0-rc01'
118118
implementation "androidx.media3:media3-exoplayer:$media3_version"
119119
implementation "androidx.media3:media3-ui:$media3_version"
120120

compose_image_picker/src/main/java/com/huhx/picker/view/AssetDisplayScreen.kt

+33-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.huhx.picker.view
22

33
import android.net.Uri
4+
import android.widget.Toast
45
import androidx.activity.compose.BackHandler
56
import androidx.activity.compose.rememberLauncherForActivityResult
67
import androidx.activity.result.contract.ActivityResultContracts
@@ -46,8 +47,10 @@ import androidx.compose.ui.Modifier
4647
import androidx.compose.ui.graphics.Color
4748
import androidx.compose.ui.graphics.FilterQuality
4849
import androidx.compose.ui.platform.LocalConfiguration
50+
import androidx.compose.ui.platform.LocalContext
4951
import androidx.compose.ui.res.stringResource
5052
import androidx.compose.ui.text.font.FontWeight
53+
import androidx.compose.ui.text.style.TextAlign
5154
import androidx.compose.ui.unit.Dp
5255
import androidx.compose.ui.unit.dp
5356
import androidx.compose.ui.unit.sp
@@ -66,7 +69,7 @@ internal fun AssetDisplayScreen(
6669
onClose: (List<AssetInfo>) -> Unit,
6770
) {
6871
BackHandler {
69-
if (viewModel.selectedList.isNotEmpty()) {
72+
if (viewModel.selectedList.isNotEmpty()) {
7073
viewModel.clear()
7174
} else {
7275
onClose(viewModel.selectedList)
@@ -193,7 +196,22 @@ private fun AssetTab(tabs: List<TabItem>, pagerState: PagerState) {
193196
@Composable
194197
private fun AssetContent(viewModel: AssetViewModel, requestType: RequestType) {
195198
val assets = viewModel.getGroupedAssets(requestType)
199+
val context = LocalContext.current
196200
val gridCount = LocalAssetConfig.current.gridCount
201+
val maxAssets = LocalAssetConfig.current.maxAssets
202+
val errorMessage = stringResource(R.string.message_selected_exceed, maxAssets)
203+
204+
if (assets.isEmpty()) {
205+
return Box(
206+
modifier = Modifier.fillMaxSize(),
207+
contentAlignment = Alignment.Center
208+
) {
209+
Text(
210+
text = "对应的资源为空",
211+
textAlign = TextAlign.Center
212+
)
213+
}
214+
}
197215

198216
LazyColumn {
199217
assets.forEach { (dateString, resources) ->
@@ -215,7 +233,9 @@ private fun AssetContent(viewModel: AssetViewModel, requestType: RequestType) {
215233
if (allSelected) {
216234
viewModel.unSelectAll(resources)
217235
} else {
218-
viewModel.selectAll(resources)
236+
if (viewModel.selectAll(resources, maxAssets)) {
237+
Toast.makeText(context, errorMessage, Toast.LENGTH_SHORT).show()
238+
}
219239
}
220240
}) {
221241
Text(
@@ -258,6 +278,9 @@ private fun AssetImage(
258278
onLongClick: (Boolean) -> Unit,
259279
) {
260280
val selected = selectedList.any { it.id == assetInfo.id }
281+
val context = LocalContext.current
282+
val maxAssets = LocalAssetConfig.current.maxAssets
283+
val errorMessage = stringResource(R.string.message_selected_exceed, maxAssets)
261284

262285
Box(
263286
modifier = modifier.fillMaxSize(),
@@ -270,7 +293,14 @@ private fun AssetImage(
270293
resourceType = assetInfo.resourceType,
271294
durationString = assetInfo.formatDuration(),
272295
navigateToPreview = navigateToPreview,
273-
onLongClick = { onLongClick(!selected) }
296+
onLongClick = {
297+
val selectResult = !selected
298+
if (!selectResult || selectedList.size < maxAssets) {
299+
onLongClick(selectResult)
300+
} else {
301+
Toast.makeText(context, errorMessage, Toast.LENGTH_SHORT).show()
302+
}
303+
}
274304
)
275305
AssetImageIndicator(assetInfo = assetInfo, selected = selected, assetSelected = selectedList)
276306
}

compose_image_picker/src/main/java/com/huhx/picker/viewmodel/AssetViewModel.kt

+3-2
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,11 @@ internal class AssetViewModel(
106106
selectedList -= resources.toSet()
107107
}
108108

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

113-
selectedList += newSelectedList
113+
selectedList += newSelectedList.subList(0, minOf(maxAssets - selectedIds.size, newSelectedList.size))
114+
return maxAssets - selectedIds.size < newSelectedList.size
114115
}
115116
}
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#Sat Jul 23 19:28:55 CST 2022
22
distributionBase=GRADLE_USER_HOME
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-8.2-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-bin.zip
44
distributionPath=wrapper/dists
55
zipStorePath=wrapper/dists
66
zipStoreBase=GRADLE_USER_HOME

0 commit comments

Comments
 (0)