Skip to content
Snippets Groups Projects
Commit 953bfaea authored by Geoffrey Métais's avatar Geoffrey Métais
Browse files

Compute progress in background

parent 587229df
No related branches found
No related tags found
No related merge requests found
Pipeline #14003 passed with stage
in 20 minutes and 39 seconds
......@@ -302,19 +302,24 @@ class AudioPlayer : Fragment(), PlaylistAdapter.IPlayer, TextWatcher, IAudioPlay
binding.timeline.progress = progress.time.toInt()
binding.progressBar.progress = progress.time.toInt()
}
val elapsedTracksTime = playlistModel.medias?.asSequence()
?.take(playlistModel.currentMediaPosition)
?.map { if (it.length != 0L) it.length else it.time }
?.sum() ?: 0L
val totalTime = elapsedTracksTime + progress.time
val currentProgressText = if (totalTime == 0L) "0s" else Tools.millisToString(totalTime, true, false, false)
val textTrack = getString(R.string.track_index, "${playlistModel.currentMediaPosition + 1} / ${playlistModel.medias?.size}")
val textProgress = getString(R.string.audio_queue_progress, "$currentProgressText / ${playlistModel.totalTime}")
binding.audioPlayProgress.text = "$textTrack • $textProgress"
binding.songsList.setPadding(binding.songsList.paddingLeft, binding.songsList.paddingTop, binding.songsList.paddingRight, binding.audioPlayProgress.height + 8.dp)
lifecycleScope.launchWhenStarted {
val text = withContext(Dispatchers.Default) {
val medias = playlistModel.medias ?: return@withContext ""
val elapsedTracksTime = medias.asSequence()
.take(playlistModel.currentMediaPosition)
.map { it.length }
.sum()
val totalTime = elapsedTracksTime + progress.time
val currentProgressText = if (totalTime == 0L) "0s" else Tools.millisToString(totalTime, true, false, false)
val textTrack = getString(R.string.track_index, "${playlistModel.currentMediaPosition + 1} / ${medias.size}")
val textProgress = getString(R.string.audio_queue_progress, "$currentProgressText / ${playlistModel.totalTime}")
"$textTrack • $textProgress"
}
binding.audioPlayProgress.text = text
}
}
override fun onSelectionSet(position: Int) {
......
......@@ -225,10 +225,13 @@ class PlaylistModel : ViewModel(), PlaybackService.Callback by EmptyPBSCallback
}
}
private fun updateTotalTime() = viewModelScope.launch{
val totalLength = medias?.asSequence()?.map {
if (it.length != 0L) it.length else it.time
}?.sum() ?: 0L
private fun updateTotalTime() = viewModelScope.launch {
val totalLength = withContext(Dispatchers.Default) {
val mediaList = medias ?: return@withContext 0L
mediaList.asSequence()
.map { it.length }
.sum()
}
totalTime = Tools.millisToString(totalLength, true, false, false)
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment