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

[#553] Define BaseScreen composable for all screens #533

Merged
merged 2 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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
Expand All @@ -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) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -25,6 +26,8 @@ fun SecondScreen(
viewModel: SecondViewModel = hiltViewModel(),
navigator: (destination: BaseDestination) -> Unit,
id: String,
) = BaseScreen(
isDarkStatusBarIcons = false,
) {
SecondScreenContent(
id = id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -23,6 +24,8 @@ fun ThirdScreen(
viewModel: ThirdViewModel = hiltViewModel(),
navigator: (destination: BaseDestination) -> 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.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"
Expand Down
Original file line number Diff line number Diff line change
@@ -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()
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) }
Expand Down
Loading