From 95cc4d11214bd5406b23ef223a5b303692b38c5d Mon Sep 17 00:00:00 2001 From: tibbi Date: Sat, 11 Mar 2017 12:03:04 +0100 Subject: [PATCH] use an itemView.apply to shorten some adapters --- .../gallery/adapters/DirectoryAdapter.kt | 68 +++++++++--------- .../gallery/adapters/MediaAdapter.kt | 69 ++++++++++--------- 2 files changed, 70 insertions(+), 67 deletions(-) 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 dd2e72917..22e60d289 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/DirectoryAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/DirectoryAdapter.kt @@ -321,45 +321,47 @@ 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) : View { - itemView.dir_name.text = directory.name - itemView.photo_cnt.text = directory.mediaCnt.toString() - itemView.dir_pin.visibility = if (isPinned) View.VISIBLE else View.GONE - toggleItemSelection(itemView, markedItems.contains(pos), pos) + itemView.apply { + dir_name.text = directory.name + photo_cnt.text = directory.mediaCnt.toString() + dir_pin.visibility = if (isPinned) View.VISIBLE else View.GONE + toggleItemSelection(this, markedItems.contains(pos), pos) - val tmb = directory.thumbnail - val timestampSignature = StringSignature(directory.date_modified.toString()) - if (tmb.isGif()) { - if (animateGifs) { - Glide.with(activity).load(tmb).asGif().diskCacheStrategy(DiskCacheStrategy.NONE).signature(timestampSignature) - .placeholder(backgroundColor).centerCrop().crossFade().into(itemView.dir_thumbnail) + val tmb = directory.thumbnail + val timestampSignature = StringSignature(directory.date_modified.toString()) + if (tmb.isGif()) { + if (animateGifs) { + Glide.with(activity).load(tmb).asGif().diskCacheStrategy(DiskCacheStrategy.NONE).signature(timestampSignature) + .placeholder(backgroundColor).centerCrop().crossFade().into(dir_thumbnail) + } else { + Glide.with(activity).load(tmb).asBitmap().diskCacheStrategy(DiskCacheStrategy.RESULT).signature(timestampSignature) + .placeholder(backgroundColor).centerCrop().into(dir_thumbnail) + } + } else if (tmb.toLowerCase().endsWith(".png")) { + Glide.with(activity).load(tmb).asBitmap().format(DecodeFormat.PREFER_ARGB_8888).diskCacheStrategy(DiskCacheStrategy.RESULT) + .signature(timestampSignature).placeholder(backgroundColor).centerCrop().into(dir_thumbnail) } else { - Glide.with(activity).load(tmb).asBitmap().diskCacheStrategy(DiskCacheStrategy.RESULT).signature(timestampSignature) - .placeholder(backgroundColor).centerCrop().into(itemView.dir_thumbnail) + Glide.with(activity).load(tmb).diskCacheStrategy(DiskCacheStrategy.RESULT).signature(timestampSignature) + .placeholder(backgroundColor).centerCrop().crossFade().into(dir_thumbnail) } - } else if (tmb.toLowerCase().endsWith(".png")) { - Glide.with(activity).load(tmb).asBitmap().format(DecodeFormat.PREFER_ARGB_8888).diskCacheStrategy(DiskCacheStrategy.RESULT) - .signature(timestampSignature).placeholder(backgroundColor).centerCrop().into(itemView.dir_thumbnail) - } else { - Glide.with(activity).load(tmb).diskCacheStrategy(DiskCacheStrategy.RESULT).signature(timestampSignature) - .placeholder(backgroundColor).centerCrop().crossFade().into(itemView.dir_thumbnail) - } - itemView.setOnClickListener { viewClicked(multiSelector, directory, pos) } - itemView.setOnLongClickListener { - if (!multiSelector.isSelectable) { - activity.startSupportActionMode(multiSelectorCallback) - multiSelector.setSelected(this, true) - updateTitle(multiSelector.selectedPositions.size) - toggleItemSelection(itemView, true, pos) - actMode?.invalidate() + setOnClickListener { viewClicked(multiSelector, directory, pos) } + setOnLongClickListener { + if (!multiSelector.isSelectable) { + activity.startSupportActionMode(multiSelectorCallback) + multiSelector.setSelected(this@ViewHolder, true) + updateTitle(multiSelector.selectedPositions.size) + toggleItemSelection(this, true, pos) + actMode?.invalidate() + } + true } - true - } - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) - (getProperView(itemView) as FrameLayout).foreground = foregroundColor.createSelector() - else - getProperView(itemView).foreground = foregroundColor.createSelector() + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) + (getProperView(this) as FrameLayout).foreground = foregroundColor.createSelector() + else + getProperView(this).foreground = foregroundColor.createSelector() + } return itemView } 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 0061d429a..e7a7b31bc 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/MediaAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/MediaAdapter.kt @@ -243,46 +243,47 @@ class MediaAdapter(val activity: SimpleActivity, var media: MutableList, class ViewHolder(view: View, val itemClick: (Medium) -> (Unit)) : SwappingHolder(view, MultiSelector()) { fun bindView(activity: SimpleActivity, multiSelectorCallback: ModalMultiSelectorCallback, multiSelector: MultiSelector, medium: Medium, pos: Int): View { - itemView.play_outline.visibility = if (medium.isVideo) View.VISIBLE else View.GONE - itemView.file_name.beVisibleIf(displayFilenames) - itemView.file_name.text = medium.name - toggleItemSelection(itemView, markedItems.contains(pos), pos) + itemView.apply { + play_outline.visibility = if (medium.isVideo) View.VISIBLE else View.GONE + file_name.beVisibleIf(displayFilenames) + file_name.text = medium.name + toggleItemSelection(this, markedItems.contains(pos), pos) - val path = medium.path - val timestampSignature = StringSignature(medium.date_modified.toString()) - if (medium.isGif()) { - if (animateGifs) { - Glide.with(activity).load(path).asGif().diskCacheStrategy(DiskCacheStrategy.NONE).signature(timestampSignature) - .placeholder(backgroundColor).centerCrop().crossFade().into(itemView.medium_thumbnail) + val path = medium.path + val timestampSignature = StringSignature(medium.date_modified.toString()) + if (medium.isGif()) { + if (animateGifs) { + Glide.with(activity).load(path).asGif().diskCacheStrategy(DiskCacheStrategy.NONE).signature(timestampSignature) + .placeholder(backgroundColor).centerCrop().crossFade().into(medium_thumbnail) + } else { + Glide.with(activity).load(path).asBitmap().diskCacheStrategy(DiskCacheStrategy.NONE).signature(timestampSignature) + .placeholder(backgroundColor).centerCrop().into(medium_thumbnail) + } + } else if (medium.isPng()) { + Glide.with(activity).load(path).asBitmap().format(DecodeFormat.PREFER_ARGB_8888).diskCacheStrategy(DiskCacheStrategy.RESULT) + .signature(timestampSignature).placeholder(backgroundColor).centerCrop().into(medium_thumbnail) } else { - Glide.with(activity).load(path).asBitmap().diskCacheStrategy(DiskCacheStrategy.NONE).signature(timestampSignature) - .placeholder(backgroundColor).centerCrop().into(itemView.medium_thumbnail) + Glide.with(activity).load(path).diskCacheStrategy(DiskCacheStrategy.RESULT).signature(timestampSignature) + .placeholder(backgroundColor).centerCrop().crossFade().into(medium_thumbnail) } - } else if (medium.isPng()) { - Glide.with(activity).load(path).asBitmap().format(DecodeFormat.PREFER_ARGB_8888).diskCacheStrategy(DiskCacheStrategy.RESULT) - .signature(timestampSignature).placeholder(backgroundColor).centerCrop().into(itemView.medium_thumbnail) - } else { - Glide.with(activity).load(path).diskCacheStrategy(DiskCacheStrategy.RESULT).signature(timestampSignature) - .placeholder(backgroundColor).centerCrop().crossFade().into(itemView.medium_thumbnail) - } - itemView.setOnClickListener { viewClicked(multiSelector, medium, pos) } - itemView.setOnLongClickListener { - if (!multiSelector.isSelectable) { - activity.startSupportActionMode(multiSelectorCallback) - multiSelector.setSelected(this, true) - updateTitle(multiSelector.selectedPositions.size) - toggleItemSelection(itemView, true, pos) - actMode?.invalidate() + setOnClickListener { viewClicked(multiSelector, medium, pos) } + setOnLongClickListener { + if (!multiSelector.isSelectable) { + activity.startSupportActionMode(multiSelectorCallback) + multiSelector.setSelected(this@ViewHolder, true) + updateTitle(multiSelector.selectedPositions.size) + toggleItemSelection(this, true, pos) + actMode?.invalidate() + } + true } - true + + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) + (getProperView(this) as FrameLayout).foreground = foregroundColor.createSelector() + else + getProperView(this).foreground = foregroundColor.createSelector() } - - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) - (getProperView(itemView) as FrameLayout).foreground = foregroundColor.createSelector() - else - getProperView(itemView).foreground = foregroundColor.createSelector() - return itemView }