[Android] Viewmodel code cleaning

Geoffrey Métais git at videolan.org
Thu May 9 13:26:50 CEST 2019


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Thu May  9 13:25:45 2019 +0200| [3de9ab5116a8629e06b485fcbd523411997dc4e6] | committer: Geoffrey Métais

Viewmodel code cleaning

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

 vlc-android/src/org/videolan/vlc/gui/HistoryFragment.kt |  4 +---
 .../org/videolan/vlc/gui/browser/BaseBrowserFragment.kt |  4 +---
 .../src/org/videolan/vlc/gui/folders/FoldersFragment.kt |  4 +---
 .../vlc/gui/tv/browser/AudioBrowserTvFragment.kt        |  4 +---
 .../src/org/videolan/vlc/providers/BrowserProvider.kt   |  5 ++---
 .../src/org/videolan/vlc/providers/NetworkProvider.kt   |  6 ++----
 vlc-android/src/org/videolan/vlc/util/ModelsHelper.kt   |  2 +-
 .../src/org/videolan/vlc/viewmodels/BaseModel.kt        |  3 +--
 .../org/videolan/vlc/viewmodels/MedialibraryModel.kt    | 17 +++++++----------
 .../org/videolan/vlc/viewmodels/browser/BrowserModel.kt |  6 +++---
 .../org/videolan/vlc/viewmodels/paged/MLPagedModel.kt   |  5 +----
 11 files changed, 21 insertions(+), 39 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/gui/HistoryFragment.kt b/vlc-android/src/org/videolan/vlc/gui/HistoryFragment.kt
index f1789890b..c220346e7 100644
--- a/vlc-android/src/org/videolan/vlc/gui/HistoryFragment.kt
+++ b/vlc-android/src/org/videolan/vlc/gui/HistoryFragment.kt
@@ -107,9 +107,7 @@ class HistoryFragment : MediaBrowserFragment<HistoryModel>(), IRefreshable, IHis
         if (fabPlay != null) fabPlay!!.hide()
     }
 
-    override fun refresh() {
-        viewModel.refresh()
-    }
+    override fun refresh() = viewModel.refresh()
 
     override fun onRefresh() {
         refresh()
diff --git a/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserFragment.kt b/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserFragment.kt
index f129cca5e..298d84050 100644
--- a/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserFragment.kt
+++ b/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserFragment.kt
@@ -249,9 +249,7 @@ abstract class BaseBrowserFragment : MediaBrowserFragment<BrowserModel>(), IRefr
         }
     }
 
