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 9787cfbb8..ac235a8a5 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt @@ -30,6 +30,7 @@ import com.simplemobiletools.gallery.dialogs.ChangeSortingDialog import com.simplemobiletools.gallery.dialogs.FilterMediaDialog import com.simplemobiletools.gallery.extensions.* import com.simplemobiletools.gallery.helpers.* +import com.simplemobiletools.gallery.interfaces.DirectoryDao import com.simplemobiletools.gallery.models.AlbumCover import com.simplemobiletools.gallery.models.Directory import com.simplemobiletools.gallery.models.Medium @@ -640,6 +641,7 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener { mIsGettingDirs = false mLoadedInitialPhotos = true + checkInvalidDirectories(dirs, directoryDao) runOnUiThread { directories_refresh_layout.isRefreshing = false @@ -719,6 +721,23 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener { } } + private fun checkInvalidDirectories(dirs: ArrayList, directoryDao: DirectoryDao) { + val invalidDirs = ArrayList() + dirs.forEach { + if (!getDoesFilePathExist(it.path)) { + invalidDirs.add(it) + } + } + + if (invalidDirs.isNotEmpty()) { + dirs.removeAll(invalidDirs) + showSortedDirs(dirs) + invalidDirs.forEach { + directoryDao.deleteDirPath(it.path) + } + } + } + private fun getCurrentlyDisplayedDirs() = getRecyclerAdapter()?.dirs ?: ArrayList() private fun getBubbleTextItem(index: Int) = getRecyclerAdapter()?.dirs?.getOrNull(index)?.getBubbleText() ?: "" @@ -766,7 +785,7 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener { override fun updateDirectories(directories: ArrayList) { Thread { storeDirectoryItems(directories) - removeInvalidDirectories() + removeInvalidDBDirectories() }.start() } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Context.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Context.kt index c82f1d3b4..f2b48b5bd 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Context.kt @@ -278,7 +278,7 @@ fun Context.getCachedDirectories(getVideosOnly: Boolean = false, getImagesOnly: callback(filteredDirectories) - removeInvalidDirectories(directories, directoryDao) + removeInvalidDBDirectories(directories, directoryDao) }.start() } @@ -310,7 +310,7 @@ fun Context.getCachedMedia(path: String, getVideosOnly: Boolean = false, getImag }.start() } -fun Context.removeInvalidDirectories(dirs: ArrayList? = null, directoryDao: DirectoryDao = galleryDB.DirectoryDao()) { +fun Context.removeInvalidDBDirectories(dirs: ArrayList? = null, directoryDao: DirectoryDao = galleryDB.DirectoryDao()) { val dirsToCheck = dirs ?: directoryDao.getAll() dirsToCheck.filter { !getDoesFilePathExist(it.path) }.forEach { directoryDao.deleteDirPath(it.path)