diff options
Diffstat (limited to 'mediapicker/src/main')
4 files changed, 33 insertions, 8 deletions
diff --git a/mediapicker/src/main/kotlin/ca/allanwang/kau/mediapicker/GlideHelper.kt b/mediapicker/src/main/kotlin/ca/allanwang/kau/mediapicker/GlideHelper.kt new file mode 100644 index 0000000..8bb341c --- /dev/null +++ b/mediapicker/src/main/kotlin/ca/allanwang/kau/mediapicker/GlideHelper.kt @@ -0,0 +1,18 @@ +package ca.allanwang.kau.mediapicker + +import android.view.View +import com.bumptech.glide.Glide +import com.bumptech.glide.RequestManager + +/** + * Created by Allan Wang on 29/08/2017. + * + * Basic helper to fetch the [RequestManager] from the activity if it exists, before creating another one + */ +internal interface GlideContract { + fun glide(v: View): RequestManager +} + +internal class GlideDelegate : GlideContract { + override fun glide(v: View) = ((v.context as? MediaPickerCore<*>)?.glide ?: Glide.with(v))!! +}
\ No newline at end of file diff --git a/mediapicker/src/main/kotlin/ca/allanwang/kau/mediapicker/MediaItem.kt b/mediapicker/src/main/kotlin/ca/allanwang/kau/mediapicker/MediaItem.kt index b6f3721..0431465 100644 --- a/mediapicker/src/main/kotlin/ca/allanwang/kau/mediapicker/MediaItem.kt +++ b/mediapicker/src/main/kotlin/ca/allanwang/kau/mediapicker/MediaItem.kt @@ -16,7 +16,7 @@ import com.mikepenz.fastadapter.FastAdapter * Created by Allan Wang on 2017-07-04. */ class MediaItem(val data: MediaModel) - : KauIItem<MediaItem, MediaItem.ViewHolder>(R.layout.kau_iitem_image, { ViewHolder(it) }) { + : KauIItem<MediaItem, MediaItem.ViewHolder>(R.layout.kau_iitem_image, { ViewHolder(it) }), GlideContract by GlideDelegate() { private var failedToLoad = false @@ -38,7 +38,7 @@ class MediaItem(val data: MediaModel) override fun bindView(holder: ViewHolder, payloads: List<Any>?) { super.bindView(holder, payloads) - Glide.with(holder.itemView) + glide(holder.itemView) .load(data.data) .applyMediaOptions(holder.itemView.context) .listener(object : RequestListener<Drawable> { @@ -59,7 +59,7 @@ class MediaItem(val data: MediaModel) override fun unbindView(holder: ViewHolder) { super.unbindView(holder) - Glide.with(holder.itemView).clear(holder.container.imageBase) + glide(holder.itemView).clear(holder.container.imageBase) holder.container.removeBlurInstantly() failedToLoad = false } diff --git a/mediapicker/src/main/kotlin/ca/allanwang/kau/mediapicker/MediaItemBasic.kt b/mediapicker/src/main/kotlin/ca/allanwang/kau/mediapicker/MediaItemBasic.kt index e546afb..2d6cefa 100644 --- a/mediapicker/src/main/kotlin/ca/allanwang/kau/mediapicker/MediaItemBasic.kt +++ b/mediapicker/src/main/kotlin/ca/allanwang/kau/mediapicker/MediaItemBasic.kt @@ -22,7 +22,7 @@ import com.mikepenz.fastadapter.FastAdapter * Created by Allan Wang on 2017-07-04. */ class MediaItemBasic(val data: MediaModel) - : KauIItem<MediaItem, MediaItemBasic.ViewHolder>(R.layout.kau_iitem_image_basic, { ViewHolder(it) }) { + : KauIItem<MediaItem, MediaItemBasic.ViewHolder>(R.layout.kau_iitem_image_basic, { ViewHolder(it) }), GlideContract by GlideDelegate() { companion object { @SuppressLint("NewApi") @@ -40,7 +40,7 @@ class MediaItemBasic(val data: MediaModel) override fun bindView(holder: ViewHolder, payloads: List<Any>?) { super.bindView(holder, payloads) - Glide.with(holder.itemView) + glide(holder.itemView) .load(data.data) .applyMediaOptions(holder.itemView.context) .listener(object : RequestListener<Drawable> { @@ -58,7 +58,7 @@ class MediaItemBasic(val data: MediaModel) override fun unbindView(holder: ViewHolder) { super.unbindView(holder) - Glide.with(holder.itemView).clear(holder.image) + glide(holder.itemView).clear(holder.image) } class ViewHolder(v: View) : RecyclerView.ViewHolder(v) { diff --git a/mediapicker/src/main/kotlin/ca/allanwang/kau/mediapicker/MediaPickerCore.kt b/mediapicker/src/main/kotlin/ca/allanwang/kau/mediapicker/MediaPickerCore.kt index eada173..d518b78 100644 --- a/mediapicker/src/main/kotlin/ca/allanwang/kau/mediapicker/MediaPickerCore.kt +++ b/mediapicker/src/main/kotlin/ca/allanwang/kau/mediapicker/MediaPickerCore.kt @@ -27,6 +27,7 @@ import ca.allanwang.kau.permissions.kauRequestPermissions import ca.allanwang.kau.utils.dimenPixelSize import ca.allanwang.kau.utils.toast import com.bumptech.glide.Glide +import com.bumptech.glide.RequestManager import com.mikepenz.fastadapter.IItem import com.mikepenz.fastadapter.adapters.HeaderAdapter import com.mikepenz.fastadapter.commons.adapters.FastItemAdapter @@ -103,6 +104,7 @@ abstract class MediaPickerCore<T : IItem<*, *>>( const val CACHE_SIZE = 80 } + lateinit var glide: RequestManager private var hasPreloaded = false private var prefetcher: Future<*>? = null @@ -113,6 +115,11 @@ abstract class MediaPickerCore<T : IItem<*, *>>( */ val extraSpace: Int by lazy { resources.displayMetrics.heightPixels } + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + glide = Glide.with(this) + } + fun initializeRecycler(recycler: RecyclerView) { val adapterWrapper = HeaderAdapter<MediaActionItem>() adapterWrapper.wrap(adapter) @@ -176,7 +183,7 @@ abstract class MediaPickerCore<T : IItem<*, *>>( hasPreloaded = true prefetcher = doAsync { models.subList(0, Math.min(models.size, 50)).map { it.data }.forEach { - val target = Glide.with(this@MediaPickerCore).load(it) + val target = glide.load(it) .applyMediaOptions(this@MediaPickerCore) .submit() try { @@ -184,7 +191,7 @@ abstract class MediaPickerCore<T : IItem<*, *>>( } catch (ignored: InterruptedException) { } catch (ignored: ExecutionException) { } finally { - Glide.with(this@MediaPickerCore).clear(target) + glide.clear(target) } } } |