[Android] Clean ViewModels code

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


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Wed Mar 28 11:59:13 2018 +0200| [244ac98f3cb4678f4bc474d5a409511da5aebc71] | committer: Geoffrey Métais

Clean ViewModels code

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

 .../vlc/gui/tv/browser/CategoriesFragment.kt       |  8 +++++---
 .../vlc/gui/tv/browser/VideoBrowserFragment.kt     |  2 +-
 .../src/org/videolan/vlc/util/ModelsHelper.kt      | 18 ++++++++++++++++++
 .../src/org/videolan/vlc/viewmodels/BaseModel.kt   | 22 +---------------------
 4 files changed, 25 insertions(+), 25 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/gui/tv/browser/CategoriesFragment.kt b/vlc-android/src/org/videolan/vlc/gui/tv/browser/CategoriesFragment.kt
index 95389c10c..e971d8d2f 100644
--- a/vlc-android/src/org/videolan/vlc/gui/tv/browser/CategoriesFragment.kt
+++ b/vlc-android/src/org/videolan/vlc/gui/tv/browser/CategoriesFragment.kt
@@ -35,7 +35,7 @@ import org.videolan.vlc.R
 import org.videolan.vlc.gui.tv.CardPresenter
 import org.videolan.vlc.gui.tv.TvUtil
 import org.videolan.vlc.gui.tv.browser.interfaces.BrowserFragmentInterface
-import org.videolan.vlc.viewmodels.RefreshModel
+import org.videolan.vlc.util.RefreshModel
 
 private const val TAG = "VLC/CategoriesFragment"
 private const val SELECTED_ITEM = "selected"
@@ -46,6 +46,7 @@ open class CategoriesFragment<T : RefreshModel> : BrowseSupportFragment(), OnIte
     private val rowsAdapter = ArrayObjectAdapter(ListRowPresenter())
     private lateinit var categoryRows: Map<String, ListRow>
     lateinit var provider: T
