Skip to content

Commit

Permalink
Merge pull request #2543 from quran/audio_repository
Browse files Browse the repository at this point in the history
Emit audio playback events via audio repository
  • Loading branch information
ahmedre authored Jan 13, 2024
2 parents 875fec5 + 1289004 commit 4239955
Show file tree
Hide file tree
Showing 13 changed files with 172 additions and 192 deletions.

This file was deleted.

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()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class AudioQueue(private val quranInfo: QuranInfo,

fun getCurrentSura() = playbackInfo.currentAyah.sura
fun getCurrentAyah() = playbackInfo.currentAyah.ayah
fun getCurrentPlaybackAyah() = playbackInfo.currentAyah

fun playNextAyah(skipAyahRepeat: Boolean = false): Boolean {
if (playbackInfo.shouldPlayBasmallah) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import com.quran.data.model.selection.AyahSelection
import com.quran.data.model.selection.SelectionIndicator
import com.quran.data.model.selection.startSuraAyah
import com.quran.labs.androidquran.common.QuranAyahInfo
import com.quran.labs.androidquran.common.audio.model.playback.currentPlaybackAyah
import com.quran.labs.androidquran.common.audio.repository.AudioStatusRepository
import com.quran.labs.androidquran.data.QuranDisplayData
import com.quran.labs.androidquran.data.SuraAyahIterator
import com.quran.labs.androidquran.presenter.Presenter
Expand All @@ -34,7 +36,6 @@ import com.quran.mobile.bookmark.model.BookmarkModel
import com.quran.mobile.translation.model.LocalTranslation
import com.quran.page.common.data.AyahCoordinates
import com.quran.page.common.data.PageCoordinates
import com.quran.reading.common.AudioEventPresenter
import com.quran.reading.common.ReadingEventPresenter
import com.quran.recitation.events.RecitationEventPresenter
import com.quran.recitation.presenter.RecitationHighlightsPresenter
Expand All @@ -58,8 +59,8 @@ class AyahTrackerPresenter @Inject constructor(
private val quranSettings: QuranSettings,
private val readingEventPresenter: ReadingEventPresenter,
private val bookmarkModel: BookmarkModel,
private val audioEventPresenter: AudioEventPresenter,
private val recitationPresenter: RecitationPresenter,
private val audioStatusRepository: AudioStatusRepository,
recitationPresenter: RecitationPresenter,
private val recitationEventPresenter: RecitationEventPresenter,
private val recitationPopupPresenter: RecitationPopupPresenter,
private val recitationHighlightsPresenter: RecitationHighlightsPresenter,
Expand All @@ -81,8 +82,8 @@ class AyahTrackerPresenter @Inject constructor(
.onEach { onAyahSelectionChanged(it) }
.launchIn(scope)

audioEventPresenter.audioPlaybackAyahFlow
.onEach { onAudioSelectionChanged(it) }
audioStatusRepository.audioPlaybackFlow
.onEach { onAudioSelectionChanged(it.currentPlaybackAyah()) }
.launchIn(scope)

items.forEach { trackerItem ->
Expand Down
Loading

0 comments on commit 4239955

Please sign in to comment.