rewriting MediaAdapter to the Commons MyRecyclerViewAdapter

This commit is contained in:
tibbi 2017-11-16 14:44:39 +01:00
parent ee883d3acc
commit 6f78a6e9ce
6 changed files with 124 additions and 261 deletions

View file

@ -47,7 +47,7 @@ ext {
}
dependencies {
compile 'com.simplemobiletools:commons:2.39.7'
compile 'com.simplemobiletools:commons:2.39.8'
compile 'com.davemorrissey.labs:subsampling-scale-image-view:3.7.2'
compile 'com.theartofdev.edmodo:android-image-cropper:2.4.0'
compile 'com.android.support:multidex:1.0.2'

View file

@ -340,8 +340,11 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener {
} else {
setupListLayoutManager()
}
getDirectoryAdapter()?.setupZoomListener(mZoomListener)
getDirectoryAdapter()?.setupDragListener(true)
getDirectoryAdapter()?.apply {
setupZoomListener(mZoomListener)
setupDragListener(true)
}
}
private fun setupGridLayoutManager() {

View file

@ -23,6 +23,7 @@ import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.PERMISSION_WRITE_STORAGE
import com.simplemobiletools.commons.helpers.REQUEST_EDIT_IMAGE
import com.simplemobiletools.commons.models.RadioItem
import com.simplemobiletools.commons.views.MyRecyclerView
import com.simplemobiletools.gallery.R
import com.simplemobiletools.gallery.adapters.MediaAdapter
import com.simplemobiletools.gallery.asynctasks.GetMediaAsynctask
@ -57,6 +58,7 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener {
private var mLatestMediaId = 0L
private var mLastMediaHandler = Handler()
private var mCurrAsyncTask: GetMediaAsynctask? = null
private var mZoomListener: MyRecyclerView.MyZoomListener? = null
companion object {
var mMedia = ArrayList<Medium>()
@ -174,8 +176,8 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener {
val currAdapter = media_grid.adapter
if (currAdapter == null) {
media_grid.adapter = MediaAdapter(this, mMedia, this, mIsGetImageIntent || mIsGetVideoIntent || mIsGetAnyIntent, mAllowPickingMultiple) {
itemClicked(it.path)
media_grid.adapter = MediaAdapter(this, mMedia, this, mIsGetImageIntent || mIsGetVideoIntent || mIsGetAnyIntent, mAllowPickingMultiple, media_grid) {
itemClicked((it as Medium).path)
}
} else {
(currAdapter as MediaAdapter).updateMedia(mMedia)
@ -395,10 +397,16 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener {
private fun getRecyclerAdapter() = (media_grid.adapter as MediaAdapter)
private fun setupLayoutManager() {
if (config.viewTypeFiles == VIEW_TYPE_GRID)
if (config.viewTypeFiles == VIEW_TYPE_GRID) {
setupGridLayoutManager()
else
} else {
setupListLayoutManager()
}
getMediaAdapter()?.apply {
setupZoomListener(mZoomListener)
setupDragListener(true)
}
}
private fun setupGridLayoutManager() {
@ -411,42 +419,30 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener {
media_refresh_layout.layoutParams = FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
}
/*media_grid.isDragSelectionEnabled = true
media_grid.isZoomingEnabled = true
layoutManager.spanCount = config.mediaColumnCnt
media_grid.listener = object : MyScalableRecyclerView.MyScalableRecyclerViewListener {
mZoomListener = object : MyRecyclerView.MyZoomListener {
override fun zoomIn() {
if (layoutManager.spanCount > 1) {
reduceColumnCount()
getRecyclerAdapter().actMode?.finish()
getRecyclerAdapter().finishActMode()
}
}
override fun zoomOut() {
if (layoutManager.spanCount < MAX_COLUMN_COUNT) {
increaseColumnCount()
getRecyclerAdapter().actMode?.finish()
getRecyclerAdapter().finishActMode()
}
}
override fun selectItem(position: Int) {
getRecyclerAdapter().selectItem(position)
}
override fun selectRange(initialSelection: Int, lastDraggedIndex: Int, minReached: Int, maxReached: Int) {
getRecyclerAdapter().selectRange(initialSelection, lastDraggedIndex, minReached, maxReached)
}
}*/
}
}
private fun setupListLayoutManager() {
//media_grid.isDragSelectionEnabled = true
//media_grid.isZoomingEnabled = false
val layoutManager = media_grid.layoutManager as GridLayoutManager
layoutManager.spanCount = 1
layoutManager.orientation = GridLayoutManager.VERTICAL
media_refresh_layout.layoutParams = FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
mZoomListener = null
}
private fun increaseColumnCount() {
@ -572,10 +568,6 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener {
}, 1000)
}
override fun itemLongClicked(position: Int) {
media_grid.setDragSelectActive(position)
}
override fun selectedPaths(paths: ArrayList<String>) {
Intent().apply {
putExtra(PICKED_PATHS, paths)

View file

@ -58,7 +58,7 @@ class DirectoryAdapter(activity: BaseSimpleActivity, var dirs: MutableList<Direc
override fun onBindViewHolder(holder: MyRecyclerViewAdapter.ViewHolder, position: Int) {
val dir = dirs[position]
val view = holder.bindView(dir, isPickIntent) {
val view = holder.bindView(dir, !isPickIntent) {
setupView(it, dir)
}
itemViews.put(position, view)
@ -87,11 +87,13 @@ class DirectoryAdapter(activity: BaseSimpleActivity, var dirs: MutableList<Direc
}
override fun prepareActionMode(menu: Menu) {
menu.findItem(R.id.cab_rename).isVisible = selectedPositions.size == 1
menu.findItem(R.id.cab_change_cover_image).isVisible = selectedPositions.size == 1
menu.apply {
findItem(R.id.cab_rename).isVisible = selectedPositions.size == 1
findItem(R.id.cab_change_cover_image).isVisible = selectedPositions.size == 1
checkHideBtnVisibility(menu)
checkPinBtnVisibility(menu)
checkHideBtnVisibility(this)
checkPinBtnVisibility(this)
}
}
private fun checkHideBtnVisibility(menu: Menu) {

View file

@ -2,22 +2,19 @@ package com.simplemobiletools.gallery.adapters
import android.graphics.PorterDuff
import android.net.Uri
import android.support.v7.view.ActionMode
import android.support.v7.widget.RecyclerView
import android.util.SparseArray
import android.view.*
import com.bignerdranch.android.multiselector.ModalMultiSelectorCallback
import com.bignerdranch.android.multiselector.MultiSelector
import com.bignerdranch.android.multiselector.SwappingHolder
import android.view.Menu
import android.view.View
import android.view.ViewGroup
import com.bumptech.glide.Glide
import com.simplemobiletools.commons.activities.BaseSimpleActivity
import com.simplemobiletools.commons.adapters.MyRecyclerViewAdapter
import com.simplemobiletools.commons.dialogs.PropertiesDialog
import com.simplemobiletools.commons.dialogs.RenameItemDialog
import com.simplemobiletools.commons.extensions.applyColorFilter
import com.simplemobiletools.commons.extensions.beGone
import com.simplemobiletools.commons.extensions.beVisibleIf
import com.simplemobiletools.commons.extensions.isActivityDestroyed
import com.simplemobiletools.commons.interfaces.MyAdapterListener
import com.simplemobiletools.commons.views.MyRecyclerView
import com.simplemobiletools.gallery.R
import com.simplemobiletools.gallery.dialogs.DeleteWithRememberDialog
import com.simplemobiletools.gallery.extensions.*
@ -27,121 +24,91 @@ import kotlinx.android.synthetic.main.photo_video_item_grid.view.*
import java.io.File
import java.util.*
class MediaAdapter(val activity: BaseSimpleActivity, var media: MutableList<Medium>, val listener: MediaOperationsListener?, val isAGetIntent: Boolean,
val allowMultiplePicks: Boolean, val itemClick: (Medium) -> Unit) : RecyclerView.Adapter<MediaAdapter.ViewHolder>() {
class MediaAdapter(activity: BaseSimpleActivity, var media: MutableList<Medium>, val listener: MediaOperationsListener?, val isAGetIntent: Boolean,
val allowMultiplePicks: Boolean, recyclerView: MyRecyclerView, itemClick: (Any) -> Unit) : MyRecyclerViewAdapter(activity, recyclerView, itemClick) {
private val config = activity.config
var actMode: ActionMode? = null
var primaryColor = config.primaryColor
private val multiSelector = MultiSelector()
private val isListViewType = config.viewTypeFiles == VIEW_TYPE_LIST
private var skipConfirmationDialog = false
private var itemViews = SparseArray<View>()
private val selectedPositions = HashSet<Int>()
private var scrollHorizontally = config.scrollHorizontally
private var animateGifs = config.animateGifs
private var cropThumbnails = config.cropThumbnails
private var textColor = config.textColor
private var displayFilenames = config.displayFileNames
fun toggleItemSelection(select: Boolean, pos: Int) {
if (select) {
if (itemViews[pos] != null) {
itemViews[pos].medium_check?.background?.applyColorFilter(primaryColor)
selectedPositions.add(pos)
}
} else {
selectedPositions.remove(pos)
}
itemViews[pos]?.medium_check?.beVisibleIf(select)
if (selectedPositions.isEmpty()) {
actMode?.finish()
return
}
updateTitle(selectedPositions.size)
init {
selectableItemCount = media.count()
}
private fun updateTitle(cnt: Int) {
actMode?.title = "$cnt / ${media.size}"
actMode?.invalidate()
override fun getActionMenuId() = R.menu.cab_media
override fun prepareItemSelection(view: View) {
view.medium_check?.background?.applyColorFilter(primaryColor)
}
private val adapterListener = object : MyAdapterListener {
override fun toggleItemSelectionAdapter(select: Boolean, position: Int) {
toggleItemSelection(select, position)
}
override fun getSelectedPositions(): HashSet<Int> = selectedPositions
override fun itemLongClicked(position: Int) {}
override fun markItemSelection(select: Boolean, view: View?) {
view?.medium_check?.beVisibleIf(select)
}
private val multiSelectorMode = object : ModalMultiSelectorCallback(multiSelector) {
override fun onActionItemClicked(mode: ActionMode, item: MenuItem): Boolean {
when (item.itemId) {
R.id.cab_confirm_selection -> confirmSelection()
R.id.cab_properties -> showProperties()
R.id.cab_rename -> renameFile()
R.id.cab_edit -> editFile()
R.id.cab_hide -> toggleFileVisibility(true)
R.id.cab_unhide -> toggleFileVisibility(false)
R.id.cab_share -> shareMedia()
R.id.cab_copy_to -> copyMoveTo(true)
R.id.cab_move_to -> copyMoveTo(false)
R.id.cab_select_all -> selectAll()
R.id.cab_open_with -> activity.openFile(Uri.fromFile(getCurrentFile()), true)
R.id.cab_set_as -> activity.setAs(Uri.fromFile(getCurrentFile()))
R.id.cab_delete -> checkDeleteConfirmation()
else -> return false
override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ViewHolder {
val layoutType = if (isListViewType) R.layout.photo_video_item_list else R.layout.photo_video_item_grid
val view = activity.layoutInflater.inflate(layoutType, parent, false)
return createViewHolder(view)
}
override fun onBindViewHolder(holder: MyRecyclerViewAdapter.ViewHolder, position: Int) {
val medium = media[position]
val view = holder.bindView(medium, !allowMultiplePicks) {
setupView(it, medium)
}
itemViews.put(position, view)
toggleItemSelection(selectedPositions.contains(position), position)
holder.itemView.tag = holder
}
override fun getItemCount() = media.size
override fun actionItemPressed(id: Int) {
when (id) {
R.id.cab_confirm_selection -> confirmSelection()
R.id.cab_properties -> showProperties()
R.id.cab_rename -> renameFile()
R.id.cab_edit -> editFile()
R.id.cab_hide -> toggleFileVisibility(true)
R.id.cab_unhide -> toggleFileVisibility(false)
R.id.cab_share -> shareMedia()
R.id.cab_copy_to -> copyMoveTo(true)
R.id.cab_move_to -> copyMoveTo(false)
R.id.cab_select_all -> selectAll()
R.id.cab_open_with -> activity.openFile(Uri.fromFile(getCurrentFile()), true)
R.id.cab_set_as -> activity.setAs(Uri.fromFile(getCurrentFile()))
R.id.cab_delete -> checkDeleteConfirmation()
}
}
override fun prepareActionMode(menu: Menu) {
menu.apply {
findItem(R.id.cab_rename).isVisible = selectedPositions.size == 1
findItem(R.id.cab_open_with).isVisible = selectedPositions.size == 1
findItem(R.id.cab_confirm_selection).isVisible = isAGetIntent && allowMultiplePicks && selectedPositions.size > 0
checkHideBtnVisibility(this)
}
}
private fun checkHideBtnVisibility(menu: Menu) {
var hiddenCnt = 0
var unhiddenCnt = 0
selectedPositions.mapNotNull { media.getOrNull(it) }.forEach {
if (it.name.startsWith('.')) {
hiddenCnt++
} else {
unhiddenCnt++
}
return true
}
override fun onCreateActionMode(actionMode: ActionMode?, menu: Menu?): Boolean {
super.onCreateActionMode(actionMode, menu)
actMode = actionMode
activity.menuInflater.inflate(R.menu.cab_media, menu)
return true
}
override fun onPrepareActionMode(actionMode: ActionMode?, menu: Menu): Boolean {
menu.findItem(R.id.cab_rename).isVisible = selectedPositions.size == 1
menu.findItem(R.id.cab_open_with).isVisible = selectedPositions.size == 1
menu.findItem(R.id.cab_confirm_selection).isVisible = isAGetIntent && allowMultiplePicks && selectedPositions.size > 0
checkHideBtnVisibility(menu)
return true
}
override fun onDestroyActionMode(actionMode: ActionMode?) {
super.onDestroyActionMode(actionMode)
selectedPositions.forEach {
itemViews[it]?.medium_check?.beGone()
}
selectedPositions.clear()
actMode = null
}
fun checkHideBtnVisibility(menu: Menu) {
var hiddenCnt = 0
var unhiddenCnt = 0
selectedPositions.mapNotNull { media.getOrNull(it) }.forEach {
if (it.name.startsWith('.')) {
hiddenCnt++
} else {
unhiddenCnt++
}
}
menu.findItem(R.id.cab_hide).isVisible = unhiddenCnt > 0
menu.findItem(R.id.cab_unhide).isVisible = hiddenCnt > 0
}
menu.findItem(R.id.cab_hide).isVisible = unhiddenCnt > 0
menu.findItem(R.id.cab_unhide).isVisible = hiddenCnt > 0
}
private fun confirmSelection() {
@ -163,14 +130,14 @@ class MediaAdapter(val activity: BaseSimpleActivity, var media: MutableList<Medi
RenameItemDialog(activity, getCurrentFile().absolutePath) {
activity.runOnUiThread {
listener?.refreshItems()
actMode?.finish()
finishActMode()
}
}
}
private fun editFile() {
activity.openEditor(Uri.fromFile(getCurrentFile()))
actMode?.finish()
finishActMode()
}
private fun toggleFileVisibility(hide: Boolean) {
@ -181,7 +148,7 @@ class MediaAdapter(val activity: BaseSimpleActivity, var media: MutableList<Medi
}
activity.runOnUiThread {
listener?.refreshItems()
actMode?.finish()
finishActMode()
}
}).start()
}
@ -203,20 +170,10 @@ class MediaAdapter(val activity: BaseSimpleActivity, var media: MutableList<Medi
if (!isCopyOperation) {
listener?.refreshItems()
}
actMode?.finish()
finishActMode()
}
}
fun selectAll() {
val cnt = media.size
for (i in 0 until cnt) {
selectedPositions.add(i)
multiSelector.setSelected(i, 0, true)
notifyItemChanged(i)
}
updateTitle(cnt)
}
private fun checkDeleteConfirmation() {
if (skipConfirmationDialog) {
deleteConfirmed()
@ -247,7 +204,7 @@ class MediaAdapter(val activity: BaseSimpleActivity, var media: MutableList<Medi
val removeMedia = ArrayList<Medium>(selectedPositions.size)
if (media.size <= selectedPositions.first()) {
actMode?.finish()
finishActMode()
return
}
@ -269,7 +226,8 @@ class MediaAdapter(val activity: BaseSimpleActivity, var media: MutableList<Medi
.forEachIndexed { curIndex, i -> newItems.put(curIndex, itemViews[i]) }
itemViews = newItems
actMode?.finish()
selectableItemCount = media.size
finishActMode()
}
}
@ -279,29 +237,18 @@ class MediaAdapter(val activity: BaseSimpleActivity, var media: MutableList<Medi
return selectedMedia
}
override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ViewHolder {
val layoutType = if (isListViewType) R.layout.photo_video_item_list else R.layout.photo_video_item_grid
val view = LayoutInflater.from(parent?.context).inflate(layoutType, parent, false)
return ViewHolder(view, adapterListener, activity, multiSelectorMode, multiSelector, listener, allowMultiplePicks || !isAGetIntent, itemClick)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
itemViews.put(position, holder.bindView(media[position], displayFilenames, scrollHorizontally, isListViewType, textColor, animateGifs, cropThumbnails))
toggleItemSelection(selectedPositions.contains(position), position)
holder.itemView.tag = holder
}
override fun onViewRecycled(holder: ViewHolder?) {
super.onViewRecycled(holder)
holder?.stopLoad()
if (!activity.isActivityDestroyed()) {
Glide.with(activity).clear(holder?.itemView?.medium_thumbnail)
}
}
override fun getItemCount() = media.size
fun updateMedia(newMedia: ArrayList<Medium>) {
media = newMedia
selectableItemCount = media.size
notifyDataSetChanged()
actMode?.finish()
finishActMode()
}
fun updateDisplayFilenames(displayFilenames: Boolean) {
@ -324,96 +271,17 @@ class MediaAdapter(val activity: BaseSimpleActivity, var media: MutableList<Medi
notifyDataSetChanged()
}
fun updateTextColor(textColor: Int) {
this.textColor = textColor
notifyDataSetChanged()
}
private fun setupView(view: View, medium: Medium) {
view.apply {
play_outline.beVisibleIf(medium.video)
photo_name.beVisibleIf(displayFilenames || isListViewType)
photo_name.text = medium.name
activity.loadImage(medium.path, medium_thumbnail, scrollHorizontally, animateGifs, cropThumbnails)
fun selectItem(pos: Int) {
toggleItemSelection(true, pos)
}
fun selectRange(from: Int, to: Int, min: Int, max: Int) {
if (from == to) {
(min..max).filter { it != from }
.forEach { toggleItemSelection(false, it) }
return
}
if (to < from) {
for (i in to..from)
toggleItemSelection(true, i)
if (min > -1 && min < to) {
(min until to).filter { it != from }
.forEach { toggleItemSelection(false, it) }
if (isListViewType) {
photo_name.setTextColor(textColor)
play_outline.setColorFilter(textColor, PorterDuff.Mode.SRC_IN)
}
if (max > -1) {
for (i in from + 1..max)
toggleItemSelection(false, i)
}
} else {
for (i in from..to)
toggleItemSelection(true, i)
if (max > -1 && max > to) {
(to + 1..max).filter { it != from }
.forEach { toggleItemSelection(false, it) }
}
if (min > -1) {
for (i in min until from)
toggleItemSelection(false, i)
}
}
}
class ViewHolder(val view: View, val adapterListener: MyAdapterListener, val activity: BaseSimpleActivity, val multiSelectorCallback: ModalMultiSelectorCallback,
val multiSelector: MultiSelector, val listener: MediaOperationsListener?, val allowMultiplePicks: Boolean,
val itemClick: (Medium) -> (Unit)) :
SwappingHolder(view, MultiSelector()) {
fun bindView(medium: Medium, displayFilenames: Boolean, scrollHorizontally: Boolean, isListViewType: Boolean, textColor: Int,
animateGifs: Boolean, cropThumbnails: Boolean): View {
itemView.apply {
play_outline.beVisibleIf(medium.video)
photo_name.beVisibleIf(displayFilenames || isListViewType)
photo_name.text = medium.name
activity.loadImage(medium.path, medium_thumbnail, scrollHorizontally, animateGifs, cropThumbnails)
if (isListViewType) {
photo_name.setTextColor(textColor)
play_outline.setColorFilter(textColor, PorterDuff.Mode.SRC_IN)
}
setOnClickListener { viewClicked(medium) }
setOnLongClickListener { if (allowMultiplePicks) viewLongClicked() else viewClicked(medium); true }
}
return itemView
}
private fun viewClicked(medium: Medium) {
if (multiSelector.isSelectable) {
val isSelected = adapterListener.getSelectedPositions().contains(adapterPosition)
adapterListener.toggleItemSelectionAdapter(!isSelected, adapterPosition)
} else {
itemClick(medium)
}
}
private fun viewLongClicked() {
if (listener != null) {
if (!multiSelector.isSelectable) {
activity.startSupportActionMode(multiSelectorCallback)
adapterListener.toggleItemSelectionAdapter(true, adapterPosition)
}
listener.itemLongClicked(adapterPosition)
}
}
fun stopLoad() {
if (!activity.isActivityDestroyed())
Glide.with(activity).clear(view.medium_thumbnail)
}
}
@ -422,8 +290,6 @@ class MediaAdapter(val activity: BaseSimpleActivity, var media: MutableList<Medi
fun deleteFiles(files: ArrayList<File>)
fun itemLongClicked(position: Int)
fun selectedPaths(paths: ArrayList<String>)
}
}

View file

@ -58,8 +58,8 @@ class PickMediumDialog(val activity: BaseSimpleActivity, val path: String, val c
return
shownMedia = media
val adapter = MediaAdapter(activity, media, null, true, false) {
callback(it.path)
val adapter = MediaAdapter(activity, media, null, true, false, view.media_grid) {
callback((it as Medium).path)
dialog.dismiss()
}