-
Notifications
You must be signed in to change notification settings - Fork 895
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2543 from quran/audio_repository
Emit audio playback events via audio repository
- Loading branch information
Showing
13 changed files
with
172 additions
and
192 deletions.
There are no files selected for viewing
27 changes: 0 additions & 27 deletions
27
app/src/main/java/com/quran/labs/androidquran/bridge/AudioEventPresenterBridge.kt
This file was deleted.
Oops, something went wrong.
60 changes: 60 additions & 0 deletions
60
app/src/main/java/com/quran/labs/androidquran/bridge/AudioStatusRepositoryBridge.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package com.quran.labs.androidquran.bridge | ||
|
||
import com.quran.data.model.SuraAyah | ||
import com.quran.labs.androidquran.common.audio.model.playback.AudioRequest | ||
import com.quran.labs.androidquran.common.audio.model.playback.AudioStatus | ||
import com.quran.labs.androidquran.common.audio.model.playback.PlaybackStatus | ||
import com.quran.labs.androidquran.common.audio.repository.AudioStatusRepository | ||
import com.quran.labs.androidquran.view.AudioStatusBar | ||
import kotlinx.coroutines.MainScope | ||
import kotlinx.coroutines.cancel | ||
import kotlinx.coroutines.flow.launchIn | ||
import kotlinx.coroutines.flow.onEach | ||
|
||
class AudioStatusRepositoryBridge( | ||
audioStatusRepository: AudioStatusRepository, | ||
audioStatusBar: () -> AudioStatusBar, | ||
onPlaybackAyahChanged: ((SuraAyah?) -> Unit) | ||
) { | ||
|
||
private val scope = MainScope() | ||
private val audioPlaybackAyahFlow = audioStatusRepository.audioPlaybackFlow | ||
|
||
init { | ||
audioPlaybackAyahFlow | ||
.onEach { status -> | ||
when (status) { | ||
is AudioStatus.Playback -> { | ||
val statusBar = audioStatusBar() | ||
if (status.playbackStatus == PlaybackStatus.PLAYING) { | ||
statusBar.switchMode(AudioStatusBar.PLAYING_MODE) | ||
if (status.audioRequest.repeatInfo >= -1) { | ||
statusBar.setRepeatCount(status.audioRequest.repeatInfo) | ||
statusBar.setSpeed(status.audioRequest.playbackSpeed) | ||
} | ||
} else if (status.playbackStatus == PlaybackStatus.PAUSED) { | ||
statusBar.switchMode(AudioStatusBar.PAUSED_MODE) | ||
} else if (status.playbackStatus == PlaybackStatus.PREPARING) { | ||
statusBar.switchMode(AudioStatusBar.LOADING_MODE) | ||
} | ||
onPlaybackAyahChanged(status.currentAyah) | ||
} | ||
AudioStatus.Stopped -> { | ||
audioStatusBar().switchMode(AudioStatusBar.STOPPED_MODE) | ||
} | ||
} | ||
} | ||
.launchIn(scope) | ||
} | ||
|
||
fun audioRequest(): AudioRequest? { | ||
return when (val status = audioPlaybackAyahFlow.value) { | ||
is AudioStatus.Playback -> status.audioRequest | ||
AudioStatus.Stopped -> null | ||
} | ||
} | ||
|
||
fun dispose() { | ||
scope.cancel() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.