From 74da4b454b7d8f009e5c64fce341716ed46d5296 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sun, 14 May 2017 17:09:25 +0200 Subject: [PATCH] remove the static variables from the MediaAdapter --- .../gallery/activities/MainActivity.kt | 22 +-- .../gallery/activities/MediaActivity.kt | 18 ++- .../gallery/adapters/DirectoryAdapter.kt | 22 ++- .../gallery/adapters/MediaAdapter.kt | 125 ++++++++++-------- 4 files changed, 95 insertions(+), 92 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt index 681c9c3ae..59f1c0c89 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt @@ -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().apply { add(Release(46, R.string.release_46)) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt index ac2b800f8..331b75b69 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt @@ -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) } } } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/DirectoryAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/DirectoryAdapter.kt index 32f4a3a75..360797efb 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/DirectoryAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/DirectoryAdapter.kt @@ -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, val listener: DirOperationsListener?, val itemClick: (Directory) -> Unit) : RecyclerView.Adapter() { - val views = ArrayList() - val config = activity.config - var pinnedFolders = config.pinnedFolders - var itemViews: HashMap = HashMap() - val selectedPositions: HashSet = HashSet() - var actMode: ActionMode? = null val multiSelector = MultiSelector() + val config = activity.config + + var actMode: ActionMode? = null + var itemViews = SparseArray() + val selectedPositions = HashSet() 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 = itemViews - override fun getSelectedPositions(): HashSet = selectedPositions } @@ -281,7 +280,7 @@ class DirectoryAdapter(val activity: SimpleActivity, val dirs: MutableList (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 - fun getSelectedPositions(): HashSet } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/MediaAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/MediaAdapter.kt index 2f4a4ba6c..b9738b860 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/MediaAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/MediaAdapter.kt @@ -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, RecyclerView.Adapter() { val multiSelector = MultiSelector() - val views = ArrayList() 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 = HashMap() - val selectedPositions = HashSet() + var actMode: ActionMode? = null + var itemViews = SparseArray() + val selectedPositions = HashSet() + 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 = 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, media.removeAll(removeMedia) selectedPositions.clear() listener?.deleteFiles(files) - itemCnt = media.size } } @@ -241,11 +244,11 @@ class MediaAdapter(val activity: SimpleActivity, var media: MutableList, 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, } } - 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, } } + interface MyAdapterListener { + fun toggleItemSelectionAdapter(select: Boolean, position: Int) + + fun setupItemForeground(itemView: View) + + fun getSelectedPositions(): HashSet + } + interface MediaOperationsListener { fun refreshItems()