small cleanup at thumbnail adapters

This commit is contained in:
tibbi 2017-06-18 21:40:00 +02:00
parent 2734fe9615
commit 5319a184f3
4 changed files with 45 additions and 42 deletions

View file

@ -361,12 +361,12 @@ class DirectoryAdapter(val activity: SimpleActivity, var 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, adapterListener, itemClick) return ViewHolder(view, adapterListener, activity, multiSelectorMode, multiSelector, listener, itemClick)
} }
override fun onBindViewHolder(holder: ViewHolder, position: Int) { override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val dir = dirs[position] val dir = dirs[position]
itemViews.put(position, holder.bindView(activity, multiSelectorMode, multiSelector, dir, pinnedFolders.contains(dir.path), listener)) itemViews.put(position, holder.bindView(dir, pinnedFolders.contains(dir.path)))
toggleItemSelection(selectedPositions.contains(position), position) toggleItemSelection(selectedPositions.contains(position), position)
holder.itemView.tag = holder holder.itemView.tag = holder
} }
@ -422,17 +422,34 @@ class DirectoryAdapter(val activity: SimpleActivity, var 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 activity: SimpleActivity, val multiSelectorCallback: ModalMultiSelectorCallback,
fun bindView(activity: SimpleActivity, multiSelectorCallback: ModalMultiSelectorCallback, multiSelector: MultiSelector, directory: Directory, val multiSelector: MultiSelector, val listener: DirOperationsListener?, val itemClick: (Directory) -> (Unit)) :
isPinned: Boolean, listener: DirOperationsListener?): View { SwappingHolder(view, MultiSelector()) {
fun bindView(directory: Directory, isPinned: Boolean): View {
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
activity.loadImage(directory.tmb, dir_thumbnail) activity.loadImage(directory.tmb, dir_thumbnail, true)
setOnClickListener { viewClicked(multiSelector, directory) } setOnClickListener { viewClicked(directory) }
setOnLongClickListener { setOnLongClickListener { viewLongClicked(); true }
adapter.setupItemForeground(this)
}
return itemView
}
fun viewClicked(directory: Directory) {
if (multiSelector.isSelectable) {
val isSelected = adapter.getSelectedPositions().contains(layoutPosition)
adapter.toggleItemSelectionAdapter(!isSelected, layoutPosition)
} else {
itemClick(directory)
}
}
fun viewLongClicked() {
if (listener != null) { if (listener != null) {
if (!multiSelector.isSelectable) { if (!multiSelector.isSelectable) {
activity.startSupportActionMode(multiSelectorCallback) activity.startSupportActionMode(multiSelectorCallback)
@ -441,21 +458,6 @@ class DirectoryAdapter(val activity: SimpleActivity, var dirs: MutableList<Direc
listener.itemLongClicked(layoutPosition) listener.itemLongClicked(layoutPosition)
} }
true
}
adapter.setupItemForeground(this)
}
return itemView
}
fun viewClicked(multiSelector: MultiSelector, directory: Directory) {
if (multiSelector.isSelectable) {
val isSelected = adapter.getSelectedPositions().contains(layoutPosition)
adapter.toggleItemSelectionAdapter(!isSelected, layoutPosition)
} else {
itemClick(directory)
}
} }
fun stopLoad() { fun stopLoad() {

View file

@ -270,11 +270,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, adapterListener, itemClick) return ViewHolder(view, adapterListener, activity, multiSelectorMode, multiSelector, listener, itemClick)
} }
override fun onBindViewHolder(holder: ViewHolder, position: Int) { override fun onBindViewHolder(holder: ViewHolder, position: Int) {
itemViews.put(position, holder.bindView(activity, multiSelectorMode, multiSelector, media[position], listener, displayFilenames)) itemViews.put(position, holder.bindView(media[position], displayFilenames))
toggleItemSelection(selectedPositions.contains(position), position) toggleItemSelection(selectedPositions.contains(position), position)
holder.itemView.tag = holder holder.itemView.tag = holder
} }
@ -335,17 +335,33 @@ class MediaAdapter(val activity: SimpleActivity, var media: MutableList<Medium>,
} }
} }
class ViewHolder(val view: View, val adapter: MyAdapterListener, val itemClick: (Medium) -> (Unit)) : SwappingHolder(view, MultiSelector()) { class ViewHolder(val view: View, val adapter: MyAdapterListener, val activity: SimpleActivity, val multiSelectorCallback: ModalMultiSelectorCallback,
fun bindView(activity: SimpleActivity, multiSelectorCallback: ModalMultiSelectorCallback, multiSelector: MultiSelector, medium: Medium, val multiSelector: MultiSelector, val listener: MediaOperationsListener?, val itemClick: (Medium) -> (Unit)) : SwappingHolder(view, MultiSelector()) {
listener: MediaOperationsListener?, displayFilenames: Boolean): View { fun bindView(medium: Medium, displayFilenames: Boolean): View {
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
activity.loadImage(medium.path, medium_thumbnail) activity.loadImage(medium.path, medium_thumbnail, true)
setOnClickListener { viewClicked(multiSelector, medium) } setOnClickListener { viewClicked(medium) }
setOnLongClickListener { setOnLongClickListener { viewLongClicked(); true }
adapter.setupItemForeground(this)
}
return itemView
}
fun viewClicked(medium: Medium) {
if (multiSelector.isSelectable) {
val isSelected = adapter.getSelectedPositions().contains(layoutPosition)
adapter.toggleItemSelectionAdapter(!isSelected, layoutPosition)
} else {
itemClick(medium)
}
}
fun viewLongClicked() {
if (listener != null) { if (listener != null) {
if (!multiSelector.isSelectable) { if (!multiSelector.isSelectable) {
activity.startSupportActionMode(multiSelectorCallback) activity.startSupportActionMode(multiSelectorCallback)
@ -354,21 +370,6 @@ class MediaAdapter(val activity: SimpleActivity, var media: MutableList<Medium>,
listener.itemLongClicked(layoutPosition) listener.itemLongClicked(layoutPosition)
} }
true
}
adapter.setupItemForeground(this)
}
return itemView
}
fun viewClicked(multiSelector: MultiSelector, medium: Medium) {
if (multiSelector.isSelectable) {
val isSelected = adapter.getSelectedPositions().contains(layoutPosition)
adapter.toggleItemSelectionAdapter(!isSelected, layoutPosition)
} else {
itemClick(medium)
}
} }
fun stopLoad() { fun stopLoad() {

View file

@ -264,7 +264,8 @@ fun SimpleActivity.toggleFileVisibility(oldFile: File, hide: Boolean, callback:
fun Activity.getFileSignature(path: String) = StringSignature(File(path).lastModified().toString()) fun Activity.getFileSignature(path: String) = StringSignature(File(path).lastModified().toString())
fun Activity.loadImage(path: String, target: MySquareImageView) { fun Activity.loadImage(path: String, target: MySquareImageView, verticalScroll: Boolean) {
target.isVerticalScrolling = verticalScroll
if (path.isImageFast() || path.isVideoFast()) { if (path.isImageFast() || path.isVideoFast()) {
if (path.isPng()) { if (path.isPng()) {
loadPng(path, target) loadPng(path, target)

View file

@ -1,12 +1,11 @@
package com.simplemobiletools.gallery.views package com.simplemobiletools.gallery.views
import android.content.Context import android.content.Context
import android.support.v7.widget.OrientationHelper
import android.util.AttributeSet import android.util.AttributeSet
import android.widget.ImageView import android.widget.ImageView
class MySquareImageView : ImageView { class MySquareImageView : ImageView {
var orientation = OrientationHelper.VERTICAL var isVerticalScrolling = true
constructor(context: Context) : super(context) constructor(context: Context) : super(context)
@ -16,7 +15,7 @@ class MySquareImageView : ImageView {
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec) super.onMeasure(widthMeasureSpec, heightMeasureSpec)
val spec = if (orientation == OrientationHelper.VERTICAL) measuredWidth else measuredHeight val spec = if (isVerticalScrolling) measuredWidth else measuredHeight
setMeasuredDimension(spec, spec) setMeasuredDimension(spec, spec)
} }
} }