properly handle moving multiple items in the recycle bin too

This commit is contained in:
tibbi 2018-06-27 11:15:58 +02:00
parent 87fde0f813
commit 721890ce67
3 changed files with 44 additions and 23 deletions

View file

@ -772,6 +772,20 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener {
override fun tryDeleteFiles(fileDirItems: ArrayList<FileDirItem>) { override fun tryDeleteFiles(fileDirItems: ArrayList<FileDirItem>) {
val filtered = fileDirItems.filter { it.path.isImageVideoGif() } as ArrayList val filtered = fileDirItems.filter { it.path.isImageVideoGif() } as ArrayList
if (config.useRecycleBin) {
movePathsInRecycleBin(filtered.map { it.path } as ArrayList<String>) {
if (it) {
deleteFilteredFiles(filtered)
} else {
toast(R.string.unknown_error_occurred)
}
}
} else {
deleteFilteredFiles(filtered)
}
}
private fun deleteFilteredFiles(filtered: ArrayList<FileDirItem>) {
deleteFiles(filtered) { deleteFiles(filtered) {
if (!it) { if (!it) {
toast(R.string.unknown_error_occurred) toast(R.string.unknown_error_occurred)
@ -782,8 +796,11 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener {
Thread { Thread {
val mediumDao = galleryDB.MediumDao() val mediumDao = galleryDB.MediumDao()
val useRecycleBin = config.useRecycleBin
filtered.forEach { filtered.forEach {
mediumDao.deleteMediumPath(it.path) if (!useRecycleBin) {
mediumDao.deleteMediumPath(it.path)
}
} }
}.start() }.start()

View file

@ -838,19 +838,18 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View
private fun deleteConfirmed() { private fun deleteConfirmed() {
val path = getCurrentMedia().getOrNull(mPos)?.path ?: return val path = getCurrentMedia().getOrNull(mPos)?.path ?: return
val fileDirItem = FileDirItem(path, path.getFilenameFromPath())
if (config.useRecycleBin) { if (config.useRecycleBin) {
Thread { movePathInRecycleBin(path) {
movePathInRecycleBin(path) { if (it) {
if (it) { tryDeleteFileDirItem(fileDirItem, false, false) {
galleryDB.MediumDao().updateDeleted(path, System.currentTimeMillis())
refreshViewPager() refreshViewPager()
} else {
toast(R.string.unknown_error_occurred)
} }
} else {
toast(R.string.unknown_error_occurred)
} }
}.start() }
} else { } else {
val fileDirItem = FileDirItem(path, path.getFilenameFromPath())
tryDeleteFileDirItem(fileDirItem, false, true) { tryDeleteFileDirItem(fileDirItem, false, true) {
refreshViewPager() refreshViewPager()
} }

View file

@ -181,18 +181,23 @@ fun BaseSimpleActivity.tryDeleteFileDirItem(fileDirItem: FileDirItem, allowDelet
} }
} }
fun BaseSimpleActivity.movePathInRecycleBin(path: String, callback: (success: Boolean) -> Unit) { fun BaseSimpleActivity.movePathInRecycleBin(path: String, callback: ((wasSuccess: Boolean) -> Unit)?) {
val file = File(path) movePathsInRecycleBin(arrayListOf(path), callback)
val internalFile = File(filesDir, path) }
return try {
file.copyRecursively(internalFile, true) fun BaseSimpleActivity.movePathsInRecycleBin(paths: ArrayList<String>, callback: ((wasSuccess: Boolean) -> Unit)?) {
val fileDirItem = FileDirItem(path, path.getFilenameFromPath()) Thread {
tryDeleteFileDirItem(fileDirItem, getIsPathDirectory(path), false) { var pathsCnt = paths.size
Thread { paths.forEach {
callback(it) val file = File(it)
}.start() val internalFile = File(filesDir, it)
} try {
} catch (ignored: Exception) { file.copyRecursively(internalFile, true)
callback(false) galleryDB.MediumDao().updateDeleted(it, System.currentTimeMillis())
} pathsCnt--
} catch (ignored: Exception) {
}
}
callback?.invoke(pathsCnt == 0)
}.start()
} }