+    private var restart = false
 
     override fun onCreate(savedInstanceState: Bundle?) {
         super.onCreate(savedInstanceState)
@@ -67,7 +68,8 @@ open class CategoriesFragment<T : RefreshModel> : BrowseSupportFragment(), OnIte
     override fun onStart() {
         super.onStart()
         if (this::selecteditem.isInitialized) TvUtil.updateBackground(backgroundManager, selecteditem)
-        refresh()
+        if (restart) refresh()
+        restart = true
     }
 
     override fun onSaveInstanceState(outState: Bundle) {
@@ -88,7 +90,7 @@ open class CategoriesFragment<T : RefreshModel> : BrowseSupportFragment(), OnIte
     }
 
     override fun refresh() {
-        provider.refresh()
+        if (this::provider.isInitialized) provider.refresh()
     }
 
     override fun updateList() {}
diff --git a/vlc-android/src/org/videolan/vlc/gui/tv/browser/VideoBrowserFragment.kt b/vlc-android/src/org/videolan/vlc/gui/tv/browser/VideoBrowserFragment.kt
index 9d6dbf851..eea881b0f 100644
--- a/vlc-android/src/org/videolan/vlc/gui/tv/browser/VideoBrowserFragment.kt
+++ b/vlc-android/src/org/videolan/vlc/gui/tv/browser/VideoBrowserFragment.kt
@@ -34,8 +34,8 @@ import org.videolan.vlc.viewmodels.VideosProvider
 class VideoBrowserFragment : CategoriesFragment<VideosProvider>() {
 
     override fun onCreate(savedInstanceState: Bundle?) {
-        super.onCreate(savedInstanceState)
         provider = VideosProvider.get(this, null, 0, Medialibrary.SORT_ALPHA)
         provider.categories.observe(this, Observer { update(it) })
+        super.onCreate(savedInstanceState)
     }
 }
diff --git a/vlc-android/src/org/videolan/vlc/util/ModelsHelper.kt b/vlc-android/src/org/videolan/vlc/util/ModelsHelper.kt
index 019d51552..17fdcada2 100644
--- a/vlc-android/src/org/videolan/vlc/util/ModelsHelper.kt
+++ b/vlc-android/src/org/videolan/vlc/util/ModelsHelper.kt
@@ -8,6 +8,7 @@ import org.videolan.medialibrary.interfaces.MediaAddedCb
 import org.videolan.medialibrary.interfaces.MediaUpdatedCb
 import org.videolan.medialibrary.media.*
 import org.videolan.vlc.PlaybackService
+import org.videolan.vlc.viewmodels.BaseModel
 
 object ModelsHelper {
 
@@ -151,5 +152,22 @@ object EmptyPBSCallback : PlaybackService.Callback {
     override fun updateProgress() {}
     override fun onMediaEvent(event: Media.Event?) {}
     override fun onMediaPlayerEvent(event: MediaPlayer.Event?) {}
+}
+
+interface RefreshModel {
+    fun refresh() : Boolean
+}
 
+fun <T : MediaLibraryItem> BaseModel<T>.canSortBy(sort: Int) = when(sort) {
+    Medialibrary.SORT_DEFAULT -> true
+    Medialibrary.SORT_ALPHA -> canSortByName()
+    Medialibrary.SORT_DURATION -> canSortByDuration()
+    Medialibrary.SORT_INSERTIONDATE -> canSortByInsertionDate()
+    Medialibrary.SORT_LASTMODIFICATIONDATE -> canSortByLastModified()
+    Medialibrary.SORT_RELEASEDATE -> canSortByReleaseDate()
+    Medialibrary.SORT_FILESIZE -> canSortByFileSize()
+    Medialibrary.SORT_ARTIST -> canSortByArtist()
+    Medialibrary.SORT_ALBUM -> canSortByAlbum()
+    Medialibrary.SORT_PLAYCOUNT -> canSortByPlayCount()
+    else -> false
 }
\ No newline at end of file
diff --git a/vlc-android/src/org/videolan/vlc/viewmodels/BaseModel.kt b/vlc-android/src/org/videolan/vlc/viewmodels/BaseModel.kt
index 8de6b4151..b409f6d19 100644
--- a/vlc-android/src/org/videolan/vlc/viewmodels/BaseModel.kt
+++ b/vlc-android/src/org/videolan/vlc/viewmodels/BaseModel.kt
@@ -31,9 +31,7 @@ import kotlinx.coroutines.experimental.launch
 import kotlinx.coroutines.experimental.withContext
 import org.videolan.medialibrary.Medialibrary
 import org.videolan.medialibrary.media.MediaLibraryItem
-import org.videolan.vlc.util.FilterDelegate
-import org.videolan.vlc.util.LiveDataset
-import org.videolan.vlc.util.ModelsHelper
+import org.videolan.vlc.util.*
 
 abstract class BaseModel<T : MediaLibraryItem> : ViewModel(), RefreshModel {
 
@@ -127,20 +125,6 @@ abstract class BaseModel<T : MediaLibraryItem> : ViewModel(), RefreshModel {
         }
     }
 
-    private fun canSortBy(sort: Int) = when(sort) {
-        Medialibrary.SORT_DEFAULT -> true
-        Medialibrary.SORT_ALPHA -> canSortByName()
-        Medialibrary.SORT_DURATION -> canSortByDuration()
-        Medialibrary.SORT_INSERTIONDATE -> canSortByInsertionDate()
-        Medialibrary.SORT_LASTMODIFICATIONDATE -> canSortByLastModified()
-        Medialibrary.SORT_RELEASEDATE -> canSortByReleaseDate()
-        Medialibrary.SORT_FILESIZE -> canSortByFileSize()
-        Medialibrary.SORT_ARTIST -> canSortByArtist()
-        Medialibrary.SORT_ALBUM -> canSortByAlbum()
-        Medialibrary.SORT_PLAYCOUNT -> canSortByPlayCount()
-        else -> false
-    }
-
     protected open suspend fun updateList() {}
 
     protected abstract fun fetch()
@@ -154,7 +138,3 @@ data class MediaAddition(val media: MediaLibraryItem) : Update()
 data class Sort(val sort: Int) : Update()
 data class Remove(val media: MediaLibraryItem) : Update()
 data class Filter(val query: String?) : Update()
-
-interface RefreshModel {
-    fun refresh() : Boolean
-}
\ No newline at end of file



More information about the Android mailing list