From ab9c70d8d6b078bbe8b4450dedc2d641b0ed6065 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sun, 14 May 2017 15:12:57 +0200 Subject: [PATCH] remove the Static fields of DirectoryAdapter --- .../gallery/activities/MainActivity.kt | 19 ++- .../gallery/adapters/DirectoryAdapter.kt | 127 ++++++++++-------- 2 files changed, 77 insertions(+), 69 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 2125eab79..681c9c3ae 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt @@ -126,11 +126,6 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener { config.temporarilyShowHidden = false } - override fun onDestroy() { - super.onDestroy() - DirectoryAdapter.cleanup() - } - private fun tryloadGallery() { if (hasWriteStoragePermission()) { if (config.showAll) @@ -199,8 +194,8 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener { } private fun checkIfColorChanged() { - if (DirectoryAdapter.foregroundColor != config.primaryColor) { - DirectoryAdapter.foregroundColor = config.primaryColor + if (getDirectoryAdapter().foregroundColor != config.primaryColor) { + getDirectoryAdapter().foregroundColor = config.primaryColor directories_fastscroller.updateHandleColor() } } @@ -215,6 +210,8 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener { } } + private fun getDirectoryAdapter() = (directories_grid.adapter as DirectoryAdapter) + override fun itemLongClicked(position: Int) { directories_grid.setDragSelectActive(position) } @@ -226,23 +223,23 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener { override fun zoomIn() { if (layoutManager.spanCount > 1) { reduceColumnCount() - DirectoryAdapter.actMode?.finish() + getDirectoryAdapter().actMode?.finish() } } override fun zoomOut() { if (layoutManager.spanCount < 10) { increaseColumnCount() - DirectoryAdapter.actMode?.finish() + getDirectoryAdapter().actMode?.finish() } } override fun selectItem(position: Int) { - (directories_grid.adapter as DirectoryAdapter).selectItem(position) + getDirectoryAdapter().selectItem(position) } override fun selectRange(initialSelection: Int, lastDraggedIndex: Int, minReached: Int, maxReached: Int) { - (directories_grid.adapter as DirectoryAdapter).selectRange(initialSelection, lastDraggedIndex, minReached, maxReached) + getDirectoryAdapter().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 81a7fde7a..32f4a3a75 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/DirectoryAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/DirectoryAdapter.kt @@ -32,57 +32,60 @@ class DirectoryAdapter(val activity: SimpleActivity, val dirs: MutableList() val config = activity.config var pinnedFolders = config.pinnedFolders + var itemViews: HashMap = HashMap() + val selectedPositions: HashSet = HashSet() + var actMode: ActionMode? = null + val multiSelector = MultiSelector() + var foregroundColor = 0 - companion object { - val multiSelector = MultiSelector() - var actMode: ActionMode? = null - var foregroundColor = 0 - var backgroundColor = 0 - var animateGifs = true - var itemCnt = 0 - var itemViews: HashMap = HashMap() - val selectedPositions: HashSet = HashSet() + 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) + else + selectedPositions.remove(pos) - if (select) - selectedPositions.add(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.dir_frame + else + itemView.dir_thumbnail + } + + fun updateTitle(cnt: Int) { + actMode?.title = "$cnt / ${dirs.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.dir_frame - else - itemView.dir_thumbnail - } + override fun getItemViews(): HashMap = itemViews - 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 - animateGifs = config.animateGifs - itemCnt = dirs.size } val multiSelectorMode = object : ModalMultiSelectorCallback(multiSelector) { @@ -293,7 +296,6 @@ class DirectoryAdapter(val activity: SimpleActivity, val dirs: MutableList (Unit)) : SwappingHolder(view, MultiSelector()) { - fun bindView(activity: SimpleActivity, multiSelectorCallback: ModalMultiSelectorCallback, directory: Directory, pos: Int, + 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 { - itemViews.put(pos, itemView) + adapter.getItemViews().put(pos, itemView) itemView.apply { dir_name.text = directory.name photo_cnt.text = directory.mediaCnt.toString() dir_pin.visibility = if (isPinned) View.VISIBLE else View.GONE - toggleItemSelection(selectedPositions.contains(pos), pos) + adapter.toggleItemSelectionAdapter(adapter.getSelectedPositions().contains(pos), pos) activity.loadImage(directory.tmb, dir_thumbnail) setOnClickListener { viewClicked(multiSelector, directory, 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, directory: Directory, 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(directory) } @@ -404,6 +405,16 @@ class DirectoryAdapter(val activity: SimpleActivity, val dirs: MutableList + + fun getSelectedPositions(): HashSet + } + interface DirOperationsListener { fun refreshItems()