Skip to content

Commit 87166b0

Browse files
committed
For touchlab#305 - Upstream the Compose theme to the shared module.
This will allow to share it with iOS also. Migrated to Material3 and Dynamic Color support for Android in the meantime. Migrated to support automatic day/night theme support for both platforms.
1 parent 0ee5d17 commit 87166b0

File tree

10 files changed

+116
-55
lines changed

10 files changed

+116
-55
lines changed

app/src/main/kotlin/co/touchlab/kampkit/android/MainActivity.kt

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ package co.touchlab.kampkit.android
33
import android.os.Bundle
44
import androidx.activity.ComponentActivity
55
import androidx.activity.compose.setContent
6+
import androidx.compose.foundation.isSystemInDarkTheme
67
import co.touchlab.kampkit.android.ui.MainScreen
7-
import co.touchlab.kampkit.android.ui.theme.KaMPKitTheme
8+
import co.touchlab.kampkit.ui.theme.KaMPKitTheme
89
import co.touchlab.kampkit.injectLogger
910
import co.touchlab.kampkit.models.BreedViewModel
1011
import co.touchlab.kermit.Logger
@@ -19,7 +20,7 @@ class MainActivity : ComponentActivity(), KoinComponent {
1920
override fun onCreate(savedInstanceState: Bundle?) {
2021
super.onCreate(savedInstanceState)
2122
setContent {
22-
KaMPKitTheme {
23+
KaMPKitTheme(isSystemInDarkTheme(), true) {
2324
MainScreen(viewModel, log)
2425
}
2526
}

app/src/main/kotlin/co/touchlab/kampkit/android/ui/theme/Theme.kt

-47
This file was deleted.

shared/build.gradle.kts

+2
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ kotlin {
4848
val commonMain by getting {
4949
dependencies {
5050
implementation(compose.runtime)
51+
implementation(compose.foundation)
52+
implementation(compose.material3)
5153
implementation(libs.koin.core)
5254
implementation(libs.coroutines.core)
5355
implementation(libs.sqlDelight.coroutinesExt)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package co.touchlab.kampkit.ui.theme
2+
3+
import android.app.Activity
4+
import android.os.Build
5+
import androidx.compose.material3.MaterialTheme
6+
import androidx.compose.material3.dynamicDarkColorScheme
7+
import androidx.compose.material3.dynamicLightColorScheme
8+
import androidx.compose.runtime.Composable
9+
import androidx.compose.runtime.SideEffect
10+
import androidx.compose.ui.graphics.toArgb
11+
import androidx.compose.ui.platform.LocalContext
12+
import androidx.compose.ui.platform.LocalView
13+
import androidx.core.view.WindowCompat
14+
15+
@Composable
16+
actual fun KaMPKitTheme(
17+
darkTheme: Boolean,
18+
dynamicColor: Boolean,
19+
content: @Composable () -> Unit
20+
) {
21+
val colorScheme = when {
22+
// Dynamic color is only supported on Android 12+
23+
dynamicColor && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> {
24+
val context = LocalContext.current
25+
if (darkTheme) dynamicDarkColorScheme(context) else dynamicLightColorScheme(context)
26+
}
27+
darkTheme -> DarkColorPalette
28+
else -> LightColorPalette
29+
}
30+
31+
// If not in Android Studio's preview then update also the system bars
32+
val view = LocalView.current
33+
if (!view.isInEditMode) {
34+
SideEffect {
35+
(view.context as Activity).window.apply {
36+
statusBarColor = colorScheme.primary.toArgb()
37+
WindowCompat
38+
.getInsetsController(this, view).apply {
39+
isAppearanceLightStatusBars = darkTheme
40+
isAppearanceLightNavigationBars = darkTheme
41+
}
42+
}
43+
}
44+
}
45+
46+
MaterialTheme(
47+
colorScheme = colorScheme,
48+
typography = Typography,
49+
shapes = Shapes,
50+
content = content
51+
)
52+
}

app/src/main/kotlin/co/touchlab/kampkit/android/ui/theme/Color.kt shared/src/commonMain/kotlin/co/touchlab/kampkit/ui/theme/Color.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package co.touchlab.kampkit.android.ui.theme
1+
package co.touchlab.kampkit.ui.theme
22

33
import androidx.compose.ui.graphics.Color
44

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package co.touchlab.kampkit.ui.theme
2+
3+
import androidx.compose.runtime.Composable
4+
5+
@Composable
6+
expect fun KaMPKitTheme(
7+
darkTheme: Boolean,
8+
dynamicColor: Boolean,
9+
content: @Composable () -> Unit
10+
)

app/src/main/kotlin/co/touchlab/kampkit/android/ui/theme/Shapes.kt shared/src/commonMain/kotlin/co/touchlab/kampkit/ui/theme/Shapes.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
package co.touchlab.kampkit.android.ui.theme
1+
package co.touchlab.kampkit.ui.theme
22

33
import androidx.compose.foundation.shape.RoundedCornerShape
4-
import androidx.compose.material.Shapes
4+
import androidx.compose.material3.Shapes
55
import androidx.compose.ui.unit.dp
66

77
val Shapes = Shapes(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package co.touchlab.kampkit.ui.theme
2+
3+
import androidx.compose.material3.darkColorScheme
4+
import androidx.compose.material3.lightColorScheme
5+
6+
val DarkColorPalette = darkColorScheme(
7+
primary = Purple200,
8+
inversePrimary = Purple700,
9+
secondary = Teal200
10+
)
11+
12+
val LightColorPalette = lightColorScheme(
13+
primary = Purple500,
14+
inversePrimary = Purple700,
15+
secondary = Teal200
16+
17+
// Other default colors to override
18+
//
19+
// background = Color.White,
20+
// surface = Color.White,
21+
// onPrimary = Color.White,
22+
// onSecondary = Color.Black,
23+
// onBackground = Color.Black,
24+
// onSurface = Color.Black,
25+
)

app/src/main/kotlin/co/touchlab/kampkit/android/ui/theme/Typography.kt shared/src/commonMain/kotlin/co/touchlab/kampkit/ui/theme/Typography.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
package co.touchlab.kampkit.android.ui.theme
1+
package co.touchlab.kampkit.ui.theme
22

3-
import androidx.compose.material.Typography
3+
import androidx.compose.material3.Typography
44
import androidx.compose.ui.text.TextStyle
55
import androidx.compose.ui.text.font.FontFamily
66
import androidx.compose.ui.text.font.FontWeight
77
import androidx.compose.ui.unit.sp
88

99
// Set of Material typography styles to start with
1010
val Typography = Typography(
11-
body1 = TextStyle(
11+
bodyLarge = TextStyle(
1212
fontFamily = FontFamily.Default,
1313
fontWeight = FontWeight.Normal,
1414
fontSize = 16.sp
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package co.touchlab.kampkit.ui.theme
2+
3+
import androidx.compose.material3.MaterialTheme
4+
import androidx.compose.runtime.Composable
5+
6+
@Composable
7+
actual fun KaMPKitTheme(
8+
darkTheme: Boolean,
9+
dynamicColor: Boolean,
10+
content: @Composable () -> Unit
11+
) {
12+
MaterialTheme(
13+
colorScheme = if (darkTheme) DarkColorPalette else LightColorPalette,
14+
typography = Typography,
15+
shapes = Shapes,
16+
content = content
17+
)
18+
}

0 commit comments

Comments
 (0)