Commit b856c756 authored by Geoffrey Métais's avatar Geoffrey Métais

Code cleaning, replace runnables by coroutine call

parent 5d55932e
......@@ -32,4 +32,6 @@ val LifecycleOwner.coroutineScope: CoroutineScope
fun <T> List<T>.getposition(target: T) : Int {
for ((index, item) in withIndex()) if (item == target) return index
return -1
}
\ No newline at end of file
}
fun LifecycleOwner.isStarted() = lifecycle.currentState.isAtLeast(Lifecycle.State.STARTED)
\ No newline at end of file
......@@ -67,7 +67,7 @@ private const val ACTION_HIDE_PLAYER = 1341
@SuppressLint("Registered")
@ExperimentalCoroutinesApi
@ObsoleteCoroutinesApi
open class AudioPlayerContainerActivity : BaseActivity(), CoroutineScope by MainScope() {
open class AudioPlayerContainerActivity : BaseActivity() {
protected lateinit var appBarLayout: AppBarLayout
protected lateinit var toolbar: Toolbar
......
......@@ -3,10 +3,12 @@ package org.videolan.vlc.gui
import android.content.SharedPreferences
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.MainScope
import org.videolan.vlc.gui.helpers.applyTheme
import org.videolan.vlc.util.Settings
open class BaseActivity : AppCompatActivity() {
open class BaseActivity : AppCompatActivity(), CoroutineScope by MainScope() {
lateinit var settings: SharedPreferences
......
......@@ -19,17 +19,19 @@ import androidx.databinding.DataBindingUtil
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.videolan.medialibrary.Medialibrary
import org.videolan.medialibrary.media.MediaLibraryItem
import org.videolan.medialibrary.media.SearchAggregate
import org.videolan.tools.isStarted
import org.videolan.vlc.R
import org.videolan.vlc.VLCApplication
import org.videolan.vlc.databinding.SearchActivityBinding
import org.videolan.vlc.gui.helpers.UiTools
import org.videolan.vlc.gui.helpers.applyTheme
import org.videolan.vlc.media.MediaUtils
import org.videolan.vlc.util.runIO
import org.videolan.vlc.util.runOnMainThread
open class SearchActivity : BaseActivity(), TextWatcher, TextView.OnEditorActionListener {
......@@ -64,20 +66,19 @@ open class SearchActivity : BaseActivity(), TextWatcher, TextView.OnEditorAction
private fun performSearh(query: String?) {
if (query == null || query.length < 3) return
runIO(Runnable {
val searchAggregate = medialibrary.search(query)
launch {
val searchAggregate = withContext(Dispatchers.IO) { medialibrary.search(query) }
if (!isStarted()) return@launch
binding.searchAggregate = searchAggregate
if (searchAggregate != null) {
runOnMainThread(Runnable {
(binding.albumsResults.adapter as SearchResultAdapter).add(searchAggregate.albums.filterNotNull().toTypedArray())
(binding.artistsResults.adapter as SearchResultAdapter).add(searchAggregate.artists.filterNotNull().toTypedArray())
(binding.genresResults.adapter as SearchResultAdapter).add(searchAggregate.genres.filterNotNull().toTypedArray())
(binding.playlistsResults.adapter as SearchResultAdapter).add(searchAggregate.playlists.filterNotNull().toTypedArray())
(binding.othersResults.adapter as SearchResultAdapter).add(searchAggregate.videos.filterNotNull().toTypedArray())
(binding.songsResults.adapter as SearchResultAdapter).add(searchAggregate.tracks.filterNotNull().toTypedArray())
})
(binding.albumsResults.adapter as SearchResultAdapter).add(searchAggregate.albums.filterNotNull().toTypedArray())
(binding.artistsResults.adapter as SearchResultAdapter).add(searchAggregate.artists.filterNotNull().toTypedArray())
(binding.genresResults.adapter as SearchResultAdapter).add(searchAggregate.genres.filterNotNull().toTypedArray())
(binding.playlistsResults.adapter as SearchResultAdapter).add(searchAggregate.playlists.filterNotNull().toTypedArray())
(binding.othersResults.adapter as SearchResultAdapter).add(searchAggregate.videos.filterNotNull().toTypedArray())
(binding.songsResults.adapter as SearchResultAdapter).add(searchAggregate.tracks.filterNotNull().toTypedArray())
}
})
}
}
private fun initializeLists() {
......
......@@ -56,7 +56,7 @@ import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.channels.actor
import org.videolan.medialibrary.Tools
import org.videolan.medialibrary.media.MediaWrapper
import org.videolan.tools.coroutineScope
import org.videolan.tools.isStarted
import org.videolan.vlc.PlaybackService
import org.videolan.vlc.R
import org.videolan.vlc.VLCApplication
......@@ -78,13 +78,13 @@ private const val SEARCH_TIMEOUT_MILLIS = 10000L
@ObsoleteCoroutinesApi
@ExperimentalCoroutinesApi
@Suppress("UNUSED_PARAMETER")
class AudioPlayer : Fragment(), PlaylistAdapter.IPlayer, TextWatcher {
class AudioPlayer : Fragment(), PlaylistAdapter.IPlayer, TextWatcher, CoroutineScope by MainScope() {
private lateinit var binding: AudioPlayerBinding
private lateinit var playlistAdapter: PlaylistAdapter
private lateinit var settings: SharedPreferences
private val handler by lazy(LazyThreadSafetyMode.NONE) { Handler() }
private val updateActor = coroutineScope.actor<Unit>(capacity = Channel.CONFLATED) { for (entry in channel) doUpdate() }
private val updateActor = actor<Unit>(capacity = Channel.CONFLATED) { for (entry in channel) doUpdate() }
private lateinit var playlistModel: PlaylistModel
private lateinit var optionsDelegate: PlayerOptionsDelegate
......@@ -262,18 +262,18 @@ class AudioPlayer : Fragment(), PlaylistAdapter.IPlayer, TextWatcher {
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
private fun updateBackground() {
if (settings.getBoolean("blurred_cover_background", true)) {
coroutineScope.launch {
launch {
val mw = playlistModel.currentMediaWrapper
if (mw === null || TextUtils.equals(currentCoverArt, mw.artworkMrl)) return@launch
if (!isStarted() || mw === null || TextUtils.equals(currentCoverArt, mw.artworkMrl)) return@launch
currentCoverArt = mw.artworkMrl
if (TextUtils.isEmpty(mw.artworkMrl)) {
setDefaultBackground()
} else {
val width = if (binding.contentLayout.width > 0) binding.contentLayout.width else activity?.getScreenWidth() ?: return@launch
val blurredCover = withContext(Dispatchers.IO) { UiTools.blurBitmap(AudioUtil.readCoverBitmap(Uri.decode(mw.artworkMrl), width)) }
if (!isStarted()) return@launch
if (blurredCover !== null) {
val activity = activity as? AudioPlayerContainerActivity
if (activity === null) return@launch
val activity = activity as? AudioPlayerContainerActivity ?: return@launch
binding.backgroundView.setColorFilter(UiTools.getColorFromAttribute(activity, R.attr.audio_player_background_tint))
binding.backgroundView.setImageBitmap(blurredCover)
binding.backgroundView.visibility = View.VISIBLE
......
......@@ -36,9 +36,9 @@ import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.ObsoleteCoroutinesApi
import kotlinx.coroutines.*
import org.videolan.medialibrary.media.MediaWrapper
import org.videolan.tools.isStarted
import org.videolan.vlc.R
import org.videolan.vlc.databinding.TvAudioPlayerBinding
import org.videolan.vlc.gui.helpers.AudioUtil
......@@ -49,6 +49,7 @@ import org.videolan.vlc.media.MediaUtils
import org.videolan.vlc.util.*
import org.videolan.vlc.viewmodels.PlayerState
import org.videolan.vlc.viewmodels.PlaylistModel
import java.lang.Runnable
import java.util.*
@ObsoleteCoroutinesApi
......@@ -112,23 +113,21 @@ class AudioPlayerActivity : BaseTvActivity() {
updateBackground()
}
private fun updateBackground() {
val width = if (binding.albumCover.width > 0) binding.albumCover.width else this.getScreenWidth()
runIO(Runnable {
val cover = AudioUtil.readCoverBitmap(Uri.decode(currentCoverArt), width)
val blurredCover = if (cover != null) UiTools.blurBitmap(cover) else null
runOnMainThread(Runnable {
if (cover == null) {
binding.albumCover.setImageResource(R.drawable.ic_no_artwork_big)
binding.background.clearColorFilter()
binding.background.setImageResource(0)
} else {
binding.albumCover.setImageBitmap(cover)
binding.background.setColorFilter(UiTools.getColorFromAttribute(binding.background.context, R.attr.audio_player_background_tint))
binding.background.setImageBitmap(blurredCover)
}
})
})
private fun updateBackground() = launch {
if (!isStarted()) return@launch
val width = if (binding.albumCover.width > 0) binding.albumCover.width else this@AudioPlayerActivity.getScreenWidth()
val cover = withContext(Dispatchers.IO) { AudioUtil.readCoverBitmap(Uri.decode(currentCoverArt), width) }
val blurredCover = if (cover != null) withContext(Dispatchers.Default) { UiTools.blurBitmap(cover) } else null
if (!isStarted()) return@launch
if (cover == null) {
binding.albumCover.setImageResource(R.drawable.ic_no_artwork_big)
binding.background.clearColorFilter()
binding.background.setImageResource(0)
} else {
binding.albumCover.setImageBitmap(cover)
binding.background.setColorFilter(UiTools.getColorFromAttribute(binding.background.context, R.attr.audio_player_background_tint))
binding.background.setImageBitmap(blurredCover)
}
}
override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
......
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