diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt index 74493144a..6e7add788 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt @@ -775,7 +775,7 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener { private fun getCurrentlyDisplayedDirs() = getRecyclerAdapter()?.dirs ?: ArrayList() - private fun getBubbleTextItem(index: Int) = getRecyclerAdapter()?.dirs?.getOrNull(index)?.getBubbleText() ?: "" + private fun getBubbleTextItem(index: Int) = getRecyclerAdapter()?.dirs?.getOrNull(index)?.getBubbleText(config.directorySorting) ?: "" private fun setupLatestMediaId() { Thread { diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/PickDirectoryDialog.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/PickDirectoryDialog.kt index 372a0483c..80a0a53f7 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/PickDirectoryDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/PickDirectoryDialog.kt @@ -72,6 +72,7 @@ class PickDirectoryDialog(val activity: BaseSimpleActivity, val sourcePath: Stri } val scrollHorizontally = activity.config.scrollHorizontally && isGridViewType + val sorting = activity.config.directorySorting view.apply { directories_grid.adapter = adapter @@ -84,12 +85,12 @@ class PickDirectoryDialog(val activity: BaseSimpleActivity, val sourcePath: Stri if (scrollHorizontally) { directories_horizontal_fastscroller.allowBubbleDisplay = activity.config.showInfoBubble directories_horizontal_fastscroller.setViews(directories_grid) { - directories_horizontal_fastscroller.updateBubbleText(dirs[it].getBubbleText()) + directories_horizontal_fastscroller.updateBubbleText(dirs[it].getBubbleText(sorting)) } } else { directories_vertical_fastscroller.allowBubbleDisplay = activity.config.showInfoBubble directories_vertical_fastscroller.setViews(directories_grid) { - directories_vertical_fastscroller.updateBubbleText(dirs[it].getBubbleText()) + directories_vertical_fastscroller.updateBubbleText(dirs[it].getBubbleText(sorting)) } } } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Context.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Context.kt index e5fe854ea..069a2f405 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Context.kt @@ -16,7 +16,7 @@ import com.bumptech.glide.load.engine.DiskCacheStrategy import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions import com.bumptech.glide.request.RequestOptions import com.simplemobiletools.commons.extensions.* -import com.simplemobiletools.commons.helpers.OTG_PATH +import com.simplemobiletools.commons.helpers.* import com.simplemobiletools.gallery.R import com.simplemobiletools.gallery.activities.SettingsActivity import com.simplemobiletools.gallery.asynctasks.GetMediaAsynctask @@ -96,9 +96,26 @@ fun Context.movePinnedDirectoriesToFront(dirs: ArrayList): ArrayList< @Suppress("UNCHECKED_CAST") fun Context.getSortedDirectories(source: ArrayList): ArrayList { - Directory.sorting = config.directorySorting + val sorting = config.directorySorting val dirs = source.clone() as ArrayList - dirs.sort() + + dirs.sortWith(Comparator { o1, o2 -> + o1 as Directory + o2 as Directory + var result = when { + sorting and SORT_BY_NAME != 0 -> AlphanumericComparator().compare(o1.name.toLowerCase(), o2.name.toLowerCase()) + sorting and SORT_BY_PATH != 0 -> AlphanumericComparator().compare(o1.path.toLowerCase(), o2.path.toLowerCase()) + sorting and SORT_BY_SIZE != 0 -> o1.size.compareTo(o2.size) + sorting and SORT_BY_DATE_MODIFIED != 0 -> o1.modified.compareTo(o2.modified) + else -> o1.taken.compareTo(o2.taken) + } + + if (sorting and SORT_DESCENDING != 0) { + result *= -1 + } + result + }) + return movePinnedDirectoriesToFront(dirs) } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/models/Directory.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/models/Directory.kt index ce5777efa..3c4dd01a0 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/models/Directory.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/models/Directory.kt @@ -6,7 +6,10 @@ import android.arch.persistence.room.Index import android.arch.persistence.room.PrimaryKey import com.simplemobiletools.commons.extensions.formatDate import com.simplemobiletools.commons.extensions.formatSize -import com.simplemobiletools.commons.helpers.* +import com.simplemobiletools.commons.helpers.SORT_BY_DATE_MODIFIED +import com.simplemobiletools.commons.helpers.SORT_BY_NAME +import com.simplemobiletools.commons.helpers.SORT_BY_PATH +import com.simplemobiletools.commons.helpers.SORT_BY_SIZE import java.io.Serializable @Entity(tableName = "directories", indices = [Index(value = "path", unique = true)]) @@ -20,42 +23,13 @@ data class Directory( @ColumnInfo(name = "date_taken") var taken: Long, @ColumnInfo(name = "size") var size: Long, @ColumnInfo(name = "location") val location: Int, - @ColumnInfo(name = "media_types") var types: Int) : Serializable, Comparable { + @ColumnInfo(name = "media_types") var types: Int) : Serializable { companion object { private const val serialVersionUID = -6553345863555455L - var sorting = 0 } - override fun compareTo(other: Directory): Int { - var result: Int - when { - sorting and SORT_BY_NAME != 0 -> result = AlphanumericComparator().compare(name.toLowerCase(), other.name.toLowerCase()) - sorting and SORT_BY_PATH != 0 -> result = AlphanumericComparator().compare(path.toLowerCase(), other.path.toLowerCase()) - sorting and SORT_BY_SIZE != 0 -> result = when { - size == other.size -> 0 - size > other.size -> 1 - else -> -1 - } - sorting and SORT_BY_DATE_MODIFIED != 0 -> result = when { - modified == other.modified -> 0 - modified > other.modified -> 1 - else -> -1 - } - else -> result = when { - taken == other.taken -> 0 - taken > other.taken -> 1 - else -> -1 - } - } - - if (sorting and SORT_DESCENDING != 0) { - result *= -1 - } - return result - } - - fun getBubbleText() = when { + fun getBubbleText(sorting: Int) = when { sorting and SORT_BY_NAME != 0 -> name sorting and SORT_BY_PATH != 0 -> path sorting and SORT_BY_SIZE != 0 -> size.formatSize()