[Android] Extend OnDeviceChangeListener to all ViewModels

Geoffrey Métais git at videolan.org
Wed Dec 5 15:03:16 CET 2018


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Wed Dec  5 15:02:35 2018 +0100| [1b680520512f145c4cc4b0d8fa552f74796152c1] | committer: Geoffrey Métais

Extend OnDeviceChangeListener to all ViewModels

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

 .../src/org/videolan/vlc/viewmodels/MedialibraryModel.kt      |  7 ++++---
 .../src/org/videolan/vlc/viewmodels/paged/MLPagedModel.kt     | 11 ++++++++++-
 2 files changed, 14 insertions(+), 4 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/viewmodels/MedialibraryModel.kt b/vlc-android/src/org/videolan/vlc/viewmodels/MedialibraryModel.kt
index 2a355e880..3fd62b7af 100644
--- a/vlc-android/src/org/videolan/vlc/viewmodels/MedialibraryModel.kt
+++ b/vlc-android/src/org/videolan/vlc/viewmodels/MedialibraryModel.kt
@@ -21,7 +21,6 @@
 package org.videolan.vlc.viewmodels
 
 import android.content.Context
-import kotlinx.coroutines.delay
 import kotlinx.coroutines.launch
 import org.videolan.medialibrary.Medialibrary
 import org.videolan.medialibrary.media.MediaLibraryItem
@@ -31,9 +30,12 @@ abstract class MedialibraryModel<T : MediaLibraryItem>(context: Context) : BaseM
 
     val medialibrary = Medialibrary.getInstance()
 
-    override fun fetch() {
+    init {
         medialibrary.addOnMedialibraryReadyListener(this)
         medialibrary.addOnDeviceChangeListener(this)
+    }
+
+    override fun fetch() {
         if (medialibrary.isStarted) onMedialibraryReady()
     }
 
@@ -47,7 +49,6 @@ abstract class MedialibraryModel<T : MediaLibraryItem>(context: Context) : BaseM
 
     override fun onDeviceChange() {
         launch {
-            delay(1000L)
             refresh()
         }
     }
diff --git a/vlc-android/src/org/videolan/vlc/viewmodels/paged/MLPagedModel.kt b/vlc-android/src/org/videolan/vlc/viewmodels/paged/MLPagedModel.kt
index a7470fd8f..dec33be20 100644
--- a/vlc-android/src/org/videolan/vlc/viewmodels/paged/MLPagedModel.kt
+++ b/vlc-android/src/org/videolan/vlc/viewmodels/paged/MLPagedModel.kt
@@ -11,7 +11,7 @@ import org.videolan.medialibrary.Medialibrary
 import org.videolan.medialibrary.media.MediaLibraryItem
 import org.videolan.vlc.viewmodels.SortableModel
 
-abstract class MLPagedModel<T : MediaLibraryItem>(context: Context) : SortableModel(context), Medialibrary.OnMedialibraryReadyListener {
+abstract class MLPagedModel<T : MediaLibraryItem>(context: Context) : SortableModel(context), Medialibrary.OnMedialibraryReadyListener, Medialibrary.OnDeviceChangeListener {
     protected val medialibrary = Medialibrary.getInstance()
     protected var filter : String? = null
     val loading = MutableLiveData<Boolean>().apply { value = false }
@@ -28,6 +28,8 @@ abstract class MLPagedModel<T : MediaLibraryItem>(context: Context) : SortableMo
     init {
         @Suppress("LeakingThis")
         medialibrary.addOnMedialibraryReadyListener(this)
+        @Suppress("LeakingThis")
+        medialibrary.addOnDeviceChangeListener(this)
         if (medialibrary.isStarted) onMedialibraryReady()
     }
 
@@ -39,8 +41,15 @@ abstract class MLPagedModel<T : MediaLibraryItem>(context: Context) : SortableMo
         launch { refresh() }
     }
 
+    override fun onDeviceChange() {
+        launch {
+            refresh()
+        }
+    }
+
     override fun onCleared() {
         medialibrary.removeOnMedialibraryReadyListener(this)
+        medialibrary.removeOnDeviceChangeListener(this)
         super.onCleared()
     }
 



More information about the Android mailing list