[Android] Code cleaning

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


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Fri Mar  9 15:59:41 2018 +0100| [0d89d14c0c2f47d1da7e97bf95e3ba2eac902876] | committer: Geoffrey Métais

Code cleaning

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

 .../util/{FilterDelagate.kt => FilterDelegate.kt}  | 22 +++++++++++-----------
 .../src/org/videolan/vlc/viewmodels/BaseModel.kt   | 13 +++++++------
 .../videolan/vlc/viewmodels/audio/AlbumProvider.kt |  8 +-------
 .../vlc/viewmodels/audio/ArtistProvider.kt         |  6 +-----
 4 files changed, 20 insertions(+), 29 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/util/FilterDelagate.kt b/vlc-android/src/org/videolan/vlc/util/FilterDelegate.kt
similarity index 67%
rename from vlc-android/src/org/videolan/vlc/util/FilterDelagate.kt
rename to vlc-android/src/org/videolan/vlc/util/FilterDelegate.kt
index d93983b68..7860bd866 100644
--- a/vlc-android/src/org/videolan/vlc/util/FilterDelagate.kt
+++ b/vlc-android/src/org/videolan/vlc/util/FilterDelegate.kt
@@ -1,11 +1,12 @@
 package org.videolan.vlc.util
 
 import android.arch.lifecycle.MutableLiveData
-import kotlinx.coroutines.experimental.async
+import kotlinx.coroutines.experimental.CommonPool
+import kotlinx.coroutines.experimental.withContext
 import org.videolan.medialibrary.media.MediaLibraryItem
 
 
