Skip to content

Commit

Permalink
Re-integrate the auto rewind.
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulWoitaschek committed Mar 10, 2023
1 parent dbbaee9 commit fbfe7e3
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions playback/src/main/kotlin/voice/playback/player/VoicePlayer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import voice.common.BookId
import voice.common.pref.CurrentBook
import voice.common.pref.PrefKeys
import voice.data.repo.BookRepository
import voice.logging.core.Logger
import voice.playback.session.chapterMarks
import java.time.Instant
import javax.inject.Inject
Expand All @@ -29,6 +30,8 @@ class VoicePlayer
private val currentBookId: DataStore<BookId?>,
@Named(PrefKeys.SEEK_TIME)
private val seekTimePref: Pref<Int>,
@Named(PrefKeys.AUTO_REWIND_AMOUNT)
private val autoRewindAmountPref: Pref<Int>,
) : ForwardingPlayer(player) {

private val scope = MainScope()
Expand Down Expand Up @@ -145,22 +148,36 @@ class VoicePlayer
}

override fun play() {
updateLastPlayedAt()
super.play()
playWhenReady = true
}

override fun setPlayWhenReady(playWhenReady: Boolean) {
if (playWhenReady) {
updateLastPlayedAt()
} else {
val currentPosition = player.currentPosition.takeUnless { it == C.TIME_UNSET }?.milliseconds ?: Duration.ZERO
if (currentPosition > Duration.ZERO) {
seekTo(
(currentPosition - autoRewindAmountPref.value.seconds)
.coerceAtLeast(Duration.ZERO)
.inWholeMilliseconds,
)
}
}
super.setPlayWhenReady(playWhenReady)
}

override fun pause() {
playWhenReady = false
}

private fun updateLastPlayedAt() {
scope.launch {
currentBookId.data.first()?.let { bookId ->
repo.updateBook(bookId) {
it.copy(lastPlayedAt = Instant.now())
val lastPlayedAt = Instant.now()
Logger.v("Update ${it.name}: lastPlayedAt to $lastPlayedAt")
it.copy(lastPlayedAt = lastPlayedAt)
}
}
}
Expand Down

0 comments on commit fbfe7e3

Please sign in to comment.