fix #1545, properly sort Favorites by date taken

This commit is contained in:
tibbi 2020-01-27 21:22:10 +01:00
parent 650a985a9c
commit f4c02d1168

View file

@ -226,7 +226,7 @@ class MediaFetcher(val context: Context) {
val checkFileExistence = config.fileLoadingPriority == PRIORITY_VALIDITY
val showHidden = config.shouldShowHidden
val showPortraits = filterMedia and TYPE_PORTRAITS != 0
val dateTakens = if (getProperDateTaken && folder != FAVORITES && !isRecycleBin) getFolderDateTakens(folder) else HashMap()
val dateTakens = if (getProperDateTaken && !isRecycleBin) getFolderDateTakens(folder) else HashMap()
val files = when (folder) {
FAVORITES -> favoritePaths.filter { showHidden || !it.contains("/.") }.map { File(it) }.toMutableList() as ArrayList<File>
@ -302,7 +302,7 @@ class MediaFetcher(val context: Context) {
val videoDuration = if (getVideoDurations && isVideo) path.getVideoDuration() else 0
if (getProperDateTaken) {
dateTaken = dateTakens.remove(filename) ?: lastModified
dateTaken = dateTakens.remove(path) ?: lastModified
}
val type = when {
@ -390,34 +390,42 @@ class MediaFetcher(val context: Context) {
}
private fun getFolderDateTakens(folder: String): HashMap<String, Long> {
val projection = arrayOf(
MediaStore.Images.Media.DISPLAY_NAME,
MediaStore.Images.Media.DATE_TAKEN
)
val uri = MediaStore.Files.getContentUri("external")
val selection = "${MediaStore.Images.Media.DATA} LIKE ? AND ${MediaStore.Images.Media.DATA} NOT LIKE ?"
val selectionArgs = arrayOf("$folder/%", "$folder/%/%")
val dateTakens = HashMap<String, Long>()
val cursor = context.contentResolver.query(uri, projection, selection, selectionArgs, null)
cursor?.use {
if (cursor.moveToFirst()) {
do {
try {
val dateTaken = cursor.getLongValue(MediaStore.Images.Media.DATE_TAKEN)
if (dateTaken != 0L) {
val name = cursor.getStringValue(MediaStore.Images.Media.DISPLAY_NAME)
dateTakens[name] = dateTaken
if (folder != FAVORITES) {
val projection = arrayOf(
MediaStore.Images.Media.DISPLAY_NAME,
MediaStore.Images.Media.DATE_TAKEN
)
val uri = MediaStore.Files.getContentUri("external")
val selection = "${MediaStore.Images.Media.DATA} LIKE ? AND ${MediaStore.Images.Media.DATA} NOT LIKE ?"
val selectionArgs = arrayOf("$folder/%", "$folder/%/%")
val cursor = context.contentResolver.query(uri, projection, selection, selectionArgs, null)
cursor?.use {
if (cursor.moveToFirst()) {
do {
try {
val dateTaken = cursor.getLongValue(MediaStore.Images.Media.DATE_TAKEN)
if (dateTaken != 0L) {
val name = cursor.getStringValue(MediaStore.Images.Media.DISPLAY_NAME)
dateTakens["$folder/$name"] = dateTaken
}
} catch (e: Exception) {
}
} catch (e: Exception) {
}
} while (cursor.moveToNext())
} while (cursor.moveToNext())
}
}
}
context.dateTakensDB.getDateTakensFromPath(folder).forEach {
dateTakens[it.filename] = it.taken
val dateTakenValues = if (folder == FAVORITES) {
context.dateTakensDB.getAllDateTakens()
} else {
context.dateTakensDB.getDateTakensFromPath(folder)
}
dateTakenValues.forEach {
dateTakens[it.fullPath] = it.taken
}
return dateTakens
@ -460,7 +468,6 @@ class MediaFetcher(val context: Context) {
}
fun groupMedia(media: ArrayList<Medium>, path: String): ArrayList<ThumbnailItem> {
val mediumGroups = LinkedHashMap<String, ArrayList<Medium>>()
val pathToCheck = if (path.isEmpty()) SHOW_ALL else path
val currentGrouping = context.config.getFolderGrouping(pathToCheck)
if (currentGrouping and GROUP_BY_NONE != 0) {
@ -473,6 +480,7 @@ class MediaFetcher(val context: Context) {
return thumbnailItems
}
val mediumGroups = LinkedHashMap<String, ArrayList<Medium>>()
media.forEach {
val key = it.getGroupingKey(currentGrouping)
if (!mediumGroups.containsKey(key)) {