[Android] ViewModels: remove lazy loading of dataset

Geoffrey Métais git at videolan.org
Mon Jan 7 10:18:27 CET 2019


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Mon Jan  7 10:09:39 2019 +0100| [c78f9d33a1a63504e148e1a2027c52b0316af53f] | committer: Geoffrey Métais

ViewModels: remove lazy loading of dataset

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

 .../org/videolan/vlc/gui/browser/FilePickerFragment.kt    |  4 ++--
 vlc-android/src/org/videolan/vlc/viewmodels/BaseModel.kt  |  7 +------
 .../src/org/videolan/vlc/viewmodels/HistoryModel.kt       |  6 +-----
 .../src/org/videolan/vlc/viewmodels/MedialibraryModel.kt  | 15 ++++++---------
 .../org/videolan/vlc/viewmodels/browser/BrowserModel.kt   |  2 --
 .../src/org/videolan/vlc/viewmodels/paged/MLPagedModel.kt | 10 +++++-----
 6 files changed, 15 insertions(+), 29 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/gui/browser/FilePickerFragment.kt b/vlc-android/src/org/videolan/vlc/gui/browser/FilePickerFragment.kt
index 9306d3384..dd51be48d 100644
--- a/vlc-android/src/org/videolan/vlc/gui/browser/FilePickerFragment.kt
+++ b/vlc-android/src/org/videolan/vlc/gui/browser/FilePickerFragment.kt
@@ -24,12 +24,12 @@
 package org.videolan.vlc.gui.browser
 
 import android.app.Activity
-import androidx.lifecycle.ViewModelProviders
 import android.content.Intent
 import android.net.Uri
 import android.os.Bundle
 import android.text.TextUtils
 import android.view.View
+import androidx.lifecycle.ViewModelProviders
 import kotlinx.coroutines.Dispatchers
 import kotlinx.coroutines.runBlocking
 import org.videolan.medialibrary.media.MediaLibraryItem
