[Android] CoroutineScope.map util function for factorization
Geoffrey Métais
git at videolan.org
Tue Jan 8 13:24:52 CET 2019
vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Tue Jan 8 13:18:20 2019 +0100| [1026d793c40784cd8103fb132bd0041bf3ab8aed] | committer: Geoffrey Métais
CoroutineScope.map util function for factorization
> https://code.videolan.org/videolan/vlc-android/commit/1026d793c40784cd8103fb132bd0041bf3ab8aed
---
vlc-android/src/org/videolan/vlc/util/Kextensions.kt | 17 ++++++++++++++---
vlc-android/src/org/videolan/vlc/util/ModelsHelper.kt | 10 ++++++----
.../src/org/videolan/vlc/viewmodels/BaseModel.kt | 14 +++-----------
3 files changed, 23 insertions(+), 18 deletions(-)
diff --git a/vlc-android/src/org/videolan/vlc/util/Kextensions.kt b/vlc-android/src/org/videolan/vlc/util/Kextensions.kt
index a3d7b9741..08cb3b47d 100644
--- a/vlc-android/src/org/videolan/vlc/util/Kextensions.kt
+++ b/vlc-android/src/org/videolan/vlc/util/Kextensions.kt
@@ -3,12 +3,12 @@ package org.videolan.vlc.util
import android.content.Context
import android.content.SharedPreferences
import android.net.Uri
+import androidx.lifecycle.LiveData
+import androidx.lifecycle.MediatorLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProviders
import androidx.preference.PreferenceManager
-import kotlinx.coroutines.Dispatchers
-import kotlinx.coroutines.delay
-import kotlinx.coroutines.withContext
+import kotlinx.coroutines.*
import org.videolan.libvlc.Media
import org.videolan.medialibrary.Medialibrary
import org.videolan.medialibrary.media.MediaWrapper
@@ -120,3 +120,14 @@ suspend fun String.scanAllowed() = withContext(Dispatchers.IO) {
for (child in children) if (child == ".nomedia") return at withContext false
true
}
+
+fun <X, Y> CoroutineScope.map(
+ source: LiveData<X>,
+ f : suspend (value: X?) -> Y
+): LiveData<Y> {
+ return MediatorLiveData<Y>().apply {
+ addSource(source) {
+ launch { value = f(it) }
+ }
+ }
+}
diff --git a/vlc-android/src/org/videolan/vlc/util/ModelsHelper.kt b/vlc-android/src/org/videolan/vlc/util/ModelsHelper.kt
index da93326e5..8022904c3 100644
--- a/vlc-android/src/org/videolan/vlc/util/ModelsHelper.kt
+++ b/vlc-android/src/org/videolan/vlc/util/ModelsHelper.kt
@@ -2,6 +2,8 @@ package org.videolan.vlc.util
import android.content.Context
import android.text.TextUtils
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.withContext
import org.videolan.libvlc.Media
import org.videolan.libvlc.MediaPlayer
import org.videolan.medialibrary.Medialibrary
@@ -17,17 +19,17 @@ private const val LENGTH_2_YEAR = 2* LENGTH_YEAR
object ModelsHelper {
- fun generateSections(sort: Int, items: List<MediaLibraryItem>) : MutableList<MediaLibraryItem> {
+ suspend fun generateSections(sort: Int, items: List<MediaLibraryItem>) = withContext(Dispatchers.IO) {
val array = splitList(sort, items)
val datalist = mutableListOf<MediaLibraryItem>()
for ((key, list) in array) {
datalist.add(DummyItem(key))
datalist.addAll(list)
}
- return datalist
+ datalist
}
- internal fun splitList(sort: Int, items: Collection<MediaLibraryItem>) : Map<String, List<MediaLibraryItem>> {
+ internal suspend fun splitList(sort: Int, items: Collection<MediaLibraryItem>) = withContext(Dispatchers.IO) {
val array = mutableMapOf<String, MutableList<MediaLibraryItem>>()
when (sort) {
Medialibrary.SORT_DEFAULT,
@@ -95,7 +97,7 @@ object ModelsHelper {
}
}
}
- return array
+ array
}
fun getHeader(context: Context, sort: Int, item: MediaLibraryItem, aboveItem: MediaLibraryItem?) = 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 0bc4313ca..987597692 100644
--- a/vlc-android/src/org/videolan/vlc/viewmodels/BaseModel.kt
+++ b/vlc-android/src/org/videolan/vlc/viewmodels/BaseModel.kt
@@ -21,7 +21,6 @@
package org.videolan.vlc.viewmodels
import android.content.Context
-import androidx.lifecycle.MediatorLiveData
import androidx.lifecycle.MutableLiveData
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.Channel
@@ -30,6 +29,7 @@ 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.map
private const val TAG = "VLC/BaseModel"
@ExperimentalCoroutinesApi
@@ -42,19 +42,11 @@ abstract class BaseModel<T : MediaLibraryItem>(context: Context) : SortableModel
open val loading = MutableLiveData<Boolean>().apply { value = false }
val categories by lazy(LazyThreadSafetyMode.NONE) {
- MediatorLiveData<Map<String, List<MediaLibraryItem>>>().apply {
- addSource(dataset) {
- launch { value = withContext(Dispatchers.Default) { ModelsHelper.splitList(sort, it!!.toList()) } }
- }
- }
+ map(dataset) { ModelsHelper.splitList(sort, it!!.toList()) }
}
val sections by lazy(LazyThreadSafetyMode.NONE) {
- MediatorLiveData<List<MediaLibraryItem>>().apply {
- addSource(dataset) {
- launch { value = withContext(Dispatchers.Default) { ModelsHelper.generateSections(sort, it!!.toList()) } }
- }
- }
+ map(dataset) { ModelsHelper.generateSections(sort, it!!.toList()) }
}
@Suppress("UNCHECKED_CAST")
More information about the Android
mailing list