From 83c72d29181522ad83f3620361872684cbcec315 Mon Sep 17 00:00:00 2001 From: tibbi Date: Fri, 25 May 2018 10:08:44 +0200 Subject: [PATCH] remove the static variable holding the media sorting --- .../gallery/activities/MainActivity.kt | 4 +- .../gallery/activities/MediaActivity.kt | 8 ++-- .../gallery/asynctasks/GetMediaAsynctask.kt | 8 ++-- .../gallery/dialogs/PickMediumDialog.kt | 5 ++- .../gallery/extensions/Context.kt | 5 +-- .../gallery/helpers/MediaFetcher.kt | 31 +++++++++++---- .../gallery/models/Medium.kt | 38 +++---------------- 7 files changed, 45 insertions(+), 54 deletions(-) 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 1f3ad621f..74493144a 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt @@ -593,7 +593,7 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener { val directoryDao = galleryDB.DirectoryDao() for (directory in dirs) { - val curMedia = mediaFetcher.getFilesFrom(directory.path, getImagesOnly, getVideosOnly) + val curMedia = mediaFetcher.getFilesFrom(directory.path, getImagesOnly, getVideosOnly, config.directorySorting) val newDir = if (curMedia.isEmpty()) { directory } else { @@ -634,7 +634,7 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener { } for (folder in foldersToScan) { - val newMedia = mediaFetcher.getFilesFrom(folder, getImagesOnly, getVideosOnly) + val newMedia = mediaFetcher.getFilesFrom(folder, getImagesOnly, getVideosOnly, config.directorySorting) if (newMedia.isEmpty()) { continue } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt index 7ef0ac69b..ca1b7226e 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt @@ -333,20 +333,22 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener { media_horizontal_fastscroller.isHorizontal = true media_horizontal_fastscroller.beVisibleIf(allowHorizontalScroll) + val sorting = config.getFileSorting(mPath) + if (allowHorizontalScroll) { media_horizontal_fastscroller.allowBubbleDisplay = config.showInfoBubble media_horizontal_fastscroller.setViews(media_grid, media_refresh_layout) { - media_horizontal_fastscroller.updateBubbleText(getBubbleTextItem(it)) + media_horizontal_fastscroller.updateBubbleText(getBubbleTextItem(it, sorting)) } } else { media_vertical_fastscroller.allowBubbleDisplay = config.showInfoBubble media_vertical_fastscroller.setViews(media_grid, media_refresh_layout) { - media_vertical_fastscroller.updateBubbleText(getBubbleTextItem(it)) + media_vertical_fastscroller.updateBubbleText(getBubbleTextItem(it, sorting)) } } } - private fun getBubbleTextItem(index: Int) = getMediaAdapter()?.media?.getOrNull(index)?.getBubbleText() ?: "" + private fun getBubbleTextItem(index: Int, sorting: Int) = getMediaAdapter()?.media?.getOrNull(index)?.getBubbleText(sorting) ?: "" private fun checkLastMediaChanged() { if (isActivityDestroyed()) 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 8f82b4c00..7f92fcfb4 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetMediaAsynctask.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetMediaAsynctask.kt @@ -13,19 +13,19 @@ class GetMediaAsynctask(val context: Context, val mPath: String, val isPickImage private val mediaFetcher = MediaFetcher(context) override fun doInBackground(vararg params: Void): ArrayList { + val sorting = context.config.getFileSorting(mPath) return if (showAll) { val foldersToScan = mediaFetcher.getFoldersToScan("") val media = ArrayList() for (folder in foldersToScan) { - val newMedia = mediaFetcher.getFilesFrom(folder, isPickImage, isPickVideo) + val newMedia = mediaFetcher.getFilesFrom(folder, isPickImage, isPickVideo, sorting) media.addAll(newMedia) } - Medium.sorting = context.config.getFileSorting("") - media.sort() + MediaFetcher(context).sortMedia(media, context.config.getFileSorting("")) media } else { - mediaFetcher.getFilesFrom(mPath, isPickImage, isPickVideo) + mediaFetcher.getFilesFrom(mPath, isPickImage, isPickVideo, sorting) } } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/PickMediumDialog.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/PickMediumDialog.kt index a717c3cfa..3998ec75d 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/PickMediumDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/PickMediumDialog.kt @@ -68,6 +68,7 @@ class PickMediumDialog(val activity: BaseSimpleActivity, val path: String, val c } val scrollHorizontally = activity.config.scrollHorizontally && isGridViewType + val sorting = activity.config.getFileSorting(path) view.apply { media_grid.adapter = adapter @@ -80,12 +81,12 @@ class PickMediumDialog(val activity: BaseSimpleActivity, val path: String, val c if (scrollHorizontally) { media_horizontal_fastscroller.allowBubbleDisplay = activity.config.showInfoBubble media_horizontal_fastscroller.setViews(media_grid) { - media_horizontal_fastscroller.updateBubbleText(media[it].getBubbleText()) + media_horizontal_fastscroller.updateBubbleText(media[it].getBubbleText(sorting)) } } else { media_vertical_fastscroller.allowBubbleDisplay = activity.config.showInfoBubble media_vertical_fastscroller.setViews(media_grid) { - media_vertical_fastscroller.updateBubbleText(media[it].getBubbleText()) + media_vertical_fastscroller.updateBubbleText(media[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 22b8b7ec9..e5fe854ea 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Context.kt @@ -313,10 +313,9 @@ fun Context.getCachedMedia(path: String, getVideosOnly: Boolean = false, getImag } }) as ArrayList - Medium.sorting = config.getFileSorting(path) - filteredMedia.sort() - + MediaFetcher(this).sortMedia(filteredMedia, config.getFileSorting(path)) callback(filteredMedia) + media.filter { !getDoesFilePathExist(it.path) }.forEach { mediumDao.deleteMediumPath(it.path) } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/MediaFetcher.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/MediaFetcher.kt index 31bb19c25..d94654dd6 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/MediaFetcher.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/MediaFetcher.kt @@ -5,10 +5,7 @@ import android.database.Cursor import android.net.Uri import android.provider.MediaStore import com.simplemobiletools.commons.extensions.* -import com.simplemobiletools.commons.helpers.OTG_PATH -import com.simplemobiletools.commons.helpers.SORT_BY_DATE_TAKEN -import com.simplemobiletools.commons.helpers.photoExtensions -import com.simplemobiletools.commons.helpers.videoExtensions +import com.simplemobiletools.commons.helpers.* import com.simplemobiletools.gallery.extensions.config import com.simplemobiletools.gallery.extensions.getDistinctPath import com.simplemobiletools.gallery.extensions.getOTGFolderChildren @@ -19,7 +16,7 @@ import java.io.File class MediaFetcher(val context: Context) { var shouldStop = false - fun getFilesFrom(curPath: String, isPickImage: Boolean, isPickVideo: Boolean): ArrayList { + fun getFilesFrom(curPath: String, isPickImage: Boolean, isPickVideo: Boolean, sorting: Int): ArrayList { val filterMedia = context.config.filterMedia if (filterMedia == 0) { return ArrayList() @@ -34,8 +31,7 @@ class MediaFetcher(val context: Context) { curMedia.addAll(newMedia) } - Medium.sorting = context.config.getFileSorting(curPath) - curMedia.sort() + sortMedia(curMedia, sorting) return curMedia } @@ -169,7 +165,7 @@ class MediaFetcher(val context: Context) { val files = File(folder).listFiles() ?: return media val doExtraCheck = context.config.doExtraCheck val showHidden = context.config.shouldShowHidden - val sorting = context.config.getFileSorting(folder) + val sorting = SORT_BY_DATE_TAKEN //context.config.getFileSorting(folder) val dateTakens = if (sorting and SORT_BY_DATE_TAKEN != 0) getFolderDateTakens(folder) else HashMap() for (file in files) { @@ -299,4 +295,23 @@ class MediaFetcher(val context: Context) { return dateTakens } + + fun sortMedia(media: ArrayList, sorting: Int) { + media.sortWith(Comparator { o1, o2 -> + o1 as Medium + o2 as Medium + 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 + }) + } } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/models/Medium.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/models/Medium.kt index b6a183cef..9203db306 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/models/Medium.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/models/Medium.kt @@ -7,7 +7,10 @@ import android.arch.persistence.room.PrimaryKey import com.simplemobiletools.commons.extensions.formatDate import com.simplemobiletools.commons.extensions.formatSize import com.simplemobiletools.commons.extensions.isDng -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 com.simplemobiletools.gallery.helpers.TYPE_GIFS import com.simplemobiletools.gallery.helpers.TYPE_IMAGES import com.simplemobiletools.gallery.helpers.TYPE_VIDEOS @@ -22,11 +25,10 @@ data class Medium( @ColumnInfo(name = "last_modified") val modified: Long, @ColumnInfo(name = "date_taken") var taken: Long, @ColumnInfo(name = "size") val size: Long, - @ColumnInfo(name = "type") val type: Int) : Serializable, Comparable { + @ColumnInfo(name = "type") val type: Int) : Serializable { companion object { private const val serialVersionUID = -6553149366975455L - var sorting: Int = 0 } fun isGif() = type == TYPE_GIFS @@ -37,35 +39,7 @@ data class Medium( fun isDng() = path.isDng() - override fun compareTo(other: Medium): 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()