remove the static variables from the MediaAdapter

This commit is contained in:
tibbi 2017-05-14 17:09:25 +02:00
parent ab9c70d8d6
commit 74da4b454b
4 changed files with 95 additions and 92 deletions

View file

@ -194,8 +194,8 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener {
} }
private fun checkIfColorChanged() { private fun checkIfColorChanged() {
if (getDirectoryAdapter().foregroundColor != config.primaryColor) { if (getRecyclerAdapter().foregroundColor != config.primaryColor) {
getDirectoryAdapter().foregroundColor = config.primaryColor getRecyclerAdapter().foregroundColor = config.primaryColor
directories_fastscroller.updateHandleColor() directories_fastscroller.updateHandleColor()
} }
} }
@ -210,11 +210,7 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener {
} }
} }
private fun getDirectoryAdapter() = (directories_grid.adapter as DirectoryAdapter) private fun getRecyclerAdapter() = (directories_grid.adapter as DirectoryAdapter)
override fun itemLongClicked(position: Int) {
directories_grid.setDragSelectActive(position)
}
private fun handleZooming() { private fun handleZooming() {
val layoutManager = directories_grid.layoutManager as GridLayoutManager val layoutManager = directories_grid.layoutManager as GridLayoutManager
@ -223,23 +219,23 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener {
override fun zoomIn() { override fun zoomIn() {
if (layoutManager.spanCount > 1) { if (layoutManager.spanCount > 1) {
reduceColumnCount() reduceColumnCount()
getDirectoryAdapter().actMode?.finish() getRecyclerAdapter().actMode?.finish()
} }
} }
override fun zoomOut() { override fun zoomOut() {
if (layoutManager.spanCount < 10) { if (layoutManager.spanCount < 10) {
increaseColumnCount() increaseColumnCount()
getDirectoryAdapter().actMode?.finish() getRecyclerAdapter().actMode?.finish()
} }
} }
override fun selectItem(position: Int) { override fun selectItem(position: Int) {
getDirectoryAdapter().selectItem(position) getRecyclerAdapter().selectItem(position)
} }
override fun selectRange(initialSelection: Int, lastDraggedIndex: Int, minReached: Int, maxReached: Int) { override fun selectRange(initialSelection: Int, lastDraggedIndex: Int, minReached: Int, maxReached: Int) {
getDirectoryAdapter().selectRange(initialSelection, lastDraggedIndex, minReached, maxReached) getRecyclerAdapter().selectRange(initialSelection, lastDraggedIndex, minReached, maxReached)
} }
} }
} }
@ -355,6 +351,10 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener {
getDirectories() getDirectories()
} }
override fun itemLongClicked(position: Int) {
directories_grid.setDragSelectActive(position)
}
private fun checkWhatsNewDialog() { private fun checkWhatsNewDialog() {
arrayListOf<Release>().apply { arrayListOf<Release>().apply {
add(Release(46, R.string.release_46)) add(Release(46, R.string.release_46))

View file

@ -94,11 +94,6 @@ 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)
@ -170,7 +165,7 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener {
private fun toggleFilenameVisibility() { private fun toggleFilenameVisibility() {
config.displayFileNames = !config.displayFileNames config.displayFileNames = !config.displayFileNames
if (media_grid.adapter != null) if (media_grid.adapter != null)
(media_grid.adapter as MediaAdapter).updateDisplayFilenames(config.displayFileNames) getRecyclerAdapter().updateDisplayFilenames(config.displayFileNames)
} }
private fun showSortingDialog() { private fun showSortingDialog() {
@ -262,6 +257,8 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener {
getMedia() getMedia()
} }
private fun getRecyclerAdapter() = (media_grid.adapter as MediaAdapter)
private fun handleZooming() { private fun handleZooming() {
val layoutManager = media_grid.layoutManager as GridLayoutManager val layoutManager = media_grid.layoutManager as GridLayoutManager
layoutManager.spanCount = config.mediaColumnCnt layoutManager.spanCount = config.mediaColumnCnt
@ -269,22 +266,23 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener {
override fun zoomIn() { override fun zoomIn() {
if (layoutManager.spanCount > 1) { if (layoutManager.spanCount > 1) {
reduceColumnCount() reduceColumnCount()
MediaAdapter.actMode?.finish() getRecyclerAdapter().actMode?.finish()
} }
} }
override fun zoomOut() { override fun zoomOut() {
if (layoutManager.spanCount < 10) { if (layoutManager.spanCount < 10) {
increaseColumnCount() increaseColumnCount()
MediaAdapter.actMode?.finish() getRecyclerAdapter().actMode?.finish()
} }
} }
override fun selectItem(position: Int) { override fun selectItem(position: Int) {
(media_grid.adapter as MediaAdapter).selectItem(position) getRecyclerAdapter().selectItem(position)
} }
override fun selectRange(initialSelection: Int, lastDraggedIndex: Int, minReached: Int, maxReached: Int) { override fun selectRange(initialSelection: Int, lastDraggedIndex: Int, minReached: Int, maxReached: Int) {
(media_grid.adapter as MediaAdapter).selectRange(initialSelection, lastDraggedIndex, minReached, maxReached) getRecyclerAdapter().selectRange(initialSelection, lastDraggedIndex, minReached, maxReached)
} }
} }
} }

View file

@ -3,6 +3,7 @@ package com.simplemobiletools.gallery.adapters
import android.os.Build import android.os.Build
import android.support.v7.view.ActionMode import android.support.v7.view.ActionMode
import android.support.v7.widget.RecyclerView import android.support.v7.widget.RecyclerView
import android.util.SparseArray
import android.view.* import android.view.*
import android.widget.FrameLayout import android.widget.FrameLayout
import com.bignerdranch.android.multiselector.ModalMultiSelectorCallback import com.bignerdranch.android.multiselector.ModalMultiSelectorCallback
@ -29,14 +30,14 @@ import java.util.*
class DirectoryAdapter(val activity: SimpleActivity, val dirs: MutableList<Directory>, val listener: DirOperationsListener?, val itemClick: (Directory) -> Unit) : class DirectoryAdapter(val activity: SimpleActivity, val dirs: MutableList<Directory>, val listener: DirOperationsListener?, val itemClick: (Directory) -> Unit) :
RecyclerView.Adapter<DirectoryAdapter.ViewHolder>() { RecyclerView.Adapter<DirectoryAdapter.ViewHolder>() {
val views = ArrayList<View>()
val config = activity.config
var pinnedFolders = config.pinnedFolders
var itemViews: HashMap<Int, View> = HashMap()
val selectedPositions: HashSet<Int> = HashSet()
var actMode: ActionMode? = null
val multiSelector = MultiSelector() val multiSelector = MultiSelector()
val config = activity.config
var actMode: ActionMode? = null
var itemViews = SparseArray<View>()
val selectedPositions = HashSet<Int>()
var foregroundColor = 0 var foregroundColor = 0
var pinnedFolders = config.pinnedFolders
fun toggleItemSelection(select: Boolean, pos: Int) { fun toggleItemSelection(select: Boolean, pos: Int) {
if (itemViews[pos] != null) if (itemViews[pos] != null)
@ -79,8 +80,6 @@ class DirectoryAdapter(val activity: SimpleActivity, val dirs: MutableList<Direc
getProperView(itemView).foreground = foregroundColor.createSelector() getProperView(itemView).foreground = foregroundColor.createSelector()
} }
override fun getItemViews(): HashMap<Int, View> = itemViews
override fun getSelectedPositions(): HashSet<Int> = selectedPositions override fun getSelectedPositions(): HashSet<Int> = selectedPositions
} }
@ -281,7 +280,7 @@ class DirectoryAdapter(val activity: SimpleActivity, val dirs: MutableList<Direc
var needPermissionForPath = "" var needPermissionForPath = ""
selectedPositions.forEach { selectedPositions.forEach {
val path = dirs[it].path val path = dirs[it].path
if (activity.needsStupidWritePermissions(path) && activity.config.treeUri.isEmpty()) { if (activity.needsStupidWritePermissions(path) && config.treeUri.isEmpty()) {
needPermissionForPath = path needPermissionForPath = path
} }
} }
@ -312,7 +311,7 @@ class DirectoryAdapter(val activity: SimpleActivity, val dirs: MutableList<Direc
override fun onBindViewHolder(holder: ViewHolder, position: Int) { override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val dir = dirs[position] val dir = dirs[position]
views.add(holder.bindView(activity, multiSelectorMode, multiSelector, dir, position, pinnedFolders.contains(dir.path), listener)) itemViews.put(position, holder.bindView(activity, multiSelectorMode, multiSelector, dir, position, pinnedFolders.contains(dir.path), listener))
holder.itemView.tag = holder holder.itemView.tag = holder
} }
@ -365,7 +364,6 @@ class DirectoryAdapter(val activity: SimpleActivity, val dirs: MutableList<Direc
class ViewHolder(val view: View, val adapter: MyAdapterListener, val itemClick: (Directory) -> (Unit)) : SwappingHolder(view, MultiSelector()) { class ViewHolder(val view: View, val adapter: MyAdapterListener, val itemClick: (Directory) -> (Unit)) : SwappingHolder(view, MultiSelector()) {
fun bindView(activity: SimpleActivity, multiSelectorCallback: ModalMultiSelectorCallback, multiSelector: MultiSelector, directory: Directory, pos: Int, fun bindView(activity: SimpleActivity, multiSelectorCallback: ModalMultiSelectorCallback, multiSelector: MultiSelector, directory: Directory, pos: Int,
isPinned: Boolean, listener: DirOperationsListener?): View { isPinned: Boolean, listener: DirOperationsListener?): View {
adapter.getItemViews().put(pos, itemView)
itemView.apply { itemView.apply {
dir_name.text = directory.name dir_name.text = directory.name
photo_cnt.text = directory.mediaCnt.toString() photo_cnt.text = directory.mediaCnt.toString()
@ -410,8 +408,6 @@ class DirectoryAdapter(val activity: SimpleActivity, val dirs: MutableList<Direc
fun setupItemForeground(itemView: View) fun setupItemForeground(itemView: View)
fun getItemViews(): HashMap<Int, View>
fun getSelectedPositions(): HashSet<Int> fun getSelectedPositions(): HashSet<Int>
} }

View file

@ -3,6 +3,7 @@ package com.simplemobiletools.gallery.adapters
import android.os.Build import android.os.Build
import android.support.v7.view.ActionMode import android.support.v7.view.ActionMode
import android.support.v7.widget.RecyclerView import android.support.v7.widget.RecyclerView
import android.util.SparseArray
import android.view.* import android.view.*
import android.widget.FrameLayout import android.widget.FrameLayout
import com.bignerdranch.android.multiselector.ModalMultiSelectorCallback import com.bignerdranch.android.multiselector.ModalMultiSelectorCallback
@ -26,57 +27,60 @@ class MediaAdapter(val activity: SimpleActivity, var media: MutableList<Medium>,
RecyclerView.Adapter<MediaAdapter.ViewHolder>() { RecyclerView.Adapter<MediaAdapter.ViewHolder>() {
val multiSelector = MultiSelector() val multiSelector = MultiSelector()
val views = ArrayList<View>()
val config = activity.config val config = activity.config
companion object { var actMode: ActionMode? = null
var actMode: ActionMode? = null var itemViews = SparseArray<View>()
var displayFilenames = false val selectedPositions = HashSet<Int>()
var foregroundColor = 0 var foregroundColor = 0
var backgroundColor = 0 var displayFilenames = false
var itemCnt = 0
var itemViews: HashMap<Int, View> = HashMap()
val selectedPositions = HashSet<Int>()
fun toggleItemSelection(select: Boolean, pos: Int) { fun toggleItemSelection(select: Boolean, pos: Int) {
if (itemViews[pos] != null) if (itemViews[pos] != null)
getProperView(itemViews[pos]!!).isSelected = select getProperView(itemViews[pos]!!).isSelected = select
if (select) if (select)
selectedPositions.add(pos) selectedPositions.add(pos)
else
selectedPositions.remove(pos)
if (selectedPositions.isEmpty()) {
actMode?.finish()
return
}
updateTitle(selectedPositions.size)
actMode?.invalidate()
}
fun getProperView(itemView: View): View {
return if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M)
itemView.medium_thumbnail_holder
else
itemView.medium_thumbnail
}
fun updateTitle(cnt: Int) {
actMode?.title = "$cnt / ${media.size}"
}
val adapterListener = object : MyAdapterListener {
override fun toggleItemSelectionAdapter(select: Boolean, position: Int) {
toggleItemSelection(select, position)
}
override fun setupItemForeground(itemView: View) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M)
(getProperView(itemView) as FrameLayout).foreground = foregroundColor.createSelector()
else else
selectedPositions.remove(pos) getProperView(itemView).foreground = foregroundColor.createSelector()
if (selectedPositions.isEmpty()) {
actMode?.finish()
return
}
updateTitle(selectedPositions.size)
actMode?.invalidate()
} }
fun getProperView(itemView: View): View { override fun getSelectedPositions(): HashSet<Int> = selectedPositions
return if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M)
itemView.medium_thumbnail_holder
else
itemView.medium_thumbnail
}
fun updateTitle(cnt: Int) {
actMode?.title = "$cnt / $itemCnt"
}
fun cleanup() {
itemViews.clear()
selectedPositions.clear()
}
} }
init { init {
foregroundColor = config.primaryColor foregroundColor = config.primaryColor
backgroundColor = config.backgroundColor
itemCnt = media.size
} }
val multiSelectorMode = object : ModalMultiSelectorCallback(multiSelector) { val multiSelectorMode = object : ModalMultiSelectorCallback(multiSelector) {
@ -229,7 +233,6 @@ class MediaAdapter(val activity: SimpleActivity, var media: MutableList<Medium>,
media.removeAll(removeMedia) media.removeAll(removeMedia)
selectedPositions.clear() selectedPositions.clear()
listener?.deleteFiles(files) listener?.deleteFiles(files)
itemCnt = media.size
} }
} }
@ -241,11 +244,11 @@ class MediaAdapter(val activity: SimpleActivity, var media: MutableList<Medium>,
override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ViewHolder { override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent?.context).inflate(R.layout.photo_video_item, parent, false) val view = LayoutInflater.from(parent?.context).inflate(R.layout.photo_video_item, parent, false)
return ViewHolder(view, itemClick) return ViewHolder(view, adapterListener, itemClick)
} }
override fun onBindViewHolder(holder: ViewHolder, position: Int) { override fun onBindViewHolder(holder: ViewHolder, position: Int) {
views.add(holder.bindView(activity, multiSelectorMode, multiSelector, media[position], position, listener)) itemViews.put(position, holder.bindView(activity, multiSelectorMode, multiSelector, media[position], position, listener, displayFilenames))
holder.itemView.tag = holder holder.itemView.tag = holder
} }
@ -305,40 +308,38 @@ 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 adapter: MyAdapterListener, val itemClick: (Medium) -> (Unit)) : SwappingHolder(view, MultiSelector()) {
fun bindView(activity: SimpleActivity, multiSelectorCallback: ModalMultiSelectorCallback, multiSelector: MultiSelector, medium: Medium, fun bindView(activity: SimpleActivity, multiSelectorCallback: ModalMultiSelectorCallback, multiSelector: MultiSelector, medium: Medium,
pos: Int, listener: MediaOperationsListener?): View { pos: Int, listener: MediaOperationsListener?, displayFilenames: Boolean): 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(selectedPositions.contains(pos), pos) adapter.toggleItemSelectionAdapter(adapter.getSelectedPositions().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 (listener != null) {
activity.startSupportActionMode(multiSelectorCallback) if (!multiSelector.isSelectable) {
toggleItemSelection(true, pos) activity.startSupportActionMode(multiSelectorCallback)
} adapter.toggleItemSelectionAdapter(true, pos)
}
listener!!.itemLongClicked(pos) listener.itemLongClicked(pos)
}
true true
} }
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) adapter.setupItemForeground(this)
(getProperView(this) as FrameLayout).foreground = foregroundColor.createSelector()
else
getProperView(this).foreground = foregroundColor.createSelector()
} }
return itemView return itemView
} }
fun viewClicked(multiSelector: MultiSelector, medium: Medium, pos: Int) { fun viewClicked(multiSelector: MultiSelector, medium: Medium, pos: Int) {
if (multiSelector.isSelectable) { if (multiSelector.isSelectable) {
val isSelected = selectedPositions.contains(layoutPosition) val isSelected = adapter.getSelectedPositions().contains(layoutPosition)
toggleItemSelection(!isSelected, pos) adapter.toggleItemSelectionAdapter(!isSelected, pos)
} else { } else {
itemClick(medium) itemClick(medium)
} }
@ -349,6 +350,14 @@ class MediaAdapter(val activity: SimpleActivity, var media: MutableList<Medium>,
} }
} }
interface MyAdapterListener {
fun toggleItemSelectionAdapter(select: Boolean, position: Int)
fun setupItemForeground(itemView: View)
fun getSelectedPositions(): HashSet<Int>
}
interface MediaOperationsListener { interface MediaOperationsListener {
fun refreshItems() fun refreshItems()