[Android] Models: code factorization

Geoffrey Métais git at videolan.org
Tue Jul 9 09:08:37 CEST 2019


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Tue Jul  9 09:08:19 2019 +0200| [40bfaa4ec5adb02889927c3fa4d8eff1e435c03d] | committer: Geoffrey Métais

Models: code factorization

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

 .../providers/medialibrary/MedialibraryProvider.kt | 17 ++-----
 .../src/org/videolan/vlc/util/ModelsHelper.kt      | 54 +++++++++++-----------
 .../org/videolan/vlc/viewmodels/SortableModel.kt   | 21 ++-------
 3 files changed, 35 insertions(+), 57 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/providers/medialibrary/MedialibraryProvider.kt b/vlc-android/src/org/videolan/vlc/providers/medialibrary/MedialibraryProvider.kt
index 76e349157..4e101fcfc 100644
--- a/vlc-android/src/org/videolan/vlc/providers/medialibrary/MedialibraryProvider.kt
+++ b/vlc-android/src/org/videolan/vlc/providers/medialibrary/MedialibraryProvider.kt
@@ -36,7 +36,9 @@ import org.videolan.vlc.providers.HeadersIndex
 import org.videolan.vlc.util.*
 import org.videolan.vlc.viewmodels.SortableModel
 
