Skip to content

Commit

Permalink
Merge pull request #990 from Automattic/task/825-wear-app-episode-to-…
Browse files Browse the repository at this point in the history
…now-playing-page

Open watch app's Now Playing screen when tapping on episode screen's Play icon
  • Loading branch information
ashiagr authored May 24, 2023
2 parents 148e8ac + bd5a809 commit 8f74482
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import au.com.shiftyjelly.pocketcasts.wear.ui.downloads.DownloadsScreen
import au.com.shiftyjelly.pocketcasts.wear.ui.episode.EpisodeScreenFlow
import au.com.shiftyjelly.pocketcasts.wear.ui.episode.EpisodeScreenFlow.episodeGraph
import au.com.shiftyjelly.pocketcasts.wear.ui.player.EffectsScreen
import au.com.shiftyjelly.pocketcasts.wear.ui.player.NowPlayingScreen
import au.com.shiftyjelly.pocketcasts.wear.ui.player.PCVolumeScreen
import au.com.shiftyjelly.pocketcasts.wear.ui.player.StreamingConfirmationScreen
import au.com.shiftyjelly.pocketcasts.wear.ui.podcast.PodcastScreen
Expand Down Expand Up @@ -120,7 +121,7 @@ fun WearApp(
navigateToRoute = navController::navigate,
toNowPlaying = {
coroutineScope.launch {
pagerState.animateScrollToPage(1)
pagerState.animateScrollToPage(NowPlayingScreen.pagerIndex)
}
},
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,16 @@ fun EpisodeScreen(
navigateToConfirmDeleteDownload: () -> Unit,
navigateToRemoveFromUpNextNotification: () -> Unit,
navigateToStreamingConfirmation: () -> Unit,
navigateToNowPlaying: () -> Unit,
) {

val viewModel = hiltViewModel<EpisodeViewModel>()
val state = viewModel.stateFlow.collectAsState().value
if (state !is EpisodeViewModel.State.Loaded) return

val showNowPlaying = viewModel.showNowPlaying.collectAsState(false).value
if (showNowPlaying) navigateToNowPlaying()

val episode = state.episode
val podcast = state.podcast
val context = LocalContext.current
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package au.com.shiftyjelly.pocketcasts.wear.ui.episode

import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
Expand All @@ -18,11 +20,13 @@ import androidx.wear.compose.material.SwipeToDismissBoxState
import au.com.shiftyjelly.pocketcasts.wear.ui.component.NotificationScreen
import au.com.shiftyjelly.pocketcasts.wear.ui.component.NowPlayingPager
import au.com.shiftyjelly.pocketcasts.wear.ui.component.ObtainConfirmationScreen
import au.com.shiftyjelly.pocketcasts.wear.ui.player.NowPlayingScreen
import au.com.shiftyjelly.pocketcasts.wear.ui.player.StreamingConfirmationScreen
import com.google.android.horologist.compose.layout.ScalingLazyColumnDefaults
import com.google.android.horologist.compose.navscaffold.NavScaffoldViewModel
import com.google.android.horologist.compose.navscaffold.composable
import com.google.android.horologist.compose.navscaffold.scrollable
import kotlinx.coroutines.launch
import au.com.shiftyjelly.pocketcasts.localization.R as LR

object EpisodeScreenFlow {
Expand All @@ -37,6 +41,7 @@ object EpisodeScreenFlow {
private const val deleteDownloadNotificationScreen = "deleteDownloadNotificationScreen"
private const val removeFromUpNextNotificationScreen = "removeFromUpNextNotificationScreen"

@OptIn(ExperimentalFoundationApi::class)
fun NavGraphBuilder.episodeGraph(
navigateToPodcast: (podcastUuid: String) -> Unit,
navController: NavController,
Expand Down Expand Up @@ -71,9 +76,13 @@ object EpisodeScreenFlow {
}
}

val pagerState = rememberPagerState()
val coroutineScope = rememberCoroutineScope()

@OptIn(ExperimentalFoundationApi::class)
NowPlayingPager(
navController = navController,
pagerState = pagerState,
swipeToDismissState = swipeToDismissState,
scrollableScaffoldContext = it,
) {
Expand All @@ -90,6 +99,11 @@ object EpisodeScreenFlow {
navigateToStreamingConfirmation = {
navController.navigate(StreamingConfirmationScreen.route)
},
navigateToNowPlaying = {
coroutineScope.launch {
pagerState.animateScrollToPage(NowPlayingScreen.pagerIndex)
}
},
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ import coil.request.SuccessResult
import dagger.hilt.android.lifecycle.HiltViewModel
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.filterIsInstance
Expand Down Expand Up @@ -104,6 +106,10 @@ class EpisodeViewModel @Inject constructor(

val stateFlow: StateFlow<State>

// SharedFlow used for one shot operation like navigating to the Now Playing screen
private val _showNowPlaying = MutableSharedFlow<Boolean>()
val showNowPlaying = _showNowPlaying.asSharedFlow()

init {
val episodeUuid = savedStateHandle.get<String>(EpisodeScreenFlow.episodeUuidArgument)
?: throw IllegalStateException("EpisodeViewModel must have an episode uuid in the SavedStateHandle")
Expand Down Expand Up @@ -259,6 +265,7 @@ class EpisodeViewModel @Inject constructor(
episode = episode,
playbackSource = analyticsSource,
)
_showNowPlaying.emit(true)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import au.com.shiftyjelly.pocketcasts.localization.R as LR

object NowPlayingScreen {
const val route = "now_playing"
const val pagerIndex = 1
}

@OptIn(ExperimentalWearFoundationApi::class)
Expand Down

0 comments on commit 8f74482

Please sign in to comment.