small code cleanup at MediaFetcher

This commit is contained in:
tibbi 2018-04-23 16:42:45 +02:00
parent 64fe17d1b9
commit cfa2f82066
1 changed files with 16 additions and 16 deletions

View File

@ -14,7 +14,6 @@ import com.simplemobiletools.gallery.models.Medium
import java.io.File import java.io.File
import java.util.LinkedHashMap import java.util.LinkedHashMap
import kotlin.collections.ArrayList import kotlin.collections.ArrayList
import kotlin.collections.HashSet
import kotlin.collections.set import kotlin.collections.set
class MediaFetcher(val context: Context) { class MediaFetcher(val context: Context) {
@ -44,7 +43,15 @@ class MediaFetcher(val context: Context) {
return try { return try {
val cursor = context.contentResolver.query(uri, projection, selection, selectionArgs, null) val cursor = context.contentResolver.query(uri, projection, selection, selectionArgs, null)
parseCursor(context, cursor, isPickImage, isPickVideo, curPath, filterMedia) val curMedia = ArrayList<Medium>()
val foldersToScan = getFoldersToScan(context, cursor, curPath)
foldersToScan.forEach {
fetchFolderContent(it, curMedia, isPickImage, isPickVideo, filterMedia)
}
Medium.sorting = context.config.getFileSorting(curPath)
curMedia.sort()
curMedia
} catch (e: Exception) { } catch (e: Exception) {
ArrayList() ArrayList()
} }
@ -105,10 +112,10 @@ class MediaFetcher(val context: Context) {
return args return args
} }
private fun parseCursor(context: Context, cursor: Cursor, isPickImage: Boolean, isPickVideo: Boolean, curPath: String, filterMedia: Int): ArrayList<Medium> { private fun getFoldersToScan(context: Context, cursor: Cursor, curPath: String): ArrayList<String> {
val config = context.config val config = context.config
val includedFolders = config.includedFolders val includedFolders = config.includedFolders
val foldersToScan = HashSet<String>() var foldersToScan = ArrayList<String>()
cursor.use { cursor.use {
if (cursor.moveToFirst()) { if (cursor.moveToFirst()) {
@ -130,24 +137,17 @@ class MediaFetcher(val context: Context) {
} }
} }
val curMedia = ArrayList<Medium>()
val showHidden = config.shouldShowHidden val showHidden = config.shouldShowHidden
val excludedFolders = config.excludedFolders val excludedFolders = config.excludedFolders
foldersToScan.filter { it.shouldFolderBeVisible(excludedFolders, includedFolders, showHidden) }.toList().forEach { foldersToScan = foldersToScan.filter { it.shouldFolderBeVisible(excludedFolders, includedFolders, showHidden) } as ArrayList<String>
fetchFolderContent(it, curMedia, isPickImage, isPickVideo, filterMedia) if (config.isThirdPartyIntent && curPath.isNotEmpty()) {
foldersToScan.add(curPath)
} }
if (config.isThirdPartyIntent && curPath.isNotEmpty() && curMedia.isEmpty()) { return foldersToScan
getMediaInFolder(curPath, curMedia, isPickImage, isPickVideo, filterMedia)
} }
Medium.sorting = config.getFileSorting(curPath) private fun addFolder(curFolders: ArrayList<String>, folder: String) {
curMedia.sort()
return curMedia
}
private fun addFolder(curFolders: HashSet<String>, folder: String) {
curFolders.add(folder) curFolders.add(folder)
val files = File(folder).listFiles() ?: return val files = File(folder).listFiles() ?: return
for (file in files) { for (file in files) {