From e237afe9162f7343cc1540046c6968f5aea88f0a Mon Sep 17 00:00:00 2001 From: tibbi Date: Sat, 21 Apr 2018 22:49:29 +0200 Subject: [PATCH] update directories if one is renamed --- .../gallery/activities/MainActivity.kt | 6 +++++- .../gallery/adapters/DirectoryAdapter.kt | 9 +++------ .../simplemobiletools/gallery/extensions/Context.kt | 13 +++++++++---- 3 files changed, 17 insertions(+), 11 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 fc2a3ed66..20f529ee5 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt @@ -681,8 +681,12 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener { gotDirectories(directories, true) } else { mDirs = directories - storeDirectories() } + + Thread { + storeDirectoryItems(directories) + removeInvalidDirectories() + }.start() } private fun checkWhatsNewDialog() { diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/DirectoryAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/DirectoryAdapter.kt index f290cdc87..96cddb557 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/DirectoryAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/DirectoryAdapter.kt @@ -155,7 +155,8 @@ class DirectoryAdapter(activity: BaseSimpleActivity, var dirs: ArrayList) -> Unit) { val directoryDao = galleryDB.DirectoryDao() val directories = directoryDao.getAll() as ArrayList callback(directories) - - directories.filter { !File(it.path).exists() }.forEach { - directoryDao.deleteDirPath(it.path) - } + removeInvalidDirectories(directories, directoryDao) }.start() } @@ -268,6 +266,13 @@ fun Context.getCachedMedia(path: String, callback: (ArrayList) -> Unit) }.start() } +fun Context.removeInvalidDirectories(dirs: ArrayList? = null, directoryDao: DirectoryDao = galleryDB.DirectoryDao()) { + val dirsToCheck = dirs ?: directoryDao.getAll() + dirsToCheck.filter { !File(it.path).exists() }.forEach { + directoryDao.deleteDirPath(it.path) + } +} + fun Context.updateMediaPath(oldPath: String, newPath: String) { val newFilename = newPath.getFilenameFromPath() val newParentPath = newPath.getParentPath()