Show total number of selected items

This commit is contained in:
Matéo Duparc 2022-04-17 15:52:34 +02:00
parent 339309b00d
commit f1d4b07726
Signed by untrusted user: hardcoresushi
GPG Key ID: AFE384344A45E13A
8 changed files with 60 additions and 26 deletions

View File

@ -35,7 +35,7 @@ import java.io.File
import java.util.*
import kotlin.NoSuchElementException
class MainActivity : BaseActivity() {
class MainActivity : BaseActivity(), VolumeAdapter.Listener {
companion object {
const val DEFAULT_VOLUME_KEY = "default_volume"
@ -101,8 +101,7 @@ class MainActivity : BaseActivity() {
volumeDatabase,
!pickMode && !dropMode,
!dropMode,
::onVolumeItemClick,
::onVolumeItemLongClick,
this,
)
binding.recyclerViewVolumes.adapter = volumeAdapter
binding.recyclerViewVolumes.layoutManager = LinearLayoutManager(this)
@ -147,14 +146,22 @@ class MainActivity : BaseActivity() {
defaultVolumeName = sharedPrefs.getString(DEFAULT_VOLUME_KEY, null)
}
private fun onVolumeItemClick(volume: Volume, position: Int) {
override fun onSelectionChanged(size: Int) {
title = if (size == 0) {
getString(R.string.app_name)
} else {
getString(R.string.elements_selected, size, volumeAdapter.volumes.size)
}
}
override fun onVolumeItemClick(volume: Volume, position: Int) {
if (volumeAdapter.selectedItems.isEmpty())
openVolume(volume, position)
else
invalidateOptionsMenu()
}
private fun onVolumeItemLongClick() {
override fun onVolumeItemLongClick() {
invalidateOptionsMenu()
}
@ -174,6 +181,7 @@ class MainActivity : BaseActivity() {
private fun unselect(position: Int) {
volumeAdapter.selectedItems.remove(position)
volumeAdapter.onVolumeChanged(position)
onSelectionChanged(0) // unselect() is always called when only one element is selected
invalidateOptionsMenu()
}

View File

@ -26,10 +26,9 @@ import java.util.*
class ExplorerElementAdapter(
val activity: AppCompatActivity,
val gocryptfsVolume: GocryptfsVolume?,
val onExplorerElementClick: (Int) -> Unit,
val onExplorerElementLongClick: (Int) -> Unit,
private val listener: Listener,
val thumbnailMaxSize: Long,
) : SelectableAdapter<ExplorerElement>() {
) : SelectableAdapter<ExplorerElement>(listener::onSelectionChanged) {
val dateFormat: DateFormat = DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT, Locale.getDefault())
var explorerElements = listOf<ExplorerElement>()
@SuppressLint("NotifyDataSetChanged")
@ -47,6 +46,12 @@ class ExplorerElementAdapter(
}
}
interface Listener {
fun onSelectionChanged(size: Int)
fun onExplorerElementClick(position: Int)
fun onExplorerElementLongClick(position: Int)
}
override fun getItems(): List<ExplorerElement> {
return explorerElements
}
@ -60,12 +65,12 @@ class ExplorerElementAdapter(
}
override fun onItemClick(position: Int): Boolean {
onExplorerElementClick(position)
listener.onExplorerElementClick(position)
return super.onItemClick(position)
}
override fun onItemLongClick(position: Int): Boolean {
onExplorerElementLongClick(position)
listener.onExplorerElementLongClick(position)
return super.onItemLongClick(position)
}

View File

@ -4,7 +4,7 @@ import android.view.View
import androidx.recyclerview.widget.RecyclerView
import sushi.hardcore.droidfs.R
abstract class SelectableAdapter<T> : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
abstract class SelectableAdapter<T>(private val onSelectionChanged: (Int) -> Unit) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
var selectedItems: MutableSet<Int> = HashSet()
protected abstract fun getItems(): List<T>
@ -14,13 +14,15 @@ abstract class SelectableAdapter<T> : RecyclerView.Adapter<RecyclerView.ViewHold
}
protected open fun toggleSelection(position: Int): Boolean {
return if (selectedItems.contains(position)) {
val isSelected = if (selectedItems.contains(position)) {
selectedItems.remove(position)
false
} else {
selectedItems.add(position)
true
}
onSelectionChanged(selectedItems.size)
return isSelected
}
protected open fun onItemClick(position: Int): Boolean {
@ -45,6 +47,7 @@ abstract class SelectableAdapter<T> : RecyclerView.Adapter<RecyclerView.ViewHold
notifyItemChanged(i)
}
}
onSelectionChanged(selectedItems.size)
}
fun unSelectAll(notifyChange: Boolean) {
@ -57,6 +60,7 @@ abstract class SelectableAdapter<T> : RecyclerView.Adapter<RecyclerView.ViewHold
} else {
selectedItems.clear()
}
onSelectionChanged(selectedItems.size)
}
private fun setBackground(rootView: View, isSelected: Boolean) {

View File

@ -17,9 +17,8 @@ class VolumeAdapter(
private val volumeDatabase: VolumeDatabase,
private val allowSelection: Boolean,
private val showReadOnly: Boolean,
private val onVolumeItemClick: (Volume, Int) -> Unit,
private val onVolumeItemLongClick: () -> Unit,
) : SelectableAdapter<Volume>() {
private val listener: Listener,
) : SelectableAdapter<Volume>(listener::onSelectionChanged) {
private val inflater: LayoutInflater = LayoutInflater.from(context)
lateinit var volumes: List<Volume>
@ -27,6 +26,12 @@ class VolumeAdapter(
reloadVolumes()
}
interface Listener {
fun onSelectionChanged(size: Int)
fun onVolumeItemClick(volume: Volume, position: Int)
fun onVolumeItemLongClick()
}
override fun getItems(): List<Volume> {
return volumes
}
@ -40,7 +45,7 @@ class VolumeAdapter(
}
override fun onItemClick(position: Int): Boolean {
onVolumeItemClick(volumes[position], position)
listener.onVolumeItemClick(volumes[position], position)
return if (allowSelection) {
super.onItemClick(position)
} else {
@ -49,7 +54,7 @@ class VolumeAdapter(
}
override fun onItemLongClick(position: Int): Boolean {
onVolumeItemLongClick()
listener.onVolumeItemLongClick()
return if (allowSelection)
super.onItemLongClick(position)
else

View File

@ -41,7 +41,7 @@ import sushi.hardcore.droidfs.util.PathUtils
import sushi.hardcore.droidfs.widgets.CustomAlertDialogBuilder
import sushi.hardcore.droidfs.widgets.EditTextDialog
open class BaseExplorerActivity : BaseActivity() {
open class BaseExplorerActivity : BaseActivity(), ExplorerElementAdapter.Listener {
private lateinit var sortOrderEntries: Array<String>
private lateinit var sortOrderValues: Array<String>
private var foldersFirst = true
@ -100,7 +100,7 @@ open class BaseExplorerActivity : BaseActivity() {
titleText = customView.findViewById(R.id.title_text)
}
title = ""
titleText.text = getString(R.string.volume, volumeName)
setVolumeNameTitle()
explorerAdapter = ExplorerElementAdapter(
this,
if (sharedPrefs.getBoolean("thumbnails", true)) {
@ -108,8 +108,7 @@ open class BaseExplorerActivity : BaseActivity() {
} else {
null
},
::onExplorerItemClick,
::onExplorerItemLongClick,
this,
sharedPrefs.getLong(ConstValues.THUMBNAIL_MAX_SIZE_KEY, ConstValues.DEFAULT_THUMBNAIL_MAX_SIZE)*1000,
)
explorerViewModel = ViewModelProvider(this).get(ExplorerViewModel::class.java)
@ -209,7 +208,19 @@ open class BaseExplorerActivity : BaseActivity() {
.show()
}
protected open fun onExplorerItemClick(position: Int) {
private fun setVolumeNameTitle() {
titleText.text = getString(R.string.volume, volumeName)
}
override fun onSelectionChanged(size: Int) {
if (size == 0) {
setVolumeNameTitle()
} else {
titleText.text = getString(R.string.elements_selected, size, explorerElements.count { !it.isParentFolder })
}
}
override fun onExplorerElementClick(position: Int) {
if (explorerAdapter.selectedItems.isEmpty()) {
val fullPath = explorerElements[position].fullPath
when {
@ -240,7 +251,7 @@ open class BaseExplorerActivity : BaseActivity() {
invalidateOptionsMenu()
}
protected open fun onExplorerItemLongClick(position: Int) {
override fun onExplorerElementLongClick(position: Int) {
invalidateOptionsMenu()
}

View File

@ -217,8 +217,8 @@ class ExplorerActivity : BaseExplorerActivity() {
usf_share = sharedPrefs.getBoolean("usf_share", false)
}
override fun onExplorerItemLongClick(position: Int) {
super.onExplorerItemLongClick(position)
override fun onExplorerElementLongClick(position: Int) {
super.onExplorerElementLongClick(position)
cancelItemAction()
}

View File

@ -21,7 +21,7 @@ class ExplorerActivityPick : BaseExplorerActivity() {
//don't bind
}
override fun onExplorerItemClick(position: Int) {
override fun onExplorerElementClick(position: Int) {
val wasSelecting = explorerAdapter.selectedItems.isNotEmpty()
if (explorerAdapter.selectedItems.isEmpty()) {
if (!wasSelecting) {

View File

@ -239,4 +239,5 @@
<string name="multiple_folders">%d folders</string>
<string name="default_open">Open this volume when launching the application</string>
<string name="remove_default_open">Don\'t open by default</string>
<string name="elements_selected">%d/%d selected</string>
</resources>