@@ -99,7 +99,7 @@ class FilePickerFragment : FileBrowserFragment() {
             TextUtils.equals(mrl?.removeFileProtocole(), AndroidDevices.EXTERNAL_PUBLIC_DIRECTORY) -> {
                 mrl = null
                 isRootDirectory = true
-                viewModel.fetch()
+                viewModel.refresh()
             }
             mrl != null -> {
                 val mw = MediaWrapper(Uri.parse(FileUtils.getParent(mrl)))
diff --git a/vlc-android/src/org/videolan/vlc/viewmodels/BaseModel.kt b/vlc-android/src/org/videolan/vlc/viewmodels/BaseModel.kt
index 2f2b3bc2b..5312f53eb 100644
--- a/vlc-android/src/org/videolan/vlc/viewmodels/BaseModel.kt
+++ b/vlc-android/src/org/videolan/vlc/viewmodels/BaseModel.kt
@@ -38,10 +38,7 @@ abstract class BaseModel<T : MediaLibraryItem>(context: Context) : SortableModel
 
     private val filter by lazy(LazyThreadSafetyMode.NONE) { FilterDelegate(dataset) }
 
-    val dataset by lazy {
-        fetch()
-        LiveDataset<T>()
-    }
+    val dataset = LiveDataset<T>()
 
     val categories by lazy(LazyThreadSafetyMode.NONE) {
         MediatorLiveData<Map<String, List<MediaLibraryItem>>>().apply {
@@ -116,8 +113,6 @@ abstract class BaseModel<T : MediaLibraryItem>(context: Context) : SortableModel
 
     protected open suspend fun updateList() {}
 
-    protected abstract fun fetch()
-
     override fun onCleared() {
         updateActor.close()
         super.onCleared()
diff --git a/vlc-android/src/org/videolan/vlc/viewmodels/HistoryModel.kt b/vlc-android/src/org/videolan/vlc/viewmodels/HistoryModel.kt
index 6da74e782..81682dc14 100644
--- a/vlc-android/src/org/videolan/vlc/viewmodels/HistoryModel.kt
+++ b/vlc-android/src/org/videolan/vlc/viewmodels/HistoryModel.kt
@@ -20,9 +20,9 @@
 
 package org.videolan.vlc.viewmodels
 
+import android.content.Context
 import androidx.lifecycle.ViewModel
 import androidx.lifecycle.ViewModelProvider
-import android.content.Context
 import kotlinx.coroutines.Dispatchers
 import kotlinx.coroutines.withContext
 import org.videolan.medialibrary.Medialibrary
@@ -32,10 +32,6 @@ class HistoryModel(context: Context) : BaseModel<MediaWrapper>(context) {
 
     override fun canSortByName() = false
 
-    override fun fetch() {
-        refresh()
-    }
-
     override suspend fun updateList() {
         dataset.value = withContext(Dispatchers.Default) { Medialibrary.getInstance().lastMediaPlayed().toMutableList() }
     }
diff --git a/vlc-android/src/org/videolan/vlc/viewmodels/MedialibraryModel.kt b/vlc-android/src/org/videolan/vlc/viewmodels/MedialibraryModel.kt
index 3fd62b7af..109169f49 100644
--- a/vlc-android/src/org/videolan/vlc/viewmodels/MedialibraryModel.kt
+++ b/vlc-android/src/org/videolan/vlc/viewmodels/MedialibraryModel.kt
@@ -31,12 +31,11 @@ abstract class MedialibraryModel<T : MediaLibraryItem>(context: Context) : BaseM
     val medialibrary = Medialibrary.getInstance()
 
     init {
-        medialibrary.addOnMedialibraryReadyListener(this)
-        medialibrary.addOnDeviceChangeListener(this)
-    }
-
-    override fun fetch() {
-        if (medialibrary.isStarted) onMedialibraryReady()
+        medialibrary.apply {
+            addOnMedialibraryReadyListener(this at MedialibraryModel)
+            medialibrary.addOnDeviceChangeListener(this at MedialibraryModel)
+            if (isStarted) refresh()
+        }
     }
 
     override fun onMedialibraryReady() {
@@ -48,9 +47,7 @@ abstract class MedialibraryModel<T : MediaLibraryItem>(context: Context) : BaseM
     }
 
     override fun onDeviceChange() {
-        launch {
-            refresh()
-        }
+        launch { refresh() }
     }
 
     override fun onCleared() {
diff --git a/vlc-android/src/org/videolan/vlc/viewmodels/browser/BrowserModel.kt b/vlc-android/src/org/videolan/vlc/viewmodels/browser/BrowserModel.kt
index 61fab775b..04251c98a 100644
--- a/vlc-android/src/org/videolan/vlc/viewmodels/browser/BrowserModel.kt
+++ b/vlc-android/src/org/videolan/vlc/viewmodels/browser/BrowserModel.kt
@@ -47,8 +47,6 @@ open class BrowserModel(context: Context, val url: String?, type: Int, showHidde
         else -> FileBrowserProvider(context, dataset, url, showHiddenFiles = showHiddenFiles)
     }
 
-    public override fun fetch() {}
-
     override fun refresh() = provider.refresh()
 
     fun browserRoot() = launch { provider.browseRoot() }
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 a5a792cc1..18f95b52a 100644
--- a/vlc-android/src/org/videolan/vlc/viewmodels/paged/MLPagedModel.kt
+++ b/vlc-android/src/org/videolan/vlc/viewmodels/paged/MLPagedModel.kt
@@ -28,11 +28,11 @@ abstract class MLPagedModel<T : MediaLibraryItem>(context: Context) : SortableMo
             .build()
 
     init {
-        @Suppress("LeakingThis")
-        medialibrary.addOnMedialibraryReadyListener(this)
-        @Suppress("LeakingThis")
-        medialibrary.addOnDeviceChangeListener(this)
-        if (medialibrary.isStarted) onMedialibraryReady()
+        medialibrary.apply {
+            medialibrary.addOnMedialibraryReadyListener(this at MLPagedModel)
+            medialibrary.addOnDeviceChangeListener(this at MLPagedModel)
+            if (isStarted) refresh()
+        }
     }
 
     override fun onMedialibraryReady() {



More information about the Android mailing list