-class FilterDelagate<T : MediaLibraryItem>(private val dataset: MutableLiveData<MutableList<T>>) {
+class FilterDelegate<T : MediaLibraryItem>(private val dataset: MutableLiveData<MutableList<T>>) {
     private var sourceSet: MutableList<T>? = null
 
     private fun initSource() : MutableList<T>? {
@@ -18,16 +19,15 @@ class FilterDelagate<T : MediaLibraryItem>(private val dataset: MutableLiveData<
 
     private suspend fun filteringJob(charSequence: CharSequence?) : MutableList<T> {
         if (charSequence !== null) initSource()?.let {
-            return async { mutableListOf<T>().apply {
+            return withContext(CommonPool) { mutableListOf<T>().apply {
                 val queryStrings = charSequence.trim().toString().split(" ").filter { it.length > 2 }
-                for (item in it) {
-                    for (query in queryStrings)
-                        if (item.title.contains(query, true)) {
-                            this.add(item)
-                            break
-                        }
-                } }
-            }.await()
+                for (item in it) for (query in queryStrings)
+                    if (item.title.contains(query, true)) {
+                        this.add(item)
+                        break
+                    }
+                }
+            }
         }
         return mutableListOf()
     }
diff --git a/vlc-android/src/org/videolan/vlc/viewmodels/BaseModel.kt b/vlc-android/src/org/videolan/vlc/viewmodels/BaseModel.kt
index 9bd47fb45..334fa35fd 100644
--- a/vlc-android/src/org/videolan/vlc/viewmodels/BaseModel.kt
+++ b/vlc-android/src/org/videolan/vlc/viewmodels/BaseModel.kt
@@ -22,14 +22,15 @@ package org.videolan.vlc.viewmodels
 
 import android.arch.lifecycle.MutableLiveData
 import android.arch.lifecycle.ViewModel
+import kotlinx.coroutines.experimental.CommonPool
 import kotlinx.coroutines.experimental.android.UI
-import kotlinx.coroutines.experimental.async
 import kotlinx.coroutines.experimental.channels.Channel
 import kotlinx.coroutines.experimental.channels.actor
 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.FilterDelagate
+import org.videolan.vlc.util.FilterDelegate
 
 abstract class BaseModel<T : MediaLibraryItem> : ViewModel() {
 
@@ -46,7 +47,7 @@ abstract class BaseModel<T : MediaLibraryItem> : ViewModel() {
     open fun canSortByAlbum ()= false
     open fun canSortByPlayCount() = false
 
-    private val filter by lazy(LazyThreadSafetyMode.NONE) { FilterDelagate(dataset) }
+    private val filter by lazy(LazyThreadSafetyMode.NONE) { FilterDelegate(dataset) }
 
     val dataset by lazy {
         launch(UI) { fetch() }
@@ -105,7 +106,7 @@ abstract class BaseModel<T : MediaLibraryItem> : ViewModel() {
     }
 
     protected open suspend fun updateItems(mediaList: List<T>) {
-        dataset.value = async {
+        dataset.value = withContext(CommonPool) {
             val list = dataset.value ?: mutableListOf()
             val iterator = list.listIterator()
             for (media in iterator) {
@@ -114,8 +115,8 @@ abstract class BaseModel<T : MediaLibraryItem> : ViewModel() {
                     break
                 }
             }
-            return at async list
-        }.await()
+            list
+        }
     }
 
     fun canSortBy(sort: Int) = when(sort) {
diff --git a/vlc-android/src/org/videolan/vlc/viewmodels/audio/AlbumProvider.kt b/vlc-android/src/org/videolan/vlc/viewmodels/audio/AlbumProvider.kt
index e3a732b1e..2bfc84304 100644
--- a/vlc-android/src/org/videolan/vlc/viewmodels/audio/AlbumProvider.kt
+++ b/vlc-android/src/org/videolan/vlc/viewmodels/audio/AlbumProvider.kt
@@ -11,7 +11,7 @@ import org.videolan.medialibrary.media.MediaLibraryItem
 import org.videolan.vlc.util.ModelsHelper
 
 
-class AlbumProvider(val parent: MediaLibraryItem? = null): AudioModel(), Medialibrary.AlbumsAddedCb, Medialibrary.AlbumsModifiedCb {
+class AlbumProvider(val parent: MediaLibraryItem? = null): AudioModel(), Medialibrary.AlbumsAddedCb {
 
     override fun canSortByDuration() = true
     override fun canSortByReleaseDate() = true
@@ -24,10 +24,6 @@ class AlbumProvider(val parent: MediaLibraryItem? = null): AudioModel(), Mediali
         refresh()
     }
 
-    override fun onAlbumsModified() {
-        refresh()
-    }
-
     override suspend fun updateList() {
         dataset.value = withContext(CommonPool) {
             ModelsHelper.generateSections(sort, when (parent) {
@@ -41,13 +37,11 @@ class AlbumProvider(val parent: MediaLibraryItem? = null): AudioModel(), Mediali
     override fun onMedialibraryReady() {
         super.onMedialibraryReady()
         medialibrary.setAlbumsAddedCb(this)
-        medialibrary.setAlbumsModifiedCb(this)
     }
 
     override fun onCleared() {
         super.onCleared()
         medialibrary.setAlbumsAddedCb(null)
-        medialibrary.setAlbumsModifiedCb(null)
     }
 
     class Factory(val parent: MediaLibraryItem?): ViewModelProvider.NewInstanceFactory() {
diff --git a/vlc-android/src/org/videolan/vlc/viewmodels/audio/ArtistProvider.kt b/vlc-android/src/org/videolan/vlc/viewmodels/audio/ArtistProvider.kt
index 4d22e41cf..7ca242cbb 100644
--- a/vlc-android/src/org/videolan/vlc/viewmodels/audio/ArtistProvider.kt
+++ b/vlc-android/src/org/videolan/vlc/viewmodels/audio/ArtistProvider.kt
@@ -2,14 +2,12 @@ package org.videolan.vlc.viewmodels.audio
 
 import kotlinx.coroutines.experimental.CommonPool
 import kotlinx.coroutines.experimental.withContext
-import org.videolan.medialibrary.Medialibrary
 import org.videolan.medialibrary.Medialibrary.ArtistsAddedCb
 import org.videolan.vlc.VLCApplication
 import org.videolan.vlc.util.Constants
-import org.videolan.vlc.util.EmptyMLCallbacks
 import org.videolan.vlc.util.ModelsHelper
 
-class ArtistProvider: AudioModel(), ArtistsAddedCb, Medialibrary.ArtistsModifiedCb by EmptyMLCallbacks {
+class ArtistProvider: AudioModel(), ArtistsAddedCb {
     override fun onArtistsAdded() {
         refresh()
     }
@@ -21,12 +19,10 @@ class ArtistProvider: AudioModel(), ArtistsAddedCb, Medialibrary.ArtistsModified
     override fun onMedialibraryReady() {
         super.onMedialibraryReady()
         medialibrary.setArtistsAddedCb(this)
-        medialibrary.setArtistsModifiedCb(this)
     }
 
     override fun onCleared() {
         super.onCleared()
         medialibrary.setArtistsAddedCb(null)
-        medialibrary.setArtistsModifiedCb(null)
     }
 }
\ No newline at end of file



More information about the Android mailing list