From 83b0d7347fb442d32fa1b93c3dd360cd5c8587e7 Mon Sep 17 00:00:00 2001 From: tibbi Date: Mon, 18 Jan 2021 18:53:51 +0100 Subject: [PATCH] optimize .nomedia checks, avoid doing duplicate work --- app/build.gradle | 2 +- .../gallery/pro/adapters/DirectoryAdapter.kt | 6 +++--- .../gallery/pro/extensions/Context.kt | 18 +++++++++++++----- .../gallery/pro/extensions/String.kt | 9 ++++----- .../gallery/pro/helpers/MediaFetcher.kt | 10 +++++++--- 5 files changed, 28 insertions(+), 17 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 1de013668..e2b703bf4 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -77,7 +77,7 @@ android { } dependencies { - implementation 'com.simplemobiletools:commons:5.33.0' + implementation 'com.simplemobiletools:commons:5.33.6' implementation 'com.theartofdev.edmodo:android-image-cropper:2.8.0' implementation 'it.sephiroth.android.exif:library:1.0.1' implementation 'pl.droidsonroids.gif:android-gif-drawable:1.2.19' diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/adapters/DirectoryAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/adapters/DirectoryAdapter.kt index 7cecc3aec..177bb91e8 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/adapters/DirectoryAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/adapters/DirectoryAdapter.kt @@ -156,8 +156,8 @@ class DirectoryAdapter(activity: BaseSimpleActivity, var dirs: ArrayList) { - menu.findItem(R.id.cab_hide).isVisible = selectedPaths.any { !it.doesThisOrParentHaveNoMedia() } - menu.findItem(R.id.cab_unhide).isVisible = selectedPaths.any { it.doesThisOrParentHaveNoMedia() } + menu.findItem(R.id.cab_hide).isVisible = selectedPaths.any { !it.doesThisOrParentHaveNoMedia(HashMap(), null) } + menu.findItem(R.id.cab_unhide).isVisible = selectedPaths.any { it.doesThisOrParentHaveNoMedia(HashMap(), null) } } private fun checkPinBtnVisibility(menu: Menu, selectedPaths: ArrayList) { @@ -327,7 +327,7 @@ class DirectoryAdapter(activity: BaseSimpleActivity, var dirs: ArrayList() val includedFolders = config.includedFolders val newDirs = dirs.filterIndexed { index, directory -> - val removeDir = directory.path.doesThisOrParentHaveNoMedia() && !includedFolders.contains(directory.path) + val removeDir = directory.path.doesThisOrParentHaveNoMedia(HashMap(), null) && !includedFolders.contains(directory.path) if (removeDir) { affectedPositions.add(index) } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/Context.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/Context.kt index 8a34e9a53..f2d55eb95 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/Context.kt @@ -380,7 +380,12 @@ fun Context.storeDirectoryItems(items: ArrayList) { fun Context.checkAppendingHidden(path: String, hidden: String, includedFolders: MutableSet, noMediaFolders: ArrayList): String { val dirName = getFolderNameFromPath(path) - return if (path.doesThisOrParentHaveNoMedia(noMediaFolders) && !path.isThisOrParentIncluded(includedFolders)) { + val folderNoMediaStatuses = java.util.HashMap() + noMediaFolders.forEach { folder -> + folderNoMediaStatuses["$folder/$NOMEDIA"] = true + } + + return if (path.doesThisOrParentHaveNoMedia(folderNoMediaStatuses, null) && !path.isThisOrParentIncluded(includedFolders)) { "$dirName $hidden" } else { dirName @@ -556,11 +561,11 @@ fun Context.getCachedDirectories(getVideosOnly: Boolean = false, getImagesOnly: val folderNoMediaStatuses = HashMap() val noMediaFolders = getNoMediaFoldersSync() noMediaFolders.forEach { folder -> - folderNoMediaStatuses["$folder/.nomedia"] = true + folderNoMediaStatuses["$folder/$NOMEDIA"] = true } var filteredDirectories = directories.filter { - it.path.shouldFolderBeVisible(excludedPaths, includedPaths, shouldShowHidden, folderNoMediaStatuses, noMediaFolders) { path, hasNoMedia -> + it.path.shouldFolderBeVisible(excludedPaths, includedPaths, shouldShowHidden, folderNoMediaStatuses) { path, hasNoMedia -> folderNoMediaStatuses[path] = hasNoMedia } } as ArrayList @@ -582,11 +587,14 @@ fun Context.getCachedDirectories(getVideosOnly: Boolean = false, getImagesOnly: if (shouldShowHidden) { val hiddenString = resources.getString(R.string.hidden) filteredDirectories.forEach { - val noMediaPath = "${it.path}/.nomedia" + val noMediaPath = "${it.path}/$NOMEDIA" val hasNoMedia = if (folderNoMediaStatuses.keys.contains(noMediaPath)) { folderNoMediaStatuses[noMediaPath]!! } else { - it.path.doesThisOrParentHaveNoMedia(noMediaFolders) + it.path.doesThisOrParentHaveNoMedia(folderNoMediaStatuses) { path, hasNoMedia -> + val newPath = "$path/$NOMEDIA" + folderNoMediaStatuses[newPath] = hasNoMedia + } } it.name = if (hasNoMedia && !it.path.isThisOrParentIncluded(includedPaths)) { diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/String.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/String.kt index 1c18f2ef0..2a4f646c6 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/String.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/String.kt @@ -11,8 +11,7 @@ fun String.isThisOrParentExcluded(excludedPaths: MutableSet) = excludedP // cache which folders contain .nomedia files to avoid checking them over and over again fun String.shouldFolderBeVisible(excludedPaths: MutableSet, includedPaths: MutableSet, showHidden: Boolean, - folderNoMediaStatuses: HashMap, noMediaFolders: ArrayList = ArrayList(), - callback: (path: String, hasNoMedia: Boolean) -> Unit): Boolean { + folderNoMediaStatuses: HashMap, callback: (path: String, hasNoMedia: Boolean) -> Unit): Boolean { if (isEmpty()) { return false } @@ -37,7 +36,7 @@ fun String.shouldFolderBeVisible(excludedPaths: MutableSet, includedPath val containsNoMedia = if (showHidden) { false } else { - noMediaFolders.contains(this) || File(this, NOMEDIA).exists() + folderNoMediaStatuses.getOrElse("$this/$NOMEDIA", { false }) || File(this, NOMEDIA).exists() } return if (!showHidden && containsNoMedia) { @@ -54,14 +53,14 @@ fun String.shouldFolderBeVisible(excludedPaths: MutableSet, includedPath var curPath = this for (i in 0 until count { it == '/' } - 1) { curPath = curPath.substringBeforeLast('/') - val pathToCheck = "$curPath/${NOMEDIA}" + val pathToCheck = "$curPath/$NOMEDIA" if (folderNoMediaStatuses.contains(pathToCheck)) { if (folderNoMediaStatuses[pathToCheck] == true) { containsNoMediaOrDot = true break } } else { - val noMediaExists = noMediaFolders.contains(pathToCheck) || File(pathToCheck).exists() + val noMediaExists = folderNoMediaStatuses.getOrElse(pathToCheck, { false }) || File(pathToCheck).exists() callback(pathToCheck, noMediaExists) if (noMediaExists) { containsNoMediaOrDot = true diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/MediaFetcher.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/MediaFetcher.kt index 867894f2b..04248ae02 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/MediaFetcher.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/MediaFetcher.kt @@ -69,7 +69,7 @@ class MediaFetcher(val context: Context) { val excludedPaths = config.excludedFolders val includedPaths = config.includedFolders - val folderNomediaStatuses = HashMap() + val folderNoMediaStatuses = HashMap() val distinctPathsMap = HashMap() val distinctPaths = folders.distinctBy { when { @@ -83,9 +83,13 @@ class MediaFetcher(val context: Context) { } val noMediaFolders = context.getNoMediaFoldersSync() + noMediaFolders.forEach { folder -> + folderNoMediaStatuses["$folder/$NOMEDIA"] = true + } + distinctPaths.filter { - it.shouldFolderBeVisible(excludedPaths, includedPaths, shouldShowHidden, folderNomediaStatuses, noMediaFolders) { path, hasNoMedia -> - folderNomediaStatuses[path] = hasNoMedia + it.shouldFolderBeVisible(excludedPaths, includedPaths, shouldShowHidden, folderNoMediaStatuses) { path, hasNoMedia -> + folderNoMediaStatuses[path] = hasNoMedia } }.toMutableList() as ArrayList } catch (e: Exception) {