限制帖子图片数量为9张
This commit is contained in:
@@ -435,7 +435,22 @@ fun AddImageGrid() {
|
|||||||
) { uris ->
|
) { uris ->
|
||||||
if (uris.isNotEmpty()) {
|
if (uris.isNotEmpty()) {
|
||||||
scope.launch {
|
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 {
|
ImageItem.fromUri(context, uri.toString())?.let {
|
||||||
model.imageList += it
|
model.imageList += it
|
||||||
}
|
}
|
||||||
@@ -449,10 +464,14 @@ fun AddImageGrid() {
|
|||||||
) { success ->
|
) { success ->
|
||||||
if (success) {
|
if (success) {
|
||||||
scope.launch {
|
scope.launch {
|
||||||
|
if (model.imageList.size >= 9) {
|
||||||
|
Toast.makeText(context, "最多只能选择9张图片", Toast.LENGTH_SHORT).show()
|
||||||
|
return@launch
|
||||||
|
}
|
||||||
|
|
||||||
ImageItem.fromUri(context, model.currentPhotoUri.toString())?.let {
|
ImageItem.fromUri(context, model.currentPhotoUri.toString())?.let {
|
||||||
model.imageList += it
|
model.imageList += it
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -502,6 +521,8 @@ fun AddImageGrid() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
val canAddMoreImages = model.imageList.size < 9
|
||||||
|
|
||||||
LazyVerticalGrid(
|
LazyVerticalGrid(
|
||||||
columns = GridCells.Fixed(5),
|
columns = GridCells.Fixed(5),
|
||||||
contentPadding = PaddingValues(8.dp),
|
contentPadding = PaddingValues(8.dp),
|
||||||
@@ -511,6 +532,7 @@ fun AddImageGrid() {
|
|||||||
horizontalArrangement = Arrangement.spacedBy(8.dp),
|
horizontalArrangement = Arrangement.spacedBy(8.dp),
|
||||||
verticalArrangement = Arrangement.spacedBy(8.dp)
|
verticalArrangement = Arrangement.spacedBy(8.dp)
|
||||||
) {
|
) {
|
||||||
|
if (canAddMoreImages) {
|
||||||
item {
|
item {
|
||||||
Box(
|
Box(
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
@@ -519,7 +541,11 @@ fun AddImageGrid() {
|
|||||||
.clip(RoundedCornerShape(16.dp)) // 设置圆角
|
.clip(RoundedCornerShape(16.dp)) // 设置圆角
|
||||||
.background(AppColors.basicMain) // 设置背景色
|
.background(AppColors.basicMain) // 设置背景色
|
||||||
.noRippleClickable {
|
.noRippleClickable {
|
||||||
|
if (model.imageList.size < 9) {
|
||||||
pickImagesLauncher.launch("image/*")
|
pickImagesLauncher.launch("image/*")
|
||||||
|
} else {
|
||||||
|
Toast.makeText(context, "最多只能选择9张图片", Toast.LENGTH_SHORT).show()
|
||||||
|
}
|
||||||
},
|
},
|
||||||
) {
|
) {
|
||||||
Icon(
|
Icon(
|
||||||
@@ -541,6 +567,7 @@ fun AddImageGrid() {
|
|||||||
.clip(RoundedCornerShape(16.dp)) // 设置圆角
|
.clip(RoundedCornerShape(16.dp)) // 设置圆角
|
||||||
.background(AppColors.basicMain) // 设置背景色
|
.background(AppColors.basicMain) // 设置背景色
|
||||||
.noRippleClickable {
|
.noRippleClickable {
|
||||||
|
if (model.imageList.size < 9) {
|
||||||
val photoFile = File(context.cacheDir, "photo.jpg")
|
val photoFile = File(context.cacheDir, "photo.jpg")
|
||||||
val photoUri: Uri = FileProvider.getUriForFile(
|
val photoUri: Uri = FileProvider.getUriForFile(
|
||||||
context,
|
context,
|
||||||
@@ -549,6 +576,9 @@ fun AddImageGrid() {
|
|||||||
)
|
)
|
||||||
model.currentPhotoUri = photoUri
|
model.currentPhotoUri = photoUri
|
||||||
takePictureLauncher.launch(photoUri)
|
takePictureLauncher.launch(photoUri)
|
||||||
|
} else {
|
||||||
|
Toast.makeText(context, "最多只能选择9张图片", Toast.LENGTH_SHORT).show()
|
||||||
|
}
|
||||||
},
|
},
|
||||||
) {
|
) {
|
||||||
Icon(
|
Icon(
|
||||||
@@ -563,4 +593,5 @@ fun AddImageGrid() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user