Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ public class LocalFileListAdapter extends RecyclerView.Adapter<RecyclerView.View
private static final int PAGE_SIZE = 50;
private int currentOffset = 0;

private static final long HEADER_ID = Long.MIN_VALUE;
private static final long FOOTER_ID = Long.MIN_VALUE + 1;

public LocalFileListAdapter(boolean localFolderPickerMode,
LocalFileListFragmentInterface localFileListFragmentInterface,
AppPreferences preferences,
Expand Down Expand Up @@ -153,12 +156,22 @@ public int getItemCount() {

@Override
public long getItemId(int position) {
if (position >= mFiles.size()) {
return RecyclerView.NO_ID;
int headerOffset = shouldShowHeader() ? 1 : 0;
return getStableItemId(position, headerOffset, mFiles);
}

@VisibleForTesting
static long getStableItemId(int position, int headerOffset, List<File> files) {
if (headerOffset == 1 && position == 0) {
return HEADER_ID;
}

int fileIndex = position - headerOffset;
if (fileIndex < 0 || fileIndex >= files.size()) {
return FOOTER_ID;
}

File file = mFiles.get(position);
return file.getAbsolutePath().hashCode();
return files.get(fileIndex).getAbsolutePath().hashCode();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Nextcloud - Android Client
*
* SPDX-FileCopyrightText: 2026 Alper Ozturk <alper.ozturk@nextcloud.com>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
package com.owncloud.android.ui.adapter

import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Test
import java.io.File

class LocalFileListAdapterTest {

private fun sampleFiles(): List<File> = listOf(File("/sdcard/a.jpg"), File("/sdcard/b.png"), File("/sdcard/c.mp4"))

private fun assertAllPositionsHaveUniqueIds(headerOffset: Int, files: List<File>) {
val itemCount = files.size + 1 + headerOffset

val ids = mutableSetOf<Long>()
for (position in 0 until itemCount) {
val id = LocalFileListAdapter.getStableItemId(position, headerOffset, files)
assertTrue("Duplicate stable ID $id at position $position", ids.add(id))
}
assertEquals("Every position must produce a distinct stable ID", itemCount, ids.size)
}

@Test
fun stableIdsAreUniqueWhenHeaderIsVisible() {
assertAllPositionsHaveUniqueIds(headerOffset = 1, files = sampleFiles())
}

@Test
fun stableIdsAreUniqueWhenHeaderIsHidden() {
assertAllPositionsHaveUniqueIds(headerOffset = 0, files = sampleFiles())
}

@Test
fun headerAndFooterKeepDedicatedIds() {
val files = sampleFiles()

val headerId = LocalFileListAdapter.getStableItemId(0, 1, files)
val footerId = LocalFileListAdapter.getStableItemId(files.size + 1, 1, files)

assertEquals(Long.MIN_VALUE, headerId)
assertEquals(Long.MIN_VALUE + 1, footerId)
}
}
Loading