From 8ae21b27cd05b023d12df1b67191c1ee9f9de861 Mon Sep 17 00:00:00 2001 From: griffinf Date: Tue, 4 Jun 2024 16:50:16 -0700 Subject: [PATCH] [APT-10128] Update Reducer logic to copy seek target to playback info progress Added tests --- .../main/java/com/scribd/armadillo/Reducer.kt | 41 +++++++++++- .../java/com/scribd/armadillo/ReducerTest.kt | 64 +++++++++++++++++++ 2 files changed, 104 insertions(+), 1 deletion(-) diff --git a/Armadillo/src/main/java/com/scribd/armadillo/Reducer.kt b/Armadillo/src/main/java/com/scribd/armadillo/Reducer.kt index 5f60b77..a075f57 100644 --- a/Armadillo/src/main/java/com/scribd/armadillo/Reducer.kt +++ b/Armadillo/src/main/java/com/scribd/armadillo/Reducer.kt @@ -175,14 +175,29 @@ internal object Reducer { isSeeking = action.isSeeking, seekTarget = action.seekPositionTarget) } else MediaControlState() - + val progress = if (action.seekPositionTarget != null) { + playbackInfo.progress.copy( + positionInDuration = action.seekPositionTarget, + currentChapterIndex = playbackInfo.audioPlayable.getChapterIndexAtOffset(action.seekPositionTarget)) + } else { + playbackInfo.progress + } oldState.copy(playbackInfo = playbackInfo.copy( + progress = progress, controlState = controlState)) .apply { debugState = newDebug } } is FastForwardAction -> { val playbackInfo = oldState.playbackInfo ?: throw ActionBeforeSetup(action) + val progress = if (action.seekPositionTarget != null) { + playbackInfo.progress.copy( + positionInDuration = action.seekPositionTarget, + currentChapterIndex = playbackInfo.audioPlayable.getChapterIndexAtOffset(action.seekPositionTarget)) + } else { + playbackInfo.progress + } oldState.copy(playbackInfo = playbackInfo.copy( + progress = progress, controlState = MediaControlState( isSeeking = true, isFastForwarding = true, @@ -191,7 +206,15 @@ internal object Reducer { } is RewindAction -> { val playbackInfo = oldState.playbackInfo ?: throw ActionBeforeSetup(action) + val progress = if (action.seekPositionTarget != null) { + playbackInfo.progress.copy( + positionInDuration = action.seekPositionTarget, + currentChapterIndex = playbackInfo.audioPlayable.getChapterIndexAtOffset(action.seekPositionTarget)) + } else { + playbackInfo.progress + } oldState.copy(playbackInfo = playbackInfo.copy( + progress = progress, controlState = MediaControlState( isSeeking = true, isRewinding = true, @@ -200,7 +223,15 @@ internal object Reducer { } is SkipNextAction -> { val playbackInfo = oldState.playbackInfo ?: throw ActionBeforeSetup(action) + val progress = if (action.seekPositionTarget != null) { + playbackInfo.progress.copy( + positionInDuration = action.seekPositionTarget, + currentChapterIndex = playbackInfo.audioPlayable.getChapterIndexAtOffset(action.seekPositionTarget)) + } else { + playbackInfo.progress + } oldState.copy(playbackInfo = playbackInfo.copy( + progress = progress, controlState = MediaControlState( isSeeking = true, isNextChapter = true, @@ -209,7 +240,15 @@ internal object Reducer { } is SkipPrevAction -> { val playbackInfo = oldState.playbackInfo ?: throw ActionBeforeSetup(action) + val progress = if (action.seekPositionTarget != null) { + playbackInfo.progress.copy( + positionInDuration = action.seekPositionTarget, + currentChapterIndex = playbackInfo.audioPlayable.getChapterIndexAtOffset(action.seekPositionTarget)) + } else { + playbackInfo.progress + } oldState.copy(playbackInfo = playbackInfo.copy( + progress = progress, controlState = MediaControlState( isSeeking = true, isPrevChapter = true, diff --git a/Armadillo/src/test/java/com/scribd/armadillo/ReducerTest.kt b/Armadillo/src/test/java/com/scribd/armadillo/ReducerTest.kt index 87c37d1..2899d10 100644 --- a/Armadillo/src/test/java/com/scribd/armadillo/ReducerTest.kt +++ b/Armadillo/src/test/java/com/scribd/armadillo/ReducerTest.kt @@ -61,6 +61,70 @@ class ReducerTest { assertThat(newState.playbackInfo!!.playbackSpeed).isEqualTo(PLAYBACK_SPEED) } + @Test + fun reduce_seek_updatesPlaybackAndControlState() { + val targetDistance = 10.milliseconds + val newState = Reducer.reduce(MockModels.appState(), SeekAction(true, targetDistance)) + assertThat(newState.playbackInfo!!.progress.positionInDuration).isEqualTo(targetDistance) + assertThat(newState.playbackInfo!!.progress.currentChapterIndex).isEqualTo(0) + + val controlState = newState.playbackInfo!!.controlState + assertThat(controlState.isSeeking).isTrue + assertThat(controlState.seekTarget).isEqualTo(targetDistance) + } + + @Test + fun reduce_fastForward_updatesPlaybackAndControlState() { + val targetDistance = 10.milliseconds + val newState = Reducer.reduce(MockModels.appState(), FastForwardAction(targetDistance)) + assertThat(newState.playbackInfo!!.progress.positionInDuration).isEqualTo(targetDistance) + assertThat(newState.playbackInfo!!.progress.currentChapterIndex).isEqualTo(0) + + val controlState = newState.playbackInfo!!.controlState + assertThat(controlState.isSeeking).isTrue + assertThat(controlState.seekTarget).isEqualTo(targetDistance) + assertThat(controlState.isFastForwarding).isTrue + } + + @Test + fun reduce_rewind_updatesPlaybackAndControlState() { + val targetDistance = 10.milliseconds + val newState = Reducer.reduce(MockModels.appState(), RewindAction(targetDistance)) + assertThat(newState.playbackInfo!!.progress.positionInDuration).isEqualTo(targetDistance) + assertThat(newState.playbackInfo!!.progress.currentChapterIndex).isEqualTo(0) + + val controlState = newState.playbackInfo!!.controlState + assertThat(controlState.isSeeking).isTrue + assertThat(controlState.seekTarget).isEqualTo(targetDistance) + assertThat(controlState.isRewinding).isTrue + } + + @Test + fun reduce_skipNext_updatesPlaybackAndControlState() { + val targetDistance = 10.milliseconds + val newState = Reducer.reduce(MockModels.appState(), SkipNextAction(targetDistance)) + assertThat(newState.playbackInfo!!.progress.positionInDuration).isEqualTo(targetDistance) + assertThat(newState.playbackInfo!!.progress.currentChapterIndex).isEqualTo(0) + + val controlState = newState.playbackInfo!!.controlState + assertThat(controlState.isSeeking).isTrue + assertThat(controlState.seekTarget).isEqualTo(targetDistance) + assertThat(controlState.isNextChapter).isTrue + } + + @Test + fun reduce_skipPrev_updatesPlaybackAndControlState() { + val targetDistance = 10.milliseconds + val newState = Reducer.reduce(MockModels.appState(), SkipPrevAction(targetDistance)) + assertThat(newState.playbackInfo!!.progress.positionInDuration).isEqualTo(targetDistance) + assertThat(newState.playbackInfo!!.progress.currentChapterIndex).isEqualTo(0) + + val controlState = newState.playbackInfo!!.controlState + assertThat(controlState.isSeeking).isTrue + assertThat(controlState.seekTarget).isEqualTo(targetDistance) + assertThat(controlState.isPrevChapter).isTrue + } + @Test fun reduce_NewAudiobookAction() { val newState = Reducer.reduce(MockModels.appState(),