refactor the MediaAdapter a bit

This commit is contained in:
tibbi 2017-05-13 23:28:45 +02:00
parent ffe7742334
commit ebc61660d4
3 changed files with 53 additions and 50 deletions

View file

@ -94,6 +94,11 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener {
config.temporarilyShowHidden = false config.temporarilyShowHidden = false
} }
override fun onDestroy() {
super.onDestroy()
MediaAdapter.cleanup()
}
private fun tryloadGallery() { private fun tryloadGallery() {
if (hasWriteStoragePermission()) { if (hasWriteStoragePermission()) {
val dirName = getHumanizedFilename(mPath) val dirName = getHumanizedFilename(mPath)

View file

@ -376,7 +376,6 @@ class DirectoryAdapter(val activity: SimpleActivity, val dirs: MutableList<Direc
if (!multiSelector.isSelectable) { if (!multiSelector.isSelectable) {
activity.startSupportActionMode(multiSelectorCallback) activity.startSupportActionMode(multiSelectorCallback)
toggleItemSelection(true, pos) toggleItemSelection(true, pos)
actMode?.invalidate()
} }
listener!!.itemLongClicked(pos) listener!!.itemLongClicked(pos)

View file

@ -31,21 +31,31 @@ class MediaAdapter(val activity: SimpleActivity, var media: MutableList<Medium>,
companion object { companion object {
var actMode: ActionMode? = null var actMode: ActionMode? = null
var displayFilenames = false var displayFilenames = false
val markedItems = HashSet<Int>()
var foregroundColor = 0 var foregroundColor = 0
var backgroundColor = 0 var backgroundColor = 0
var itemCnt = 0 var itemCnt = 0
var itemViews: HashMap<Int, View> = HashMap()
val selectedPositions = HashSet<Int>()
fun toggleItemSelection(itemView: View, select: Boolean, pos: Int = -1) { fun toggleItemSelection(select: Boolean, pos: Int) {
getProperView(itemView).isSelected = select if (itemViews[pos] != null)
getProperView(itemViews[pos]!!).isSelected = select
if (pos == -1) if (pos == -1)
return return
if (select) if (select)
markedItems.add(pos) selectedPositions.add(pos)
else else
markedItems.remove(pos) selectedPositions.remove(pos)
if (selectedPositions.isEmpty()) {
actMode?.finish()
return
}
updateTitle(selectedPositions.size)
actMode?.invalidate()
} }
fun getProperView(itemView: View): View { fun getProperView(itemView: View): View {
@ -58,6 +68,11 @@ class MediaAdapter(val activity: SimpleActivity, var media: MutableList<Medium>,
fun updateTitle(cnt: Int) { fun updateTitle(cnt: Int) {
actMode?.title = "$cnt / $itemCnt" actMode?.title = "$cnt / $itemCnt"
} }
fun cleanup() {
itemViews.clear()
selectedPositions.clear()
}
} }
init { init {
@ -92,26 +107,27 @@ class MediaAdapter(val activity: SimpleActivity, var media: MutableList<Medium>,
} }
override fun onPrepareActionMode(actionMode: ActionMode?, menu: Menu): Boolean { override fun onPrepareActionMode(actionMode: ActionMode?, menu: Menu): Boolean {
val positions = multiSelector.selectedPositions menu.findItem(R.id.cab_rename).isVisible = selectedPositions.size <= 1
menu.findItem(R.id.cab_rename).isVisible = positions.size <= 1 menu.findItem(R.id.cab_edit).isVisible = selectedPositions.size == 1 && media[selectedPositions.first()].isImage()
menu.findItem(R.id.cab_edit).isVisible = positions.size == 1 && media[positions[0]].isImage()
checkHideBtnVisibility(menu, positions) checkHideBtnVisibility(menu)
return true return true
} }
override fun onDestroyActionMode(actionMode: ActionMode?) { override fun onDestroyActionMode(actionMode: ActionMode?) {
super.onDestroyActionMode(actionMode) super.onDestroyActionMode(actionMode)
views.forEach { toggleItemSelection(it, false) } selectedPositions.forEach {
markedItems.clear() getProperView(itemViews[it]!!).isSelected = false
}
selectedPositions.clear()
actMode = null actMode = null
} }
fun checkHideBtnVisibility(menu: Menu, positions: List<Int>) { fun checkHideBtnVisibility(menu: Menu) {
var hiddenCnt = 0 var hiddenCnt = 0
var unhiddenCnt = 0 var unhiddenCnt = 0
positions.map { media[it] }.forEach { selectedPositions.map { media[it] }.forEach {
if (it.name.startsWith('.')) if (it.name.startsWith('.'))
hiddenCnt++ hiddenCnt++
else else
@ -124,12 +140,11 @@ class MediaAdapter(val activity: SimpleActivity, var media: MutableList<Medium>,
} }
private fun showProperties() { private fun showProperties() {
val selections = multiSelector.selectedPositions if (selectedPositions.size <= 1) {
if (selections.size <= 1) { PropertiesDialog(activity, media[selectedPositions.first()].path, config.shouldShowHidden)
PropertiesDialog(activity, media[selections[0]].path, config.shouldShowHidden)
} else { } else {
val paths = ArrayList<String>() val paths = ArrayList<String>()
selections.forEach { paths.add(media[it].path) } selectedPositions.forEach { paths.add(media[it].path) }
PropertiesDialog(activity, paths, config.shouldShowHidden) PropertiesDialog(activity, paths, config.shouldShowHidden)
} }
} }
@ -162,8 +177,7 @@ class MediaAdapter(val activity: SimpleActivity, var media: MutableList<Medium>,
} }
private fun shareMedia() { private fun shareMedia() {
val selections = multiSelector.selectedPositions if (selectedPositions.size <= 1) {
if (selections.size <= 1) {
activity.shareMedium(getSelectedMedia()[0]) activity.shareMedium(getSelectedMedia()[0])
} else { } else {
activity.shareMedia(getSelectedMedia()) activity.shareMedia(getSelectedMedia())
@ -172,8 +186,7 @@ class MediaAdapter(val activity: SimpleActivity, var media: MutableList<Medium>,
private fun copyMoveTo(isCopyOperation: Boolean) { private fun copyMoveTo(isCopyOperation: Boolean) {
val files = ArrayList<File>() val files = ArrayList<File>()
val positions = multiSelector.selectedPositions selectedPositions.forEach { files.add(File(media[it].path)) }
positions.forEach { files.add(File(media[it].path)) }
activity.tryCopyMoveFilesTo(files, isCopyOperation) { activity.tryCopyMoveFilesTo(files, isCopyOperation) {
if (!isCopyOperation) { if (!isCopyOperation) {
@ -186,7 +199,7 @@ class MediaAdapter(val activity: SimpleActivity, var media: MutableList<Medium>,
fun selectAll() { fun selectAll() {
val cnt = media.size val cnt = media.size
for (i in 0..cnt - 1) { for (i in 0..cnt - 1) {
markedItems.add(i) selectedPositions.add(i)
multiSelector.setSelected(i, 0, true) multiSelector.setSelected(i, 0, true)
notifyItemChanged(i) notifyItemChanged(i)
} }
@ -201,16 +214,14 @@ class MediaAdapter(val activity: SimpleActivity, var media: MutableList<Medium>,
} }
} }
private fun getCurrentFile() = File(media[multiSelector.selectedPositions[0]].path) private fun getCurrentFile() = File(media[selectedPositions.first()].path)
private fun deleteFiles() { private fun deleteFiles() {
val selections = multiSelector.selectedPositions val files = ArrayList<File>(selectedPositions.size)
val files = ArrayList<File>(selections.size) val removeMedia = ArrayList<Medium>(selectedPositions.size)
val removeMedia = ArrayList<Medium>(selections.size)
activity.handleSAFDialog(File(media[selections[0]].path)) { activity.handleSAFDialog(File(media[selectedPositions.first()].path)) {
selections.reverse() selectedPositions.reversed().forEach {
selections.forEach {
val medium = media[it] val medium = media[it]
files.add(File(medium.path)) files.add(File(medium.path))
removeMedia.add(medium) removeMedia.add(medium)
@ -218,16 +229,15 @@ class MediaAdapter(val activity: SimpleActivity, var media: MutableList<Medium>,
} }
media.removeAll(removeMedia) media.removeAll(removeMedia)
markedItems.clear() selectedPositions.clear()
listener?.deleteFiles(files) listener?.deleteFiles(files)
itemCnt = media.size itemCnt = media.size
} }
} }
private fun getSelectedMedia(): List<Medium> { private fun getSelectedMedia(): List<Medium> {
val positions = multiSelector.selectedPositions val selectedMedia = ArrayList<Medium>(selectedPositions.size)
val selectedMedia = ArrayList<Medium>(positions.size) selectedPositions.forEach { selectedMedia.add(media[it]) }
positions.forEach { selectedMedia.add(media[it]) }
return selectedMedia return selectedMedia
} }
@ -259,21 +269,19 @@ class MediaAdapter(val activity: SimpleActivity, var media: MutableList<Medium>,
class ViewHolder(val view: View, val itemClick: (Medium) -> (Unit)) : SwappingHolder(view, MultiSelector()) { class ViewHolder(val view: View, val itemClick: (Medium) -> (Unit)) : SwappingHolder(view, MultiSelector()) {
fun bindView(activity: SimpleActivity, multiSelectorCallback: ModalMultiSelectorCallback, multiSelector: MultiSelector, medium: Medium, pos: Int): View { fun bindView(activity: SimpleActivity, multiSelectorCallback: ModalMultiSelectorCallback, multiSelector: MultiSelector, medium: Medium, pos: Int): View {
itemViews.put(pos, itemView)
itemView.apply { itemView.apply {
play_outline.visibility = if (medium.video) View.VISIBLE else View.GONE play_outline.visibility = if (medium.video) View.VISIBLE else View.GONE
photo_name.beVisibleIf(displayFilenames) photo_name.beVisibleIf(displayFilenames)
photo_name.text = medium.name photo_name.text = medium.name
toggleItemSelection(this, markedItems.contains(pos), pos) toggleItemSelection(selectedPositions.contains(pos), pos)
activity.loadImage(medium.path, medium_thumbnail) activity.loadImage(medium.path, medium_thumbnail)
setOnClickListener { viewClicked(multiSelector, medium, pos) } setOnClickListener { viewClicked(multiSelector, medium, pos) }
setOnLongClickListener { setOnLongClickListener {
if (!multiSelector.isSelectable) { if (!multiSelector.isSelectable) {
activity.startSupportActionMode(multiSelectorCallback) activity.startSupportActionMode(multiSelectorCallback)
multiSelector.setSelected(this@ViewHolder, true) toggleItemSelection(true, pos)
updateTitle(multiSelector.selectedPositions.size)
toggleItemSelection(this, true, pos)
actMode?.invalidate()
} }
true true
} }
@ -288,17 +296,8 @@ class MediaAdapter(val activity: SimpleActivity, var media: MutableList<Medium>,
fun viewClicked(multiSelector: MultiSelector, medium: Medium, pos: Int) { fun viewClicked(multiSelector: MultiSelector, medium: Medium, pos: Int) {
if (multiSelector.isSelectable) { if (multiSelector.isSelectable) {
val isSelected = multiSelector.selectedPositions.contains(layoutPosition) val isSelected = selectedPositions.contains(layoutPosition)
multiSelector.setSelected(this, !isSelected) toggleItemSelection(!isSelected, pos)
toggleItemSelection(itemView, !isSelected, pos)
val selectedCnt = multiSelector.selectedPositions.size
if (selectedCnt == 0) {
actMode?.finish()
} else {
updateTitle(selectedCnt)
}
actMode?.invalidate()
} else { } else {
itemClick(medium) itemClick(medium)
} }