Skip to content

Commit

Permalink
[APT-10128] Update Reducer logic to copy seek target to playback info…
Browse files Browse the repository at this point in the history
… progress

Added tests
  • Loading branch information
griffinfscribd committed Jun 5, 2024
1 parent 7f123c9 commit 8ae21b2
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 1 deletion.
41 changes: 40 additions & 1 deletion Armadillo/src/main/java/com/scribd/armadillo/Reducer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down
64 changes: 64 additions & 0 deletions Armadillo/src/test/java/com/scribd/armadillo/ReducerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down

0 comments on commit 8ae21b2

Please sign in to comment.