Skip to content
Merged
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 @@ -24,7 +24,6 @@ import com.owncloud.android.datamodel.OCFile
import com.owncloud.android.ui.activity.FileActivity
import com.owncloud.android.ui.activity.FileDisplayActivity
import com.owncloud.android.ui.dialog.ConfirmationDialogFragment.ConfirmationDialogFragmentListener
import com.owncloud.android.ui.preview.PreviewImageActivity
import javax.inject.Inject

/**
Expand Down Expand Up @@ -125,14 +124,11 @@ class RemoveFilesDialogFragment :
}

finishActionMode()
finishPreviewImageActivity()
}
}

override fun onNeutral(callerTag: String?) = Unit

private fun finishPreviewImageActivity() = getTypedActivity(PreviewImageActivity::class.java)?.finish()

private fun setActionMode(actionMode: ActionMode?) {
this.actionMode = actionMode
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import edu.umd.cs.findbugs.annotations.SuppressFBWarnings
import java.io.Serializable
import javax.inject.Inject
import kotlin.math.max
import kotlin.math.min

/**
* Holds a swiping gallery where image files contained in an Nextcloud directory are shown.
Expand Down Expand Up @@ -216,6 +217,17 @@ class PreviewImageActivity :
}
}

private fun updateViewPagerAfterDeletionAndAdvanceForward() {
val deletePosition = viewPager?.currentItem ?: return
previewImagePagerAdapter?.let { adapter ->
val nextPosition = min(deletePosition, adapter.itemCount - 1)
viewPager?.setCurrentItem(nextPosition, true)
adapter.delete(deletePosition)
// Page needs to be reselected after the adapter has been updated. Otherwise, wrong title is shown
selectPage(nextPosition)
}
}

private fun handleBackPress() {
onBackPressedDispatcher.addCallback(object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
Expand Down Expand Up @@ -285,22 +297,16 @@ class PreviewImageActivity :
super.onRemoteOperationFinish(operation, result)

if (operation is RemoveFileOperation) {
val deletePosition = viewPager?.currentItem ?: return
val nextPosition = if (deletePosition > 0) deletePosition - 1 else 0

previewImagePagerAdapter?.let {
if (it.itemCount <= 1) {
backToDisplayActivity()
return
}
}

if (user.isPresent) {
initViewPager(user.get())
if (result.isSuccess) {
updateViewPagerAfterDeletionAndAdvanceForward()
}

viewPager?.setCurrentItem(nextPosition, true)
previewImagePagerAdapter?.delete(deletePosition)
} else if (operation is SynchronizeFileOperation) {
onSynchronizeFileOperationFinish(result)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,4 +205,11 @@ class PreviewImagePagerAdapter : FragmentStateAdapter {
override fun createFragment(position: Int): Fragment = getItem(position)

override fun getItemCount(): Int = imageFiles.size

override fun getItemId(position: Int): Long {
// The item ID function is needed to detect whether the deletion of the current item needs a UI update
return imageFiles.getOrNull(position)?.fileId ?: position.toLong()
}

override fun containsItem(itemId: Long): Boolean = imageFiles.any { it.fileId == itemId }
}