[Android] ImageLoader: Sanity check

Geoffrey Métais git at videolan.org
Tue Feb 11 16:00:56 CET 2020


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Tue Feb 11 11:51:10 2020 +0100| [b5e6c19556cd23c2cc99560a7eeaf8d2b69bfd21] | committer: Geoffrey Métais

ImageLoader: Sanity check

> https://code.videolan.org/videolan/vlc-android/commit/b5e6c19556cd23c2cc99560a7eeaf8d2b69bfd21
---

 .../vlc-android/src/org/videolan/vlc/gui/helpers/ImageLoader.kt  | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)

diff --git a/application/vlc-android/src/org/videolan/vlc/gui/helpers/ImageLoader.kt b/application/vlc-android/src/org/videolan/vlc/gui/helpers/ImageLoader.kt
index 4018326bb..69acf34ec 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/helpers/ImageLoader.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/helpers/ImageLoader.kt
@@ -80,7 +80,7 @@ fun loadImage(v: View, item: MediaLibraryItem?, imageWidth: Int = 0, tv: Boolean
     val bitmap = if (cacheKey !== null) BitmapCache.getBitmapFromMemCache(cacheKey) else null
     if (bitmap !== null) updateImageView(bitmap, v, binding, tv = tv, card = card)
     else {
-        v.scope.launch { getImage(v, findInLibrary(item, isMedia), binding, imageWidth, tv = tv, card = card) }
+        v.scope.takeIf { it.isActive }?.launch { getImage(v, findInLibrary(item, isMedia), binding, imageWidth, tv = tv, card = card) }
     }
 }
 
@@ -88,7 +88,7 @@ fun loadPlaylistImageWithWidth(v: ImageView, item: MediaLibraryItem?, imageWidth
     if (imageWidth == 0) return
     if (item == null) return
     val binding = DataBindingUtil.findBinding<ViewDataBinding>(v)
-    v.scope.launch { getPlaylistImage(v, item, binding, imageWidth) }
+    v.scope.takeIf { it.isActive }?.launch { getPlaylistImage(v, item, binding, imageWidth) }
 }
 
 fun getAudioIconDrawable(context: Context?, type: Int, big: Boolean = false): BitmapDrawable? = context?.let {
@@ -180,7 +180,7 @@ fun imageCardViewContent(v: View, content: String?) {
 @BindingAdapter(value = ["imageUri", "tv" ], requireAll = false)
 fun downloadIcon(v: View, imageUri: Uri?, tv: Boolean = true) {
     if (imageUri?.scheme == "http" || imageUri?.scheme == "https") {
-        v.scope.launch {
+        v.scope.takeIf { it.isActive }?.launch {
             val image = withContext(Dispatchers.IO) { HttpImageLoader.downloadBitmap(imageUri.toString()) }
             updateImageView(image, v, DataBindingUtil.findBinding(v), tv = tv)
         }
@@ -192,8 +192,7 @@ fun downloadIcon(v: View, imageUrl: String?, tv: Boolean = true) {
     if (imageUrl.isNullOrEmpty()) return
     val imageUri = Uri.parse(imageUrl)
     if (imageUri?.scheme == "http" || imageUri?.scheme == "https") {
-        val scope = (v.context as? CoroutineScope) ?: AppScope
-        scope.launch {
+        v.scope.takeIf { it.isActive }?.launch {
             val image = withContext(Dispatchers.IO) { HttpImageLoader.downloadBitmap(imageUri.toString()) }
             updateImageView(image, v, DataBindingUtil.findBinding(v), tv = tv)
         }



More information about the Android mailing list