remove the Static fields of DirectoryAdapter

This commit is contained in:
tibbi 2017-05-14 15:12:57 +02:00
parent 883e6ce511
commit ab9c70d8d6
2 changed files with 77 additions and 69 deletions

View File

@ -126,11 +126,6 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener {
config.temporarilyShowHidden = false config.temporarilyShowHidden = false
} }
override fun onDestroy() {
super.onDestroy()
DirectoryAdapter.cleanup()
}
private fun tryloadGallery() { private fun tryloadGallery() {
if (hasWriteStoragePermission()) { if (hasWriteStoragePermission()) {
if (config.showAll) if (config.showAll)
@ -199,8 +194,8 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener {
} }
private fun checkIfColorChanged() { private fun checkIfColorChanged() {
if (DirectoryAdapter.foregroundColor != config.primaryColor) { if (getDirectoryAdapter().foregroundColor != config.primaryColor) {
DirectoryAdapter.foregroundColor = config.primaryColor getDirectoryAdapter().foregroundColor = config.primaryColor
directories_fastscroller.updateHandleColor() 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) { override fun itemLongClicked(position: Int) {
directories_grid.setDragSelectActive(position) directories_grid.setDragSelectActive(position)
} }
@ -226,23 +223,23 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener {
override fun zoomIn() { override fun zoomIn() {
if (layoutManager.spanCount > 1) { if (layoutManager.spanCount > 1) {
reduceColumnCount() reduceColumnCount()
DirectoryAdapter.actMode?.finish() getDirectoryAdapter().actMode?.finish()
} }
} }
override fun zoomOut() { override fun zoomOut() {
if (layoutManager.spanCount < 10) { if (layoutManager.spanCount < 10) {
increaseColumnCount() increaseColumnCount()
DirectoryAdapter.actMode?.finish() getDirectoryAdapter().actMode?.finish()
} }
} }
override fun selectItem(position: Int) { 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) { 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)
} }
} }
} }

View File

@ -32,57 +32,60 @@ class DirectoryAdapter(val activity: SimpleActivity, val dirs: MutableList<Direc
val views = ArrayList<View>() val views = ArrayList<View>()
val config = activity.config val config = activity.config
var pinnedFolders = config.pinnedFolders var pinnedFolders = config.pinnedFolders
var itemViews: HashMap<Int, View> = HashMap()
val selectedPositions: HashSet<Int> = HashSet()
var actMode: ActionMode? = null
val multiSelector = MultiSelector()
var foregroundColor = 0
companion object { fun toggleItemSelection(select: Boolean, pos: Int) {
val multiSelector = MultiSelector() if (itemViews[pos] != null)
var actMode: ActionMode? = null getProperView(itemViews[pos]!!).isSelected = select
var foregroundColor = 0
var backgroundColor = 0
var animateGifs = true
var itemCnt = 0
var itemViews: HashMap<Int, View> = HashMap()
val selectedPositions: HashSet<Int> = HashSet()
fun toggleItemSelection(select: Boolean, pos: Int) { if (select)
if (itemViews[pos] != null) selectedPositions.add(pos)
getProperView(itemViews[pos]!!).isSelected = select else
selectedPositions.remove(pos)
if (select) if (selectedPositions.isEmpty()) {
selectedPositions.add(pos) 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 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 getItemViews(): HashMap<Int, View> = itemViews
return if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M)
itemView.dir_frame
else
itemView.dir_thumbnail
}
fun updateTitle(cnt: Int) { override fun getSelectedPositions(): HashSet<Int> = selectedPositions
actMode?.title = "$cnt / $itemCnt"
}
fun cleanup() {
itemViews.clear()
selectedPositions.clear()
}
} }
init { init {
foregroundColor = config.primaryColor foregroundColor = config.primaryColor
backgroundColor = config.backgroundColor
animateGifs = config.animateGifs
itemCnt = dirs.size
} }
val multiSelectorMode = object : ModalMultiSelectorCallback(multiSelector) { val multiSelectorMode = object : ModalMultiSelectorCallback(multiSelector) {
@ -293,7 +296,6 @@ class DirectoryAdapter(val activity: SimpleActivity, val dirs: MutableList<Direc
dirs.removeAll(removeFolders) dirs.removeAll(removeFolders)
listener?.tryDeleteFolders(folders) listener?.tryDeleteFolders(folders)
itemCnt = dirs.size
} }
} }
@ -305,12 +307,12 @@ class DirectoryAdapter(val activity: SimpleActivity, val dirs: MutableList<Direc
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.directory_item, parent, false) val view = LayoutInflater.from(parent?.context).inflate(R.layout.directory_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) {
val dir = dirs[position] val dir = dirs[position]
views.add(holder.bindView(activity, multiSelectorMode, dir, position, pinnedFolders.contains(dir.path), listener)) views.add(holder.bindView(activity, multiSelectorMode, multiSelector, dir, position, pinnedFolders.contains(dir.path), listener))
holder.itemView.tag = holder holder.itemView.tag = holder
} }
@ -360,40 +362,39 @@ class DirectoryAdapter(val activity: SimpleActivity, val dirs: MutableList<Direc
} }
} }
class ViewHolder(val view: View, 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, 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 {
itemViews.put(pos, itemView) 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()
dir_pin.visibility = if (isPinned) View.VISIBLE else View.GONE 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) activity.loadImage(directory.tmb, dir_thumbnail)
setOnClickListener { viewClicked(multiSelector, directory, pos) } setOnClickListener { viewClicked(multiSelector, directory, 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, directory: Directory, pos: Int) { fun viewClicked(multiSelector: MultiSelector, directory: Directory, 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(directory) itemClick(directory)
} }
@ -404,6 +405,16 @@ class DirectoryAdapter(val activity: SimpleActivity, val dirs: MutableList<Direc
} }
} }
interface MyAdapterListener {
fun toggleItemSelectionAdapter(select: Boolean, position: Int)
fun setupItemForeground(itemView: View)
fun getItemViews(): HashMap<Int, View>
fun getSelectedPositions(): HashSet<Int>
}
interface DirOperationsListener { interface DirOperationsListener {
fun refreshItems() fun refreshItems()