From 4f1689965a6b54c12f2b84456d7300d0fb698a5b Mon Sep 17 00:00:00 2001 From: tibbi Date: Fri, 17 Mar 2017 22:02:24 +0100 Subject: [PATCH] reuse the function for loading directory and media thumbnails, dont copy paste --- .../gallery/adapters/DirectoryAdapter.kt | 41 +++---------------- .../gallery/adapters/MediaAdapter.kt | 37 +---------------- .../gallery/extensions/activity.kt | 39 ++++++++++++++++++ 3 files changed, 45 insertions(+), 72 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 dfd22a4da..e0c29ebb4 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/DirectoryAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/DirectoryAdapter.kt @@ -9,12 +9,14 @@ import com.bignerdranch.android.multiselector.ModalMultiSelectorCallback import com.bignerdranch.android.multiselector.MultiSelector import com.bignerdranch.android.multiselector.SwappingHolder import com.bumptech.glide.Glide -import com.bumptech.glide.load.engine.DiskCacheStrategy import com.simplemobiletools.commons.asynctasks.CopyMoveTask import com.simplemobiletools.commons.dialogs.ConfirmationDialog import com.simplemobiletools.commons.dialogs.PropertiesDialog import com.simplemobiletools.commons.dialogs.RenameItemDialog -import com.simplemobiletools.commons.extensions.* +import com.simplemobiletools.commons.extensions.isAStorageRootFolder +import com.simplemobiletools.commons.extensions.isImageVideoGif +import com.simplemobiletools.commons.extensions.needsStupidWritePermissions +import com.simplemobiletools.commons.extensions.toast import com.simplemobiletools.gallery.R import com.simplemobiletools.gallery.activities.MainActivity import com.simplemobiletools.gallery.activities.SimpleActivity @@ -22,7 +24,6 @@ import com.simplemobiletools.gallery.dialogs.CopyDialog import com.simplemobiletools.gallery.dialogs.ExcludeFolderDialog import com.simplemobiletools.gallery.extensions.* import com.simplemobiletools.gallery.models.Directory -import com.squareup.picasso.Picasso import kotlinx.android.synthetic.main.directory_item.view.* import kotlinx.android.synthetic.main.directory_tmb.view.* import java.io.File @@ -328,39 +329,7 @@ class DirectoryAdapter(val activity: SimpleActivity, val dirs: MutableList, photo_name.beVisibleIf(displayFilenames) photo_name.text = medium.name toggleItemSelection(this, markedItems.contains(pos), pos) - - val path = medium.path - if (medium.isGif()) { - if (animateGifs) { - Glide.with(activity) - .load(path) - .asGif() - .diskCacheStrategy(DiskCacheStrategy.SOURCE) - .centerCrop() - .crossFade() - .into(medium_thumbnail) - } else { - Glide.with(activity) - .load(path) - .asBitmap() - .diskCacheStrategy(DiskCacheStrategy.SOURCE) - .centerCrop() - .into(medium_thumbnail) - } - } else if (path.isVideoFast()) { - Glide.with(activity) - .load(path) - .diskCacheStrategy(DiskCacheStrategy.RESULT) - .centerCrop() - .crossFade() - .into(medium_thumbnail) - } else { - Picasso.with(activity) - .load("file:$path") - .resize(MediaActivity.thumbnailSize, MediaActivity.thumbnailSize) - .centerCrop() - .into(medium_thumbnail) - } + activity.loadImage(medium.path, MediaActivity.thumbnailSize, medium_thumbnail) setOnClickListener { viewClicked(multiSelector, medium, pos) } setOnLongClickListener { diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/activity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/activity.kt index 2d349e2b9..801fdbd40 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/activity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/activity.kt @@ -10,15 +10,20 @@ import android.view.KeyCharacterMap import android.view.KeyEvent import android.view.View import android.view.ViewConfiguration +import com.bumptech.glide.Glide +import com.bumptech.glide.load.engine.DiskCacheStrategy import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.helpers.* import com.simplemobiletools.gallery.BuildConfig import com.simplemobiletools.gallery.R import com.simplemobiletools.gallery.activities.SimpleActivity +import com.simplemobiletools.gallery.adapters.MediaAdapter import com.simplemobiletools.gallery.helpers.NOMEDIA import com.simplemobiletools.gallery.helpers.REQUEST_EDIT_IMAGE import com.simplemobiletools.gallery.helpers.REQUEST_SET_WALLPAPER import com.simplemobiletools.gallery.models.Medium +import com.simplemobiletools.gallery.views.MyImageView +import com.squareup.picasso.Picasso import java.io.File import java.util.* @@ -180,3 +185,37 @@ fun SimpleActivity.removeNoMedia(path: String, callback: () -> Unit) { } } } + +fun Activity.loadImage(path: String, size: Int, target: MyImageView) { + if (path.isGif()) { + if (MediaAdapter.animateGifs) { + Glide.with(this) + .load(path) + .asGif() + .diskCacheStrategy(DiskCacheStrategy.SOURCE) + .centerCrop() + .crossFade() + .into(target) + } else { + Glide.with(this) + .load(path) + .asBitmap() + .diskCacheStrategy(DiskCacheStrategy.SOURCE) + .centerCrop() + .into(target) + } + } else if (path.isVideoFast()) { + Glide.with(this) + .load(path) + .diskCacheStrategy(DiskCacheStrategy.RESULT) + .centerCrop() + .crossFade() + .into(target) + } else { + Picasso.with(this) + .load("file:$path") + .resize(size, size) + .centerCrop() + .into(target) + } +}