Skip to content

Commit

Permalink
common screen enter way
Browse files Browse the repository at this point in the history
  • Loading branch information
stslex committed Nov 22, 2023
1 parent 8214008 commit 936111d
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 129 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ sealed class AppArguments(
data class UserScreen(
val username: String
) : AppArguments(username)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,4 @@ enum class AppDestination(
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import androidx.compose.ui.platform.LocalContext
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavGraphBuilder
import androidx.navigation.compose.composable
import st.slex.csplashscreen.core.navigation.AppDestination
import st.slex.csplashscreen.core.ui.di.builder.Feature
import st.slex.csplashscreen.core.ui.di.builder.FeatureBuilder
import st.slex.csplashscreen.core.ui.di.mainUiApi
Expand Down Expand Up @@ -39,4 +42,22 @@ inline fun <reified VM : ViewModel, F : Feature> setupComponent(
.build(context.mainUiApi)
.viewModelFactory
}
}

inline fun <reified VM : ViewModel, F : Feature> NavGraphBuilder.createScreen(
appDestination: AppDestination,
featureBuilder: FeatureBuilder<F>,
crossinline content: @Composable (VM, List<String>) -> Unit
) {
composable(
route = appDestination.navigationRoute,
arguments = appDestination.composableArguments
) { navBackStackEntry ->
val arguments = appDestination.parseArguments(navBackStackEntry)
val viewModel: VM = setupComponent(
builder = featureBuilder,
key = arguments.hashCode().toString()
)
content(viewModel, arguments)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.navigation.NavGraphBuilder
import androidx.navigation.compose.composable
import androidx.paging.compose.collectAsLazyPagingItems
import st.slex.csplashscreen.core.navigation.AppArguments
import st.slex.csplashscreen.core.navigation.AppDestination
import st.slex.csplashscreen.core.ui.base.setupComponent
import st.slex.csplashscreen.core.ui.base.createScreen
import st.slex.csplashscreen.core.ui.utils.CollectAsEvent
import st.slex.csplashscreen.feature.collection.di.SingleCollectionBuilder
import st.slex.csplashscreen.feature.collection.ui.CollectionScreen
Expand All @@ -20,27 +19,13 @@ import st.slex.csplashscreen.feature.collection.ui.presenter.SingleCollectionVie
fun NavGraphBuilder.singleCollectionGraph(
modifier: Modifier = Modifier,
) {
composable(
route = AppDestination.COLLECTION.navigationRoute,
arguments = AppDestination.COLLECTION.composableArguments
) { navBackStackEntry ->
val arguments = AppDestination.COLLECTION
.parseArguments(navBackStackEntry)
.first()
.let(AppArguments::CollectionScreen)

val viewModel: SingleCollectionViewModel = setupComponent(
key = arguments.collectionId,
builder = SingleCollectionBuilder
)

val state by remember {
viewModel.state
}.collectAsState()

val photos = remember {
state.photos()
}.collectAsLazyPagingItems()
createScreen(
appDestination = AppDestination.COLLECTION,
featureBuilder = SingleCollectionBuilder
) { viewModel: SingleCollectionViewModel, args ->
val arguments = args.first().let(AppArguments::CollectionScreen)
val state by remember { viewModel.state }.collectAsState()
val photos = remember { state.photos() }.collectAsLazyPagingItems()

LaunchedEffect(Unit) {
viewModel.sendAction(Action.Init(arguments.collectionId))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.navigation.NavGraphBuilder
import androidx.navigation.compose.composable
import androidx.paging.compose.collectAsLazyPagingItems
import st.slex.csplashscreen.core.navigation.AppDestination
import st.slex.csplashscreen.core.ui.base.setupComponent
import st.slex.csplashscreen.core.ui.base.createScreen
import st.slex.csplashscreen.core.ui.utils.CollectAsEvent
import st.slex.csplashscreen.feature.favourite.di.FavouriteComponentBuilder
import st.slex.csplashscreen.feature.favourite.ui.FavouriteScreen
Expand All @@ -18,20 +17,13 @@ import st.slex.csplashscreen.feature.favourite.ui.presenter.FavouriteViewModel
fun NavGraphBuilder.favouriteGraph(
modifier: Modifier = Modifier,
) {
composable(
route = AppDestination.FAVOURITE.navigationRoute,
arguments = AppDestination.FAVOURITE.composableArguments
) {
createScreen(
appDestination = AppDestination.FAVOURITE,
featureBuilder = FavouriteComponentBuilder
) { viewModel: FavouriteViewModel, _ ->
val state by remember { viewModel.state }.collectAsState()

val viewModel: FavouriteViewModel = setupComponent(FavouriteComponentBuilder)

val state by remember {
viewModel.state
}.collectAsState()

val photos = remember {
state.photos()
}.collectAsLazyPagingItems()
val photos = remember(state.photos).collectAsLazyPagingItems()

viewModel.event.CollectAsEvent { event ->
// TODO NOT IMPLEMENTED YET
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,27 @@ import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.navigation.NavGraphBuilder
import androidx.navigation.compose.composable
import androidx.paging.compose.collectAsLazyPagingItems
import st.slex.csplashscreen.core.navigation.AppDestination
import st.slex.csplashscreen.core.ui.base.setupComponent
import st.slex.csplashscreen.core.ui.base.createScreen
import st.slex.csplashscreen.core.ui.utils.CollectAsEvent
import st.slex.csplashscreen.feature.home.di.HomeComponentBuilder
import st.slex.csplashscreen.feature.home.ui.MainScreen
import st.slex.csplashscreen.feature.home.ui.presenter.HomeStore
import st.slex.csplashscreen.feature.home.ui.presenter.HomeViewModel
import st.slex.csplashscreen.feature.home.ui.MainScreen

fun NavGraphBuilder.homeGraph(
modifier: Modifier = Modifier,
) {
composable(
route = AppDestination.HOME.navigationRoute
) {
val viewModel: HomeViewModel = setupComponent(HomeComponentBuilder)

val state by remember {
viewModel.state
}.collectAsState()
createScreen(
appDestination = AppDestination.HOME,
featureBuilder = HomeComponentBuilder
) { viewModel: HomeViewModel, _ ->

val collections = remember {
state.collections()
}.collectAsLazyPagingItems()
val state by remember { viewModel.state }.collectAsState()

val photos = remember {
state.photos()
}.collectAsLazyPagingItems()
val collections = remember(state.collections).collectAsLazyPagingItems()
val photos = remember(state.photos).collectAsLazyPagingItems()

viewModel.event.CollectAsEvent { event ->
// TODO NOT IMPLEMENTED YET
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ import androidx.compose.ui.draw.blur
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Dialog
import androidx.navigation.NavGraphBuilder
import androidx.navigation.compose.composable
import st.slex.csplashscreen.core.navigation.AppArguments
import st.slex.csplashscreen.core.navigation.AppDestination
import st.slex.csplashscreen.core.ui.base.setupComponent
import st.slex.csplashscreen.core.ui.base.createScreen
import st.slex.csplashscreen.core.ui.utils.CollectAsEvent
import st.slex.csplashscreen.feature.feature_photo_detail.di.ImageDetailComponentBuilder
import st.slex.csplashscreen.feature.feature_photo_detail.ui.ImageDetailScreen
Expand All @@ -29,34 +28,28 @@ import st.slex.csplashscreen.feature.feature_photo_detail.ui.presenter.ImageDeta
fun NavGraphBuilder.imageDetailGraph(
modifier: Modifier = Modifier,
) {
composable(
route = AppDestination.IMAGE_DETAIL.navigationRoute,
arguments = AppDestination.IMAGE_DETAIL.composableArguments
) { navBackStackEntry ->
val arguments = AppDestination.IMAGE_DETAIL.parseArguments(navBackStackEntry).let { args ->
AppArguments.ImageDetailScreen(args[0])
}

val viewModel: ImageDetailViewModel = setupComponent(
key = arguments.imageId,
builder = ImageDetailComponentBuilder
)
createScreen(
appDestination = AppDestination.IMAGE_DETAIL,
featureBuilder = ImageDetailComponentBuilder
) { viewModel: ImageDetailViewModel, args ->
val arguments = args.firstOrNull()
.orEmpty()
.let(AppArguments::ImageDetailScreen)

LaunchedEffect(arguments) {
viewModel.sendAction(Action.Init(arguments))
}

val state by remember {
viewModel.state
}.collectAsState()
val state by remember { viewModel.state }.collectAsState()

var dialogType by remember {
mutableStateOf(DialogType.NONE)
}

val blurBackground by animateDpAsState(
targetValue = if (dialogType == DialogType.NONE) 0.dp else 16.dp,
animationSpec = tween(durationMillis = 600)
animationSpec = tween(durationMillis = 600),
label = "anim_blur_dialog_background"
)

viewModel.event.CollectAsEvent { event ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.navigation.NavGraphBuilder
import androidx.navigation.compose.composable
import androidx.paging.compose.collectAsLazyPagingItems
import st.slex.csplashscreen.core.navigation.AppArguments
import st.slex.csplashscreen.core.navigation.AppDestination
import st.slex.csplashscreen.core.ui.base.setupComponent
import st.slex.csplashscreen.core.ui.base.createScreen
import st.slex.csplashscreen.core.ui.utils.CollectAsEvent
import st.slex.csplashscreen.feature.search.di.SearchPhotosComponentBuilder
import st.slex.csplashscreen.feature.search.ui.SearchPhotosScreen
Expand All @@ -20,18 +19,15 @@ import st.slex.csplashscreen.feature.search.ui.presenter.SearchViewModel
fun NavGraphBuilder.searchPhotosGraph(
modifier: Modifier = Modifier,
) {
composable(
route = AppDestination.SEARCH_PHOTOS.navigationRoute,
arguments = AppDestination.SEARCH_PHOTOS.composableArguments
) { navBackStackEntry ->
val arguments = AppDestination.SEARCH_PHOTOS.parseArguments(navBackStackEntry).let { args ->
AppArguments.SearchPhotosScreen(args[0])
}
createScreen(
appDestination = AppDestination.SEARCH_PHOTOS,
featureBuilder = SearchPhotosComponentBuilder
) { viewModel: SearchViewModel, args ->
val arguments = args.firstOrNull().orEmpty().let(AppArguments::SearchPhotosScreen)

val viewModel: SearchViewModel = setupComponent(
key = arguments.hashCode().toString(),
builder = SearchPhotosComponentBuilder
)
val state by remember { viewModel.state }.collectAsState()
val photos = remember(state.searchItems).collectAsLazyPagingItems()
val searchHistory = remember(state.historyItems).collectAsLazyPagingItems()

LaunchedEffect(arguments) {
viewModel.sendAction(Action.Init(arguments))
Expand All @@ -41,18 +37,6 @@ fun NavGraphBuilder.searchPhotosGraph(
// TODO NOT IMPLEMENTED YET
}

val state by remember {
viewModel.state
}.collectAsState()

val photos = remember {
state.searchItems()
}.collectAsLazyPagingItems()

val searchHistory = remember {
state.historyItems()
}.collectAsLazyPagingItems()

SearchPhotosScreen(
modifier = modifier,
photos = photos,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.navigation.NavGraphBuilder
import androidx.navigation.compose.composable
import androidx.paging.compose.collectAsLazyPagingItems
import st.slex.csplashscreen.core.navigation.AppArguments
import st.slex.csplashscreen.core.navigation.AppDestination
import st.slex.csplashscreen.core.ui.base.setupComponent
import st.slex.csplashscreen.core.ui.base.createScreen
import st.slex.csplashscreen.core.ui.utils.CollectAsEvent
import st.slex.csplashscreen.feature.user.di.UserComponentBuilder
import st.slex.csplashscreen.feature.user.ui.UserScreen
Expand All @@ -26,31 +25,15 @@ import st.slex.csplashscreen.feature.user.ui.state.rememberUserSwipeState
fun NavGraphBuilder.userGraph(
modifier: Modifier = Modifier,
) {
composable(
route = AppDestination.USER.navigationRoute,
arguments = AppDestination.USER.composableArguments
) { navBackStackEntry ->
createScreen(
appDestination = AppDestination.USER,
featureBuilder = UserComponentBuilder
) { viewModel: UserViewModel, args ->
val arguments = args.firstOrNull()
.orEmpty()
.let(AppArguments::UserScreen)

val arguments = AppDestination.USER.parseArguments(navBackStackEntry).let { args ->
AppArguments.UserScreen(args.first())
}

val viewModel: UserViewModel = setupComponent(
key = arguments.username,
builder = UserComponentBuilder
)

LaunchedEffect(arguments) {
viewModel.sendAction(Init(arguments))
}

viewModel.event.CollectAsEvent { event ->
// TODO NOT IMPLEMENTED YET
}

val state by remember {
viewModel.state
}.collectAsState()
val state by remember { viewModel.state }.collectAsState()

val photos = remember(arguments) {
state.photos(arguments.username)
Expand All @@ -64,16 +47,22 @@ fun NavGraphBuilder.userGraph(
state.collections(arguments.username)
}.collectAsLazyPagingItems()

LaunchedEffect(arguments) {
viewModel.sendAction(Init(arguments))
}

viewModel.event.CollectAsEvent { event ->
// TODO NOT IMPLEMENTED YET
}

val userPagerState = rememberUserPagerState(
photos = photos,
likes = likes,
collections = collections,
)

val userSwipeState = rememberUserSwipeState(
isOnPreFlingAllow = {
userPagerState.isOnPreFlingAllow
},
isOnPreFlingAllow = { userPagerState.isOnPreFlingAllow },
)

UserScreen(
Expand Down

0 comments on commit 936111d

Please sign in to comment.