Skip to content

Commit

Permalink
refactor: move private functions to the bottom
Browse files Browse the repository at this point in the history
  • Loading branch information
mebarbosa committed Dec 17, 2024
1 parent e6c816f commit fc4e802
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2279,57 +2279,36 @@ open class PlaybackManager @Inject constructor(
val currentEpisodeUui = getCurrentEpisode()?.uuid

if (!isSleepAfterChapterEnabled()) {
updateLastListenedState(episodeUuid = null, chapterUuid = null)
updateLastListenedState { copy(chapterUuid = null, episodeUuid = null) }
return
}

val lastListenedState = playbackState.lastListenedState
if (lastListenedState.chapterUuid.isNullOrEmpty()) {
updateLastListenedStateChapter(currentChapterUuid)
updateLastListenedState { copy(chapterUuid = currentChapterUuid) }
}

if (lastListenedState.episodeUuid.isNullOrEmpty()) {
updateLastListenedStateEpisode(currentEpisodeUui)
updateLastListenedState { copy(episodeUuid = currentEpisodeUui) }
}

// When we switch from a episode that contains chapters to another one that does not have chapters
// the current chapter is null, so for this case we would need to verify if the episode changed to update the sleep timer counter for end of chapter
if (currentChapterUuid.isNullOrEmpty() && !lastListenedState.episodeUuid.isNullOrEmpty() && lastListenedState.episodeUuid != currentEpisodeUui) {
applicationScope.launch {
updateLastListenedStateEpisode(currentEpisodeUui)
updateLastListenedState { copy(episodeUuid = currentEpisodeUui) }
sleepEndOfChapter()
}
} else if (lastListenedState.chapterUuid == currentChapterUuid) { // Same chapter
return
} else { // Changed chapter
applicationScope.launch {
updateLastListenedState(currentChapterUuid, getCurrentEpisode()?.uuid)
updateLastListenedState { copy(chapterUuid = currentChapterUuid, episodeUuid = getCurrentEpisode()?.uuid) }
sleepEndOfChapter()
}
}
}

private fun updateLastListenedState(chapterUuid: String?, episodeUuid: String?) {
updateLastListenedState {
copy(
chapterUuid = chapterUuid,
episodeUuid = episodeUuid,
)
}
}

private fun updateLastListenedStateChapter(chapterUuid: String?) {
updateLastListenedState {
copy(chapterUuid = chapterUuid)
}
}

private fun updateLastListenedStateEpisode(episodeUuid: String?) {
updateLastListenedState {
copy(episodeUuid = episodeUuid)
}
}

private fun updateLastListenedState(update: PlaybackState.LastListenedState.() -> PlaybackState.LastListenedState) {
playbackStateRelay.blockingFirst().let { currentState ->
val updatedLastListenedState = currentState.lastListenedState.update()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,6 @@ class SleepTimer @Inject constructor(
}
}

fun updateSleepTimerEndOfEpisodes(sleepAfterEpisodes: Int) {
updateSleepTimer {
copy(numberOfEpisodesLeft = sleepAfterEpisodes)
}
}

fun updateSleepTimerEndOfChapters(sleepAfterChapters: Int) {
updateSleepTimer {
copy(numberOfChaptersLeft = sleepAfterChapters)
}
}

fun sleepAfter(duration: Duration, onSuccess: () -> Unit) {
updateSleepTimerStatus(sleepTimeRunning = true, timeLeft = duration)

Expand Down Expand Up @@ -138,7 +126,9 @@ class SleepTimer @Inject constructor(
suspend fun sleepEndOfEpisode(episode: BaseEpisode, onSleepEndOfEpisode: suspend () -> Unit) {
if (getState().isSleepEndOfEpisodeRunning) {
setEndOfEpisodeUuid(episode.uuid)
updateSleepTimerEndOfEpisodes(getState().numberOfEpisodesLeft - 1)
updateSleepTimer {
copy(numberOfEpisodesLeft = getState().numberOfEpisodesLeft - 1)
}
}

if (getState().isSleepEndOfEpisodeRunning) return
Expand All @@ -150,19 +140,11 @@ class SleepTimer @Inject constructor(
onSleepEndOfEpisode()
}

private fun setEndOfEpisodeUuid(uuid: String) {
LogBuffer.i(TAG, "Episode $uuid was marked as end of episode")
sleepTimerHistory = sleepTimerHistory.copy(
lastEpisodeUuidAutomaticEnded = uuid,
lastTimeSleepTimeHasFinished = System.currentTimeMillis().milliseconds,
)
cancelAutomaticSleepAfterTimeRestart()
cancelAutomaticSleepOnChapterEndRestart()
}

suspend fun sleepEndOfChapter(onSleepEndOfChapter: suspend () -> Unit) {
if (getState().isSleepEndOfChapterRunning) {
updateSleepTimerEndOfChapters(getState().numberOfChaptersLeft - 1)
updateSleepTimer {
copy(numberOfChaptersLeft = getState().numberOfChaptersLeft - 1)
}
setEndOfChapter()
}

Expand All @@ -175,6 +157,24 @@ class SleepTimer @Inject constructor(
onSleepEndOfChapter()
}

fun cancelTimer() {
LogBuffer.i(TAG, "Cleaning automatic sleep timer feature...")
updateSleepTimerStatus(sleepTimeRunning = false, sleepAfterChapters = 0, sleepAfterEpisodes = 0)
cancelAutomaticSleepAfterTimeRestart()
cancelAutomaticSleepOnEpisodeEndRestart()
cancelAutomaticSleepOnChapterEndRestart()
}

private fun setEndOfEpisodeUuid(uuid: String) {
LogBuffer.i(TAG, "Episode $uuid was marked as end of episode")
sleepTimerHistory = sleepTimerHistory.copy(
lastEpisodeUuidAutomaticEnded = uuid,
lastTimeSleepTimeHasFinished = System.currentTimeMillis().milliseconds,
)
cancelAutomaticSleepAfterTimeRestart()
cancelAutomaticSleepOnChapterEndRestart()
}

private fun setEndOfChapter() {
LogBuffer.i(TAG, "End of chapter was reached")
val time = System.currentTimeMillis().milliseconds
Expand All @@ -196,14 +196,6 @@ class SleepTimer @Inject constructor(

private fun shouldRestartSleepEndOfChapter(diffTime: Duration, isSleepEndOfChapterRunning: Boolean) = diffTime < MIN_TIME_TO_RESTART_SLEEP_TIMER_IN_MINUTES && !isSleepEndOfChapterRunning && sleepTimerHistory.lastSleepAfterEndOfChapterTime != null

fun cancelTimer() {
LogBuffer.i(TAG, "Cleaning automatic sleep timer feature...")
updateSleepTimerStatus(sleepTimeRunning = false, sleepAfterChapters = 0, sleepAfterEpisodes = 0)
cancelAutomaticSleepAfterTimeRestart()
cancelAutomaticSleepOnEpisodeEndRestart()
cancelAutomaticSleepOnChapterEndRestart()
}

private fun updateSleepTimer(update: SleepTimerState.() -> SleepTimerState) {
_stateFlow.update { currentState -> currentState.update() }
}
Expand Down

0 comments on commit fc4e802

Please sign in to comment.