Skip to content

Commit

Permalink
Sort by date taken by default if available, otherwise sort by last mo…
Browse files Browse the repository at this point in the history
…dified

Partial fix of [Enhancement] Default sort order should be Date Taken (from EXIF tag) #529

Signed-off-by: IacobIonut01 <paulionut2003@gmail.com>
  • Loading branch information
IacobIonut01 committed Dec 8, 2024
1 parent 37f20ed commit f0d9150
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ import android.database.Cursor
import android.os.Build
import android.os.Bundle
import android.provider.MediaStore
import androidx.core.database.getLongOrNull
import androidx.core.os.bundleOf
import com.dot.gallery.feature_node.data.data_source.mediastore.MediaQuery
import com.dot.gallery.core.util.PickerUtils
import com.dot.gallery.core.util.Query
import com.dot.gallery.core.util.and
import com.dot.gallery.core.util.eq
import com.dot.gallery.core.util.join
import com.dot.gallery.core.util.ext.queryFlow
import com.dot.gallery.core.util.ext.tryGetString
import com.dot.gallery.core.util.join
import com.dot.gallery.feature_node.data.data_source.mediastore.MediaQuery
import com.dot.gallery.feature_node.domain.model.Album
import com.dot.gallery.feature_node.domain.model.MediaType
import kotlinx.coroutines.flow.Flow
Expand Down Expand Up @@ -85,6 +86,7 @@ class AlbumsFlow(
val thumbnailPathIndex = it.getColumnIndex(MediaStore.Files.FileColumns.DATA)
val thumbnailRelativePathIndex =
it.getColumnIndex(MediaStore.Files.FileColumns.RELATIVE_PATH)
val thumbnailDateTakenIndex = it.getColumnIndex(MediaStore.Files.FileColumns.DATE_TAKEN)
val thumbnailDateIndex = it.getColumnIndex(MediaStore.Files.FileColumns.DATE_MODIFIED)
val sizeIndex = it.getColumnIndex(MediaStore.Files.FileColumns.SIZE)
val mimeTypeIndex = it.getColumnIndex(MediaStore.Files.FileColumns.MIME_TYPE)
Expand All @@ -105,6 +107,7 @@ class AlbumsFlow(
val label = it.tryGetString(labelIndex, Build.MODEL)
val thumbnailPath = it.getString(thumbnailPathIndex)
val thumbnailRelativePath = it.getString(thumbnailRelativePathIndex)
val thumbnailDateTaken = it.getLongOrNull(thumbnailDateTakenIndex)
val thumbnailDate = it.getLong(thumbnailDateIndex)
val size = it.getLong(sizeIndex)
val mimeType = it.getString(mimeTypeIndex)
Expand All @@ -119,7 +122,7 @@ class AlbumsFlow(
uri = ContentUris.withAppendedId(contentUri, id),
pathToThumbnail = thumbnailPath,
relativePath = thumbnailRelativePath,
timestamp = thumbnailDate
timestamp = thumbnailDateTaken ?: thumbnailDate
).apply {
this.count += 1
this.size += size
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class MediaRepositoryImpl(
contentResolver = contentResolver,
buckedId = MediaStoreBuckets.MEDIA_STORE_BUCKET_TIMELINE.id
).flowData().map {
Resource.Success(it)
Resource.Success(MediaOrder.Date(OrderType.Descending).sortMedia(it))
}.flowOn(Dispatchers.IO)

override fun getMediaByType(allowedMedia: AllowedMedia): Flow<Resource<List<Media>>> =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,17 @@ sealed class MediaOrder(open val orderType: OrderType) {
return when (orderType) {
OrderType.Ascending -> {
when (this) {
is Date -> media.sortedBy { it.timestamp }
is Date -> media.sortedBy { it.takenTimestamp ?: it.timestamp }
is Label -> media.sortedBy { it.label.lowercase() }
is Expiry -> media.sortedBy { it.expiryTimestamp ?: it.timestamp }
is Expiry -> media.sortedBy { it.expiryTimestamp ?: it.takenTimestamp ?: it.timestamp }
}
}

OrderType.Descending -> {
when (this) {
is Date -> media.sortedByDescending { it.timestamp }
is Date -> media.sortedByDescending { it.takenTimestamp ?: it.timestamp }
is Label -> media.sortedByDescending { it.label.lowercase() }
is Expiry -> media.sortedByDescending { it.expiryTimestamp ?: it.timestamp }
is Expiry -> media.sortedByDescending { it.expiryTimestamp ?: it.takenTimestamp ?: it.timestamp }
}
}
}
Expand Down

0 comments on commit f0d9150

Please sign in to comment.