delete medium from the local db on delete too

This commit is contained in:
tibbi 2018-04-20 21:37:15 +02:00
parent ee07da6277
commit cb5ac8510f
5 changed files with 29 additions and 7 deletions

View file

@ -244,7 +244,7 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener {
val newFolder = File(config.tempFolderPath) val newFolder = File(config.tempFolderPath)
if (newFolder.exists() && newFolder.isDirectory) { if (newFolder.exists() && newFolder.isDirectory) {
if (newFolder.list()?.isEmpty() == true) { if (newFolder.list()?.isEmpty() == true) {
deleteFile(newFolder.toFileDirItem(applicationContext), true) tryDeleteFileDirItem(newFolder.toFileDirItem(applicationContext), true)
} }
} }
config.tempFolderPath = "" config.tempFolderPath = ""

View file

@ -444,7 +444,7 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener {
private fun deleteDirectoryIfEmpty() { private fun deleteDirectoryIfEmpty() {
val fileDirItem = FileDirItem(mPath, mPath.getFilenameFromPath()) val fileDirItem = FileDirItem(mPath, mPath.getFilenameFromPath())
if (config.deleteEmptyFolders && !fileDirItem.isDownloadsFolder() && fileDirItem.isDirectory && fileDirItem.getProperFileCount(applicationContext, true) == 0) { 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<FileDirItem>) { override fun deleteFiles(fileDirItems: ArrayList<FileDirItem>) {
val filtered = fileDirItems.filter { it.path.isImageVideoGif() } as ArrayList val filtered = fileDirItems.filter { it.path.isImageVideoGif() } as ArrayList
deleteFiles(filtered) { deleteFiles(filtered) {
Thread {
val mediumDao = galleryDB.MediumDao()
filtered.forEach {
if (!File(it.path).exists()) {
mediumDao.deleteMediumPath(it.path)
}
}
}.start()
if (!it) { if (!it) {
toast(R.string.unknown_error_occurred) toast(R.string.unknown_error_occurred)
} else if (mMedia.isEmpty()) { } else if (mMedia.isEmpty()) {

View file

@ -547,7 +547,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View
} }
if (tmpFile.length() > 0 && getDoesFilePathExist(newPath)) { if (tmpFile.length() > 0 && getDoesFilePathExist(newPath)) {
deleteFile(FileDirItem(newPath, newPath.getFilenameFromPath())) tryDeleteFileDirItem(FileDirItem(newPath, newPath.getFilenameFromPath()))
} }
copyFile(tmpFile, newFile) copyFile(tmpFile, newFile)
scanPath(newPath) scanPath(newPath)
@ -575,7 +575,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View
} catch (e: Exception) { } catch (e: Exception) {
showErrorToast(e) showErrorToast(e)
} finally { } 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() { private fun deleteConfirmed() {
val path = getCurrentMedia()[mPos].path val path = getCurrentMedia()[mPos].path
deleteFile(FileDirItem(path, path.getFilenameFromPath())) { tryDeleteFileDirItem(FileDirItem(path, path.getFilenameFromPath())) {
refreshViewPager() refreshViewPager()
} }
} }
@ -819,7 +819,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View
private fun deleteDirectoryIfEmpty() { private fun deleteDirectoryIfEmpty() {
val fileDirItem = FileDirItem(mDirectory, mDirectory.getFilenameFromPath(), getIsPathDirectory(mDirectory)) val fileDirItem = FileDirItem(mDirectory, mDirectory.getFilenameFromPath(), getIsPathDirectory(mDirectory))
if (config.deleteEmptyFolders && !fileDirItem.isDownloadsFolder() && fileDirItem.isDirectory && fileDirItem.getProperFileCount(applicationContext, true) == 0) { if (config.deleteEmptyFolders && !fileDirItem.isDownloadsFolder() && fileDirItem.isDirectory && fileDirItem.getProperFileCount(applicationContext, true) == 0) {
deleteFile(fileDirItem, true) tryDeleteFileDirItem(fileDirItem, true)
} }
scanPath(mDirectory) scanPath(mDirectory)

View file

@ -134,7 +134,7 @@ fun BaseSimpleActivity.removeNoMedia(path: String, callback: (() -> Unit)? = nul
return return
} }
deleteFile(file.toFileDirItem(applicationContext)) { tryDeleteFileDirItem(file.toFileDirItem(applicationContext)) {
callback?.invoke() callback?.invoke()
} }
} }
@ -165,3 +165,13 @@ fun BaseSimpleActivity.tryCopyMoveFilesTo(fileDirItems: ArrayList<FileDirItem>,
copyMoveFilesTo(fileDirItems, source.trimEnd('/'), it, isCopyOperation, true, config.shouldShowHidden, callback) 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()
}
}

View file

@ -19,4 +19,7 @@ interface MediumDao {
@Insert(onConflict = REPLACE) @Insert(onConflict = REPLACE)
fun insertAll(media: List<Medium>) fun insertAll(media: List<Medium>)
@Query("DELETE FROM media WHERE full_path = :path")
fun deleteMediumPath(path: String)
} }