-    override fun refresh() {
-        viewModel.refresh()
-    }
+    override fun refresh() = viewModel.refresh()
 
     override fun onClick(v: View) {
         when (v.id) {
diff --git a/vlc-android/src/org/videolan/vlc/gui/folders/FoldersFragment.kt b/vlc-android/src/org/videolan/vlc/gui/folders/FoldersFragment.kt
index a199bb8ae..7764a7f19 100644
--- a/vlc-android/src/org/videolan/vlc/gui/folders/FoldersFragment.kt
+++ b/vlc-android/src/org/videolan/vlc/gui/folders/FoldersFragment.kt
@@ -92,9 +92,7 @@ class FoldersFragment : MediaBrowserFragment<PagedFoldersModel>(), CoroutineScop
 
     override fun getTitle(): String = getString(R.string.video)
 
-    override fun onRefresh() {
-        viewModel.refresh()
-    }
+    override fun onRefresh() = viewModel.refresh()
 
     override fun onPrepareOptionsMenu(menu: Menu) {
         super.onPrepareOptionsMenu(menu)
diff --git a/vlc-android/src/org/videolan/vlc/gui/tv/browser/AudioBrowserTvFragment.kt b/vlc-android/src/org/videolan/vlc/gui/tv/browser/AudioBrowserTvFragment.kt
index 3fd582607..96a8831d1 100644
--- a/vlc-android/src/org/videolan/vlc/gui/tv/browser/AudioBrowserTvFragment.kt
+++ b/vlc-android/src/org/videolan/vlc/gui/tv/browser/AudioBrowserTvFragment.kt
@@ -329,9 +329,7 @@ class AudioBrowserTvFragment : Fragment(), BrowserFragmentInterface, IEventsHand
     }
 
 
-    override fun refresh() {
-        viewModel.refresh()
-    }
+    override fun refresh() = viewModel.refresh()
 
     override fun onClick(v: View, position: Int, item: MediaLibraryItem) {
         launch { TvUtil.openMediaFromPaged(requireActivity(), item, viewModel.provider) }
diff --git a/vlc-android/src/org/videolan/vlc/providers/BrowserProvider.kt b/vlc-android/src/org/videolan/vlc/providers/BrowserProvider.kt
index 12b27a358..d42f3902f 100644
--- a/vlc-android/src/org/videolan/vlc/providers/BrowserProvider.kt
+++ b/vlc-android/src/org/videolan/vlc/providers/BrowserProvider.kt
@@ -116,11 +116,10 @@ abstract class BrowserProvider(val context: Context, val dataset: LiveDataset<Me
 
     protected open fun addMedia(media: MediaLibraryItem) = dataset.add(media)
 
-    open fun refresh() : Boolean {
-        if (url === null) return false
+    open fun refresh() {
+        if (url === null) return
         loading.value = true
         browserActor.post(Refresh)
-        return true
     }
 
     internal open fun parseSubDirectories() {
diff --git a/vlc-android/src/org/videolan/vlc/providers/NetworkProvider.kt b/vlc-android/src/org/videolan/vlc/providers/NetworkProvider.kt
index 37d735a2c..8af08c9f2 100644
--- a/vlc-android/src/org/videolan/vlc/providers/NetworkProvider.kt
+++ b/vlc-android/src/org/videolan/vlc/providers/NetworkProvider.kt
@@ -49,19 +49,17 @@ class NetworkProvider(context: Context, dataset: LiveDataset<MediaLibraryItem>,
 
     override fun fetch() {}
 
-    override fun refresh(): Boolean {
+    override fun refresh() {
         val list by lazy(LazyThreadSafetyMode.NONE) { getList(url!!) }
-        return if (url == null) {
+        if (url == null) {
             dataset.value = mutableListOf<MediaLibraryItem>().apply {
                 getFavoritesList(favorites?.value)?.let { addAll(it) }
             }
             browseRoot()
-            true
         } else if (list !== null) {
             dataset.value = list as MutableList<MediaLibraryItem>
             removeList(url)
             parseSubDirectories()
-            true
         } else super.refresh()
 
     }
diff --git a/vlc-android/src/org/videolan/vlc/util/ModelsHelper.kt b/vlc-android/src/org/videolan/vlc/util/ModelsHelper.kt
index 87fdfb89c..6c538c233 100644
--- a/vlc-android/src/org/videolan/vlc/util/ModelsHelper.kt
+++ b/vlc-android/src/org/videolan/vlc/util/ModelsHelper.kt
@@ -252,7 +252,7 @@ object EmptyPBSCallback : PlaybackService.Callback {
 }
 
 interface RefreshModel {
-    fun refresh(): Boolean
+    fun refresh()
 }
 
 fun SortableModel.canSortBy(sort: Int) = when (sort) {
diff --git a/vlc-android/src/org/videolan/vlc/viewmodels/BaseModel.kt b/vlc-android/src/org/videolan/vlc/viewmodels/BaseModel.kt
index 477c177e4..277ba7ee3 100644
--- a/vlc-android/src/org/videolan/vlc/viewmodels/BaseModel.kt
+++ b/vlc-android/src/org/videolan/vlc/viewmodels/BaseModel.kt
@@ -65,9 +65,8 @@ abstract class BaseModel<T : MediaLibraryItem>(context: Context) : SortableModel
 
     fun isEmpty() = dataset.value.isNullOrEmpty()
 
-    override fun refresh() : Boolean {
+    override fun refresh() {
         if (!updateActor.isClosedForSend) updateActor.offer(Refresh)
-        return true
     }
 
     fun remove(mw: T) {
diff --git a/vlc-android/src/org/videolan/vlc/viewmodels/MedialibraryModel.kt b/vlc-android/src/org/videolan/vlc/viewmodels/MedialibraryModel.kt
index 6b9a63d6c..a085cf26c 100644
--- a/vlc-android/src/org/videolan/vlc/viewmodels/MedialibraryModel.kt
+++ b/vlc-android/src/org/videolan/vlc/viewmodels/MedialibraryModel.kt
@@ -21,10 +21,13 @@
 package org.videolan.vlc.viewmodels
 
 import android.content.Context
-import kotlinx.coroutines.launch
+import kotlinx.coroutines.ExperimentalCoroutinesApi
+import kotlinx.coroutines.ObsoleteCoroutinesApi
 import org.videolan.medialibrary.Medialibrary
 import org.videolan.medialibrary.media.MediaLibraryItem
 
+ at ObsoleteCoroutinesApi
+ at ExperimentalCoroutinesApi
 abstract class MedialibraryModel<T : MediaLibraryItem>(context: Context) : BaseModel<T>(context), Medialibrary.OnMedialibraryReadyListener, Medialibrary.OnDeviceChangeListener {
 
     val medialibrary = Medialibrary.getInstance()
@@ -36,17 +39,11 @@ abstract class MedialibraryModel<T : MediaLibraryItem>(context: Context) : BaseM
         }
     }
 
-    override fun onMedialibraryReady() {
-        launch { refresh() }
-    }
+    override fun onMedialibraryReady() = refresh()
 
-    override fun onMedialibraryIdle() {
-        launch { refresh() }
-    }
+    override fun onMedialibraryIdle() = refresh()
 
-    override fun onDeviceChange() {
-        launch { refresh() }
-    }
+    override fun onDeviceChange() = refresh()
 
     override fun onCleared() {
         medialibrary.removeOnMedialibraryReadyListener(this)
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 38ffcad28..9378c9ed7 100644
--- a/vlc-android/src/org/videolan/vlc/viewmodels/browser/BrowserModel.kt
+++ b/vlc-android/src/org/videolan/vlc/viewmodels/browser/BrowserModel.kt
@@ -24,9 +24,7 @@ import android.content.Context
 import androidx.annotation.MainThread
 import androidx.lifecycle.ViewModel
 import androidx.lifecycle.ViewModelProvider
-import kotlinx.coroutines.Dispatchers
-import kotlinx.coroutines.launch
-import kotlinx.coroutines.withContext
+import kotlinx.coroutines.*
 import org.videolan.medialibrary.media.MediaLibraryItem
 import org.videolan.medialibrary.media.MediaWrapper
 import org.videolan.vlc.providers.*
@@ -38,6 +36,8 @@ const val TYPE_NETWORK = 1
 const val TYPE_PICKER = 2
 const val TYPE_STORAGE = 3
 
+ at ObsoleteCoroutinesApi
+ at ExperimentalCoroutinesApi
 open class BrowserModel(context: Context, val url: String?, type: Int, showHiddenFiles: Boolean) : BaseModel<MediaLibraryItem>(context) {
 
     protected val provider: BrowserProvider = when (type) {
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 831b636e4..6540ba551 100644
--- a/vlc-android/src/org/videolan/vlc/viewmodels/paged/MLPagedModel.kt
+++ b/vlc-android/src/org/videolan/vlc/viewmodels/paged/MLPagedModel.kt
@@ -109,14 +109,11 @@ abstract class MLPagedModel<T : MediaLibraryItem>(context: Context) : SortableMo
 
     fun isEmpty() = pagedList.value.isNullOrEmpty()
 
-    override fun refresh(): Boolean {
+    override fun refresh() {
         if (this::restoreJob.isInitialized && restoreJob.isActive) restoreJob.cancel()
         launch { provider.refresh() }
-        return true
     }
 
-    protected fun completeHeaders(list: Array<T>, startposition: Int) = provider.completeHeaders(list, startposition)
-
     @MainThread
     fun getSectionforPosition(position: Int) = provider.getSectionforPosition(position)
 



More information about the Android mailing list