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()