use proper grid position at thumbnails with grouping and section titles

This commit is contained in:
tibbi 2021-01-18 23:08:54 +01:00
parent 78a75fe873
commit efe26e9c58
4 changed files with 27 additions and 14 deletions

View file

@ -747,7 +747,9 @@ class MediaActivity : SimpleActivity(), MediaOperationsListener {
if (viewType == VIEW_TYPE_GRID) {
val spanCount = config.mediaColumnCnt
val spacing = config.thumbnailSpacing
media_grid.addItemDecoration(GridSpacingItemDecoration(spanCount, spacing, config.scrollHorizontally, config.fileRoundedCorners))
val useGridPosition = mMedia.firstOrNull() is ThumbnailSection
val decoration = GridSpacingItemDecoration(spanCount, spacing, config.scrollHorizontally, config.fileRoundedCorners, mMedia, useGridPosition)
media_grid.addItemDecoration(decoration)
}
}

View file

@ -3,12 +3,17 @@ package com.simplemobiletools.gallery.pro.helpers
import android.graphics.Rect
import android.view.View
import androidx.recyclerview.widget.RecyclerView
import com.simplemobiletools.gallery.pro.models.Medium
import com.simplemobiletools.gallery.pro.models.ThumbnailItem
class GridSpacingItemDecoration(val spanCount: Int, val spacing: Int, val isScrollingHorizontally: Boolean, val addSideSpacing: Boolean) : RecyclerView.ItemDecoration() {
class GridSpacingItemDecoration(val spanCount: Int, val spacing: Int, val isScrollingHorizontally: Boolean, val addSideSpacing: Boolean,
val items: ArrayList<ThumbnailItem>, val useGridPosition: Boolean) : RecyclerView.ItemDecoration() {
override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
val position = parent.getChildAdapterPosition(view)
val column = position % spanCount
val medium = items[position] as? Medium ?: return
val gridPositionToUse = if (useGridPosition) medium.gridPosition else position
val column = gridPositionToUse % spanCount
if (isScrollingHorizontally) {
if (addSideSpacing) {
@ -32,13 +37,14 @@ class GridSpacingItemDecoration(val spanCount: Int, val spacing: Int, val isScro
outRect.right = (column + 1) * spacing / spanCount
outRect.bottom = spacing
if (position < spanCount) {
if (position < spanCount && !useGridPosition) {
outRect.top = spacing
}
} else {
outRect.left = column * spacing / spanCount
outRect.right = spacing - (column + 1) * spacing / spanCount
if (position >= spanCount) {
if (gridPositionToUse >= spanCount) {
outRect.top = spacing
}
}

View file

@ -635,12 +635,15 @@ class MediaFetcher(val context: Context) {
return thumbnailItems
}
var currentGridPosition = 0
val mediumGroups = LinkedHashMap<String, ArrayList<Medium>>()
media.forEach {
val key = it.getGroupingKey(currentGrouping)
if (!mediumGroups.containsKey(key)) {
mediumGroups[key] = ArrayList()
currentGridPosition = 0
}
it.gridPosition = currentGridPosition++
mediumGroups[key]!!.add(it)
}

View file

@ -1,10 +1,7 @@
package com.simplemobiletools.gallery.pro.models
import android.content.Context
import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.Index
import androidx.room.PrimaryKey
import androidx.room.*
import com.bumptech.glide.signature.ObjectKey
import com.simplemobiletools.commons.extensions.formatDate
import com.simplemobiletools.commons.extensions.formatSize
@ -25,13 +22,18 @@ data class Medium(
@ColumnInfo(name = "filename") var name: String,
@ColumnInfo(name = "full_path") var path: String,
@ColumnInfo(name = "parent_path") var parentPath: String,
@ColumnInfo(name = "last_modified") val modified: Long,
@ColumnInfo(name = "last_modified") var modified: Long,
@ColumnInfo(name = "date_taken") var taken: Long,
@ColumnInfo(name = "size") val size: Long,
@ColumnInfo(name = "type") val type: Int,
@ColumnInfo(name = "video_duration") val videoDuration: Int,
@ColumnInfo(name = "size") var size: Long,
@ColumnInfo(name = "type") var type: Int,
@ColumnInfo(name = "video_duration") var videoDuration: Int,
@ColumnInfo(name = "is_favorite") var isFavorite: Boolean,
@ColumnInfo(name = "deleted_ts") var deletedTS: Long) : Serializable, ThumbnailItem() {
@ColumnInfo(name = "deleted_ts") var deletedTS: Long,
@Ignore var gridPosition: Int = 0 // used at grid view decoration at Grouping enabled
) : Serializable, ThumbnailItem() {
constructor() : this(null, "", "", "", 0L, 0L, 0L, 0, 0, false, 0L, 0)
companion object {
private const val serialVersionUID = -6553149366975655L