[Android] VideosViewModel: Add media callbacks

Geoffrey Métais git at videolan.org
Tue Jul 2 18:00:11 CEST 2019


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Tue Jul  2 17:59:51 2019 +0200| [97ebb51ec3da8b308ebebeeee419387aa43582a9] | committer: Geoffrey Métais

VideosViewModel: Add media callbacks

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

 .../videolan/vlc/viewmodels/mobile/VideosViewModel.kt | 19 +++++++++++++++++--
 1 file changed, 17 insertions(+), 2 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/viewmodels/mobile/VideosViewModel.kt b/vlc-android/src/org/videolan/vlc/viewmodels/mobile/VideosViewModel.kt
index 6605e8f24..a4706211e 100644
--- a/vlc-android/src/org/videolan/vlc/viewmodels/mobile/VideosViewModel.kt
+++ b/vlc-android/src/org/videolan/vlc/viewmodels/mobile/VideosViewModel.kt
@@ -26,6 +26,7 @@ import androidx.lifecycle.ViewModelProvider
 import androidx.lifecycle.ViewModelProviders
 import kotlinx.coroutines.ExperimentalCoroutinesApi
 import kotlinx.coroutines.ObsoleteCoroutinesApi
+import org.videolan.medialibrary.interfaces.AbstractMedialibrary
 import org.videolan.medialibrary.interfaces.media.AbstractFolder
 import org.videolan.medialibrary.media.MediaLibraryItem
 import org.videolan.vlc.gui.video.VideoGridFragment
@@ -34,10 +35,24 @@ import org.videolan.vlc.providers.medialibrary.VideosProvider
 import org.videolan.vlc.viewmodels.MedialibraryViewModel
 
 @ExperimentalCoroutinesApi
-class VideosViewModel(context: Context, val folder: AbstractFolder?) : MedialibraryViewModel(context) {
+class VideosViewModel(context: Context, val folder: AbstractFolder?) : MedialibraryViewModel(context),
+        AbstractMedialibrary.MediaCb {
     val provider = VideosProvider(folder, context, this)
     override val providers: Array<MedialibraryProvider<out MediaLibraryItem>> = arrayOf(provider)
 
+    init {
+        medialibrary.addMediaCb(this)
+    }
+
+    override fun onCleared() {
+        super.onCleared()
+        medialibrary.removeMediaCb(this)
+    }
+
+    override fun onMediaAdded() { refresh() }
+    override fun onMediaModified() { refresh() }
+    override fun onMediaDeleted() { refresh() }
+
     class Factory(val context: Context, val folder: AbstractFolder?): ViewModelProvider.NewInstanceFactory() {
         override fun <T : ViewModel> create(modelClass: Class<T>): T {
             @Suppress("UNCHECKED_CAST")
@@ -48,4 +63,4 @@ class VideosViewModel(context: Context, val folder: AbstractFolder?) : Medialibr
 
 @ExperimentalCoroutinesApi
 @ObsoleteCoroutinesApi
-internal fun VideoGridFragment.getViewModel(folder: AbstractFolder?) = ViewModelProviders.of(requireActivity(), VideosViewModel.Factory(requireContext(), folder)).get(VideosViewModel::class.java)
\ No newline at end of file
+internal fun VideoGridFragment.getViewModel(folder: AbstractFolder?) = ViewModelProviders.of(requireActivity(), VideosViewModel.Factory(requireContext(), folder)).get(VideosViewModel::class.java)



More information about the Android mailing list