限制帖子图片数量为9张

This commit is contained in:
2025-08-05 16:20:30 +08:00
parent 6433d4a23c
commit 0f5d3d7960

View File

@@ -435,7 +435,22 @@ fun AddImageGrid() {
) { uris ->
if (uris.isNotEmpty()) {
scope.launch {
for (uri in uris) {
val currentCount = model.imageList.size
val remainingSlots = 9 - currentCount
if (remainingSlots <= 0) {
Toast.makeText(context, "最多只能选择9张图片", Toast.LENGTH_SHORT).show()
return@launch
}
val urisToProcess = if (uris.size > remainingSlots) {
Toast.makeText(context, "已选择${uris.size}张图片,但只能添加前${remainingSlots}", Toast.LENGTH_SHORT).show()
uris.take(remainingSlots)
} else {
uris
}
for (uri in urisToProcess) {
ImageItem.fromUri(context, uri.toString())?.let {
model.imageList += it
}
@@ -449,10 +464,14 @@ fun AddImageGrid() {
) { success ->
if (success) {
scope.launch {
if (model.imageList.size >= 9) {
Toast.makeText(context, "最多只能选择9张图片", Toast.LENGTH_SHORT).show()
return@launch
}
ImageItem.fromUri(context, model.currentPhotoUri.toString())?.let {
model.imageList += it
}
}
}
}
@@ -502,6 +521,8 @@ fun AddImageGrid() {
}
}
}
val canAddMoreImages = model.imageList.size < 9
LazyVerticalGrid(
columns = GridCells.Fixed(5),
contentPadding = PaddingValues(8.dp),
@@ -511,6 +532,7 @@ fun AddImageGrid() {
horizontalArrangement = Arrangement.spacedBy(8.dp),
verticalArrangement = Arrangement.spacedBy(8.dp)
) {
if (canAddMoreImages) {
item {
Box(
modifier = Modifier
@@ -519,7 +541,11 @@ fun AddImageGrid() {
.clip(RoundedCornerShape(16.dp)) // 设置圆角
.background(AppColors.basicMain) // 设置背景色
.noRippleClickable {
if (model.imageList.size < 9) {
pickImagesLauncher.launch("image/*")
} else {
Toast.makeText(context, "最多只能选择9张图片", Toast.LENGTH_SHORT).show()
}
},
) {
Icon(
@@ -541,6 +567,7 @@ fun AddImageGrid() {
.clip(RoundedCornerShape(16.dp)) // 设置圆角
.background(AppColors.basicMain) // 设置背景色
.noRippleClickable {
if (model.imageList.size < 9) {
val photoFile = File(context.cacheDir, "photo.jpg")
val photoUri: Uri = FileProvider.getUriForFile(
context,
@@ -549,6 +576,9 @@ fun AddImageGrid() {
)
model.currentPhotoUri = photoUri
takePictureLauncher.launch(photoUri)
} else {
Toast.makeText(context, "最多只能选择9张图片", Toast.LENGTH_SHORT).show()
}
},
) {
Icon(
@@ -562,5 +592,6 @@ fun AddImageGrid() {
}
}
}
}
}