Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show playing tab when current chapter is tapped #59

Merged
merged 6 commits into from
Jul 7, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class ChaptersFragment : BaseFragment(), ChapterListener {
playerViewModel.listDataLive.observe(viewLifecycleOwner) {
adapter.submitList(it.chapters)
view.setBackgroundColor(it.podcastHeader.backgroundColor)
if (it.showPlayer) showPlayer()
}
}

Expand All @@ -56,8 +57,8 @@ class ChaptersFragment : BaseFragment(), ChapterListener {
}
}

override fun onChapterClick(chapter: Chapter) {
playerViewModel.skipToChapter(chapter)
override fun onChapterClick(chapter: Chapter) {
playerViewModel.onChapterClick(chapter)
}

override fun onChapterUrlClick(chapter: Chapter) {
Expand All @@ -71,4 +72,9 @@ class ChaptersFragment : BaseFragment(), ChapterListener {
}
}
}

private fun showPlayer() {
(parentFragment as? PlayerContainerFragment)?.openPlayer()
playerViewModel.onPlayerShown()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,12 @@ class PlayerContainerFragment : BaseFragment(), HasBackstack {
(activity as? FragmentHostListener)?.showBottomSheet(upNextFragment)
}

fun openPlayer() {
val index = adapter.indexOfPlayer
if (index == -1) return
binding?.viewPager?.currentItem = index
}

fun openChaptersAt(chapter: Chapter) {
val index = adapter.indexOfChapters
if (index == -1) {
Expand Down Expand Up @@ -202,6 +208,9 @@ private class ViewPagerAdapter(fragmentManager: FragmentManager, lifecycle: Life

private var sections = listOf<Section>(Section.Player)

val indexOfPlayer: Int
get() = sections.indexOf(Section.Player)

val indexOfChapters: Int
get() = sections.indexOf(Section.Chapters)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ class PlayerViewModel @Inject constructor(
var chapters: List<Chapter>,
var upNextExpanded: Boolean,
var upNextEpisodes: List<Playable>,
var upNextSummary: UpNextSummary
var upNextSummary: UpNextSummary,
var showPlayer: Boolean,
)

var upNextExpanded = settings.getBooleanForKey(Settings.PREFERENCE_UPNEXT_EXPANDED, true)
Expand All @@ -155,6 +156,7 @@ class PlayerViewModel @Inject constructor(

private val upNextExpandedObservable = BehaviorRelay.create<Boolean>().apply { accept(upNextExpanded) }
private val chaptersExpandedObservable = BehaviorRelay.create<Boolean>().apply { accept(chaptersExpanded) }
private val showPlayerObservable = BehaviorRelay.create<Boolean>().apply { accept(false) }

val listDataRx = Observables.combineLatest(
upNextStateObservable,
Expand All @@ -164,6 +166,7 @@ class PlayerViewModel @Inject constructor(
upNextExpandedObservable,
chaptersExpandedObservable,
settings.playbackEffectsObservable,
showPlayerObservable,
this::mergeListData
)
.distinctUntilChanged()
Expand Down Expand Up @@ -274,7 +277,7 @@ class PlayerViewModel @Inject constructor(
updateSleepTimer()
}

fun mergeListData(upNextState: UpNextQueue.State, playbackState: PlaybackState, skipBackwardInSecs: Int, skipForwardInSecs: Int, upNextExpanded: Boolean, chaptersExpanded: Boolean, globalPlaybackEffects: PlaybackEffects): ListData {
fun mergeListData(upNextState: UpNextQueue.State, playbackState: PlaybackState, skipBackwardInSecs: Int, skipForwardInSecs: Int, upNextExpanded: Boolean, chaptersExpanded: Boolean, globalPlaybackEffects: PlaybackEffects, showPlayer: Boolean): ListData {
val podcast: Podcast? = (upNextState as? UpNextQueue.State.Loaded)?.podcast
val episode = (upNextState as? UpNextQueue.State.Loaded)?.episode

Expand Down Expand Up @@ -350,7 +353,8 @@ class PlayerViewModel @Inject constructor(
chapters = chapters,
upNextExpanded = upNextExpanded,
upNextEpisodes = upNextEpisodes,
upNextSummary = upNextFooter
upNextSummary = upNextFooter,
showPlayer = showPlayer
)
}

Expand Down Expand Up @@ -575,7 +579,17 @@ class PlayerViewModel @Inject constructor(
playbackManager.playPause()
}

fun skipToChapter(chapter: Chapter) {
playbackManager.skipToChapter(chapter)
fun onChapterClick(chapter: Chapter) {
launch {
if (playbackManager.isSameChapter(chapter)) {
showPlayerObservable.accept(true)
} else {
playbackManager.skipToChapter(chapter)
}
}
}

fun onPlayerShown() {
showPlayerObservable.accept(false)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't used it before but is this where you could use one of the new SharedFlow classes?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, SharedFlow can be a good candidate here. Thanks for suggesting it. Used it in e058f41.

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,12 @@ open class PlaybackManager @Inject constructor(
return upNextQueue.currentEpisode
}

private suspend fun getCurrentChapter() = getCurrentEpisode()?.let { episode ->
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if it's possible to get the current chapter with the data already in the PlayerViewModel.

    fun onChapterClick(chapter: Chapter) {
        launch {
            val listData = listDataLive.value
            if (listData?.isSameChapter(chapter) == true) {
                showPlayerObservable.accept(true)
            } else {
                playbackManager.skipToChapter(chapter)
            }
        }
    }
    data class ListData(
        var podcastHeader: PlayerHeader,
        var chaptersExpanded: Boolean,
        var chapters: List<Chapter>,
        var currentChapter: Chapter?,
        var upNextExpanded: Boolean,
        var upNextEpisodes: List<Playable>,
        var upNextSummary: UpNextSummary,
        var showPlayer: Boolean,
    ) {
        fun isSameChapter(chapter: Chapter) = currentChapter?
           .let { it.index == chapter.index } ?: false
    }
        val currentChapter = playbackState.chapters.getChapter(playbackState.positionMs)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks better, see in fb81785.

playbackStateRelay.blockingFirst().chapters.getChapter(getCurrentTimeMs(episode))
}

suspend fun isSameChapter(chapter: Chapter) = getCurrentChapter()?.index == chapter.index

private suspend fun autoLoadEpisode(autoPlay: Boolean): Playable? {
val nextEpisode = getCurrentEpisode()
if (nextEpisode != null) {
Expand Down