make sure gotDirectories is called on a background thread

This commit is contained in:
tibbi 2018-07-01 20:19:28 +02:00
parent d78eff3cee
commit 0316a21e39

View file

@ -337,7 +337,9 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener {
if (config.directorySorting and SORT_BY_DATE_MODIFIED > 0 || config.directorySorting and SORT_BY_DATE_TAKEN > 0) { if (config.directorySorting and SORT_BY_DATE_MODIFIED > 0 || config.directorySorting and SORT_BY_DATE_TAKEN > 0) {
getDirectories() getDirectories()
} else { } else {
gotDirectories(getCurrentlyDisplayedDirs()) Thread {
gotDirectories(getCurrentlyDisplayedDirs())
}.start()
} }
} }
} }
@ -516,7 +518,9 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener {
FilePickerDialog(this, internalStoragePath, false, config.shouldShowHidden) { FilePickerDialog(this, internalStoragePath, false, config.shouldShowHidden) {
CreateNewFolderDialog(this, it) { CreateNewFolderDialog(this, it) {
config.tempFolderPath = it config.tempFolderPath = it
gotDirectories(addTempFolderIfNeeded(getCurrentlyDisplayedDirs())) Thread {
gotDirectories(addTempFolderIfNeeded(getCurrentlyDisplayedDirs()))
}.start()
} }
} }
} }
@ -673,106 +677,104 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener {
} }
// cached folders have been loaded, recheck folders one by one starting with the first displayed // cached folders have been loaded, recheck folders one by one starting with the first displayed
Thread { val mediaFetcher = MediaFetcher(applicationContext)
val mediaFetcher = MediaFetcher(applicationContext) val getImagesOnly = mIsPickImageIntent || mIsGetImageContentIntent
val getImagesOnly = mIsPickImageIntent || mIsGetImageContentIntent val getVideosOnly = mIsPickVideoIntent || mIsGetVideoContentIntent
val getVideosOnly = mIsPickVideoIntent || mIsGetVideoContentIntent val hiddenString = getString(R.string.hidden)
val hiddenString = getString(R.string.hidden) val albumCovers = config.parseAlbumCovers()
val albumCovers = config.parseAlbumCovers() val includedFolders = config.includedFolders
val includedFolders = config.includedFolders val isSortingAscending = config.directorySorting and SORT_DESCENDING == 0
val isSortingAscending = config.directorySorting and SORT_DESCENDING == 0 val mediumDao = galleryDB.MediumDao()
val mediumDao = galleryDB.MediumDao() val directoryDao = galleryDB.DirectoryDao()
val directoryDao = galleryDB.DirectoryDao() val getProperDateTaken = config.directorySorting and SORT_BY_DATE_TAKEN != 0
val getProperDateTaken = config.directorySorting and SORT_BY_DATE_TAKEN != 0 val favoritePaths = getFavoritePaths()
val favoritePaths = getFavoritePaths()
try { try {
for (directory in dirs) { for (directory in dirs) {
val curMedia = mediaFetcher.getFilesFrom(directory.path, getImagesOnly, getVideosOnly, getProperDateTaken, favoritePaths) val curMedia = mediaFetcher.getFilesFrom(directory.path, getImagesOnly, getVideosOnly, getProperDateTaken, favoritePaths)
val newDir = if (curMedia.isEmpty()) { val newDir = if (curMedia.isEmpty()) {
directory directory
} else { } else {
createDirectoryFromMedia(directory.path, curMedia, albumCovers, hiddenString, includedFolders, isSortingAscending) createDirectoryFromMedia(directory.path, curMedia, albumCovers, hiddenString, includedFolders, isSortingAscending)
} }
// we are looping through the already displayed folders looking for changes, do not do anything if nothing changed // we are looping through the already displayed folders looking for changes, do not do anything if nothing changed
if (directory == newDir) { if (directory == newDir) {
continue continue
} }
directory.apply { directory.apply {
tmb = newDir.tmb tmb = newDir.tmb
name = newDir.name name = newDir.name
mediaCnt = newDir.mediaCnt mediaCnt = newDir.mediaCnt
modified = newDir.modified modified = newDir.modified
taken = newDir.taken taken = newDir.taken
this@apply.size = newDir.size this@apply.size = newDir.size
types = newDir.types types = newDir.types
} }
showSortedDirs(dirs) showSortedDirs(dirs)
// update directories and media files in the local db, delete invalid items // update directories and media files in the local db, delete invalid items
updateDBDirectory(directory) updateDBDirectory(directory)
if (!directory.isRecycleBin()) { if (!directory.isRecycleBin()) {
mediumDao.insertAll(curMedia) mediumDao.insertAll(curMedia)
} }
getCachedMedia(directory.path, getVideosOnly, getImagesOnly) { getCachedMedia(directory.path, getVideosOnly, getImagesOnly) {
it.forEach { it.forEach {
if (!curMedia.contains(it)) { if (!curMedia.contains(it)) {
val path = (it as? Medium)?.path val path = (it as? Medium)?.path
if (path != null) { if (path != null) {
mediumDao.deleteMediumPath(path) mediumDao.deleteMediumPath(path)
}
} }
} }
} }
} }
} catch (ignored: Exception) { }
} catch (ignored: Exception) {
}
val foldersToScan = mediaFetcher.getFoldersToScan()
foldersToScan.add(FAVORITES)
foldersToScan.add(RECYCLE_BIN)
dirs.forEach {
foldersToScan.remove(it.path)
}
// check the remaining folders which were not cached at all yet
for (folder in foldersToScan) {
val newMedia = mediaFetcher.getFilesFrom(folder, getImagesOnly, getVideosOnly, getProperDateTaken, favoritePaths)
if (newMedia.isEmpty()) {
continue
} }
val foldersToScan = mediaFetcher.getFoldersToScan() if (isPlaceholderVisible) {
foldersToScan.add(FAVORITES) isPlaceholderVisible = false
foldersToScan.add(RECYCLE_BIN) runOnUiThread {
dirs.forEach { directories_empty_text_label.beGone()
foldersToScan.remove(it.path) directories_empty_text.beGone()
} directories_grid.beVisible()
// check the remaining folders which were not cached at all yet
for (folder in foldersToScan) {
val newMedia = mediaFetcher.getFilesFrom(folder, getImagesOnly, getVideosOnly, getProperDateTaken, favoritePaths)
if (newMedia.isEmpty()) {
continue
}
if (isPlaceholderVisible) {
isPlaceholderVisible = false
runOnUiThread {
directories_empty_text_label.beGone()
directories_empty_text.beGone()
directories_grid.beVisible()
}
}
val newDir = createDirectoryFromMedia(folder, newMedia, albumCovers, hiddenString, includedFolders, isSortingAscending)
dirs.add(newDir)
showSortedDirs(dirs)
directoryDao.insert(newDir)
if (folder != RECYCLE_BIN) {
mediumDao.insertAll(newMedia)
} }
} }
mIsGettingDirs = false val newDir = createDirectoryFromMedia(folder, newMedia, albumCovers, hiddenString, includedFolders, isSortingAscending)
mLoadedInitialPhotos = true dirs.add(newDir)
checkLastMediaChanged() showSortedDirs(dirs)
directoryDao.insert(newDir)
runOnUiThread { if (folder != RECYCLE_BIN) {
directories_refresh_layout.isRefreshing = false mediumDao.insertAll(newMedia)
checkPlaceholderVisibility(dirs)
} }
checkInvalidDirectories(dirs, directoryDao) }
}.start()
mIsGettingDirs = false
mLoadedInitialPhotos = true
checkLastMediaChanged()
runOnUiThread {
directories_refresh_layout.isRefreshing = false
checkPlaceholderVisibility(dirs)
}
checkInvalidDirectories(dirs, directoryDao)
} }
private fun checkPlaceholderVisibility(dirs: ArrayList<Directory>) { private fun checkPlaceholderVisibility(dirs: ArrayList<Directory>) {
@ -956,7 +958,9 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener {
} }
override fun recheckPinnedFolders() { override fun recheckPinnedFolders() {
gotDirectories(movePinnedDirectoriesToFront(getCurrentlyDisplayedDirs())) Thread {
gotDirectories(movePinnedDirectoriesToFront(getCurrentlyDisplayedDirs()))
}.start()
} }
override fun updateDirectories(directories: ArrayList<Directory>) { override fun updateDirectories(directories: ArrayList<Directory>) {