Skip to content

Commit

Permalink
refactor: simplify restart method
Browse files Browse the repository at this point in the history
  • Loading branch information
mebarbosa committed Dec 16, 2024
1 parent b23d0cf commit eeb7fab
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2111,24 +2111,7 @@ open class PlaybackManager @Inject constructor(

player?.play(currentTimeMs)

sleepTimer.restartSleepTimerIfApplies(
autoSleepTimerEnabled = settings.autoSleepTimerRestart.value,
currentEpisodeUuid = episode.uuid,
timerState = sleepTimer.getState(),
onRestartSleepAfterTime = {
sleepTimer.updateSleepTimerStatus(sleepTimeRunning = true)
},
onRestartSleepOnEpisodeEnd = {
val episodes = settings.getlastSleepEndOfEpisodes()
LogBuffer.i(SleepTimer.TAG, "Sleep timer was restarted with end of $episodes episodes set")
sleepTimer.updateSleepTimerStatus(sleepTimeRunning = true, sleepAfterEpisodes = episodes)
},
onRestartSleepOnChapterEnd = {
val chapter = settings.getlastSleepEndOfChapter()
LogBuffer.i(SleepTimer.TAG, "Sleep timer was restarted with end of $chapter chapter set")
sleepTimer.updateSleepTimerStatus(sleepTimeRunning = true, sleepAfterChapters = chapter)
},
)
sleepTimer.restartSleepTimerIfApplies(currentEpisodeUuid = episode.uuid)

trackPlayback(AnalyticsEvent.PLAYBACK_PLAY, sourceView)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,29 +106,29 @@ class SleepTimer @Inject constructor(
}

fun restartSleepTimerIfApplies(
autoSleepTimerEnabled: Boolean,
currentEpisodeUuid: String,
timerState: SleepTimerState,
onRestartSleepAfterTime: () -> Unit,
onRestartSleepOnEpisodeEnd: () -> Unit,
onRestartSleepOnChapterEnd: () -> Unit,
) {
if (!autoSleepTimerEnabled) return
if (!settings.autoSleepTimerRestart.value) return

sleepTimerHistory.lastTimeSleepTimeHasFinished?.let { lastTimeHasFinished ->
val diffTime = System.currentTimeMillis().milliseconds - lastTimeHasFinished

if (shouldRestartSleepEndOfChapter(diffTime, timerState.isSleepEndOfChapterRunning)) {
onRestartSleepOnChapterEnd()
if (shouldRestartSleepEndOfChapter(diffTime, getState().isSleepEndOfChapterRunning)) {
val chapter = settings.getlastSleepEndOfChapter()
LogBuffer.i(TAG, "Sleep timer was restarted with end of $chapter chapter set")
updateSleepTimerStatus(sleepTimeRunning = true, sleepAfterChapters = chapter)
analyticsTracker.track(PLAYER_SLEEP_TIMER_RESTARTED, mapOf(TIME_KEY to END_OF_CHAPTER_VALUE, NUMBER_OF_CHAPTERS_KEY to settings.getlastSleepEndOfChapter()))
} else if (shouldRestartSleepEndOfEpisode(diffTime, currentEpisodeUuid, timerState.isSleepEndOfEpisodeRunning)) {
onRestartSleepOnEpisodeEnd()
} else if (shouldRestartSleepEndOfEpisode(diffTime, currentEpisodeUuid, getState().isSleepEndOfEpisodeRunning)) {
val episodes = settings.getlastSleepEndOfEpisodes()
LogBuffer.i(TAG, "Sleep timer was restarted with end of $episodes episodes set")
updateSleepTimerStatus(sleepTimeRunning = true, sleepAfterEpisodes = episodes)
analyticsTracker.track(PLAYER_SLEEP_TIMER_RESTARTED, mapOf(TIME_KEY to END_OF_EPISODE_VALUE, NUMBER_OF_EPISODES_KEY to settings.getlastSleepEndOfEpisodes()))
} else if (shouldRestartSleepAfterTime(diffTime, timerState.isSleepTimerRunning)) {
} else if (shouldRestartSleepAfterTime(diffTime, getState().isSleepTimerRunning)) {
sleepTimerHistory.lastSleepAfterTime?.let {
analyticsTracker.track(PLAYER_SLEEP_TIMER_RESTARTED, mapOf(TIME_KEY to it.inWholeSeconds))
LogBuffer.i(TAG, "Was restarted with ${it.inWholeMinutes} minutes set")
sleepAfter(it, onRestartSleepAfterTime)
sleepAfter(it) {
analyticsTracker.track(PLAYER_SLEEP_TIMER_RESTARTED, mapOf(TIME_KEY to it.inWholeSeconds))
LogBuffer.i(TAG, "Was restarted with ${it.inWholeMinutes} minutes set")
}
}
}
}
Expand Down

0 comments on commit eeb7fab

Please sign in to comment.