Commit ad0dca68 authored by Geoffrey Métais's avatar Geoffrey Métais
Browse files

Use IO dispatcher for IO operations

parent 6b314dae
......@@ -7,8 +7,7 @@ import android.support.v7.widget.RecyclerView
import kotlinx.coroutines.experimental.android.UI
import kotlinx.coroutines.experimental.channels.Channel
import kotlinx.coroutines.experimental.channels.actor
import kotlinx.coroutines.experimental.newSingleThreadContext
import kotlinx.coroutines.experimental.withContext
import kotlinx.coroutines.experimental.launch
import java.util.*
abstract class DiffUtilAdapter<D, VH : RecyclerView.ViewHolder> : RecyclerView.Adapter<VH>() {
......@@ -16,7 +15,7 @@ abstract class DiffUtilAdapter<D, VH : RecyclerView.ViewHolder> : RecyclerView.A
protected var dataset: List<D> = listOf()
private set
private val diffCallback by lazy(LazyThreadSafetyMode.NONE) { createCB() }
private val updateActor = actor<List<D>>(newSingleThreadContext("vlc-updater"), capacity = Channel.CONFLATED) {
private val updateActor = actor<List<D>>(capacity = Channel.CONFLATED) {
for (list in channel) internalUpdate(list)
}
protected abstract fun onUpdateFinished()
......@@ -30,11 +29,11 @@ abstract class DiffUtilAdapter<D, VH : RecyclerView.ViewHolder> : RecyclerView.A
private suspend fun internalUpdate(list: List<D>) {
val finalList = prepareList(list)
val result = DiffUtil.calculateDiff(diffCallback.apply { update(dataset, finalList) }, detectMoves())
withContext(UI) {
launch(UI) {
dataset = finalList
result.dispatchUpdatesTo(this@DiffUtilAdapter)
onUpdateFinished()
}
}.join()
}
protected open fun prepareList(list: List<D>) : List<D> = ArrayList(list)
......
......@@ -13,7 +13,6 @@ import android.support.v4.view.ViewCompat
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import kotlinx.coroutines.experimental.CommonPool
import kotlinx.coroutines.experimental.CoroutineStart
import kotlinx.coroutines.experimental.android.UI
import kotlinx.coroutines.experimental.launch
......@@ -24,6 +23,7 @@ import org.videolan.vlc.BR
import org.videolan.vlc.VLCApplication
import org.videolan.vlc.util.HttpImageLoader
import org.videolan.vlc.util.ThumbnailsProvider
import org.videolan.vlc.util.VLCIO
private val sBitmapCache = BitmapCache.getInstance()
private val sMedialibrary = VLCApplication.getMLInstance()
......@@ -47,7 +47,7 @@ fun loadImage(v: View, item: MediaLibraryItem?) {
@BindingAdapter("imageUri")
fun downloadIcon(v: View, imageUri: Uri?) {
if (imageUri != null && imageUri.scheme == "http") launch(UI, CoroutineStart.UNDISPATCHED) {
val image = withContext(CommonPool) { HttpImageLoader.downloadBitmap(imageUri.toString()) }
val image = withContext(VLCIO) { HttpImageLoader.downloadBitmap(imageUri.toString()) }
updateImageView(image, v, DataBindingUtil.findBinding(v))
}
}
......@@ -70,7 +70,7 @@ private suspend fun getImage(v: View, item: MediaLibraryItem, binding: ViewDataB
}
private suspend fun obtainBitmap(item: MediaLibraryItem, width: Int) : Bitmap? {
return withContext(CommonPool) {
return withContext(VLCIO) {
if (item.itemType == MediaLibraryItem.TYPE_MEDIA) ThumbnailsProvider.getMediaThumbnail(item as MediaWrapper)
else AudioUtil.readCoverBitmap(Uri.decode(item.artworkMrl), width)
}
......@@ -107,7 +107,7 @@ private suspend fun findInLibrary(item: MediaLibraryItem, isMedia: Boolean, isGr
val isMediaFile = type == MediaWrapper.TYPE_AUDIO || type == MediaWrapper.TYPE_VIDEO
val uri = mw.uri
if (!isMediaFile && !(type == MediaWrapper.TYPE_DIR && "upnp" == uri.scheme)) return item
if (isMediaFile && "file" == uri.scheme) return withContext(CommonPool) { sMedialibrary.getMedia(uri) } ?: item
if (isMediaFile && "file" == uri.scheme) return withContext(VLCIO) { sMedialibrary.getMedia(uri) } ?: item
}
return item
}
\ No newline at end of file
......@@ -151,7 +151,7 @@ class PlaylistManager(val service: PlaybackService) : MediaWrapperList.EventList
service.onPlaylistLoaded()
launch(UI, CoroutineStart.UNDISPATCHED) {
determinePrevAndNextIndices()
launch { mediaList.updateWithMLMeta() }
launch(VLCIO) { mediaList.updateWithMLMeta() }
}
}
......@@ -546,7 +546,7 @@ class PlaylistManager(val service: PlaybackService) : MediaWrapperList.EventList
private suspend fun setStartTime(media: Media, mw: MediaWrapper) {
if (savedTime <= 0L ) {
if (mw.time >= 0L) savedTime = mw.time
else if (mw.time == 0L && mw.type == MediaWrapper.TYPE_VIDEO || mw.isPodcast) savedTime = withContext(CommonPool) { medialibrary.findMedia(mw).getMetaLong(MediaWrapper.META_PROGRESS) }
else if (mw.time == 0L && mw.type == MediaWrapper.TYPE_VIDEO || mw.isPodcast) savedTime = withContext(VLCIO) { medialibrary.findMedia(mw).getMetaLong(MediaWrapper.META_PROGRESS) }
}
if (savedTime <= 0L) return
......
package org.videolan.vlc.viewmodels
import kotlinx.coroutines.experimental.CommonPool
import kotlinx.coroutines.experimental.withContext
import org.videolan.medialibrary.Medialibrary
import org.videolan.medialibrary.media.MediaWrapper
import org.videolan.vlc.util.VLCIO
class HistoryProvider: BaseModel<MediaWrapper>() {
......@@ -14,7 +14,7 @@ class HistoryProvider: BaseModel<MediaWrapper>() {
}
override suspend fun updateList() {
dataset.value = withContext(CommonPool) { Medialibrary.getInstance().lastMediaPlayed().toMutableList() }
dataset.value = withContext(VLCIO) { Medialibrary.getInstance().lastMediaPlayed().toMutableList() }
}
fun moveUp(media: MediaWrapper) {
......
......@@ -25,7 +25,6 @@ import android.arch.lifecycle.ViewModel
import android.arch.lifecycle.ViewModelProvider
import android.arch.lifecycle.ViewModelProviders
import android.support.v4.app.Fragment
import kotlinx.coroutines.experimental.CommonPool
import kotlinx.coroutines.experimental.withContext
import org.videolan.medialibrary.Medialibrary
import org.videolan.medialibrary.interfaces.MediaAddedCb
......@@ -34,6 +33,7 @@ import org.videolan.vlc.R
import org.videolan.vlc.VLCApplication
import org.videolan.vlc.media.MediaGroup
import org.videolan.vlc.util.Util
import org.videolan.vlc.util.VLCIO
open class VideosProvider(private val group: String?, private val minGroupLen: Int, customSort : Int) : MedialibraryModel<MediaWrapper>(), MediaAddedCb {
......@@ -61,7 +61,7 @@ open class VideosProvider(private val group: String?, private val minGroupLen: I
}
override suspend fun updateList() {
dataset.value = withContext(CommonPool) {
dataset.value = withContext(VLCIO) {
val list = medialibrary.getVideos(sort, desc)
val displayList = mutableListOf<MediaWrapper>()
when {
......
......@@ -2,13 +2,13 @@ package org.videolan.vlc.viewmodels.audio
import android.arch.lifecycle.ViewModel
import android.arch.lifecycle.ViewModelProvider
import kotlinx.coroutines.experimental.CommonPool
import kotlinx.coroutines.experimental.withContext
import org.videolan.medialibrary.Medialibrary
import org.videolan.medialibrary.media.Artist
import org.videolan.medialibrary.media.Genre
import org.videolan.medialibrary.media.MediaLibraryItem
import org.videolan.vlc.VLCApplication
import org.videolan.vlc.util.VLCIO
class AlbumProvider(val parent: MediaLibraryItem? = null): AudioModel(), Medialibrary.AlbumsAddedCb {
......@@ -29,7 +29,7 @@ class AlbumProvider(val parent: MediaLibraryItem? = null): AudioModel(), Mediali
@Suppress("UNCHECKED_CAST")
override suspend fun updateList() {
dataset.value = withContext(CommonPool) { when (parent) {
dataset.value = withContext(VLCIO) { when (parent) {
is Artist -> parent.getAlbums(sort, desc)
is Genre -> parent.getAlbums(sort, desc)
else -> medialibrary.getAlbums(sort, desc)
......
......@@ -2,12 +2,12 @@ package org.videolan.vlc.viewmodels.audio
import android.arch.lifecycle.ViewModel
import android.arch.lifecycle.ViewModelProvider
import kotlinx.coroutines.experimental.CommonPool
import kotlinx.coroutines.experimental.withContext
import org.videolan.medialibrary.Medialibrary
import org.videolan.medialibrary.Medialibrary.ArtistsAddedCb
import org.videolan.medialibrary.media.MediaLibraryItem
import org.videolan.vlc.VLCApplication
import org.videolan.vlc.util.VLCIO
class ArtistProvider(private var showAll: Boolean = false): AudioModel(), ArtistsAddedCb {
......@@ -27,7 +27,7 @@ class ArtistProvider(private var showAll: Boolean = false): AudioModel(), Artist
//VLCApplication.getSettings().getBoolean(Constants.KEY_ARTISTS_SHOW_ALL, false)
@Suppress("UNCHECKED_CAST")
override suspend fun updateList() {
dataset.value = withContext(CommonPool) {
dataset.value = withContext(VLCIO) {
medialibrary.getArtists(showAll, sort, desc).toMutableList() as MutableList<MediaLibraryItem>
}
}
......
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.media.MediaLibraryItem
import org.videolan.vlc.VLCApplication
import org.videolan.vlc.util.VLCIO
class Genresprovider: AudioModel() {
......@@ -16,7 +16,7 @@ class Genresprovider: AudioModel() {
@Suppress("UNCHECKED_CAST")
override suspend fun updateList() {
dataset.value = withContext(CommonPool) {
dataset.value = withContext(VLCIO) {
medialibrary.getGenres(sort, desc).toMutableList() as MutableList<MediaLibraryItem>
}
}
......
package org.videolan.vlc.viewmodels.audio
import kotlinx.coroutines.experimental.CommonPool
import kotlinx.coroutines.experimental.withContext
import org.videolan.medialibrary.media.MediaLibraryItem
import org.videolan.vlc.util.VLCIO
class PlaylistsProvider: AudioModel() {
......@@ -11,6 +11,6 @@ class PlaylistsProvider: AudioModel() {
@Suppress("UNCHECKED_CAST")
override suspend fun updateList() {
dataset.value = withContext(CommonPool) { medialibrary.getPlaylists(sort, desc).toMutableList() as MutableList<MediaLibraryItem> }
dataset.value = withContext(VLCIO) { medialibrary.getPlaylists(sort, desc).toMutableList() as MutableList<MediaLibraryItem> }
}
}
\ No newline at end of file
......@@ -2,11 +2,11 @@ package org.videolan.vlc.viewmodels.audio
import android.arch.lifecycle.ViewModel
import android.arch.lifecycle.ViewModelProvider
import kotlinx.coroutines.experimental.CommonPool
import kotlinx.coroutines.experimental.withContext
import org.videolan.medialibrary.Medialibrary
import org.videolan.medialibrary.media.*
import org.videolan.vlc.VLCApplication
import org.videolan.vlc.util.VLCIO
class TracksProvider(val parent: MediaLibraryItem? = null): AudioModel() {
......@@ -34,7 +34,7 @@ class TracksProvider(val parent: MediaLibraryItem? = null): AudioModel() {
@Suppress("UNCHECKED_CAST")
override suspend fun updateList() {
dataset.value = withContext(CommonPool) { when (parent) {
dataset.value = withContext(VLCIO) { when (parent) {
is Artist -> parent.getTracks(sort, desc)
is Album -> parent.getTracks(sort, desc)
is Genre -> parent.getTracks(sort, desc)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment