Skip to content

Commit

Permalink
Twelve: NowPlayingViewModel: Don't poll current position w/o lyrics
Browse files Browse the repository at this point in the history
Change-Id: Iffa0a22cdc8443c47521af73784f489cb3564eec
  • Loading branch information
SebaUbuntu committed Mar 4, 2025
1 parent b669300 commit 437fced
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ import org.lineageos.twelve.R
import org.lineageos.twelve.ext.getViewProperty
import org.lineageos.twelve.ext.updateMargin
import org.lineageos.twelve.ext.updatePadding
import org.lineageos.twelve.models.FlowResult
import org.lineageos.twelve.models.Lyrics
import org.lineageos.twelve.models.Result
import org.lineageos.twelve.ui.recyclerview.CenterSmoothScroller
import org.lineageos.twelve.ui.recyclerview.SimpleListAdapter
import org.lineageos.twelve.viewmodels.LyricsViewModel
Expand Down Expand Up @@ -165,11 +165,11 @@ class LyricsFragment : Fragment(R.layout.fragment_lyrics) {
launch {
viewModel.lyricsLines.collectLatest {
when (it) {
null -> {
is FlowResult.Loading -> {
// Do nothing
}

is Result.Success -> {
is FlowResult.Success -> {
val (lyricsWithState, currentIndex) = it.data

adapter.submitList(lyricsWithState)
Expand All @@ -187,7 +187,7 @@ class LyricsFragment : Fragment(R.layout.fragment_lyrics) {
noElementsNestedScrollView.isVisible = isEmpty
}

is Result.Error -> {
is FlowResult.Error -> {
Log.e(
LOG_TAG,
"Error while loading lyrics, error: ${it.error}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -543,11 +543,11 @@ class NowPlayingFragment : Fragment(R.layout.fragment_now_playing) {
launch {
viewModel.lyricsLines.collectLatest {
when (it) {
null -> {
is FlowResult.Loading -> {
// Do nothing
}

is Result.Success -> {
is FlowResult.Success -> {
val (lyrics, currentIndex) = it.data

val index = currentIndex ?: 0
Expand All @@ -563,7 +563,7 @@ class NowPlayingFragment : Fragment(R.layout.fragment_now_playing) {
lyricsMaterialCardView.isVisible = true
}

is Result.Error -> {
is FlowResult.Error -> {
Log.e(
LOG_TAG,
"Error while loading lyrics: ${it.error}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ import org.lineageos.twelve.ext.tracksFlow
import org.lineageos.twelve.models.Error
import org.lineageos.twelve.models.FlowResult
import org.lineageos.twelve.models.FlowResult.Companion.asFlowResult
import org.lineageos.twelve.models.FlowResult.Companion.flatMapLatestData
import org.lineageos.twelve.models.PlaybackProgress
import org.lineageos.twelve.models.PlaybackState
import org.lineageos.twelve.models.RepeatMode
import org.lineageos.twelve.models.Result
import org.lineageos.twelve.models.Result.Companion.map
import org.lineageos.twelve.services.PlaybackService
import org.lineageos.twelve.services.PlaybackService.CustomCommand.Companion.sendCustomCommand
import org.lineageos.twelve.utils.MimeUtils
Expand Down Expand Up @@ -348,51 +348,51 @@ open class NowPlayingViewModel(application: Application) : TwelveViewModel(appli
.flatMapLatest { mediaItemUri ->
mediaItemUri?.let {
mediaRepository.lyrics(it)
} ?: flowOf(null)
} ?: flowOf(Result.Error(Error.NOT_FOUND))
}
.asFlowResult()
.flowOn(Dispatchers.IO)
.stateIn(
viewModelScope,
started = SharingStarted.WhileSubscribed(),
initialValue = null
initialValue = FlowResult.Loading()
)

val lyricsLines = combine(
lyrics,
durationCurrentPositionMs,
) { lyrics, durationCurrentPositionMs ->
lyrics.map {
var currentIndex: Int? = null

val linesWithState = it.lines.withIndex().map { (i, line) ->
val lyricsLineState = line.durationMs?.let { durationMs ->
durationCurrentPositionMs.second?.let { currentPositionMs ->
when {
currentPositionMs < durationMs.first -> LyricsLineState.PENDING
currentPositionMs in durationMs -> LyricsLineState.ACTIVE
currentPositionMs > durationMs.last -> LyricsLineState.PAST
else -> LyricsLineState.UNKNOWN
@OptIn(ExperimentalCoroutinesApi::class)
val lyricsLines = lyrics
.flatMapLatestData { lyrics ->
durationCurrentPositionMs.mapLatest { durationCurrentPositionMs ->
var currentIndex: Int? = null

val linesWithState = lyrics.lines.withIndex().map { (i, line) ->
val lyricsLineState = line.durationMs?.let { durationMs ->
durationCurrentPositionMs.second?.let { currentPositionMs ->
when {
currentPositionMs < durationMs.first -> LyricsLineState.PENDING
currentPositionMs in durationMs -> LyricsLineState.ACTIVE
currentPositionMs > durationMs.last -> LyricsLineState.PAST
else -> LyricsLineState.UNKNOWN
}
}
} ?: LyricsLineState.UNKNOWN

if (lyricsLineState == LyricsLineState.ACTIVE
|| lyricsLineState == LyricsLineState.PAST
) {
currentIndex = i
}
} ?: LyricsLineState.UNKNOWN

if (lyricsLineState == LyricsLineState.ACTIVE
|| lyricsLineState == LyricsLineState.PAST
) {
currentIndex = i
line to lyricsLineState
}

line to lyricsLineState
FlowResult.Success(linesWithState to currentIndex)
}

linesWithState to currentIndex
}
}
.flowOn(Dispatchers.IO)
.stateIn(
viewModelScope,
started = SharingStarted.WhileSubscribed(),
initialValue = null
initialValue = FlowResult.Loading()
)

fun togglePlayPause() {
Expand Down

0 comments on commit 437fced

Please sign in to comment.