look for invalid directories after loading

This commit is contained in:
tibbi 2018-04-24 14:54:28 +02:00
parent f963274942
commit ddfbdaa275
2 changed files with 22 additions and 3 deletions

View File

@ -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<Directory>, directoryDao: DirectoryDao) {
val invalidDirs = ArrayList<Directory>()
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<Directory>) {
Thread {
storeDirectoryItems(directories)
removeInvalidDirectories()
removeInvalidDBDirectories()
}.start()
}

View File

@ -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<Directory>? = null, directoryDao: DirectoryDao = galleryDB.DirectoryDao()) {
fun Context.removeInvalidDBDirectories(dirs: ArrayList<Directory>? = null, directoryDao: DirectoryDao = galleryDB.DirectoryDao()) {
val dirsToCheck = dirs ?: directoryDao.getAll()
dirsToCheck.filter { !getDoesFilePathExist(it.path) }.forEach {
directoryDao.deleteDirPath(it.path)