Industry Level Compose App, Navigation and Scaffold

I am trying to build a complex app, that has multiple different nav graphs, screens etc. I got stuck while implementing the Flow for Login.

I don’t really see which is the most perfect way of implementing the Scaffold, while keeping only one NavHost and not causing too many recompsition and too complex ui logic.

There is like a flow of three different navgraphs which we are talking about:

Login Graph -> First Login Graph (after successfully authenticating but the user needs to provide some additional data the first time the user logs in) -> The Main Screen.

            Surface(
                modifier = Modifier.fillMaxSize(),
                color = MaterialTheme.colorScheme.background,
            ) {
                if (!state.isLoading) {
                    NavHost(
                        navController = navController,
                        startDestination = LoginNavigationGraph
                    ) {
                        loginNavigationGraph(
                            navHostController = navController,
                        )

                        firstLoginOwnerNavGraph(
                            navHostController = navController,
                        )

                        mainScreenOwnerNavGraph(
                            navHostController = navController,
                        )
                    }
                }

On Each composable I used a Scaffold there so each screen had a Scaffold at its root like this:

@Composable
private fun FirstLoginOwnerFirstScreen(
    isContinueButtonAvailable: Boolean,
    lodgingPropertyList: ImmutableList<PropertyTag>,
    selectedPropertyList: ImmutableList<Int>,
    isLoading: Boolean,
    onContinueButtonClicked: () -> Unit,
    onAction: (FirstLoginOwnerFirstScreenAction) -> Unit,
) {
    Scaffold(
        topBar = {
            MyAppHeader(
                showBackButton = false
            )
        }
    ) { paddingValues -> .... }

and it is being used by a similalry named composable with the end name “Root” so I can use the composable witouth the postif for Preview

@Composable
fun FirstLoginOwnerFirstScreenRoot(
    viewModel: FirstLoginOwnerFirstScreenViewModel = hiltViewModel(),
    firstLoginOwnerFlowViewModel: FirstLoginOwnerFlowViewModel,
    onNextScreenPressed: (ImmutableList<PropertyTag>) -> Unit,
) { .....

And i add them in each nav graph like this:

fun NavGraphBuilder.firstLoginOwnerNavGraph(
    navHostController: NavHostController,
) {
    navigation<OwnerGraph.FirstLogin>(
        startDestination = OwnerGraph.FirstLogin.FirstLoginScreen1
    ) {
        composable<OwnerGraph.FirstLogin.FirstLoginScreen1> { navBackStackEntry ->
            val registrationFlowViewModel =
                navBackStackEntry.sharedViewModel<FirstLoginOwnerFlowViewModel>(navController = navHostController)

            FirstLoginOwnerFirstScreenRoot(
                firstLoginOwnerFlowViewModel = registrationFlowViewModel,
                onNextScreenPressed = { propertyTags ->
                    navHostController.navigate(
                        OwnerGraph.FirstLogin.FirstLoginScreen2(
                            list = propertyTags.toPropertyTagNavigation()
                        )
                    )
                }
            )
        }

And I came to the point that after the First login is completed, I navigate the user to the First Main Screen and this time I started having doubts about how I went with it so far because now I would need my bottom nav bar and using the top app bar more extensively.

First I thouth of adding a Scaffold here and a Second NavHost so I can manage the bottom navigation at one place

fun NavGraphBuilder.mainScreenOwnerNavGraph(
    navHostController: NavHostController,
) {
    navigation<OwnerGraph.MainScreen>(
        startDestination = OwnerGraph.MainScreen.MainScreenFirstScreen()
    ) {
        composable<OwnerGraph.MainScreen.MainScreenFirstScreen> { navBackStackEntry ->
            Scaffold() {
               NavHost(...
        }
    }
}

But I read from multiple sources it is not a good idea to do so.

So I started of thinking like “elavating” one Root scaffold all the way up the hierarchy which I could use, and started removing the scaffold from each composables in a NavGraph

            Scaffold(
                topBar = {
                    ???
                }
            ) {
                if (!state.isLoading) {
                    NavHost(
                        navController = navController,
                        startDestination = LoginNavigationGraph,
                        modifier = Modifier
                            .padding(it)
                    ) {
                        loginNavigationGraph(
                            navHostController = navController,
                        )

                        firstLoginOwnerNavGraph(
                            navHostController = navController,
                        )

                        mainScreenOwnerNavGraph(
                            navHostController = navController,
                        )
                    }

                }
            }

But now I started wondering now how do I set the TopAppBar for example when the user navigates to the FirstLoginOwnerFirstScreenRoot? How do I control its visibility or its content? What if depending on the screen different top app bar needed? How do I set the function of the navigation button. Of course 99% it is just a navcontroller.popBackStack() but what if I want to do something else in some random screen down the line?

So I had the idea of creating a ScaffoldElement data class, where I would store them in a hashMap so accordingly of the current navigation destination I could get it from the list like this:

data class MyAppScaffoldElement(
    val topAppBar: @Composable (NavController) -> Unit = {
        MyAppHeader()
    },
)

val myAppList = hashMapOf(
    LoginNavigationGraph.LoginMainScreen::class.qualifiedName to MyAppScaffoldElement()
)

and in the main place:

    setContent {
        val navController = rememberNavController()
        val backStackEntry = navController.currentBackStackEntryAsState()


        val myAppScaffoldItemTopBar = remember(backStackEntry.value) {
            backStackEntry.value?.destination?.route?.let { myAppList[it] }?.topAppBar
        }

        MyAppTheme {
            Surface {
                Scaffold(
                    topBar = {
                        myAppScaffoldItemTopBar?.invoke(navController)
                    }
                ) { ...

But I feel like this is a terrible idea in general.

Maybe the top app bar is handled now but thinking of handling the bottom nav bar like this seems to be a really bad idea.

So i started of thinkin in using CompositionLocals so like I would keep the list and change its value

    val MyAppScaffold = compositionLocalOf<String?> { null }
    .....
    
     val name = remember { mutableStateOf("") }

MyAppTheme {


    Scaffold(
        topBar = {
            CompositionLocalProvider(MyAppScaffold provides name.value) {

            }
        }
    )

But this also seems to be a terrible idea.

So my question is how should I handle or build up a scalable commpose app?

I tried lookin at the nowInAndroid and other sample apps but did not really help.

Or should i just continue adding a Scaffold into each Screen?

What I am afraid is combining it with the bottom navigation this way after each recompisition it will look janky.

Or should I just add a second NavHost?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật