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>) {
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) {
if (!it) {
toast(R.string.unknown_error_occurred)
@ -782,9 +796,12 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener {
Thread {
val mediumDao = galleryDB.MediumDao()
val useRecycleBin = config.useRecycleBin
filtered.forEach {
if (!useRecycleBin) {
mediumDao.deleteMediumPath(it.path)
}
}
}.start()
if (mMedia.isEmpty()) {

View file

@ -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())
tryDeleteFileDirItem(fileDirItem, false, false) {
refreshViewPager()
}
} else {
toast(R.string.unknown_error_occurred)
}
}
}.start()
} else {
val fileDirItem = FileDirItem(path, path.getFilenameFromPath())
tryDeleteFileDirItem(fileDirItem, false, true) {
refreshViewPager()
}

View file

@ -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<String>, 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()
}