From ae5b31d338cf7b3b7a34d7699242b9e0efe14d1d Mon Sep 17 00:00:00 2001 From: Luong Vo Date: Fri, 6 Oct 2023 11:05:26 +0700 Subject: [PATCH 1/2] [#553] Define a "BaseScreen" component to switch status bar color in sample-compose --- sample-compose/app/build.gradle.kts | 3 ++- .../sample/compose/ui/base/BaseScreen.kt | 21 ++++++++++++++++++ .../ui/screens/main/home/HomeScreen.kt | 3 +++ .../ui/screens/main/second/SecondScreen.kt | 3 +++ .../ui/screens/main/third/ThirdScreen.kt | 3 +++ .../sample/compose/util/ComposableUtil.kt | 22 +++++++++++++++++++ .../app/src/main/res/values/colors.xml | 2 +- .../app/src/main/res/values/styles.xml | 2 +- .../buildSrc/src/main/java/Versions.kt | 2 +- 9 files changed, 57 insertions(+), 4 deletions(-) create mode 100644 sample-compose/app/src/main/java/co/nimblehq/sample/compose/ui/base/BaseScreen.kt create mode 100644 sample-compose/app/src/main/java/co/nimblehq/sample/compose/util/ComposableUtil.kt diff --git a/sample-compose/app/build.gradle.kts b/sample-compose/app/build.gradle.kts index 1ddb5fd9e..38739b5d8 100644 --- a/sample-compose/app/build.gradle.kts +++ b/sample-compose/app/build.gradle.kts @@ -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}") diff --git a/sample-compose/app/src/main/java/co/nimblehq/sample/compose/ui/base/BaseScreen.kt b/sample-compose/app/src/main/java/co/nimblehq/sample/compose/ui/base/BaseScreen.kt new file mode 100644 index 000000000..e2255c0e3 --- /dev/null +++ b/sample-compose/app/src/main/java/co/nimblehq/sample/compose/ui/base/BaseScreen.kt @@ -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() +} diff --git a/sample-compose/app/src/main/java/co/nimblehq/sample/compose/ui/screens/main/home/HomeScreen.kt b/sample-compose/app/src/main/java/co/nimblehq/sample/compose/ui/screens/main/home/HomeScreen.kt index b63436bcc..6f5689d74 100644 --- a/sample-compose/app/src/main/java/co/nimblehq/sample/compose/ui/screens/main/home/HomeScreen.kt +++ b/sample-compose/app/src/main/java/co/nimblehq/sample/compose/ui/screens/main/home/HomeScreen.kt @@ -16,6 +16,7 @@ import co.nimblehq.sample.compose.extensions.collectAsEffect import co.nimblehq.sample.compose.extensions.showToast import co.nimblehq.sample.compose.lib.IsLoading import co.nimblehq.sample.compose.ui.base.BaseDestination +import co.nimblehq.sample.compose.ui.base.BaseScreen import co.nimblehq.sample.compose.ui.common.AppBar import co.nimblehq.sample.compose.ui.models.UiModel import co.nimblehq.sample.compose.ui.showToast @@ -28,6 +29,8 @@ fun HomeScreen( viewModel: HomeViewModel = hiltViewModel(), navigator: (destination: BaseDestination) -> Unit, isResultOk: Boolean = false, +) = BaseScreen( + isDarkStatusBarIcons = true, ) { val context = LocalContext.current viewModel.error.collectAsEffect { e -> e.showToast(context) } diff --git a/sample-compose/app/src/main/java/co/nimblehq/sample/compose/ui/screens/main/second/SecondScreen.kt b/sample-compose/app/src/main/java/co/nimblehq/sample/compose/ui/screens/main/second/SecondScreen.kt index 605d093db..5fc0172d1 100644 --- a/sample-compose/app/src/main/java/co/nimblehq/sample/compose/ui/screens/main/second/SecondScreen.kt +++ b/sample-compose/app/src/main/java/co/nimblehq/sample/compose/ui/screens/main/second/SecondScreen.kt @@ -15,6 +15,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.base.BaseDestination +import co.nimblehq.sample.compose.ui.base.BaseScreen import co.nimblehq.sample.compose.ui.base.KeyResultOk import co.nimblehq.sample.compose.ui.common.AppBar import co.nimblehq.sample.compose.ui.theme.AppTheme.dimensions @@ -25,6 +26,8 @@ fun SecondScreen( viewModel: SecondViewModel = hiltViewModel(), navigator: (destination: BaseDestination) -> Unit, id: String, +) = BaseScreen( + isDarkStatusBarIcons = false, ) { SecondScreenContent( id = id, diff --git a/sample-compose/app/src/main/java/co/nimblehq/sample/compose/ui/screens/main/third/ThirdScreen.kt b/sample-compose/app/src/main/java/co/nimblehq/sample/compose/ui/screens/main/third/ThirdScreen.kt index 05d20cfa6..6e47f5209 100644 --- a/sample-compose/app/src/main/java/co/nimblehq/sample/compose/ui/screens/main/third/ThirdScreen.kt +++ b/sample-compose/app/src/main/java/co/nimblehq/sample/compose/ui/screens/main/third/ThirdScreen.kt @@ -14,6 +14,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.base.BaseDestination +import co.nimblehq.sample.compose.ui.base.BaseScreen import co.nimblehq.sample.compose.ui.common.AppBar import co.nimblehq.sample.compose.ui.models.UiModel import co.nimblehq.sample.compose.ui.theme.ComposeTheme @@ -23,6 +24,8 @@ fun ThirdScreen( viewModel: ThirdViewModel = hiltViewModel(), navigator: (destination: BaseDestination) -> Unit, model: UiModel?, +) = BaseScreen( + isDarkStatusBarIcons = true, ) { ThirdScreenContent(data = model) } diff --git a/sample-compose/app/src/main/java/co/nimblehq/sample/compose/util/ComposableUtil.kt b/sample-compose/app/src/main/java/co/nimblehq/sample/compose/util/ComposableUtil.kt new file mode 100644 index 000000000..f4830423f --- /dev/null +++ b/sample-compose/app/src/main/java/co/nimblehq/sample/compose/util/ComposableUtil.kt @@ -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, + ) + } +} diff --git a/sample-compose/app/src/main/res/values/colors.xml b/sample-compose/app/src/main/res/values/colors.xml index 764d6f58e..cd0fde27d 100644 --- a/sample-compose/app/src/main/res/values/colors.xml +++ b/sample-compose/app/src/main/res/values/colors.xml @@ -1,4 +1,4 @@ - #FF669900 + #FF669900 diff --git a/sample-compose/app/src/main/res/values/styles.xml b/sample-compose/app/src/main/res/values/styles.xml index c82a3c111..380abe9eb 100644 --- a/sample-compose/app/src/main/res/values/styles.xml +++ b/sample-compose/app/src/main/res/values/styles.xml @@ -1,6 +1,6 @@ diff --git a/sample-compose/buildSrc/src/main/java/Versions.kt b/sample-compose/buildSrc/src/main/java/Versions.kt index 5ee485de9..b3ac0e98c 100644 --- a/sample-compose/buildSrc/src/main/java/Versions.kt +++ b/sample-compose/buildSrc/src/main/java/Versions.kt @@ -9,7 +9,7 @@ object Versions { const val ANDROID_VERSION_NAME = "3.29.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" From a43d6c0d7457027c20f8a89d4e8bea20d1335853 Mon Sep 17 00:00:00 2001 From: Luong Vo Date: Fri, 6 Oct 2023 11:07:56 +0700 Subject: [PATCH 2/2] [#553] Define an initial "BaseScreen" component in compose-template --- .../nimblehq/template/compose/ui/base/BaseScreen.kt | 13 +++++++++++++ .../compose/ui/screens/main/home/HomeScreen.kt | 3 ++- 2 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 template-compose/app/src/main/java/co/nimblehq/template/compose/ui/base/BaseScreen.kt diff --git a/template-compose/app/src/main/java/co/nimblehq/template/compose/ui/base/BaseScreen.kt b/template-compose/app/src/main/java/co/nimblehq/template/compose/ui/base/BaseScreen.kt new file mode 100644 index 000000000..713fab898 --- /dev/null +++ b/template-compose/app/src/main/java/co/nimblehq/template/compose/ui/base/BaseScreen.kt @@ -0,0 +1,13 @@ +package co.nimblehq.template.compose.ui.base + +import androidx.compose.runtime.Composable + +@Composable +fun BaseScreen( + // TODO Base parameters to request on all screens here + content: @Composable () -> Unit, +) { + // TODO Base logic for all screens here + + content() +} diff --git a/template-compose/app/src/main/java/co/nimblehq/template/compose/ui/screens/main/home/HomeScreen.kt b/template-compose/app/src/main/java/co/nimblehq/template/compose/ui/screens/main/home/HomeScreen.kt index dda7bd4d4..c45fe8e2a 100644 --- a/template-compose/app/src/main/java/co/nimblehq/template/compose/ui/screens/main/home/HomeScreen.kt +++ b/template-compose/app/src/main/java/co/nimblehq/template/compose/ui/screens/main/home/HomeScreen.kt @@ -18,6 +18,7 @@ import androidx.lifecycle.compose.collectAsStateWithLifecycle import co.nimblehq.template.compose.R import co.nimblehq.template.compose.extensions.collectAsEffect import co.nimblehq.template.compose.ui.base.BaseDestination +import co.nimblehq.template.compose.ui.base.BaseScreen import co.nimblehq.template.compose.ui.models.UiModel import co.nimblehq.template.compose.ui.showToast import co.nimblehq.template.compose.ui.theme.AppTheme.dimensions @@ -28,7 +29,7 @@ import timber.log.Timber fun HomeScreen( viewModel: HomeViewModel = hiltViewModel(), navigator: (destination: BaseDestination) -> Unit, -) { +) = BaseScreen { val context = LocalContext.current viewModel.error.collectAsEffect { e -> e.showToast(context) } viewModel.navigator.collectAsEffect { destination -> navigator(destination) }