Skip to content

Commit

Permalink
Updated toolbars to not be shared on the homepage
Browse files Browse the repository at this point in the history
  • Loading branch information
diareuse committed Sep 19, 2023
1 parent 8bfca98 commit f6dc7c7
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,17 @@ import movie.metropolis.app.screen.booking.component.TicketItemEmpty
import movie.metropolis.app.screen.booking.component.TicketItemError
import movie.metropolis.app.screen.booking.component.TicketItemExpired
import movie.metropolis.app.screen.booking.component.TicketItemLoading
import movie.metropolis.app.screen.home.component.HomeScreenToolbar
import movie.style.AppButton
import movie.style.state.ImmutableList
import movie.style.theme.Theme

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun BookingScreen(
padding: PaddingValues,
behavior: TopAppBarScrollBehavior,
onMovieClick: (String) -> Unit,
profileIcon: @Composable () -> Unit,
viewModel: BookingViewModel = hiltViewModel(),
actions: ActivityActions = LocalActivityActions.current
) {
Expand All @@ -43,19 +44,18 @@ fun BookingScreen(
val scope = rememberCoroutineScope()
var isReaderActive by rememberSaveable { mutableStateOf(false) }
BookingScreenContent(
padding = padding,
behavior = behavior,
active = active,
expired = expired,
profileIcon = profileIcon,
onRefreshClick = viewModel::refresh,
onMovieClick = onMovieClick,
onShareClick = {
scope.launch {
actions.actionShare(viewModel.saveAsFile(it))
}
},
onCameraClick = { isReaderActive = true }
)
}
) { isReaderActive = true }
ReaderDialog(
isVisible = isReaderActive,
onVisibilityChanged = { isReaderActive = it },
Expand All @@ -66,15 +66,23 @@ fun BookingScreen(
@OptIn(ExperimentalMaterial3Api::class, ExperimentalFoundationApi::class)
@Composable
private fun BookingScreenContent(
padding: PaddingValues,
active: Loadable<ImmutableList<BookingView.Active>>,
expired: Loadable<ImmutableList<BookingView.Expired>>,
behavior: TopAppBarScrollBehavior,
profileIcon: @Composable () -> Unit,
onRefreshClick: () -> Unit = {},
onMovieClick: (String) -> Unit = {},
onShareClick: (BookingView.Active) -> Unit = {},
onCameraClick: () -> Unit = {}
) {
) = Scaffold(
topBar = {
HomeScreenToolbar(
profileIcon = profileIcon,
behavior = behavior,
title = { Text(stringResource(id = R.string.tickets)) }
)
}
) { padding ->
Column(
modifier = Modifier
.nestedScroll(behavior.nestedScrollConnection)
Expand Down Expand Up @@ -159,10 +167,10 @@ private fun BookingScreenContent(
private fun Preview() {
Theme {
BookingScreenContent(
padding = PaddingValues(),
active = Loadable.loading(),
expired = Loadable.loading(),
behavior = TopAppBarDefaults.exitUntilCollapsedScrollBehavior()
behavior = TopAppBarDefaults.exitUntilCollapsedScrollBehavior(),
profileIcon = {}
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.*
import androidx.compose.ui.input.nestedscroll.*
import androidx.compose.ui.res.*
import androidx.compose.ui.tooling.preview.*
import androidx.compose.ui.tooling.preview.datasource.*
import androidx.compose.ui.unit.*
import androidx.hilt.navigation.compose.hiltViewModel
import movie.metropolis.app.R
import movie.metropolis.app.feature.location.rememberLocation
import movie.metropolis.app.model.CinemaView
import movie.metropolis.app.presentation.Loadable
Expand All @@ -25,14 +27,15 @@ import movie.metropolis.app.screen.cinema.component.CinemaItemError
import movie.metropolis.app.screen.cinema.component.CinemaItemLoading
import movie.metropolis.app.screen.cinema.component.CinemaViewParameter
import movie.metropolis.app.screen.detail.plus
import movie.metropolis.app.screen.home.component.HomeScreenToolbar
import movie.style.layout.PreviewLayout

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun CinemasScreen(
padding: PaddingValues,
onClickCinema: (String) -> Unit,
state: LazyListState,
profileIcon: @Composable () -> Unit,
behavior: TopAppBarScrollBehavior,
viewModel: CinemasViewModel = hiltViewModel()
) {
Expand All @@ -44,7 +47,7 @@ fun CinemasScreen(
CinemasScreen(
items = items,
behavior = behavior,
padding = padding,
profileIcon = profileIcon,
onClickCinema = onClickCinema,
state = state
)
Expand All @@ -54,11 +57,19 @@ fun CinemasScreen(
@Composable
private fun CinemasScreen(
items: Loadable<List<CinemaView>>,
padding: PaddingValues,
behavior: TopAppBarScrollBehavior,
profileIcon: @Composable () -> Unit,
onClickCinema: (String) -> Unit,
state: LazyListState = rememberLazyListState()
) {
) = Scaffold(
topBar = {
HomeScreenToolbar(
profileIcon = profileIcon,
behavior = behavior,
title = { Text(stringResource(id = R.string.cinemas)) }
)
}
) { padding ->
LazyColumn(
modifier = Modifier
.nestedScroll(behavior.nestedScrollConnection)
Expand Down Expand Up @@ -100,7 +111,7 @@ private fun CinemaItemPreview(
) = PreviewLayout(padding = PaddingValues()) {
CinemasScreen(
items = items,
padding = PaddingValues(),
profileIcon = {},
behavior = TopAppBarDefaults.exitUntilCollapsedScrollBehavior(),
onClickCinema = {}
)
Expand Down
84 changes: 27 additions & 57 deletions app/src/main/java/movie/metropolis/app/screen/home/HomeScreen.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package movie.metropolis.app.screen.home

import androidx.compose.animation.*
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.*
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.*
Expand All @@ -21,8 +19,8 @@ import movie.metropolis.app.screen.cinema.CinemasScreen
import movie.metropolis.app.screen.cinema.CinemasViewModel
import movie.metropolis.app.screen.currentDestinationAsState
import movie.metropolis.app.screen.home.component.HomeScreenContent
import movie.metropolis.app.screen.home.component.HomeScreenToolbar
import movie.metropolis.app.screen.home.component.ProfileIcon
import movie.metropolis.app.screen.home.component.rememberScreenState
import movie.metropolis.app.screen.listing.ListingScreen
import movie.metropolis.app.screen.listing.ListingViewModel
import movie.metropolis.app.screen.settings.SettingsScreen
Expand All @@ -35,7 +33,7 @@ import movie.style.haptic.ClickOnChange
import movie.style.theme.Theme

@OptIn(
ExperimentalAnimationApi::class, ExperimentalMaterial3Api::class,
ExperimentalMaterial3Api::class,
ExperimentalComposeUiApi::class
)
@Composable
Expand All @@ -50,45 +48,28 @@ fun HomeScreen(
) {
val email = viewModel.email
val destination by controller.currentDestinationAsState()
val behavior = TopAppBarDefaults.exitUntilCollapsedScrollBehavior()
LaunchedEffect(destination) {
behavior.state.contentOffset = 0f
}
HomeScreen(
isLoggedIn = email != null,
route = destination?.route ?: startWith,
behavior = behavior,
onRouteChanged = listener@{
if (destination?.route == it) return@listener
while (controller.popBackStack()) {
/* no-op */
}
controller.navigate(it)
},
title = {
val text = when (destination?.route) {
Route.Cinemas() -> stringResource(id = R.string.cinemas)
Route.Movies() -> stringResource(id = R.string.movies)
Route.Tickets() -> stringResource(id = R.string.tickets)
Route.Settings() -> stringResource(id = R.string.settings)
else -> ""
}
Text(text)
},
profileIcon = {
onNavigateToLogin = onClickLogin
) {
val listing = rememberScreenState<ListingViewModel>()
val cinemas = rememberScreenState<CinemasViewModel>()
val booking = rememberScreenState<BookingViewModel>()
val settings = rememberScreenState<SettingsViewModel>()
val profileIcon = @Composable {
if (email != null) ProfileIcon(
email = email,
onClick = onClickUser
)
},
onNavigateToLogin = onClickLogin
) {
val moviesState = rememberLazyListState()
val cinemasState = rememberLazyListState()
val listing: ListingViewModel = hiltViewModel()
val cinemas: CinemasViewModel = hiltViewModel()
val booking: BookingViewModel = hiltViewModel()
val settings: SettingsViewModel = hiltViewModel()
}
HomeScreenContent(
modifier = Modifier.semantics {
testTagsAsResourceId = true
Expand All @@ -97,64 +78,53 @@ fun HomeScreen(
controller = controller,
listing = {
ListingScreen(
padding = PaddingValues(),
behavior = behavior,
onClickMovie = onClickMovie,
state = moviesState,
viewModel = listing
behavior = listing.behavior,
state = listing.list,
viewModel = listing.viewModel,
profileIcon = profileIcon,
onClickMovie = onClickMovie
)
},
cinemas = {
CinemasScreen(
padding = PaddingValues(),
onClickCinema = onClickCinema,
state = cinemasState,
behavior = behavior,
viewModel = cinemas
behavior = cinemas.behavior,
state = cinemas.list,
viewModel = cinemas.viewModel,
profileIcon = profileIcon,
onClickCinema = onClickCinema
)
},
booking = {
BookingScreen(
padding = PaddingValues(),
behavior = behavior,
onMovieClick = { onClickMovie(it, true) },
viewModel = booking
behavior = booking.behavior,
viewModel = booking.viewModel,
profileIcon = profileIcon,
onMovieClick = { onClickMovie(it, true) }
)
},
settings = {
SettingsScreen(
padding = PaddingValues(),
behavior = behavior,
viewModel = settings
behavior = settings.behavior,
viewModel = settings.viewModel,
profileIcon = profileIcon,
)
}
)
}
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
private fun HomeScreen(
isLoggedIn: Boolean,
route: String,
onNavigateToLogin: () -> Unit,
title: @Composable () -> Unit,
profileIcon: @Composable () -> Unit,
onRouteChanged: (String) -> Unit,
behavior: TopAppBarScrollBehavior = TopAppBarDefaults.exitUntilCollapsedScrollBehavior(),
content: @Composable () -> Unit
) {
ClickOnChange(route)
AppScaffold(
contentWindowInsets = ScaffoldDefaults.contentWindowInsets
.only(WindowInsetsSides.Bottom + WindowInsetsSides.Horizontal),
topBar = {
HomeScreenToolbar(
profileIcon = profileIcon,
behavior = behavior,
title = title
)
},
floatingActionButton = {
if (!isLoggedIn) AppButton(
onClick = onNavigateToLogin,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package movie.metropolis.app.screen.home.component

import androidx.compose.foundation.lazy.*
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.ViewModel

@OptIn(ExperimentalMaterial3Api::class)
data class ScreenState<T>(
val behavior: TopAppBarScrollBehavior,
val list: LazyListState,
val viewModel: T
)

@OptIn(ExperimentalMaterial3Api::class)
@Composable
inline fun <reified V : ViewModel> rememberScreenState(): ScreenState<V> {
return ScreenState(
behavior = TopAppBarDefaults.exitUntilCollapsedScrollBehavior(),
list = rememberLazyListState(),
viewModel = hiltViewModel()
)
}
Loading

0 comments on commit f6dc7c7

Please sign in to comment.