@@ -206,15 +206,15 @@ class CustomSelectorActivity : BaseActivity(), FolderClickListener, ImageSelectL
206
206
markAsNotForUpload(arrayListOf ())
207
207
return
208
208
}
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
212
214
val file = File (path)
213
215
if (! file.exists()) {
214
- selectedImages.removeAt(i)
215
- i--
216
+ iterator.remove()
216
217
}
217
- i++
218
218
}
219
219
markAsNotForUpload(selectedImages)
220
220
toolbarBinding.imageLimitError.visibility = View .INVISIBLE
@@ -241,66 +241,64 @@ class CustomSelectorActivity : BaseActivity(), FolderClickListener, ImageSelectL
241
241
*/
242
242
private fun insertIntoNotForUpload (images : ArrayList <Image >) {
243
243
scope.launch {
244
- imageFragment !! .showMarkUnmarkProgressDialog(
245
- text= progressDialogText
246
- )
244
+ withContext( Dispatchers . Main ) {
245
+ imageFragment?.showMarkUnmarkProgressDialog( text = progressDialogText)
246
+ }
247
247
248
248
var allImagesAlreadyNotForUpload = true
249
- images.forEach {
249
+ images.forEach { image ->
250
250
val imageSHA1 = CustomSelectorUtils .getImageSHA1(
251
- it .uri,
251
+ image .uri,
252
252
ioDispatcher,
253
253
fileUtilsWrapper,
254
254
contentResolver
255
255
)
256
256
val exists = notForUploadStatusDao.find(imageSHA1)
257
-
258
- // If image exists in not for upload table make allImagesAlreadyNotForUpload false
259
257
if (exists < 1 ) {
260
258
allImagesAlreadyNotForUpload = false
261
259
}
262
260
}
263
261
264
- // if all images is not already marked as not for upload, insert all images in
265
- // not for upload table
266
262
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 ->
268
265
val imageSHA1 = CustomSelectorUtils .getImageSHA1(
269
- it .uri,
266
+ image .uri,
270
267
ioDispatcher,
271
268
fileUtilsWrapper,
272
269
contentResolver
273
270
)
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()
281
278
}
282
- imageFragment!! .clearSelectedImages()
283
- // if all images is already marked as not for upload, delete all images from
284
- // not for upload table
285
279
} else {
286
- images.forEach {
280
+ images.forEach { image ->
287
281
val imageSHA1 = CustomSelectorUtils .getImageSHA1(
288
- it .uri,
282
+ image .uri,
289
283
ioDispatcher,
290
284
fileUtilsWrapper,
291
285
contentResolver
292
286
)
293
287
notForUploadStatusDao.deleteNotForUploadWithImageSHA1(imageSHA1)
294
288
}
295
- imageFragment!! .refresh()
296
- }
297
289
298
- imageFragment!! .dismissMarkUnmarkProgressDialog()
290
+ withContext(Dispatchers .Main ) {
291
+ imageFragment?.refresh()
292
+ }
293
+ }
299
294
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
+ }
302
301
}
303
- changeTitle(bucketName, 0 )
304
302
}
305
303
306
304
/* *
0 commit comments