From 721890ce678b145dde82ffc94ae7ffd366481402 Mon Sep 17 00:00:00 2001 From: tibbi Date: Wed, 27 Jun 2018 11:15:58 +0200 Subject: [PATCH] properly handle moving multiple items in the recycle bin too --- .../gallery/activities/MediaActivity.kt | 19 ++++++++++- .../gallery/activities/ViewPagerActivity.kt | 15 ++++----- .../gallery/extensions/Activity.kt | 33 +++++++++++-------- 3 files changed, 44 insertions(+), 23 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt index e3ba78a58..1a8234c7d 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt @@ -772,6 +772,20 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener { override fun tryDeleteFiles(fileDirItems: ArrayList) { val filtered = fileDirItems.filter { it.path.isImageVideoGif() } as ArrayList + if (config.useRecycleBin) { + movePathsInRecycleBin(filtered.map { it.path } as ArrayList) { + if (it) { + deleteFilteredFiles(filtered) + } else { + toast(R.string.unknown_error_occurred) + } + } + } else { + deleteFilteredFiles(filtered) + } + } + + private fun deleteFilteredFiles(filtered: ArrayList) { deleteFiles(filtered) { if (!it) { toast(R.string.unknown_error_occurred) @@ -782,8 +796,11 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener { Thread { val mediumDao = galleryDB.MediumDao() + val useRecycleBin = config.useRecycleBin filtered.forEach { - mediumDao.deleteMediumPath(it.path) + if (!useRecycleBin) { + mediumDao.deleteMediumPath(it.path) + } } }.start() diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/ViewPagerActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/ViewPagerActivity.kt index d7a194288..6ac4f0744 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/ViewPagerActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/ViewPagerActivity.kt @@ -838,19 +838,18 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View private fun deleteConfirmed() { val path = getCurrentMedia().getOrNull(mPos)?.path ?: return + val fileDirItem = FileDirItem(path, path.getFilenameFromPath()) if (config.useRecycleBin) { - Thread { - movePathInRecycleBin(path) { - if (it) { - galleryDB.MediumDao().updateDeleted(path, System.currentTimeMillis()) + movePathInRecycleBin(path) { + if (it) { + tryDeleteFileDirItem(fileDirItem, false, false) { refreshViewPager() - } else { - toast(R.string.unknown_error_occurred) } + } else { + toast(R.string.unknown_error_occurred) } - }.start() + } } else { - val fileDirItem = FileDirItem(path, path.getFilenameFromPath()) tryDeleteFileDirItem(fileDirItem, false, true) { refreshViewPager() } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Activity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Activity.kt index c138baa31..736a67fe4 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Activity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Activity.kt @@ -181,18 +181,23 @@ fun BaseSimpleActivity.tryDeleteFileDirItem(fileDirItem: FileDirItem, allowDelet } } -fun BaseSimpleActivity.movePathInRecycleBin(path: String, callback: (success: Boolean) -> Unit) { - val file = File(path) - val internalFile = File(filesDir, path) - return try { - file.copyRecursively(internalFile, true) - val fileDirItem = FileDirItem(path, path.getFilenameFromPath()) - tryDeleteFileDirItem(fileDirItem, getIsPathDirectory(path), false) { - Thread { - callback(it) - }.start() - } - } catch (ignored: Exception) { - callback(false) - } +fun BaseSimpleActivity.movePathInRecycleBin(path: String, callback: ((wasSuccess: Boolean) -> Unit)?) { + movePathsInRecycleBin(arrayListOf(path), callback) +} + +fun BaseSimpleActivity.movePathsInRecycleBin(paths: ArrayList, callback: ((wasSuccess: Boolean) -> Unit)?) { + Thread { + var pathsCnt = paths.size + paths.forEach { + val file = File(it) + val internalFile = File(filesDir, it) + try { + file.copyRecursively(internalFile, true) + galleryDB.MediumDao().updateDeleted(it, System.currentTimeMillis()) + pathsCnt-- + } catch (ignored: Exception) { + } + } + callback?.invoke(pathsCnt == 0) + }.start() }