[Android] Setup PlaylistModel on service connect

Geoffrey Métais git at videolan.org
Wed Apr 4 18:35:41 CEST 2018


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Wed Mar 21 11:18:39 2018 +0100| [0fd14b5524ba6b2bdcef5cd28f21e39e0dee945f] | committer: Geoffrey Métais

Setup PlaylistModel on service connect

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

 .../src/org/videolan/vlc/gui/audio/AudioPlayer.kt  |  3 +--
 .../org/videolan/vlc/viewmodels/PlaylistModel.kt   | 25 ++++++++++++----------
 2 files changed, 15 insertions(+), 13 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/gui/audio/AudioPlayer.kt b/vlc-android/src/org/videolan/vlc/gui/audio/AudioPlayer.kt
index ea6b994ec..021a05dda 100644
--- a/vlc-android/src/org/videolan/vlc/gui/audio/AudioPlayer.kt
+++ b/vlc-android/src/org/videolan/vlc/gui/audio/AudioPlayer.kt
@@ -23,7 +23,6 @@ package org.videolan.vlc.gui.audio
 import android.Manifest
 import android.annotation.TargetApi
 import android.arch.lifecycle.Observer
-import android.arch.lifecycle.ViewModelProviders
 import android.content.Context
 import android.content.SharedPreferences
 import android.net.Uri
@@ -461,7 +460,7 @@ class AudioPlayer : Fragment(), PlaylistAdapter.IPlayer, TextWatcher, PlaybackSe
 
     override fun onConnected(service: PlaybackService) {
         this.service = service
-        playlistModel = ViewModelProviders.of(this, PlaylistModel.Factory(service)).get(PlaylistModel::class.java)
+        playlistModel = PlaylistModel.get(this, service).apply { setup() }
         playlistModel.progress.observe(this,  Observer { it?.let { updateProgress(it) } })
         playlistModel.dataset.observe(this, Observer {
             playlistAdapter.update(it!!)
diff --git a/vlc-android/src/org/videolan/vlc/viewmodels/PlaylistModel.kt b/vlc-android/src/org/videolan/vlc/viewmodels/PlaylistModel.kt
index 3144fd089..574f911af 100644
--- a/vlc-android/src/org/videolan/vlc/viewmodels/PlaylistModel.kt
+++ b/vlc-android/src/org/videolan/vlc/viewmodels/PlaylistModel.kt
@@ -3,7 +3,8 @@ package org.videolan.vlc.viewmodels
 import android.arch.lifecycle.MutableLiveData
 import android.arch.lifecycle.ViewModel
 import android.arch.lifecycle.ViewModelProvider
-import android.util.Log
+import android.arch.lifecycle.ViewModelProviders
+import android.support.v4.app.Fragment
 import kotlinx.coroutines.experimental.CoroutineStart
 import kotlinx.coroutines.experimental.android.UI
 import kotlinx.coroutines.experimental.launch
@@ -14,7 +15,6 @@ import org.videolan.vlc.util.EmptyPBSCallback
 import org.videolan.vlc.util.LiveDataset
 import org.videolan.vlc.util.PlaylistFilterDelegate
 
-
 class PlaylistModel(private val service: PlaybackService) : ViewModel(), PlaybackService.Callback by EmptyPBSCallback {
 
     val dataset = LiveDataset<MediaWrapper>()
@@ -22,13 +22,12 @@ class PlaylistModel(private val service: PlaybackService) : ViewModel(), Playbac
 
     private val filter by lazy(LazyThreadSafetyMode.NONE) { PlaylistFilterDelegate(dataset) }
 
-    init {
+    fun setup() {
         service.addCallback(this)
         update()
     }
 
     override fun update() {
-        Log.d("PlaylistModel", "update")
         dataset.value = service.medias
     }
 
@@ -42,19 +41,23 @@ class PlaylistModel(private val service: PlaybackService) : ViewModel(), Playbac
         service.removeCallback(this)
     }
 
-    class Factory(val service: PlaybackService): ViewModelProvider.NewInstanceFactory() {
-        override fun <T : ViewModel> create(modelClass: Class<T>): T {
-            @Suppress("UNCHECKED_CAST")
-            return PlaylistModel(service) as T
-        }
-    }
-
     fun getItemPosition(position: Int, media: MediaWrapper): Int {
         val list = dataset.value
         if (list[position] == media) return position
         else for ((index, item) in list.withIndex()) if (item == media) return index
         return -1
     }
+
+    companion object {
+        fun get(fragment: Fragment, service: PlaybackService) = ViewModelProviders.of(fragment, PlaylistModel.Factory(service)).get(PlaylistModel::class.java)
+    }
+
+    class Factory(val service: PlaybackService): ViewModelProvider.NewInstanceFactory() {
+        override fun <T : ViewModel> create(modelClass: Class<T>): T {
+            @Suppress("UNCHECKED_CAST")
+            return PlaylistModel(service) as T
+        }
+    }
 }
 
 data class PlaybackProgress(val time: Long, val length: Long, val timeText : String = Tools.millisToString(time), val lengthText : String  = Tools.millisToString(length))
\ No newline at end of file



More information about the Android mailing list