Skip to content

Commit

Permalink
Revert e6f5a5a (#2264)
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulWoitaschek authored Jan 20, 2024
1 parent 4401d41 commit d940795
Show file tree
Hide file tree
Showing 18 changed files with 21 additions and 103 deletions.
7 changes: 0 additions & 7 deletions playback/src/main/kotlin/voice/playback/PlayerController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,6 @@ class PlayerController
controller.setPlaybackSpeed(speed)
}

fun pauseAtStart() = executeAfterPrepare {
it.pause()
val bookId = currentBookId.data.first() ?: return@executeAfterPrepare
val book = bookRepository.get(bookId) ?: return@executeAfterPrepare
it.seekTo(book.currentMark.startMs)
}

fun setGain(gain: Decibel) = executeAfterPrepare { controller ->
controller.sendCustomCommand(CustomCommand.SetGain(gain))
}
Expand Down
13 changes: 4 additions & 9 deletions playback/src/main/kotlin/voice/playback/di/PlaybackModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import dagger.Provides
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
Expand All @@ -27,7 +26,6 @@ import voice.playback.player.OnlyAudioRenderersFactory
import voice.playback.player.VoicePlayer
import voice.playback.player.onAudioSessionIdChanged
import voice.playback.playstate.PlayStateDelegatingListener
import voice.playback.playstate.PlayStateManager
import voice.playback.playstate.PositionUpdater
import voice.playback.session.LibrarySessionCallback
import voice.playback.session.PlaybackService
Expand Down Expand Up @@ -92,20 +90,17 @@ object PlaybackModule {
scope: CoroutineScope,
sleepTimer: SleepTimer,
sleepTimerCommandUpdater: SleepTimerCommandUpdater,
playStateManager: PlayStateManager,
): MediaLibraryService.MediaLibrarySession {
return MediaLibraryService.MediaLibrarySession.Builder(service, player, callback)
.setSessionActivity(mainActivityIntentProvider.toCurrentBook())
.build()
.also { session ->
scope.launch {
sleepTimer.leftSleepTimeFlow.map { it != Duration.ZERO }
.combine(playStateManager.sleepAtEocFlow) { sleepTimerActive, sleepEocActive ->
sleepTimerActive || sleepEocActive
}
sleepTimer.leftSleepTimeFlow
.map { it != Duration.ZERO }
.distinctUntilChanged()
.collect { sleepActive ->
sleepTimerCommandUpdater.update(session, sleepActive)
.collect { sleepTimerActive ->
sleepTimerCommandUpdater.update(session, sleepTimerActive)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package voice.playback.playstate

import androidx.media3.common.MediaItem
import androidx.media3.common.Player
import voice.playback.PlayerController
import javax.inject.Inject

class PlayStateDelegatingListener
@Inject constructor(
private val playStateManager: PlayStateManager,
private val playerController: PlayerController,
) : Player.Listener {

private lateinit var player: Player
Expand All @@ -30,16 +27,6 @@ class PlayStateDelegatingListener
updatePlayState()
}

override fun onMediaItemTransition(
mediaItem: MediaItem?,
reason: Int,
) {
if (playStateManager.sleepAtEoc) {
playStateManager.sleepAtEoc = false
playerController.pauseAtStart()
}
}

private fun updatePlayState() {
val playbackState = player.playbackState
playStateManager.playState = when {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,4 @@ constructor() {
Playing,
Paused,
}

// Sleep at eoc state
private val _sleepAtEoc = MutableStateFlow(false)

val sleepAtEocFlow: StateFlow<Boolean>
get() = _sleepAtEoc

var sleepAtEoc: Boolean
set(value) {
_sleepAtEoc.value = value
}
get() = _sleepAtEoc.value
}
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ class LibrarySessionCallback
): ListenableFuture<SessionResult> {
when (customCommand) {
PublishedCustomCommand.Sleep.sessionCommand -> {
sleepTimer.setEocActive(!sleepTimer.sleepTimerActive())
sleepTimer.setActive(!sleepTimer.sleepTimerActive())
}
else -> {
val command = CustomCommand.parse(customCommand, args)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,4 @@ interface SleepTimer {
val leftSleepTimeFlow: Flow<Duration>
fun sleepTimerActive(): Boolean
fun setActive(enable: Boolean)
fun setEocActive(enable: Boolean)
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ class BookPlayController(bundle: Bundle) : ComposeController(bundle) {
onIncrementSleepTime = viewModel::incrementSleepTime,
onDecrementSleepTime = viewModel::decrementSleepTime,
onAcceptSleepTime = viewModel::onAcceptSleepTime,
onAcceptSleepAtEoc = viewModel::onAcceptSleepAtEoc,
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,16 +84,11 @@ class BookPlayViewModel
}.collectAsState()

val sleepTime by remember { sleepTimer.leftSleepTimeFlow }.collectAsState()
val sleepAtEoc by remember { playStateManager.sleepAtEocFlow }.collectAsState()

val currentMark = book.currentChapter.markForPosition(book.content.positionInChapter)
val hasMoreThanOneChapter = book.chapters.sumOf { it.chapterMarks.count() } > 1
return BookPlayViewState(
sleepTimer = when {
sleepAtEoc -> BookPlayViewState.SleepTimerViewState.SleepAtEndOfChapter
sleepTime > Duration.ZERO -> BookPlayViewState.SleepTimerViewState.SleepAfterDuration(sleepTime)
else -> null
},
sleepTime = sleepTime,
playing = playState == PlayStateManager.PlayState.Playing,
title = book.content.name,
showPreviousNextButtons = hasMoreThanOneChapter,
Expand Down Expand Up @@ -150,13 +145,6 @@ class BookPlayViewModel
}
}

fun onAcceptSleepAtEoc() {
updateSleepTimeViewState {
sleepTimer.setEocActive(true)
null
}
}

private fun updateSleepTimeViewState(update: (SleepTimerViewState) -> SleepTimerViewState?) {
val current = dialogState.value
val updated: SleepTimerViewState? = if (current is BookPlayDialogViewState.SleepTimer) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,19 @@ data class BookPlayViewState(
val chapterName: String?,
val showPreviousNextButtons: Boolean,
val title: String,
val sleepTime: Duration,
val playedTime: Duration,
val duration: Duration,
val playing: Boolean,
val cover: ImmutableFile?,
val skipSilence: Boolean,
val sleepTimer: SleepTimerViewState?,
) {

init {
require(duration > Duration.ZERO) {
"Duration must be positive in $this"
}
}

@Immutable
sealed interface SleepTimerViewState {
data object SleepAtEndOfChapter : SleepTimerViewState
data class SleepAfterDuration(val remaining: Duration) : SleepTimerViewState
}
}

internal sealed interface BookPlayDialogViewState {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import voice.playbackScreen.BookPlayViewState
import voice.strings.R
import kotlin.time.Duration

@Composable
internal fun BookPlayAppBar(
Expand All @@ -39,7 +40,7 @@ internal fun BookPlayAppBar(
val appBarActions: @Composable RowScope.() -> Unit = {
IconButton(onClick = onSleepTimerClick) {
Icon(
imageVector = if (viewState.sleepTimer == null) {
imageVector = if (viewState.sleepTime == Duration.ZERO) {
Icons.Outlined.Bedtime
} else {
Icons.Outlined.BedtimeOff
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ internal fun BookPlayContent(
CoverRow(
cover = viewState.cover,
onPlayClick = onPlayClick,
sleepTimer = viewState.sleepTimer,
sleepTime = viewState.sleepTime,
modifier = Modifier
.fillMaxHeight()
.weight(1F)
Expand Down Expand Up @@ -74,7 +74,7 @@ internal fun BookPlayContent(
CoverRow(
onPlayClick = onPlayClick,
cover = viewState.cover,
sleepTimer = viewState.sleepTimer,
sleepTime = viewState.sleepTime,
modifier = Modifier
.fillMaxWidth()
.weight(1F)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ private class BookPlayViewStatePreviewProvider : PreviewParameterProvider<BookPl
playedTime = 3.minutes,
playing = true,
skipSilence = true,
sleepTimer = BookPlayViewState.SleepTimerViewState.SleepAfterDuration(4.minutes),
sleepTime = 4.minutes,
title = "Das Ende der Welt",
)
yield(initial)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,21 @@ import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import voice.common.compose.ImmutableFile
import voice.common.formatTime
import voice.playbackScreen.BookPlayViewState
import voice.strings.R
import kotlin.time.Duration

@Composable
internal fun CoverRow(
cover: ImmutableFile?,
sleepTimer: BookPlayViewState.SleepTimerViewState?,
sleepTime: Duration,
onPlayClick: () -> Unit,
modifier: Modifier = Modifier,
) {
Box(modifier) {
Cover(onDoubleClick = onPlayClick, cover = cover)
if (sleepTimer != null) {
if (sleepTime != Duration.ZERO) {
Text(
modifier = Modifier
.align(Alignment.TopEnd)
Expand All @@ -35,13 +33,10 @@ internal fun CoverRow(
shape = RoundedCornerShape(20.dp),
)
.padding(horizontal = 20.dp, vertical = 16.dp),
text = when (sleepTimer) {
BookPlayViewState.SleepTimerViewState.SleepAtEndOfChapter -> stringResource(R.string.end_of_chapter)
is BookPlayViewState.SleepTimerViewState.SleepAfterDuration -> formatTime(
timeMs = sleepTimer.remaining.inWholeMilliseconds,
durationMs = sleepTimer.remaining.inWholeMilliseconds,
)
},
text = formatTime(
timeMs = sleepTime.inWholeMilliseconds,
durationMs = sleepTime.inWholeMilliseconds,
),
color = Color.White,
)
}
Expand Down
11 changes: 1 addition & 10 deletions sleepTimer/src/main/kotlin/voice/sleepTimer/SleepTimer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class SleepTimer
}
override val leftSleepTimeFlow: StateFlow<Duration> get() = _leftSleepTime

override fun sleepTimerActive(): Boolean = (sleepJob?.isActive == true && leftSleepTime > Duration.ZERO) || playStateManager.sleepAtEoc
override fun sleepTimerActive(): Boolean = sleepJob?.isActive == true && leftSleepTime > Duration.ZERO

private var sleepJob: Job? = null

Expand All @@ -62,14 +62,6 @@ class SleepTimer
}
}

override fun setEocActive(enable: Boolean) {
if (enable) {
playStateManager.sleepAtEoc = true
} else {
cancel()
}
}

fun setActive(sleepTime: Duration = sleepTimePref.value.minutes) {
Logger.i("Starting sleepTimer. Pause in $sleepTime.")
leftSleepTime = sleepTime
Expand Down Expand Up @@ -129,6 +121,5 @@ class SleepTimer
sleepJob?.cancel()
leftSleepTime = Duration.ZERO
playerController.setVolume(1F)
playStateManager.sleepAtEoc = false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ fun SleepTimerDialog(
onIncrementSleepTime: () -> Unit,
onDecrementSleepTime: () -> Unit,
onAcceptSleepTime: (Int) -> Unit,
onAcceptSleepAtEoc: () -> Unit,
modifier: Modifier = Modifier,
) {
ModalBottomSheet(
Expand Down Expand Up @@ -61,14 +60,6 @@ fun SleepTimerDialog(
},
)
}
ListItem(
modifier = Modifier.clickable {
onAcceptSleepAtEoc()
},
headlineContent = {
Text(text = stringResource(id = StringsR.string.end_of_chapter))
},
)
ListItem(
modifier = Modifier.clickable {
onAcceptSleepTime(viewState.customSleepTime)
Expand Down
3 changes: 1 addition & 2 deletions strings/src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@
<string name="notification_sleep_timer_disable">Einschlaftimer ausschalten</string>
<string name="play">Abspielen</string>
<string name="pause">Pause</string>
<string name="end_of_chapter">Kapitelende</string>
<string name="generic_error_message">Etwas ist schief gelaufen 😢</string>
<string name="generic_error_retry">Erneut versuchen</string>
<string name="cover_search_template_with_author">%1$s von %2$s Hörbuch cover</string>
Expand Down Expand Up @@ -124,4 +123,4 @@
<string name="review.request.content">Ihr Feedback ist uns wichtig! Wenn Sie einen Moment Zeit haben, bewerten Sie uns und teilen Sie Ihre Erfahrungen. Es hilft uns, Verbesserungen vorzunehmen und ein besseres Hörerlebnis zu bieten.</string>
<string name="review.feedback.title">Es tut uns leid, das zu hören!</string>
<string name="review.feedback.content">Könnten Sie uns sagen, was schief gelaufen ist\? Wir schätzen Ihr Feedback und es hilft uns, Ihre Erfahrung zu verbessern.</string>
</resources>
</resources>
1 change: 0 additions & 1 deletion strings/src/main/res/values-es/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@
<string name="media_session_recent">Reciente</string>
<string name="pause">Pausar</string>
<string name="play">Reproducir</string>
<string name="end_of_chapter">Fin del capítulo</string>
<string name="generic_error_retry">Reintentar</string>
<string name="generic_error_message">Algo salió mal 😢</string>
<string name="cover_search_template_no_author">%s portada del audiolibro</string>
Expand Down
1 change: 0 additions & 1 deletion strings/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@
<string name="media_session_recent">Recent</string>
<string name="notification_sleep_timer_enable">Enable Sleep Timer</string>
<string name="notification_sleep_timer_disable">Disable Sleep Timer</string>
<string name="end_of_chapter">End of Chapter</string>
<string name="generic_error_message">Something went wrong 😢</string>
<string name="generic_error_retry">Retry</string>
<string name="storage_bug_title">Permission required</string>
Expand Down

0 comments on commit d940795

Please sign in to comment.