display cached dirs and media only when appropriate

This commit is contained in:
tibbi 2018-04-21 23:15:31 +02:00
parent e237afe916
commit 570e471dec
4 changed files with 44 additions and 34 deletions

View file

@ -276,8 +276,13 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener {
mIsGettingDirs = true
if (!mLoadedInitialPhotos) {
getCachedDirectories {
if (it.isNotEmpty()) {
gotDirectories(it, true)
val shouldShowHidden = config.shouldShowHidden
val excludedPaths = config.excludedFolders
val includedPaths = config.includedFolders
val dirs = it.filter { it.path.shouldFolderBeVisible(excludedPaths, includedPaths, shouldShowHidden) } as ArrayList<Directory>
if (dirs.isNotEmpty()) {
gotDirectories(dirs, true)
}
}
}
@ -566,17 +571,16 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener {
}
val dirs = getSortedDirectories(newDirs)
directories_refresh_layout.isRefreshing = false
mIsGettingDirs = false
directories_empty_text_label.beVisibleIf(dirs.isEmpty() && !isFromCache)
directories_empty_text.beVisibleIf(dirs.isEmpty() && !isFromCache)
directories_grid.beVisibleIf(directories_empty_text_label.isGone())
checkLastMediaChanged()
mDirs = dirs
runOnUiThread {
directories_refresh_layout.isRefreshing = false
directories_empty_text_label.beVisibleIf(dirs.isEmpty() && !isFromCache)
directories_empty_text.beVisibleIf(dirs.isEmpty() && !isFromCache)
directories_grid.beVisibleIf(directories_empty_text_label.isGone())
val allowHorizontalScroll = config.scrollHorizontally && config.viewTypeFiles == VIEW_TYPE_GRID
directories_vertical_fastscroller.beVisibleIf(directories_grid.isVisible() && !allowHorizontalScroll)
directories_horizontal_fastscroller.beVisibleIf(directories_grid.isVisible() && allowHorizontalScroll)

View file

@ -456,10 +456,16 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener {
mIsGettingMedia = true
if (!mLoadedInitialPhotos) {
getCachedMedia(mPath) {
if (it.isEmpty()) {
val shouldShowHidden = config.shouldShowHidden
var media = it
if (!shouldShowHidden) {
media = media.filter { !it.name.startsWith('.') } as ArrayList<Medium>
}
if (media.isEmpty()) {
media_refresh_layout.isRefreshing = true
} else {
gotMedia(it, true)
gotMedia(media, true)
}
}
} else {
@ -636,7 +642,6 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener {
}.start()
mIsGettingMedia = false
checkLastMediaChanged()
mMedia = media

View file

@ -11,3 +11,24 @@ fun String.getFileSignature(): ObjectKey {
fun String.isThisOrParentIncluded(includedPaths: MutableSet<String>) = includedPaths.any { startsWith(it, true) }
fun String.isThisOrParentExcluded(excludedPaths: MutableSet<String>) = excludedPaths.any { startsWith(it, true) }
fun String.shouldFolderBeVisible(excludedPaths: MutableSet<String>, includedPaths: MutableSet<String>, showHidden: Boolean): Boolean {
val file = File(this)
return if (isEmpty()) {
false
} else if (!showHidden && file.containsNoMedia()) {
false
} else if (isThisOrParentIncluded(includedPaths)) {
true
} else if (isThisOrParentExcluded(excludedPaths)) {
false
} else if (!showHidden && file.isDirectory && file.canonicalFile == file.absoluteFile) {
var containsNoMediaOrDot = file.containsNoMedia() || contains("/.")
if (!containsNoMediaOrDot) {
containsNoMediaOrDot = file.doesThisOrParentHaveNoMedia()
}
!containsNoMediaOrDot
} else {
true
}
}

View file

@ -8,7 +8,8 @@ import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.OTG_PATH
import com.simplemobiletools.commons.helpers.photoExtensions
import com.simplemobiletools.commons.helpers.videoExtensions
import com.simplemobiletools.gallery.extensions.*
import com.simplemobiletools.gallery.extensions.config
import com.simplemobiletools.gallery.extensions.shouldFolderBeVisible
import com.simplemobiletools.gallery.models.Medium
import java.io.File
import java.util.LinkedHashMap
@ -132,7 +133,7 @@ class MediaFetcher(val context: Context) {
val curMedia = ArrayList<Medium>()
val showHidden = config.shouldShowHidden
val excludedFolders = config.excludedFolders
foldersToScan.filter { shouldFolderBeVisible(it, excludedFolders, includedFolders, showHidden) }.toList().forEach {
foldersToScan.filter { it.shouldFolderBeVisible(excludedFolders, includedFolders, showHidden) }.toList().forEach {
fetchFolderContent(it, curMedia, isPickImage, isPickVideo, filterMedia)
}
@ -182,27 +183,6 @@ class MediaFetcher(val context: Context) {
return directories
}
private fun shouldFolderBeVisible(path: String, excludedPaths: MutableSet<String>, includedPaths: MutableSet<String>, showHidden: Boolean): Boolean {
val file = File(path)
return if (path.isEmpty()) {
false
} else if (!showHidden && file.containsNoMedia()) {
false
} else if (path.isThisOrParentIncluded(includedPaths)) {
true
} else if (path.isThisOrParentExcluded(excludedPaths)) {
false
} else if (!showHidden && file.isDirectory && file.canonicalFile == file.absoluteFile) {
var containsNoMediaOrDot = file.containsNoMedia() || path.contains("/.")
if (!containsNoMediaOrDot) {
containsNoMediaOrDot = file.doesThisOrParentHaveNoMedia()
}
!containsNoMediaOrDot
} else {
true
}
}
private fun getMediaInFolder(folder: String, curMedia: ArrayList<Medium>, isPickImage: Boolean, isPickVideo: Boolean, filterMedia: Int) {
val files = File(folder).listFiles() ?: return
val doExtraCheck = context.config.doExtraCheck