diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetDirectoriesAsynctask.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetDirectoriesAsynctask.kt index af61ac9bd..944dcb0cd 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetDirectoriesAsynctask.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetDirectoriesAsynctask.kt @@ -2,13 +2,14 @@ package com.simplemobiletools.gallery.asynctasks import android.content.Context import android.os.AsyncTask -import com.simplemobiletools.commons.extensions.* +import com.simplemobiletools.commons.extensions.getFilenameFromPath +import com.simplemobiletools.commons.extensions.hasWriteStoragePermission +import com.simplemobiletools.commons.extensions.internalStoragePath +import com.simplemobiletools.commons.extensions.sdCardPath import com.simplemobiletools.gallery.R import com.simplemobiletools.gallery.extensions.config import com.simplemobiletools.gallery.extensions.containsNoMedia -import com.simplemobiletools.gallery.extensions.getParents -import com.simplemobiletools.gallery.helpers.IMAGES -import com.simplemobiletools.gallery.helpers.VIDEOS +import com.simplemobiletools.gallery.extensions.getFilesFrom import com.simplemobiletools.gallery.models.Directory import com.simplemobiletools.gallery.models.Medium import java.io.File @@ -24,54 +25,12 @@ class GetDirectoriesAsynctask(val context: Context, val isPickVideo: Boolean, va if (!context.hasWriteStoragePermission()) return ArrayList() - val media = ArrayList() - val showMedia = config.showMedia - val fileSorting = config.fileSorting - val parents = context.getParents() - removeExcludedFolders(parents) - - parents.forEach { - val filenames = File(it).list() - if (filenames?.size ?: 0 > 0) { - for (filename in filenames) { - if (shouldStop) - cancel(true) - - val isImage = filename.isImageFast() || filename.isGif() - val isVideo = if (isImage) false else filename.isVideoFast() - - if (!isImage && !isVideo) - continue - - if (isVideo && (isPickImage || showMedia == IMAGES)) - continue - - if (isImage && (isPickVideo || showMedia == VIDEOS)) - continue - - if (!showHidden && filename.startsWith('.')) - continue - - val file = File(it, filename) - val size = file.length() - if (size == 0L) - continue - - val dateModified = file.lastModified() - val medium = Medium(filename, file.absolutePath, isVideo, dateModified, dateModified, size) - media.add(medium) - } - } - } - - Medium.sorting = fileSorting - media.sort() - + val media = context.getFilesFrom("", isPickImage, isPickVideo) + val excludedPaths = config.excludedFolders val directories = groupDirectories(media) - val dirs = ArrayList(directories.values.filter { File(it.path).exists() }) + val dirs = ArrayList(directories.values.filter { File(it.path).exists() }).filter { !isThisOrParentExcluded(it.path, excludedPaths) } as ArrayList Directory.sorting = config.directorySorting dirs.sort() - return movePinnedToFront(dirs) } @@ -110,12 +69,6 @@ class GetDirectoriesAsynctask(val context: Context, val isPickVideo: Boolean, va return directories } - private fun removeExcludedFolders(paths: MutableList) { - val excludedPaths = config.excludedFolders - val ignorePaths = paths.filter { isThisOrParentExcluded(it, excludedPaths) } - paths.removeAll(ignorePaths) - } - private fun isThisOrParentExcluded(path: String, excludedPaths: MutableSet) = excludedPaths.any { path.startsWith(it) } private fun movePinnedToFront(dirs: ArrayList): ArrayList {