move folder filtering to getParents()

This commit is contained in:
tibbi 2017-01-13 23:52:04 +01:00
parent a48358bef3
commit c2741f6dcb
2 changed files with 30 additions and 26 deletions

View file

@ -26,7 +26,6 @@ class GetDirectoriesAsynctask(val context: Context, val isPickVideo: Boolean, va
val showMedia = config.showMedia
val fileSorting = config.fileSorting
val parents = context.getParents(isPickImage, isPickVideo)
filterDirectories(parents)
parents.mapNotNull { File(it).listFiles() }
.forEach {
@ -96,30 +95,5 @@ class GetDirectoriesAsynctask(val context: Context, val isPickVideo: Boolean, va
callback.invoke(dirs)
}
private fun filterDirectories(dirs: MutableList<String>) {
if (!config.showHiddenFolders) {
removeHiddenFolders(dirs)
removeNoMediaFolders(dirs)
}
}
private fun removeHiddenFolders(paths: MutableList<String>) {
val hiddenPaths = config.hiddenFolders
val ignorePaths = paths.filter { hiddenPaths.contains(it) }
paths.removeAll(ignorePaths)
}
private fun removeNoMediaFolders(paths: MutableList<String>) {
val ignorePaths = ArrayList<String>()
for (path in paths) {
val dir = File(path)
if (dir.exists() && dir.isDirectory) {
val res = dir.list { file, filename -> filename == ".nomedia" }
if (res?.isNotEmpty() == true)
ignorePaths.add(path)
}
}
paths.removeAll(ignorePaths)
}
}

View file

@ -61,6 +61,8 @@ fun Context.getParents(isPickImage: Boolean, isPickVideo: Boolean): ArrayList<St
} finally {
cursor?.close()
}
filterDirectories(parents)
return parents
}
@ -84,4 +86,32 @@ fun Context.getArgs(isPickImage: Boolean, isPickVideo: Boolean): Array<String> {
}
}
fun Context.filterDirectories(dirs: MutableList<String>) {
if (!config.showHiddenFolders) {
removeHiddenFolders(dirs)
removeNoMediaFolders(dirs)
}
}
fun Context.removeHiddenFolders(paths: MutableList<String>) {
val hiddenPaths = config.hiddenFolders
val ignorePaths = paths.filter { hiddenPaths.contains(it) }
paths.removeAll(ignorePaths)
}
private fun removeNoMediaFolders(paths: MutableList<String>) {
val ignorePaths = ArrayList<String>()
for (path in paths) {
val dir = File(path)
if (dir.exists() && dir.isDirectory) {
val res = dir.list { file, filename -> filename == ".nomedia" }
if (res?.isNotEmpty() == true)
ignorePaths.add(path)
}
}
paths.removeAll(ignorePaths)
}
val Context.config: Config get() = Config.newInstance(this)