-abstract class MedialibraryProvider<T : MediaLibraryItem>(val context: Context, val scope: SortableModel) : HeaderProvider() {
+abstract class MedialibraryProvider<T : MediaLibraryItem>(val context: Context, val scope: SortableModel) : HeaderProvider(),
+    ISortModel
+{
     protected val medialibrary = AbstractMedialibrary.getInstance()
     private lateinit var dataSource : DataSource<Int, T>
     val loading = MutableLiveData<Boolean>().apply { value = false }
@@ -60,18 +62,7 @@ abstract class MedialibraryProvider<T : MediaLibraryItem>(val context: Context,
     abstract fun getPage(loadSize: Int, startposition: Int): Array<T>
     abstract fun getAll(): Array<T>
 
-    open fun canSortByName() = true
-    open fun canSortByFileNameName() = false
-    open fun canSortByDuration() = false
-    open fun canSortByInsertionDate() = false
-    open fun canSortByLastModified() = false
-    open fun canSortByReleaseDate() = false
-    open fun canSortByFileSize() = false
-    open fun canSortByArtist() = false
-    open fun canSortByAlbum ()= false
-    open fun canSortByPlayCount() = false
-
-    open fun sort(sort: Int) {
+    override fun sort(sort: Int) {
         if (canSortBy(sort)) {
             desc = when (this.sort) {
                 AbstractMedialibrary.SORT_DEFAULT -> sort == AbstractMedialibrary.SORT_ALPHA
diff --git a/vlc-android/src/org/videolan/vlc/util/ModelsHelper.kt b/vlc-android/src/org/videolan/vlc/util/ModelsHelper.kt
index 993e4fe29..ee8dbe3ec 100644
--- a/vlc-android/src/org/videolan/vlc/util/ModelsHelper.kt
+++ b/vlc-android/src/org/videolan/vlc/util/ModelsHelper.kt
@@ -243,32 +243,30 @@ interface RefreshModel {
     fun refresh()
 }
 
-fun SortableModel.canSortBy(sort: Int) = when (sort) {
-    AbstractMedialibrary.SORT_DEFAULT -> true
-    AbstractMedialibrary.SORT_ALPHA -> canSortByName()
-    AbstractMedialibrary.SORT_FILENAME -> canSortByFileNameName()
-    AbstractMedialibrary.SORT_DURATION -> canSortByDuration()
-    AbstractMedialibrary.SORT_INSERTIONDATE -> canSortByInsertionDate()
-    AbstractMedialibrary.SORT_LASTMODIFICATIONDATE -> canSortByLastModified()
-    AbstractMedialibrary.SORT_RELEASEDATE -> canSortByReleaseDate()
-    AbstractMedialibrary.SORT_FILESIZE -> canSortByFileSize()
-    AbstractMedialibrary.SORT_ARTIST -> canSortByArtist()
-    AbstractMedialibrary.SORT_ALBUM -> canSortByAlbum()
-    AbstractMedialibrary.SORT_PLAYCOUNT -> canSortByPlayCount()
-    else -> false
-}
-
-fun MedialibraryProvider<*>.canSortBy(sort: Int) = when (sort) {
-    AbstractMedialibrary.SORT_DEFAULT -> true
-    AbstractMedialibrary.SORT_ALPHA -> canSortByName()
-    AbstractMedialibrary.SORT_FILENAME -> canSortByFileNameName()
-    AbstractMedialibrary.SORT_DURATION -> canSortByDuration()
-    AbstractMedialibrary.SORT_INSERTIONDATE -> canSortByInsertionDate()
-    AbstractMedialibrary.SORT_LASTMODIFICATIONDATE -> canSortByLastModified()
-    AbstractMedialibrary.SORT_RELEASEDATE -> canSortByReleaseDate()
-    AbstractMedialibrary.SORT_FILESIZE -> canSortByFileSize()
-    AbstractMedialibrary.SORT_ARTIST -> canSortByArtist()
-    AbstractMedialibrary.SORT_ALBUM -> canSortByAlbum()
-    AbstractMedialibrary.SORT_PLAYCOUNT -> canSortByPlayCount()
-    else -> false
+interface ISortModel {
+    fun sort(sort: Int)
+    fun canSortByName() = true
+    fun canSortByFileNameName() = false
+    fun canSortByDuration() = false
+    fun canSortByInsertionDate() = false
+    fun canSortByLastModified() = false
+    fun canSortByReleaseDate() = false
+    fun canSortByFileSize() = false
+    fun canSortByArtist() = false
+    fun canSortByAlbum ()= false
+    fun canSortByPlayCount() = false
+    fun canSortBy(sort: Int) = when (sort) {
+        AbstractMedialibrary.SORT_DEFAULT -> true
+        AbstractMedialibrary.SORT_ALPHA -> canSortByName()
+        AbstractMedialibrary.SORT_FILENAME -> canSortByFileNameName()
+        AbstractMedialibrary.SORT_DURATION -> canSortByDuration()
+        AbstractMedialibrary.SORT_INSERTIONDATE -> canSortByInsertionDate()
+        AbstractMedialibrary.SORT_LASTMODIFICATIONDATE -> canSortByLastModified()
+        AbstractMedialibrary.SORT_RELEASEDATE -> canSortByReleaseDate()
+        AbstractMedialibrary.SORT_FILESIZE -> canSortByFileSize()
+        AbstractMedialibrary.SORT_ARTIST -> canSortByArtist()
+        AbstractMedialibrary.SORT_ALBUM -> canSortByAlbum()
+        AbstractMedialibrary.SORT_PLAYCOUNT -> canSortByPlayCount()
+        else -> false
+    }
 }
\ No newline at end of file
diff --git a/vlc-android/src/org/videolan/vlc/viewmodels/SortableModel.kt b/vlc-android/src/org/videolan/vlc/viewmodels/SortableModel.kt
index cd0c3f88d..c2a5b9293 100644
--- a/vlc-android/src/org/videolan/vlc/viewmodels/SortableModel.kt
+++ b/vlc-android/src/org/videolan/vlc/viewmodels/SortableModel.kt
@@ -2,11 +2,11 @@ package org.videolan.vlc.viewmodels
 
 import android.content.Context
 import org.videolan.medialibrary.interfaces.AbstractMedialibrary
-import org.videolan.vlc.util.RefreshModel
-import org.videolan.vlc.util.Settings
-import org.videolan.vlc.util.canSortBy
+import org.videolan.vlc.util.*
 
-abstract class SortableModel(protected val context: Context): ScopedModel(), RefreshModel {
+abstract class SortableModel(protected val context: Context): ScopedModel(), RefreshModel,
+    ISortModel
+{
 
     protected open val sortKey : String = this.javaClass.simpleName
     var sort = AbstractMedialibrary.SORT_DEFAULT
@@ -14,20 +14,9 @@ abstract class SortableModel(protected val context: Context): ScopedModel(), Ref
 
     var filterQuery : String? = null
 
-    open fun canSortByName() = true
-    open fun canSortByFileNameName() = false
-    open fun canSortByDuration() = false
-    open fun canSortByInsertionDate() = false
-    open fun canSortByLastModified() = false
-    open fun canSortByReleaseDate() = false
-    open fun canSortByFileSize() = false
-    open fun canSortByArtist() = false
-    open fun canSortByAlbum ()= false
-    open fun canSortByPlayCount() = false
-
     fun getKey() = sortKey
 
-    open fun sort(sort: Int) {
+    override fun sort(sort: Int) {
         if (canSortBy(sort)) {
             desc = when (this.sort) {
                 AbstractMedialibrary.SORT_DEFAULT -> sort == AbstractMedialibrary.SORT_ALPHA



More information about the Android mailing list