-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3508 from owncloud/new_arch/multiselection_contro…
…l_selection Selected items controlled and hide/show FAB button
- Loading branch information
Showing
3 changed files
with
241 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
...pp/src/main/java/com/owncloud/android/presentation/adapters/filelist/SelectableAdapter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/** | ||
* ownCloud Android client application | ||
* | ||
* @author Fernando Sanz Velasco | ||
* Copyright (C) 2022 ownCloud GmbH. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License version 2, | ||
* as published by the Free Software Foundation. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
package com.owncloud.android.presentation.adapters.filelist | ||
|
||
import android.util.SparseBooleanArray | ||
import androidx.recyclerview.widget.RecyclerView | ||
|
||
abstract class SelectableAdapter<VH : RecyclerView.ViewHolder?> : | ||
RecyclerView.Adapter<VH>() { | ||
private val selectedItems: SparseBooleanArray = SparseBooleanArray() | ||
|
||
/** | ||
* Indicates if the item at position position is selected | ||
* @param position Position of the item to check | ||
* @return true if the item is selected, false otherwise | ||
*/ | ||
fun isSelected(position: Int): Boolean { | ||
return getSelectedItems().contains(position) | ||
} | ||
|
||
/** | ||
* Toggle the selection status of the item at a given position | ||
* @param position Position of the item to toggle the selection status for | ||
*/ | ||
fun toggleSelection(position: Int) { | ||
if (selectedItems[position, false]) { | ||
selectedItems.delete(position) | ||
} else { | ||
selectedItems.put(position, true) | ||
} | ||
notifyItemChanged(position) | ||
} | ||
|
||
/** | ||
* Clear the selection status for all items | ||
*/ | ||
fun clearSelection() { | ||
val selection = getSelectedItems() | ||
selectedItems.clear() | ||
for (i in selection) { | ||
notifyItemChanged(i) | ||
} | ||
} | ||
|
||
/** | ||
* Count the selected items | ||
* @return Selected items count | ||
*/ | ||
val selectedItemCount: Int | ||
get() = selectedItems.size() | ||
|
||
/** | ||
* Indicates the list of selected items | ||
* @return List of selected items ids | ||
*/ | ||
fun getSelectedItems(): List<Int> { | ||
val items: MutableList<Int> = ArrayList(selectedItems.size()) | ||
for (i in 0 until selectedItems.size()) { | ||
items.add(selectedItems.keyAt(i)) | ||
} | ||
return items | ||
} | ||
|
||
companion object { | ||
private val TAG = SelectableAdapter::class.java.simpleName | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters