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
51 changes: 42 additions & 9 deletions app/src/main/java/com/infomaniak/drive/ui/menu/GalleryAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,17 @@ class GalleryAdapter(
}
}

override fun onBindViewHolder(holder: ViewHolder, position: Int, payloads: List<Any>) {
val binding = (holder as GalleryViewHolder).binding
val file = itemList.getOrNull(position) as? File
if (payloads.isNotEmpty() && payloads.all { it == SELECTION_PAYLOAD } && binding is CardviewGalleryBinding && file != null) {
// Only the checkmark changed, so the thumbnail isn't reloaded
binding.handleCheckmark(file)
} else {
super.onBindViewHolder(holder, position, payloads)
}
}

private fun CardviewGalleryBinding.bindGalleryDisplayType(position: Int) {
val file = (itemList[position] as File)
displayThumbnail(file)
Expand Down Expand Up @@ -119,11 +130,15 @@ class GalleryAdapter(
private fun CardviewGalleryBinding.setupCardClicksListeners(file: File) = with(multiSelectManager) {

root.apply {
setOnClickListener { if (isMultiSelectOn) mediaChecked.onFileSelected(file) else onFileClicked(file) }
setOnClickListener {
if (isMultiSelectOn) mediaChecked.onFileSelected(file, !isSelectedFile(file)) else onFileClicked(file)
}

setOnLongClickListener {
if (isMultiSelectAuthorized) {
mediaChecked.onFileSelected(file)
// Select rather than toggle: the drag-select listener handles the same long press and may have
// selected the file already, and a toggle would then unselect it
mediaChecked.onFileSelected(file, isSelected = true)
if (!isMultiSelectOn) openMultiSelect?.invoke()
true
} else {
Expand All @@ -133,16 +148,17 @@ class GalleryAdapter(
}
}

private fun MaterialCheckBox.onFileSelected(file: File) = with(multiSelectManager) {
isChecked = !isChecked
private fun MaterialCheckBox.onFileSelected(file: File, isSelected: Boolean) {
setFileSelected(file, isSelected)
isChecked = multiSelectManager.isSelectedFile(file)
}

private fun setFileSelected(file: File, isSelected: Boolean) = with(multiSelectManager) {
if (file.isUsable()) {
if (isChecked) {
selectedItemsIds.add(file.id)
selectedItems.add(file)
if (isSelected) {
if (selectedItemsIds.add(file.id)) selectedItems.add(file)
} else {
selectedItemsIds.remove(file.id)
selectedItems.remove(file)
if (selectedItemsIds.remove(file.id)) selectedItems.remove(file)
}
} else {
resetSelectedItems()
Expand All @@ -151,6 +167,19 @@ class GalleryAdapter(
updateMultiSelect?.invoke()
}

fun isFileSelectableAt(position: Int): Boolean = (itemList.getOrNull(position) as? File)?.isUsable() == true

/** Section titles in the range are skipped, so [start] and [end] are adapter positions, not file indexes. */
fun setFilesSelectedInRange(start: Int, end: Int, isSelected: Boolean) {
for (position in start..end) {
val file = itemList.getOrNull(position) as? File ?: continue
if (!file.isUsable() || multiSelectManager.isSelectedFile(file) == isSelected) continue

setFileSelected(file, isSelected)
notifyItemChanged(position, SELECTION_PAYLOAD)
}
}

/**
* TODO: Move to viewModel
*/
Expand Down Expand Up @@ -284,4 +313,8 @@ class GalleryAdapter(
}

class GalleryViewHolder(val binding: ViewBinding) : ViewHolder(binding.root)

companion object {
private val SELECTION_PAYLOAD = Any()
}
}
32 changes: 32 additions & 0 deletions app/src/main/java/com/infomaniak/drive/ui/menu/GalleryFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import com.infomaniak.drive.databinding.FragmentGalleryBinding
import com.infomaniak.drive.databinding.FragmentMenuGalleryBinding
import com.infomaniak.drive.databinding.MultiSelectLayoutBinding
import com.infomaniak.drive.extensions.onApplyWindowInsetsListener
import com.infomaniak.drive.ui.fileList.DragSelectTouchListener
import com.infomaniak.drive.ui.fileList.multiSelect.MultiSelectFragment
import com.infomaniak.drive.utils.AccountUtils
import com.infomaniak.drive.utils.Utils
Expand Down Expand Up @@ -126,6 +127,7 @@ class GalleryFragment : MultiSelectFragment(
with(binding) {
galleryRecyclerView.adapter = galleryAdapter
configGalleryLayoutManager()
setupDragSelection()

noGalleryLayout.iNoItemsLayoutView = this@GalleryFragment

Expand Down Expand Up @@ -252,6 +254,36 @@ class GalleryFragment : MultiSelectFragment(
binding.galleryRecyclerView.layoutManager = gridLayoutManager
}

private fun setupDragSelection() {
val dragSelectTouchListener = DragSelectTouchListener(
recyclerView = binding.galleryRecyclerView,
callback = object : DragSelectTouchListener.DragSelectCallback {
override fun isMultiSelectAuthorized() = multiSelectManager.isMultiSelectAuthorized
override fun isMultiSelectOn() = multiSelectManager.isMultiSelectOn

override fun onDragSelectStarted(startPosition: Int) {
// openMultiSelect() also disables the swipe refresh, so it can't start while dragging
if (!multiSelectManager.isMultiSelectOn) openMultiSelect()
galleryAdapter.setFilesSelectedInRange(startPosition, startPosition, isSelected = true)
}

override fun onDragSelectChanged(start: Int, end: Int) {
galleryAdapter.setFilesSelectedInRange(start, end, isSelected = true)
}

override fun onDragSelectRangeDeselected(start: Int, end: Int) {
galleryAdapter.setFilesSelectedInRange(start, end, isSelected = false)
}

override fun onDragSelectFinished() = Unit

// Section titles share the list with the files, and can't be selected
override fun isPositionSelectable(position: Int) = galleryAdapter.isFileSelectableAt(position)
}
)
binding.galleryRecyclerView.addOnItemTouchListener(dragSelectTouchListener)
}

private fun loadGallery(driveId: Int, isRefresh: Boolean = false) {
galleryAdapter.apply {
if (isRefresh) clean()
Expand Down
Loading