Skip to content

Commit

Permalink
[#553] Define a "BaseScreen" component to switch status bar color in …
Browse files Browse the repository at this point in the history
…sample-compose
  • Loading branch information
luongvo committed Oct 31, 2023
1 parent 790795a commit 91520cc
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 4 deletions.
3 changes: 2 additions & 1 deletion sample-compose/app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ dependencies {
implementation("androidx.compose.material:material")

implementation("androidx.navigation:navigation-compose:${Versions.COMPOSE_NAVIGATION_VERSION}")
implementation("com.google.accompanist:accompanist-permissions:${Versions.ACCOMPANIST_PERMISSIONS_VERSION}")
implementation("com.google.accompanist:accompanist-permissions:${Versions.ACCOMPANIST_VERSION}")
implementation("com.google.accompanist:accompanist-systemuicontroller:${Versions.ACCOMPANIST_VERSION}")

implementation("androidx.datastore:datastore-preferences:${Versions.ANDROIDX_DATASTORE_PREFERENCES_VERSION}")

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package co.nimblehq.sample.compose.ui.base

import androidx.compose.runtime.Composable
import androidx.compose.ui.res.colorResource
import co.nimblehq.sample.compose.R
import co.nimblehq.sample.compose.util.setStatusBarColor

@Composable
fun BaseScreen(
isDarkStatusBarIcons: Boolean? = null,
content: @Composable () -> Unit,
) {
if (isDarkStatusBarIcons != null) {
setStatusBarColor(
color = colorResource(id = R.color.statusBarColor),
darkIcons = isDarkStatusBarIcons,
)
}

content()
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import co.nimblehq.sample.compose.extensions.showToast
import co.nimblehq.sample.compose.lib.IsLoading
import co.nimblehq.sample.compose.model.UiModel
import co.nimblehq.sample.compose.ui.AppDestination
import co.nimblehq.sample.compose.ui.base.BaseScreen
import co.nimblehq.sample.compose.ui.common.AppBar
import co.nimblehq.sample.compose.ui.showToast
import co.nimblehq.sample.compose.ui.theme.ComposeTheme
Expand All @@ -27,6 +28,8 @@ import kotlinx.coroutines.flow.*
fun HomeScreen(
viewModel: HomeViewModel = hiltViewModel(),
navigator: (destination: AppDestination) -> Unit,
) = BaseScreen(
isDarkStatusBarIcons = true,
) {
val context = LocalContext.current
viewModel.error.collectAsEffect { e -> e.showToast(context) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import androidx.compose.ui.tooling.preview.Preview
import androidx.hilt.navigation.compose.hiltViewModel
import co.nimblehq.sample.compose.R
import co.nimblehq.sample.compose.ui.AppDestination
import co.nimblehq.sample.compose.ui.base.BaseScreen
import co.nimblehq.sample.compose.ui.common.AppBar
import co.nimblehq.sample.compose.ui.theme.ComposeTheme

Expand All @@ -21,6 +22,8 @@ fun SecondScreen(
viewModel: SecondViewModel = hiltViewModel(),
navigator: (destination: AppDestination) -> Unit,
id: String,
) = BaseScreen(
isDarkStatusBarIcons = false,
) {
SecondScreenContent(id)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import androidx.hilt.navigation.compose.hiltViewModel
import co.nimblehq.sample.compose.R
import co.nimblehq.sample.compose.model.UiModel
import co.nimblehq.sample.compose.ui.AppDestination
import co.nimblehq.sample.compose.ui.base.BaseScreen
import co.nimblehq.sample.compose.ui.common.AppBar
import co.nimblehq.sample.compose.ui.theme.ComposeTheme

Expand All @@ -23,6 +24,8 @@ fun ThirdScreen(
viewModel: ThirdViewModel = hiltViewModel(),
navigator: (destination: AppDestination) -> Unit,
model: UiModel?,
) = BaseScreen(
isDarkStatusBarIcons = true,
) {
ThirdScreenContent(data = model)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package co.nimblehq.sample.compose.util

import android.annotation.SuppressLint
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.graphics.Color
import com.google.accompanist.systemuicontroller.rememberSystemUiController

@SuppressLint("ComposableNaming")
@Composable
fun setStatusBarColor(
color: Color,
darkIcons: Boolean,
) {
val systemUiController = rememberSystemUiController()
LaunchedEffect(key1 = darkIcons) {
systemUiController.setStatusBarColor(
color = color,
darkIcons = darkIcons,
)
}
}
2 changes: 1 addition & 1 deletion sample-compose/app/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="greenChristi">#FF669900</color>
<color name="statusBarColor">#FF669900</color>
</resources>
2 changes: 1 addition & 1 deletion sample-compose/app/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:statusBarColor">@color/greenChristi</item>
<item name="android:statusBarColor">@color/statusBarColor</item>
</style>
</resources>
2 changes: 1 addition & 1 deletion sample-compose/buildSrc/src/main/java/Versions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ object Versions {
const val ANDROID_VERSION_NAME = "3.26.0"

// Dependencies (Alphabet sorted)
const val ACCOMPANIST_PERMISSIONS_VERSION = "0.30.1"
const val ACCOMPANIST_VERSION = "0.30.1"
const val ANDROID_COMMON_KTX_VERSION = "0.1.1"
const val ANDROID_CRYPTO_VERSION = "1.0.0"
const val ANDROIDX_CORE_KTX_VERSION = "1.10.1"
Expand Down

0 comments on commit 91520cc

Please sign in to comment.