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.dialogs.FilterMediaDialog
import com.simplemobiletools.gallery.extensions.* import com.simplemobiletools.gallery.extensions.*
import com.simplemobiletools.gallery.helpers.* import com.simplemobiletools.gallery.helpers.*
import com.simplemobiletools.gallery.interfaces.DirectoryDao
import com.simplemobiletools.gallery.models.AlbumCover import com.simplemobiletools.gallery.models.AlbumCover
import com.simplemobiletools.gallery.models.Directory import com.simplemobiletools.gallery.models.Directory
import com.simplemobiletools.gallery.models.Medium import com.simplemobiletools.gallery.models.Medium
@ -640,6 +641,7 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener {
mIsGettingDirs = false mIsGettingDirs = false
mLoadedInitialPhotos = true mLoadedInitialPhotos = true
checkInvalidDirectories(dirs, directoryDao)
runOnUiThread { runOnUiThread {
directories_refresh_layout.isRefreshing = false 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 getCurrentlyDisplayedDirs() = getRecyclerAdapter()?.dirs ?: ArrayList()
private fun getBubbleTextItem(index: Int) = getRecyclerAdapter()?.dirs?.getOrNull(index)?.getBubbleText() ?: "" 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>) { override fun updateDirectories(directories: ArrayList<Directory>) {
Thread { Thread {
storeDirectoryItems(directories) storeDirectoryItems(directories)
removeInvalidDirectories() removeInvalidDBDirectories()
}.start() }.start()
} }

View file

@ -278,7 +278,7 @@ fun Context.getCachedDirectories(getVideosOnly: Boolean = false, getImagesOnly:
callback(filteredDirectories) callback(filteredDirectories)
removeInvalidDirectories(directories, directoryDao) removeInvalidDBDirectories(directories, directoryDao)
}.start() }.start()
} }
@ -310,7 +310,7 @@ fun Context.getCachedMedia(path: String, getVideosOnly: Boolean = false, getImag
}.start() }.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() val dirsToCheck = dirs ?: directoryDao.getAll()
dirsToCheck.filter { !getDoesFilePathExist(it.path) }.forEach { dirsToCheck.filter { !getDoesFilePathExist(it.path) }.forEach {
directoryDao.deleteDirPath(it.path) directoryDao.deleteDirPath(it.path)