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 69f9fe488..a3fca44ca 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetDirectoriesAsynctask.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetDirectoriesAsynctask.kt @@ -11,6 +11,7 @@ import com.simplemobiletools.gallery.extensions.getLongValue import com.simplemobiletools.gallery.extensions.getStringValue import com.simplemobiletools.gallery.helpers.* import com.simplemobiletools.gallery.models.Directory +import com.simplemobiletools.gallery.models.Medium import java.io.File import java.util.* @@ -24,6 +25,7 @@ class GetDirectoriesAsynctask(val context: Context, val isPickVideo: Boolean, va } override fun doInBackground(vararg params: Void): ArrayList { + val media = ArrayList() val directories = LinkedHashMap() val invalidFiles = ArrayList() val showMedia = mConfig.showMedia @@ -38,7 +40,8 @@ class GetDirectoriesAsynctask(val context: Context, val isPickVideo: Boolean, va uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI } - val columns = arrayOf(MediaStore.Images.Media.DATA, MediaStore.Images.Media.DATE_MODIFIED, MediaStore.Images.Media.DATE_TAKEN, MediaStore.Images.Media.SIZE) + val columns = arrayOf(MediaStore.Images.Media.DATA, MediaStore.Images.Media.DISPLAY_NAME, MediaStore.Images.Media.DATE_MODIFIED, + MediaStore.Images.Media.DATE_TAKEN, MediaStore.Images.Media.SIZE) val order = getSortOrder() var cursor: Cursor? = null @@ -56,22 +59,10 @@ class GetDirectoriesAsynctask(val context: Context, val isPickVideo: Boolean, va continue } - val parentDir = file.parent - if (directories.containsKey(parentDir)) { - val directory: Directory = directories[parentDir]!! - val newImageCnt = directory.mediaCnt + 1 - directory.mediaCnt = newImageCnt - directory.addSize(size) - } else if (!mToBeDeleted.contains(parentDir)) { - var dirName = context.getHumanizedFilename(parentDir) - if (mConfig.getIsFolderHidden(parentDir)) { - dirName += " ${context.resources.getString(R.string.hidden)}" - } - - val dateModified = cursor.getLongValue(MediaStore.Images.Media.DATE_MODIFIED) - val dateTaken = cursor.getLongValue(MediaStore.Images.Media.DATE_TAKEN) - directories.put(parentDir, Directory(parentDir, fullPath, dirName, 1, dateModified, dateTaken, size)) - } + val name = cursor.getStringValue(MediaStore.Images.Media.DISPLAY_NAME) ?: "" + val dateModified = cursor.getLongValue(MediaStore.Images.Media.DATE_MODIFIED) + val dateTaken = cursor.getLongValue(MediaStore.Images.Media.DATE_TAKEN) + media.add(Medium(name, fullPath, i == 1, dateModified, dateTaken, size)) } while (cursor.moveToNext()) } } finally { @@ -79,6 +70,26 @@ class GetDirectoriesAsynctask(val context: Context, val isPickVideo: Boolean, va } } + Medium.sorting = mConfig.fileSorting + media.sort() + + for ((name, path, isVideo, dateModified, dateTaken, size) in media) { + val parentDir = File(path).parent + if (directories.containsKey(parentDir)) { + val directory: Directory = directories[parentDir]!! + val newImageCnt = directory.mediaCnt + 1 + directory.mediaCnt = newImageCnt + directory.addSize(size) + } else if (!mToBeDeleted.contains(parentDir)) { + var dirName = context.getHumanizedFilename(parentDir) + if (mConfig.getIsFolderHidden(parentDir)) { + dirName += " ${context.resources.getString(R.string.hidden)}" + } + + directories.put(parentDir, Directory(parentDir, path, dirName, 1, dateModified, dateTaken, size)) + } + } + context.scanFiles(invalidFiles) {} val dirs = ArrayList(directories.values.filter { File(it.path).exists() }) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetMediaAsynctask.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetMediaAsynctask.kt index cc785020d..c2817cdbd 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetMediaAsynctask.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetMediaAsynctask.kt @@ -76,7 +76,7 @@ class GetMediaAsynctask(val context: Context, val mPath: String, val isPickVideo } context.scanFiles(invalidFiles) {} - Medium.sorting = mConfig.sorting + Medium.sorting = mConfig.fileSorting media.sort() return media } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/ChangeSortingDialog.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/ChangeSortingDialog.kt index a40b050cb..f6acd3b8b 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/ChangeSortingDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/ChangeSortingDialog.kt @@ -29,7 +29,7 @@ class ChangeSortingDialog(val activity: SimpleActivity, val isDirectorySorting: activity.setupDialogStuff(view, this, R.string.sort_by) } - currSorting = if (isDirectorySorting) config.directorySorting else config.sorting + currSorting = if (isDirectorySorting) config.directorySorting else config.fileSorting setupSortRadio() setupOrderRadio() } @@ -73,7 +73,7 @@ class ChangeSortingDialog(val activity: SimpleActivity, val isDirectorySorting: if (isDirectorySorting) { config.directorySorting = sorting } else { - config.sorting = sorting + config.fileSorting = sorting } callback.invoke() } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/Config.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/Config.kt index ae198ca09..0df570507 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/Config.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/Config.kt @@ -21,7 +21,7 @@ class Config(context: Context) : BaseConfig(context) { get() = mPrefs.getBoolean(IS_SAME_SORTING, true) set(isSameSorting) = mPrefs.edit().putBoolean(IS_SAME_SORTING, isSameSorting).apply() - var sorting: Int + var fileSorting: Int get() = if (isSameSorting) directorySorting else mPrefs.getInt(SORT_ORDER, SORT_BY_DATE_MODIFIED or SORT_DESCENDING) set(order) = if (isSameSorting) directorySorting = order else mPrefs.edit().putInt(SORT_ORDER, order).apply()