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

View file

@ -94,11 +94,6 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener {
config.temporarilyShowHidden = false
}
override fun onDestroy() {
super.onDestroy()
MediaAdapter.cleanup()
}
private fun tryloadGallery() {
if (hasWriteStoragePermission()) {
val dirName = getHumanizedFilename(mPath)
@ -170,7 +165,7 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener {
private fun toggleFilenameVisibility() {
config.displayFileNames = !config.displayFileNames
if (media_grid.adapter != null)
(media_grid.adapter as MediaAdapter).updateDisplayFilenames(config.displayFileNames)
getRecyclerAdapter().updateDisplayFilenames(config.displayFileNames)
}
private fun showSortingDialog() {
@ -262,6 +257,8 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener {
getMedia()
}
private fun getRecyclerAdapter() = (media_grid.adapter as MediaAdapter)
private fun handleZooming() {
val layoutManager = media_grid.layoutManager as GridLayoutManager
layoutManager.spanCount = config.mediaColumnCnt
@ -269,22 +266,23 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener {
override fun zoomIn() {
if (layoutManager.spanCount > 1) {
reduceColumnCount()
MediaAdapter.actMode?.finish()
getRecyclerAdapter().actMode?.finish()
}
}
override fun zoomOut() {
if (layoutManager.spanCount < 10) {
increaseColumnCount()
MediaAdapter.actMode?.finish()
getRecyclerAdapter().actMode?.finish()
}
}
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) {
(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.support.v7.view.ActionMode
import android.support.v7.widget.RecyclerView
import android.util.SparseArray
import android.view.*
import android.widget.FrameLayout
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) :
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 config = activity.config
var actMode: ActionMode? = null
var itemViews = SparseArray<View>()
val selectedPositions = HashSet<Int>()
var foregroundColor = 0
var pinnedFolders = config.pinnedFolders
fun toggleItemSelection(select: Boolean, pos: Int) {
if (itemViews[pos] != null)
@ -79,8 +80,6 @@ class DirectoryAdapter(val activity: SimpleActivity, val dirs: MutableList<Direc
getProperView(itemView).foreground = foregroundColor.createSelector()
}
override fun getItemViews(): HashMap<Int, View> = itemViews
override fun getSelectedPositions(): HashSet<Int> = selectedPositions
}
@ -281,7 +280,7 @@ class DirectoryAdapter(val activity: SimpleActivity, val dirs: MutableList<Direc
var needPermissionForPath = ""
selectedPositions.forEach {
val path = dirs[it].path
if (activity.needsStupidWritePermissions(path) && activity.config.treeUri.isEmpty()) {
if (activity.needsStupidWritePermissions(path) && config.treeUri.isEmpty()) {
needPermissionForPath = path
}
}
@ -312,7 +311,7 @@ class DirectoryAdapter(val activity: SimpleActivity, val dirs: MutableList<Direc
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
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
}
@ -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()) {
fun bindView(activity: SimpleActivity, multiSelectorCallback: ModalMultiSelectorCallback, multiSelector: MultiSelector, directory: Directory, pos: Int,
isPinned: Boolean, listener: DirOperationsListener?): View {
adapter.getItemViews().put(pos, itemView)
itemView.apply {
dir_name.text = directory.name
photo_cnt.text = directory.mediaCnt.toString()
@ -410,8 +408,6 @@ class DirectoryAdapter(val activity: SimpleActivity, val dirs: MutableList<Direc
fun setupItemForeground(itemView: View)
fun getItemViews(): HashMap<Int, View>
fun getSelectedPositions(): HashSet<Int>
}

View file

@ -3,6 +3,7 @@ package com.simplemobiletools.gallery.adapters
import android.os.Build
import android.support.v7.view.ActionMode
import android.support.v7.widget.RecyclerView
import android.util.SparseArray
import android.view.*
import android.widget.FrameLayout
import com.bignerdranch.android.multiselector.ModalMultiSelectorCallback
@ -26,57 +27,60 @@ class MediaAdapter(val activity: SimpleActivity, var media: MutableList<Medium>,
RecyclerView.Adapter<MediaAdapter.ViewHolder>() {
val multiSelector = MultiSelector()
val views = ArrayList<View>()
val config = activity.config
companion object {
var actMode: ActionMode? = null
var displayFilenames = false
var foregroundColor = 0
var backgroundColor = 0
var itemCnt = 0
var itemViews: HashMap<Int, View> = HashMap()
val selectedPositions = HashSet<Int>()
var actMode: ActionMode? = null
var itemViews = SparseArray<View>()
val selectedPositions = HashSet<Int>()
var foregroundColor = 0
var displayFilenames = false
fun toggleItemSelection(select: Boolean, pos: Int) {
if (itemViews[pos] != null)
getProperView(itemViews[pos]!!).isSelected = select
fun toggleItemSelection(select: Boolean, pos: Int) {
if (itemViews[pos] != null)
getProperView(itemViews[pos]!!).isSelected = select
if (select)
selectedPositions.add(pos)
if (select)
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
selectedPositions.remove(pos)
if (selectedPositions.isEmpty()) {
actMode?.finish()
return
}
updateTitle(selectedPositions.size)
actMode?.invalidate()
getProperView(itemView).foreground = foregroundColor.createSelector()
}
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 / $itemCnt"
}
fun cleanup() {
itemViews.clear()
selectedPositions.clear()
}
override fun getSelectedPositions(): HashSet<Int> = selectedPositions
}
init {
foregroundColor = config.primaryColor
backgroundColor = config.backgroundColor
itemCnt = media.size
}
val multiSelectorMode = object : ModalMultiSelectorCallback(multiSelector) {
@ -229,7 +233,6 @@ class MediaAdapter(val activity: SimpleActivity, var media: MutableList<Medium>,
media.removeAll(removeMedia)
selectedPositions.clear()
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 {
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) {
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
}
@ -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,
pos: Int, listener: MediaOperationsListener?): View {
itemViews.put(pos, itemView)
pos: Int, listener: MediaOperationsListener?, displayFilenames: Boolean): View {
itemView.apply {
play_outline.visibility = if (medium.video) View.VISIBLE else View.GONE
photo_name.beVisibleIf(displayFilenames)
photo_name.text = medium.name
toggleItemSelection(selectedPositions.contains(pos), pos)
adapter.toggleItemSelectionAdapter(adapter.getSelectedPositions().contains(pos), pos)
activity.loadImage(medium.path, medium_thumbnail)
setOnClickListener { viewClicked(multiSelector, medium, pos) }
setOnLongClickListener {
if (!multiSelector.isSelectable) {
activity.startSupportActionMode(multiSelectorCallback)
toggleItemSelection(true, pos)
}
if (listener != null) {
if (!multiSelector.isSelectable) {
activity.startSupportActionMode(multiSelectorCallback)
adapter.toggleItemSelectionAdapter(true, pos)
}
listener!!.itemLongClicked(pos)
listener.itemLongClicked(pos)
}
true
}
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M)
(getProperView(this) as FrameLayout).foreground = foregroundColor.createSelector()
else
getProperView(this).foreground = foregroundColor.createSelector()
adapter.setupItemForeground(this)
}
return itemView
}
fun viewClicked(multiSelector: MultiSelector, medium: Medium, pos: Int) {
if (multiSelector.isSelectable) {
val isSelected = selectedPositions.contains(layoutPosition)
toggleItemSelection(!isSelected, pos)
val isSelected = adapter.getSelectedPositions().contains(layoutPosition)
adapter.toggleItemSelectionAdapter(!isSelected, pos)
} else {
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 {
fun refreshItems()