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

Define BaseScreen composable for all screens #553

Closed
luongvo opened this issue Oct 31, 2023 · 0 comments · Fixed by #533
Closed

Define BaseScreen composable for all screens #553

luongvo opened this issue Oct 31, 2023 · 0 comments · Fixed by #533

Comments

@luongvo
Copy link
Member

luongvo commented Oct 31, 2023

Why

The composable screens in the Compose Template do not yet have a base screen component. So, it's hard to manage a logic that should be implemented from all screens. We can implement that logic on each screen. However, this approach easily leads to missing or inconsistent implementations.

For example, we want to switch the status bar color between light & dark between screens. Implementing the call to request dark or light status bar on each screen works.

val systemUiController = rememberSystemUiController()
systemUiController.setStatusBarColor(
    color = Transparent,
    darkIcons = false/true,
)

However, having a logic from a base screen component to force requests on all screens makes more sense.

Solution

Create a BaseScreen composable for all screens, as we did with BaseActivity or BaseFragment. This BaseScreen will contain base parameters to request & base logic in the body to execute for all screens.
For example,

@Composable
fun BaseScreen(
    isDarkStatusBarIcons: Boolean? = null,
    content: @Composable () -> Unit,
) {
    if (isDarkStatusBarIcons != null) {
        setStatusBarColor(darkIcons = isDarkStatusBarIcons)
    }

    content()
}

@Composable
fun HomeScreen(
    navigator: (destination: AppDestination) -> Unit,
    viewModel: HomeViewModel = hiltViewModel(),
) = BaseScreen(
    isDarkStatusBarIcons = false
) {
    HomeScreenContent(...)
}

@Composable
fun setStatusBarColor(darkIcons: Boolean) {
    val systemUiController = rememberSystemUiController()
    LaunchedEffect(key1 = darkIcons) {
        systemUiController.setStatusBarColor(
            color = Transparent,
            darkIcons = darkIcons,
        )
    }
}

Who Benefits?

Developers

@luongvo luongvo added this to the 3.26.0 milestone Oct 31, 2023
@luongvo luongvo self-assigned this Oct 31, 2023
@luongvo luongvo linked a pull request Oct 31, 2023 that will close this issue
luongvo added a commit that referenced this issue Oct 31, 2023
@ryan-conway ryan-conway modified the milestones: 3.26.0, 3.27.0 Dec 1, 2023
@ryan-conway ryan-conway modified the milestones: 3.27.0, 3.28.0 Dec 29, 2023
@ryan-conway ryan-conway modified the milestones: 3.28.0, 3.29.0 Apr 1, 2024
ryan-conway added a commit that referenced this issue Jun 5, 2024
…nent

[#553] Define BaseScreen composable for all screens
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants