From 570e471dec52d6751e617804445551b953b4a045 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sat, 21 Apr 2018 23:15:31 +0200 Subject: [PATCH] display cached dirs and media only when appropriate --- .../gallery/activities/MainActivity.kt | 20 ++++++++------ .../gallery/activities/MediaActivity.kt | 11 +++++--- .../gallery/extensions/String.kt | 21 +++++++++++++++ .../gallery/helpers/MediaFetcher.kt | 26 +++---------------- 4 files changed, 44 insertions(+), 34 deletions(-) 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 20f529ee5..027ff2055 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt @@ -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 + + 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) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt index d4e1b039d..665c98558 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt @@ -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 + } + + 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 diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/String.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/String.kt index db7b0ec87..dd28e208b 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/String.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/String.kt @@ -11,3 +11,24 @@ fun String.getFileSignature(): ObjectKey { fun String.isThisOrParentIncluded(includedPaths: MutableSet) = includedPaths.any { startsWith(it, true) } fun String.isThisOrParentExcluded(excludedPaths: MutableSet) = excludedPaths.any { startsWith(it, true) } + +fun String.shouldFolderBeVisible(excludedPaths: MutableSet, includedPaths: MutableSet, 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 + } +} diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/MediaFetcher.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/MediaFetcher.kt index 8c5847192..ccb160104 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/MediaFetcher.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/MediaFetcher.kt @@ -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() 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, includedPaths: MutableSet, 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, isPickImage: Boolean, isPickVideo: Boolean, filterMedia: Int) { val files = File(folder).listFiles() ?: return val doExtraCheck = context.config.doExtraCheck