Skip to content

Commit 378dbbf

Browse files
Merge pull request #15919 from nextcloud/fix/gallery-adapter-ids
fix: gallery adapter ids
2 parents 55f3275 + 69b9452 commit 378dbbf

File tree

1 file changed

+48
-42
lines changed

1 file changed

+48
-42
lines changed

app/src/main/java/com/owncloud/android/ui/adapter/GalleryAdapter.kt

Lines changed: 48 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ package com.owncloud.android.ui.adapter
1515

1616
import android.annotation.SuppressLint
1717
import android.content.Context
18-
import android.os.Handler
19-
import android.os.Looper
2018
import android.view.LayoutInflater
2119
import android.view.View
2220
import android.view.ViewGroup
@@ -65,6 +63,10 @@ class GalleryAdapter(
6563

6664
// fileId -> (section, row)
6765
private val filePositionMap = mutableMapOf<Long, Pair<Int, Int>>()
66+
67+
// (section, row) -> unique stable ID for that row
68+
private val rowIdMap = mutableMapOf<Pair<Int, Int>, Long>()
69+
6870
private var cachedAllFiles: List<OCFile>? = null
6971
private var cachedFilesCount: Int = 0
7072

@@ -104,21 +106,42 @@ class GalleryAdapter(
104106
}
105107

106108
private fun updateFilesCount() {
107-
cachedFilesCount = files.sumOf { it.rows.size }
109+
cachedFilesCount = files.fold(0) { acc, item -> acc + item.rows.size }
108110
}
109111

110112
private fun rebuildFilePositionMap() {
111113
filePositionMap.clear()
114+
rowIdMap.clear()
115+
112116
files.forEachIndexed { sectionIndex, galleryItem ->
113117
galleryItem.rows.forEachIndexed { rowIndex, row ->
118+
val position = sectionIndex to rowIndex
119+
120+
// since row can contain files two to five use first files id as adapter id
121+
row.files.firstOrNull()?.fileId?.let { firstFileId ->
122+
rowIdMap[position] = firstFileId
123+
}
124+
125+
// map all row files
114126
row.files.forEach { file ->
115-
filePositionMap[file.fileId] = sectionIndex to rowIndex
127+
filePositionMap[file.fileId] = position
116128
}
117129
}
118130
}
119131
}
120132

121-
override fun getItemId(section: Int, position: Int): Long = files[section].rows[position].calculateHashCode()
133+
override fun getItemId(section: Int, position: Int): Long = rowIdMap[section to position] ?: -1L
134+
135+
override fun getItemCount(section: Int): Int = files.getOrNull(section)?.rows?.size ?: 0
136+
137+
override fun getSectionCount(): Int = files.size
138+
139+
override fun getFilesCount(): Int = cachedFilesCount
140+
141+
override fun getItemPosition(file: OCFile): Int {
142+
val (section, row) = filePositionMap[file.fileId] ?: return -1
143+
return getAbsolutePosition(section, row)
144+
}
122145

123146
override fun selectAll(value: Boolean) {
124147
if (value) {
@@ -156,16 +179,12 @@ class GalleryAdapter(
156179
relativePosition: Int,
157180
absolutePosition: Int
158181
) {
159-
if (holder != null) {
160-
val rowHolder = holder as GalleryRowHolder
161-
rowHolder.bind(files[section].rows[relativePosition])
182+
if (holder is GalleryRowHolder) {
183+
val row = files.getOrNull(section)?.rows?.getOrNull(relativePosition)
184+
row?.let { holder.bind(it) }
162185
}
163186
}
164187

165-
override fun getItemCount(section: Int): Int = files[section].rows.size
166-
167-
override fun getSectionCount(): Int = files.size
168-
169188
override fun getPopupText(p0: View, position: Int): CharSequence = DisplayUtils.getDateByPattern(
170189
files[getRelativePosition(position).section()].date,
171190
context,
@@ -230,10 +249,8 @@ class GalleryAdapter(
230249
photoFragment.setEmptyListMessage(SearchType.GALLERY_SEARCH)
231250
}
232251

233-
Handler(Looper.getMainLooper()).post {
234-
files = finalSortedList.toGalleryItems()
235-
notifyDataSetChanged()
236-
}
252+
files = finalSortedList.toGalleryItems()
253+
notifyDataSetChanged()
237254
}
238255

239256
private fun transformToRows(list: List<OCFile>): List<GalleryRow> {
@@ -247,22 +264,17 @@ class GalleryAdapter(
247264

248265
@SuppressLint("NotifyDataSetChanged")
249266
fun clear() {
250-
Handler(Looper.getMainLooper()).post {
251-
files = emptyList()
252-
notifyDataSetChanged()
253-
}
267+
files = emptyList()
268+
notifyDataSetChanged()
254269
}
255270

256-
private fun firstOfMonth(timestamp: Long): Long {
257-
val cal = Calendar.getInstance()
258-
cal.time = Date(timestamp)
259-
cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH))
260-
cal.set(Calendar.HOUR_OF_DAY, 0)
261-
cal.set(Calendar.MINUTE, 0)
262-
cal.set(Calendar.SECOND, 0)
263-
264-
return cal.timeInMillis
265-
}
271+
private fun firstOfMonth(timestamp: Long): Long = Calendar.getInstance().apply {
272+
time = Date(timestamp)
273+
set(Calendar.DAY_OF_MONTH, getActualMinimum(Calendar.DAY_OF_MONTH))
274+
set(Calendar.HOUR_OF_DAY, 0)
275+
set(Calendar.MINUTE, 0)
276+
set(Calendar.SECOND, 0)
277+
}.timeInMillis
266278

267279
fun isEmpty(): Boolean = files.isEmpty()
268280

@@ -283,11 +295,6 @@ class GalleryAdapter(
283295
ocFileListDelegate.cancelAllPendingTasks()
284296
}
285297

286-
override fun getItemPosition(file: OCFile): Int {
287-
val (section, row) = filePositionMap[file.fileId] ?: return -1
288-
return getAbsolutePosition(section, row)
289-
}
290-
291298
override fun addCheckedFile(file: OCFile) {
292299
ocFileListDelegate.addCheckedFile(file)
293300
}
@@ -301,11 +308,12 @@ class GalleryAdapter(
301308
}
302309

303310
override fun notifyItemChanged(file: OCFile) {
304-
notifyItemChanged(getItemPosition(file))
311+
val position = getItemPosition(file)
312+
if (position >= 0) {
313+
notifyItemChanged(position)
314+
}
305315
}
306316

307-
override fun getFilesCount(): Int = cachedFilesCount
308-
309317
override fun setMultiSelect(boolean: Boolean) {
310318
ocFileListDelegate.isMultiSelect = boolean
311319
}
@@ -336,10 +344,8 @@ class GalleryAdapter(
336344
val allFiles = getAllFiles()
337345
allFiles.firstOrNull { it.remotePath == remotePath }?.also { file ->
338346
file.isFavorite = favorite
339-
Handler(Looper.getMainLooper()).post {
340-
files = allFiles.toGalleryItems()
341-
notifyItemChanged(file)
342-
}
347+
files = allFiles.toGalleryItems()
348+
notifyItemChanged(file)
343349
}
344350
}
345351

0 commit comments

Comments
 (0)