[Android] Improve viewmodels ML callback delegation

Geoffrey Métais git at videolan.org
Mon Jul 8 18:28:49 CEST 2019


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Mon Jul  8 18:00:33 2019 +0200| [aeb400640a654f6bd7ff95988c45c326784452d2] | committer: Geoffrey Métais

Improve viewmodels ML callback delegation

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

 .../videolan/vlc/viewmodels/CallBackDelegate.kt    | 37 ++++++----------------
 .../vlc/viewmodels/tv/MediaBrowserViewModel.kt     | 18 +++--------
 2 files changed, 13 insertions(+), 42 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/viewmodels/CallBackDelegate.kt b/vlc-android/src/org/videolan/vlc/viewmodels/CallBackDelegate.kt
index 6d9e033f3..0db25298e 100644
--- a/vlc-android/src/org/videolan/vlc/viewmodels/CallBackDelegate.kt
+++ b/vlc-android/src/org/videolan/vlc/viewmodels/CallBackDelegate.kt
@@ -26,14 +26,7 @@ import kotlinx.coroutines.channels.SendChannel
 import org.videolan.medialibrary.interfaces.AbstractMedialibrary
 import org.videolan.tools.conflatedActor
 
-interface ICallBackHandler : AbstractMedialibrary.OnMedialibraryReadyListener,
-        AbstractMedialibrary.OnDeviceChangeListener,
-        AbstractMedialibrary.MediaCb,
-        AbstractMedialibrary.ArtistsCb,
-        AbstractMedialibrary.AlbumsCb,
-        AbstractMedialibrary.GenresCb,
-        AbstractMedialibrary.PlaylistsCb
-{
+interface ICallBackHandler {
     val medialibrary : AbstractMedialibrary
 
     fun CoroutineScope.registerCallBacks(refresh: () -> Unit)
@@ -43,28 +36,16 @@ interface ICallBackHandler : AbstractMedialibrary.OnMedialibraryReadyListener,
     fun watchAlbums()
     fun watchGenres()
     fun watchPlaylists()
-
-    override fun onMedialibraryReady()
-    override fun onMedialibraryIdle()
-    override fun onDeviceChange()
-    override fun onMediaAdded()
-    override fun onMediaModified()
-    override fun onMediaDeleted()
-    override fun onArtistsAdded()
-    override fun onArtistsModified()
-    override fun onArtistsDeleted()
-    override fun onAlbumsAdded()
-    override fun onAlbumsModified()
-    override fun onAlbumsDeleted()
-    override fun onGenresAdded()
-    override fun onGenresModified()
-    override fun onGenresDeleted()
-    override fun onPlaylistsAdded()
-    override fun onPlaylistsModified()
-    override fun onPlaylistsDeleted()
 }
 
-class CallBackDelegate : ICallBackHandler {
+class CallBackDelegate : ICallBackHandler,
+        AbstractMedialibrary.OnMedialibraryReadyListener,
+        AbstractMedialibrary.OnDeviceChangeListener,
+        AbstractMedialibrary.MediaCb,
+        AbstractMedialibrary.ArtistsCb,
+        AbstractMedialibrary.AlbumsCb,
+        AbstractMedialibrary.GenresCb,
+        AbstractMedialibrary.PlaylistsCb{
 
     override val medialibrary = AbstractMedialibrary.getInstance()
     private lateinit var refreshActor: SendChannel<Unit>
diff --git a/vlc-android/src/org/videolan/vlc/viewmodels/tv/MediaBrowserViewModel.kt b/vlc-android/src/org/videolan/vlc/viewmodels/tv/MediaBrowserViewModel.kt
index 836ea10c5..68fd4ea54 100644
--- a/vlc-android/src/org/videolan/vlc/viewmodels/tv/MediaBrowserViewModel.kt
+++ b/vlc-android/src/org/videolan/vlc/viewmodels/tv/MediaBrowserViewModel.kt
@@ -33,23 +33,13 @@ class MediaBrowserViewModel(context: Context, val category: Long) : Medialibrary
 
     init {
         when(category){
-            CATEGORY_ALBUMS -> medialibrary.addAlbumsCb(this)
-            CATEGORY_ARTISTS -> medialibrary.addArtistsCb(this)
-            CATEGORY_GENRES -> medialibrary.addGenreCb(this)
-            else -> medialibrary.addMediaCb(this)
+            CATEGORY_ALBUMS -> watchAlbums()
+            CATEGORY_ARTISTS -> watchArtists()
+            CATEGORY_GENRES -> watchGenres()
+            else -> watchMedia()
         }
     }
 
-    override fun onCleared() {
-        when(category){
-            CATEGORY_ALBUMS -> medialibrary.removeAlbumsCb(this)
-            CATEGORY_ARTISTS -> medialibrary.removeArtistsCb(this)
-            CATEGORY_GENRES -> medialibrary.removeGenreCb(this)
-            else -> medialibrary.removeMediaCb(this)
-        }
-        super.onCleared()
-    }
-
     class Factory(private val context: Context, private val category: Long) : ViewModelProvider.NewInstanceFactory() {
         override fun <T : ViewModel> create(modelClass: Class<T>): T {
             @Suppress("UNCHECKED_CAST")



More information about the Android mailing list