optimize .nomedia checks, avoid doing duplicate work

This commit is contained in:
tibbi 2021-01-18 18:53:51 +01:00
parent a2decb1cd0
commit 83b0d7347f
5 changed files with 28 additions and 17 deletions

View file

@ -77,7 +77,7 @@ android {
} }
dependencies { 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 'com.theartofdev.edmodo:android-image-cropper:2.8.0'
implementation 'it.sephiroth.android.exif:library:1.0.1' implementation 'it.sephiroth.android.exif:library:1.0.1'
implementation 'pl.droidsonroids.gif:android-gif-drawable:1.2.19' implementation 'pl.droidsonroids.gif:android-gif-drawable:1.2.19'

View file

@ -156,8 +156,8 @@ class DirectoryAdapter(activity: BaseSimpleActivity, var dirs: ArrayList<Directo
} }
private fun checkHideBtnVisibility(menu: Menu, selectedPaths: ArrayList<String>) { private fun checkHideBtnVisibility(menu: Menu, selectedPaths: ArrayList<String>) {
menu.findItem(R.id.cab_hide).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() } menu.findItem(R.id.cab_unhide).isVisible = selectedPaths.any { it.doesThisOrParentHaveNoMedia(HashMap(), null) }
} }
private fun checkPinBtnVisibility(menu: Menu, selectedPaths: ArrayList<String>) { private fun checkPinBtnVisibility(menu: Menu, selectedPaths: ArrayList<String>) {
@ -327,7 +327,7 @@ class DirectoryAdapter(activity: BaseSimpleActivity, var dirs: ArrayList<Directo
val affectedPositions = ArrayList<Int>() val affectedPositions = ArrayList<Int>()
val includedFolders = config.includedFolders val includedFolders = config.includedFolders
val newDirs = dirs.filterIndexed { index, directory -> 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) { if (removeDir) {
affectedPositions.add(index) affectedPositions.add(index)
} }

View file

@ -380,7 +380,12 @@ fun Context.storeDirectoryItems(items: ArrayList<Directory>) {
fun Context.checkAppendingHidden(path: String, hidden: String, includedFolders: MutableSet<String>, noMediaFolders: ArrayList<String>): String { fun Context.checkAppendingHidden(path: String, hidden: String, includedFolders: MutableSet<String>, noMediaFolders: ArrayList<String>): String {
val dirName = getFolderNameFromPath(path) val dirName = getFolderNameFromPath(path)
return if (path.doesThisOrParentHaveNoMedia(noMediaFolders) && !path.isThisOrParentIncluded(includedFolders)) { val folderNoMediaStatuses = java.util.HashMap<String, Boolean>()
noMediaFolders.forEach { folder ->
folderNoMediaStatuses["$folder/$NOMEDIA"] = true
}
return if (path.doesThisOrParentHaveNoMedia(folderNoMediaStatuses, null) && !path.isThisOrParentIncluded(includedFolders)) {
"$dirName $hidden" "$dirName $hidden"
} else { } else {
dirName dirName
@ -556,11 +561,11 @@ fun Context.getCachedDirectories(getVideosOnly: Boolean = false, getImagesOnly:
val folderNoMediaStatuses = HashMap<String, Boolean>() val folderNoMediaStatuses = HashMap<String, Boolean>()
val noMediaFolders = getNoMediaFoldersSync() val noMediaFolders = getNoMediaFoldersSync()
noMediaFolders.forEach { folder -> noMediaFolders.forEach { folder ->
folderNoMediaStatuses["$folder/.nomedia"] = true folderNoMediaStatuses["$folder/$NOMEDIA"] = true
} }
var filteredDirectories = directories.filter { 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 folderNoMediaStatuses[path] = hasNoMedia
} }
} as ArrayList<Directory> } as ArrayList<Directory>
@ -582,11 +587,14 @@ fun Context.getCachedDirectories(getVideosOnly: Boolean = false, getImagesOnly:
if (shouldShowHidden) { if (shouldShowHidden) {
val hiddenString = resources.getString(R.string.hidden) val hiddenString = resources.getString(R.string.hidden)
filteredDirectories.forEach { filteredDirectories.forEach {
val noMediaPath = "${it.path}/.nomedia" val noMediaPath = "${it.path}/$NOMEDIA"
val hasNoMedia = if (folderNoMediaStatuses.keys.contains(noMediaPath)) { val hasNoMedia = if (folderNoMediaStatuses.keys.contains(noMediaPath)) {
folderNoMediaStatuses[noMediaPath]!! folderNoMediaStatuses[noMediaPath]!!
} else { } 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)) { it.name = if (hasNoMedia && !it.path.isThisOrParentIncluded(includedPaths)) {

View file

@ -11,8 +11,7 @@ fun String.isThisOrParentExcluded(excludedPaths: MutableSet<String>) = excludedP
// cache which folders contain .nomedia files to avoid checking them over and over again // cache which folders contain .nomedia files to avoid checking them over and over again
fun String.shouldFolderBeVisible(excludedPaths: MutableSet<String>, includedPaths: MutableSet<String>, showHidden: Boolean, fun String.shouldFolderBeVisible(excludedPaths: MutableSet<String>, includedPaths: MutableSet<String>, showHidden: Boolean,
folderNoMediaStatuses: HashMap<String, Boolean>, noMediaFolders: ArrayList<String> = ArrayList(), folderNoMediaStatuses: HashMap<String, Boolean>, callback: (path: String, hasNoMedia: Boolean) -> Unit): Boolean {
callback: (path: String, hasNoMedia: Boolean) -> Unit): Boolean {
if (isEmpty()) { if (isEmpty()) {
return false return false
} }
@ -37,7 +36,7 @@ fun String.shouldFolderBeVisible(excludedPaths: MutableSet<String>, includedPath
val containsNoMedia = if (showHidden) { val containsNoMedia = if (showHidden) {
false false
} else { } else {
noMediaFolders.contains(this) || File(this, NOMEDIA).exists() folderNoMediaStatuses.getOrElse("$this/$NOMEDIA", { false }) || File(this, NOMEDIA).exists()
} }
return if (!showHidden && containsNoMedia) { return if (!showHidden && containsNoMedia) {
@ -54,14 +53,14 @@ fun String.shouldFolderBeVisible(excludedPaths: MutableSet<String>, includedPath
var curPath = this var curPath = this
for (i in 0 until count { it == '/' } - 1) { for (i in 0 until count { it == '/' } - 1) {
curPath = curPath.substringBeforeLast('/') curPath = curPath.substringBeforeLast('/')
val pathToCheck = "$curPath/${NOMEDIA}" val pathToCheck = "$curPath/$NOMEDIA"
if (folderNoMediaStatuses.contains(pathToCheck)) { if (folderNoMediaStatuses.contains(pathToCheck)) {
if (folderNoMediaStatuses[pathToCheck] == true) { if (folderNoMediaStatuses[pathToCheck] == true) {
containsNoMediaOrDot = true containsNoMediaOrDot = true
break break
} }
} else { } else {
val noMediaExists = noMediaFolders.contains(pathToCheck) || File(pathToCheck).exists() val noMediaExists = folderNoMediaStatuses.getOrElse(pathToCheck, { false }) || File(pathToCheck).exists()
callback(pathToCheck, noMediaExists) callback(pathToCheck, noMediaExists)
if (noMediaExists) { if (noMediaExists) {
containsNoMediaOrDot = true containsNoMediaOrDot = true

View file

@ -69,7 +69,7 @@ class MediaFetcher(val context: Context) {
val excludedPaths = config.excludedFolders val excludedPaths = config.excludedFolders
val includedPaths = config.includedFolders val includedPaths = config.includedFolders
val folderNomediaStatuses = HashMap<String, Boolean>() val folderNoMediaStatuses = HashMap<String, Boolean>()
val distinctPathsMap = HashMap<String, String>() val distinctPathsMap = HashMap<String, String>()
val distinctPaths = folders.distinctBy { val distinctPaths = folders.distinctBy {
when { when {
@ -83,9 +83,13 @@ class MediaFetcher(val context: Context) {
} }
val noMediaFolders = context.getNoMediaFoldersSync() val noMediaFolders = context.getNoMediaFoldersSync()
noMediaFolders.forEach { folder ->
folderNoMediaStatuses["$folder/$NOMEDIA"] = true
}
distinctPaths.filter { distinctPaths.filter {
it.shouldFolderBeVisible(excludedPaths, includedPaths, shouldShowHidden, folderNomediaStatuses, noMediaFolders) { path, hasNoMedia -> it.shouldFolderBeVisible(excludedPaths, includedPaths, shouldShowHidden, folderNoMediaStatuses) { path, hasNoMedia ->
folderNomediaStatuses[path] = hasNoMedia folderNoMediaStatuses[path] = hasNoMedia
} }
}.toMutableList() as ArrayList<String> }.toMutableList() as ArrayList<String>
} catch (e: Exception) { } catch (e: Exception) {