From cb5ac8510fddfdaab579c2174b9f4442538c37b5 Mon Sep 17 00:00:00 2001 From: tibbi Date: Fri, 20 Apr 2018 21:37:15 +0200 Subject: [PATCH] delete medium from the local db on delete too --- .../gallery/activities/MainActivity.kt | 2 +- .../gallery/activities/MediaActivity.kt | 11 ++++++++++- .../gallery/activities/ViewPagerActivity.kt | 8 ++++---- .../simplemobiletools/gallery/extensions/Activity.kt | 12 +++++++++++- .../gallery/interfaces/MediumDao.kt | 3 +++ 5 files changed, 29 insertions(+), 7 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt index fbe985495..fc2a3ed66 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt @@ -244,7 +244,7 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener { val newFolder = File(config.tempFolderPath) if (newFolder.exists() && newFolder.isDirectory) { if (newFolder.list()?.isEmpty() == true) { - deleteFile(newFolder.toFileDirItem(applicationContext), true) + tryDeleteFileDirItem(newFolder.toFileDirItem(applicationContext), true) } } config.tempFolderPath = "" 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 6bfaecad5..3833627b2 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt @@ -444,7 +444,7 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener { private fun deleteDirectoryIfEmpty() { val fileDirItem = FileDirItem(mPath, mPath.getFilenameFromPath()) if (config.deleteEmptyFolders && !fileDirItem.isDownloadsFolder() && fileDirItem.isDirectory && fileDirItem.getProperFileCount(applicationContext, true) == 0) { - deleteFile(fileDirItem, true) + tryDeleteFileDirItem(fileDirItem, true) } } @@ -669,6 +669,15 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener { override fun deleteFiles(fileDirItems: ArrayList) { val filtered = fileDirItems.filter { it.path.isImageVideoGif() } as ArrayList deleteFiles(filtered) { + Thread { + val mediumDao = galleryDB.MediumDao() + filtered.forEach { + if (!File(it.path).exists()) { + mediumDao.deleteMediumPath(it.path) + } + } + }.start() + if (!it) { toast(R.string.unknown_error_occurred) } else if (mMedia.isEmpty()) { 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 a262ade53..ec9ea974d 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/ViewPagerActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/ViewPagerActivity.kt @@ -547,7 +547,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View } if (tmpFile.length() > 0 && getDoesFilePathExist(newPath)) { - deleteFile(FileDirItem(newPath, newPath.getFilenameFromPath())) + tryDeleteFileDirItem(FileDirItem(newPath, newPath.getFilenameFromPath())) } copyFile(tmpFile, newFile) scanPath(newPath) @@ -575,7 +575,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View } catch (e: Exception) { showErrorToast(e) } finally { - deleteFile(FileDirItem(tmpFile.absolutePath, tmpFile.absolutePath.getFilenameFromPath())) + tryDeleteFileDirItem(FileDirItem(tmpFile.absolutePath, tmpFile.absolutePath.getFilenameFromPath())) } } @@ -741,7 +741,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View private fun deleteConfirmed() { val path = getCurrentMedia()[mPos].path - deleteFile(FileDirItem(path, path.getFilenameFromPath())) { + tryDeleteFileDirItem(FileDirItem(path, path.getFilenameFromPath())) { refreshViewPager() } } @@ -819,7 +819,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View private fun deleteDirectoryIfEmpty() { val fileDirItem = FileDirItem(mDirectory, mDirectory.getFilenameFromPath(), getIsPathDirectory(mDirectory)) if (config.deleteEmptyFolders && !fileDirItem.isDownloadsFolder() && fileDirItem.isDirectory && fileDirItem.getProperFileCount(applicationContext, true) == 0) { - deleteFile(fileDirItem, true) + tryDeleteFileDirItem(fileDirItem, true) } scanPath(mDirectory) 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 145013260..01a510955 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Activity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Activity.kt @@ -134,7 +134,7 @@ fun BaseSimpleActivity.removeNoMedia(path: String, callback: (() -> Unit)? = nul return } - deleteFile(file.toFileDirItem(applicationContext)) { + tryDeleteFileDirItem(file.toFileDirItem(applicationContext)) { callback?.invoke() } } @@ -165,3 +165,13 @@ fun BaseSimpleActivity.tryCopyMoveFilesTo(fileDirItems: ArrayList, copyMoveFilesTo(fileDirItems, source.trimEnd('/'), it, isCopyOperation, true, config.shouldShowHidden, callback) } } + +fun BaseSimpleActivity.tryDeleteFileDirItem(fileDirItem: FileDirItem, allowDeleteFolder: Boolean = false, callback: ((wasSuccess: Boolean) -> Unit)? = null) { + deleteFile(fileDirItem, allowDeleteFolder) { + callback?.invoke(it) + + Thread { + galleryDB.MediumDao().deleteMediumPath(fileDirItem.path) + }.start() + } +} diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/interfaces/MediumDao.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/interfaces/MediumDao.kt index 51d8d3b77..8f04294a2 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/interfaces/MediumDao.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/interfaces/MediumDao.kt @@ -19,4 +19,7 @@ interface MediumDao { @Insert(onConflict = REPLACE) fun insertAll(media: List) + + @Query("DELETE FROM media WHERE full_path = :path") + fun deleteMediumPath(path: String) }