aboutsummaryrefslogtreecommitdiff
path: root/mediapicker/src/main/kotlin
diff options
context:
space:
mode:
authorAllan Wang <me@allanwang.ca>2018-12-24 20:05:06 -0500
committerGitHub <noreply@github.com>2018-12-24 20:05:06 -0500
commit8447b1ae8ce89b3f1bbe79dbae8847d901831c12 (patch)
treece516950e452581766e905ead32970d891bb46f6 /mediapicker/src/main/kotlin
parent701b94ab09ff53aca682fac6c4ef5364566339be (diff)
downloadkau-8447b1ae8ce89b3f1bbe79dbae8847d901831c12.tar.gz
kau-8447b1ae8ce89b3f1bbe79dbae8847d901831c12.tar.bz2
kau-8447b1ae8ce89b3f1bbe79dbae8847d901831c12.zip
Enhancement/coroutines (#180)
* Add coroutine dependency * Add coroutines to kprefactivity * Change base job to supervisor * Update coroutines for faq * Update changelog * Use preloading in media picker core * Make test logging internal * Remove anko
Diffstat (limited to 'mediapicker/src/main/kotlin')
-rw-r--r--mediapicker/src/main/kotlin/ca/allanwang/kau/mediapicker/MediaPickerCore.kt33
1 files changed, 10 insertions, 23 deletions
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 abdc266..7004967 100644
--- a/mediapicker/src/main/kotlin/ca/allanwang/kau/mediapicker/MediaPickerCore.kt
+++ b/mediapicker/src/main/kotlin/ca/allanwang/kau/mediapicker/MediaPickerCore.kt
@@ -49,10 +49,8 @@ import com.mikepenz.fastadapter.adapters.ItemAdapter
import com.mikepenz.google_material_typeface_library.GoogleMaterial
import com.mikepenz.iconics.IconicsDrawable
import com.mikepenz.iconics.typeface.IIcon
-import org.jetbrains.anko.doAsync
+import kotlinx.coroutines.CancellationException
import java.io.File
-import java.util.concurrent.ExecutionException
-import java.util.concurrent.Future
/**
* Created by Allan Wang on 2017-07-23.
@@ -121,7 +119,6 @@ abstract class MediaPickerCore<T : IItem<*, *>>(
lateinit var glide: RequestManager
private var hasPreloaded = false
- private var prefetcher: Future<*>? = null
val adapter = ItemAdapter<T>()
@@ -137,7 +134,7 @@ abstract class MediaPickerCore<T : IItem<*, *>>(
fun initializeRecycler(recycler: RecyclerView) {
val adapterHeader = ItemAdapter<MediaActionItem>()
- val fulladapter = fastAdapter(adapterHeader, adapter)
+ val fulladapter = fastAdapter<IItem<*, *>>(adapterHeader, adapter)
adapterHeader.add(mediaActions.map { MediaActionItem(it, mediaType) })
recycler.apply {
val manager = object : GridLayoutManager(context, computeColumnCount(context)) {
@@ -146,7 +143,6 @@ abstract class MediaPickerCore<T : IItem<*, *>>(
}
}
setItemViewCacheSize(CACHE_SIZE)
- isDrawingCacheEnabled = true
layoutManager = manager
adapter = fulladapter
setHasFixedSize(true)
@@ -195,18 +191,14 @@ abstract class MediaPickerCore<T : IItem<*, *>>(
addItems(models.map { converter(it) })
if (!hasPreloaded && mediaType == MediaType.VIDEO) {
hasPreloaded = true
- prefetcher = doAsync {
- models.subList(0, Math.min(models.size, 50)).map { it.data }.forEach {
- val target = glide.load(it)
- .applyMediaOptions(this@MediaPickerCore)
- .submit()
- try {
- target.get()
- } catch (ignored: InterruptedException) {
- } catch (ignored: ExecutionException) {
- } finally {
- glide.clear(target)
- }
+ val preloads = models.subList(0, Math.min(models.size, 50)).map {
+ glide.load(it.data)
+ .applyMediaOptions(this@MediaPickerCore)
+ .preload()
+ }
+ job.invokeOnCompletion {
+ if (it is CancellationException) {
+ preloads.forEach(glide::clear)
}
}
}
@@ -242,11 +234,6 @@ abstract class MediaPickerCore<T : IItem<*, *>>(
open fun onStatusChange(loaded: Boolean) {}
- override fun onDestroy() {
- prefetcher?.cancel(true)
- super.onDestroy()
- }
-
/**
* Method used to retrieve uri data for API 19+
* See <a href="http://hmkcode.com/android-display-selected-image-and-its-real-path/"></a>