remove the static variable holding the media sorting

This commit is contained in:
tibbi 2018-05-25 10:08:44 +02:00
parent 4e58f1e6c1
commit 83c72d2918
7 changed files with 45 additions and 54 deletions

View File

@ -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
}

View File

@ -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())

View File

@ -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<Medium> {
val sorting = context.config.getFileSorting(mPath)
return if (showAll) {
val foldersToScan = mediaFetcher.getFoldersToScan("")
val media = ArrayList<Medium>()
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)
}
}

View File

@ -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))
}
}
}

View File

@ -313,10 +313,9 @@ fun Context.getCachedMedia(path: String, getVideosOnly: Boolean = false, getImag
}
}) as ArrayList<Medium>
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)
}

View File

@ -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<Medium> {
fun getFilesFrom(curPath: String, isPickImage: Boolean, isPickVideo: Boolean, sorting: Int): ArrayList<Medium> {
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<Medium>, 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
})
}
}

View File

@ -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<Medium> {
@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()