Skip to content

Commit e5c4230

Browse files
Fix - Custom picker sometimes crashing when marking many pictures as "not for upload" towards the bottom (commons-app#5639)
* fix crash * fix crash
1 parent 7e5789d commit e5c4230

File tree

1 file changed

+33
-35
lines changed

1 file changed

+33
-35
lines changed

app/src/main/java/fr/free/nrw/commons/customselector/ui/selector/CustomSelectorActivity.kt

Lines changed: 33 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -206,15 +206,15 @@ class CustomSelectorActivity : BaseActivity(), FolderClickListener, ImageSelectL
206206
markAsNotForUpload(arrayListOf())
207207
return
208208
}
209-
var i = 0
210-
while (i < selectedImages.size) {
211-
val path = selectedImages[i].path
209+
210+
val iterator = selectedImages.iterator()
211+
while (iterator.hasNext()) {
212+
val image = iterator.next()
213+
val path = image.path
212214
val file = File(path)
213215
if (!file.exists()) {
214-
selectedImages.removeAt(i)
215-
i--
216+
iterator.remove()
216217
}
217-
i++
218218
}
219219
markAsNotForUpload(selectedImages)
220220
toolbarBinding.imageLimitError.visibility = View.INVISIBLE
@@ -241,66 +241,64 @@ class CustomSelectorActivity : BaseActivity(), FolderClickListener, ImageSelectL
241241
*/
242242
private fun insertIntoNotForUpload(images: ArrayList<Image>) {
243243
scope.launch {
244-
imageFragment!!.showMarkUnmarkProgressDialog(
245-
text= progressDialogText
246-
)
244+
withContext(Dispatchers.Main) {
245+
imageFragment?.showMarkUnmarkProgressDialog(text = progressDialogText)
246+
}
247247

248248
var allImagesAlreadyNotForUpload = true
249-
images.forEach {
249+
images.forEach { image ->
250250
val imageSHA1 = CustomSelectorUtils.getImageSHA1(
251-
it.uri,
251+
image.uri,
252252
ioDispatcher,
253253
fileUtilsWrapper,
254254
contentResolver
255255
)
256256
val exists = notForUploadStatusDao.find(imageSHA1)
257-
258-
// If image exists in not for upload table make allImagesAlreadyNotForUpload false
259257
if (exists < 1) {
260258
allImagesAlreadyNotForUpload = false
261259
}
262260
}
263261

264-
// if all images is not already marked as not for upload, insert all images in
265-
// not for upload table
266262
if (!allImagesAlreadyNotForUpload) {
267-
images.forEach {
263+
// Insert or delete images as necessary, but the UI updates should be posted back to the main thread
264+
images.forEach { image ->
268265
val imageSHA1 = CustomSelectorUtils.getImageSHA1(
269-
it.uri,
266+
image.uri,
270267
ioDispatcher,
271268
fileUtilsWrapper,
272269
contentResolver
273270
)
274-
notForUploadStatusDao.insert(
275-
NotForUploadStatus(
276-
imageSHA1
277-
)
278-
)
279-
imageFragment!!.removeImage(it)
280-
271+
notForUploadStatusDao.insert(NotForUploadStatus(imageSHA1))
272+
}
273+
withContext(Dispatchers.Main) {
274+
images.forEach { image ->
275+
imageFragment?.removeImage(image)
276+
}
277+
imageFragment?.clearSelectedImages()
281278
}
282-
imageFragment!!.clearSelectedImages()
283-
// if all images is already marked as not for upload, delete all images from
284-
// not for upload table
285279
} else {
286-
images.forEach {
280+
images.forEach { image ->
287281
val imageSHA1 = CustomSelectorUtils.getImageSHA1(
288-
it.uri,
282+
image.uri,
289283
ioDispatcher,
290284
fileUtilsWrapper,
291285
contentResolver
292286
)
293287
notForUploadStatusDao.deleteNotForUploadWithImageSHA1(imageSHA1)
294288
}
295-
imageFragment!!.refresh()
296-
}
297289

298-
imageFragment!!.dismissMarkUnmarkProgressDialog()
290+
withContext(Dispatchers.Main) {
291+
imageFragment?.refresh()
292+
}
293+
}
299294

300-
val bottomLayout: ConstraintLayout = findViewById(R.id.bottom_layout)
301-
bottomLayout.visibility = View.GONE
295+
withContext(Dispatchers.Main) {
296+
imageFragment?.dismissMarkUnmarkProgressDialog()
297+
val bottomLayout: ConstraintLayout = findViewById(R.id.bottom_layout)
298+
bottomLayout.visibility = View.GONE
299+
changeTitle(bucketName, 0)
300+
}
302301
}
303-
changeTitle(bucketName, 0)
304302
}
305303

306304
/**

0 commit comments

Comments
 (0)