Skip to content

Commit a93ea4d

Browse files
committed
fix: deletion from slideshow for cases were user is not present
In that case, deleting the currently viewed file had the following issues: 1. When deleting the first image, the UI just stayed with it. 2. When deleting another image, the UI switched to the previous image, instead of the next one, which is inconsistent to the behavior when user is available 3. When switching to the next image, the title shown at the top did not correctly update Signed-off-by: Philipp Hasper <[email protected]>
1 parent 3b2a40c commit a93ea4d

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

app/src/main/java/com/owncloud/android/ui/preview/PreviewImageActivity.kt

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ import edu.umd.cs.findbugs.annotations.SuppressFBWarnings
5555
import java.io.Serializable
5656
import javax.inject.Inject
5757
import kotlin.math.max
58+
import kotlin.math.min
5859

5960
/**
6061
* Holds a swiping gallery where image files contained in an Nextcloud directory are shown.
@@ -287,12 +288,19 @@ class PreviewImageActivity :
287288
}
288289

289290
if (user.isPresent) {
291+
// Re-init the view pager, which will advance to the next image
290292
initViewPager(user.get())
291-
} else {
293+
} else if (result.isSuccess) {
294+
// If deletion was successful, update adapter and display next image
292295
val deletePosition = viewPager?.currentItem ?: return
293-
val nextPosition = if (deletePosition > 0) deletePosition - 1 else 0
294-
viewPager?.setCurrentItem(nextPosition, true)
295-
previewImagePagerAdapter?.delete(deletePosition)
296+
previewImagePagerAdapter?.let { adapter ->
297+
// advance to the next image if possible, since initViewPager() also advances forwards
298+
val nextPosition = min(deletePosition, adapter.itemCount - 1)
299+
viewPager?.setCurrentItem(nextPosition, true)
300+
adapter.delete(deletePosition)
301+
// Page needs to be reselected after the adapter has been updated. Otherwise, wrong title is shown
302+
selectPage(nextPosition)
303+
}
296304
}
297305
} else if (operation is SynchronizeFileOperation) {
298306
onSynchronizeFileOperationFinish(result)

app/src/main/java/com/owncloud/android/ui/preview/PreviewImagePagerAdapter.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,4 +201,13 @@ class PreviewImagePagerAdapter : FragmentStateAdapter {
201201
override fun createFragment(position: Int): Fragment = getItem(position)
202202

203203
override fun getItemCount(): Int = imageFiles.size
204+
205+
override fun getItemId(position: Int): Long {
206+
// Use the OCFile id, fallback to position if not available
207+
return imageFiles.getOrNull(position)?.fileId ?: position.toLong()
208+
}
209+
210+
override fun containsItem(itemId: Long): Boolean {
211+
return imageFiles.any { it.fileId == itemId }
212+
}
204213
}

0 commit comments

Comments
 (0)