Skip to content

Commit

Permalink
For touchlab#305 - Upstream the Compose theme to the shared module.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Mugurell committed Jul 21, 2023
1 parent 0ee5d17 commit 87166b0
Show file tree
Hide file tree
Showing 10 changed files with 116 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package co.touchlab.kampkit.android
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.isSystemInDarkTheme
import co.touchlab.kampkit.android.ui.MainScreen
import co.touchlab.kampkit.android.ui.theme.KaMPKitTheme
import co.touchlab.kampkit.ui.theme.KaMPKitTheme
import co.touchlab.kampkit.injectLogger
import co.touchlab.kampkit.models.BreedViewModel
import co.touchlab.kermit.Logger
Expand All @@ -19,7 +20,7 @@ class MainActivity : ComponentActivity(), KoinComponent {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
KaMPKitTheme {
KaMPKitTheme(isSystemInDarkTheme(), true) {
MainScreen(viewModel, log)
}
}
Expand Down
47 changes: 0 additions & 47 deletions app/src/main/kotlin/co/touchlab/kampkit/android/ui/theme/Theme.kt

This file was deleted.

2 changes: 2 additions & 0 deletions shared/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ kotlin {
val commonMain by getting {
dependencies {
implementation(compose.runtime)
implementation(compose.foundation)
implementation(compose.material3)
implementation(libs.koin.core)
implementation(libs.coroutines.core)
implementation(libs.sqlDelight.coroutinesExt)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package co.touchlab.kampkit.ui.theme

import android.app.Activity
import android.os.Build
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.dynamicDarkColorScheme
import androidx.compose.material3.dynamicLightColorScheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.SideEffect
import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalView
import androidx.core.view.WindowCompat

@Composable
actual fun KaMPKitTheme(
darkTheme: Boolean,
dynamicColor: Boolean,
content: @Composable () -> Unit
) {
val colorScheme = when {
// Dynamic color is only supported on Android 12+
dynamicColor && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> {
val context = LocalContext.current
if (darkTheme) dynamicDarkColorScheme(context) else dynamicLightColorScheme(context)
}
darkTheme -> DarkColorPalette
else -> LightColorPalette
}

// If not in Android Studio's preview then update also the system bars
val view = LocalView.current
if (!view.isInEditMode) {
SideEffect {
(view.context as Activity).window.apply {
statusBarColor = colorScheme.primary.toArgb()
WindowCompat
.getInsetsController(this, view).apply {
isAppearanceLightStatusBars = darkTheme
isAppearanceLightNavigationBars = darkTheme
}
}
}
}

MaterialTheme(
colorScheme = colorScheme,
typography = Typography,
shapes = Shapes,
content = content
)
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package co.touchlab.kampkit.android.ui.theme
package co.touchlab.kampkit.ui.theme

import androidx.compose.ui.graphics.Color

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package co.touchlab.kampkit.ui.theme

import androidx.compose.runtime.Composable

@Composable
expect fun KaMPKitTheme(
darkTheme: Boolean,
dynamicColor: Boolean,
content: @Composable () -> Unit
)
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package co.touchlab.kampkit.android.ui.theme
package co.touchlab.kampkit.ui.theme

import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Shapes
import androidx.compose.material3.Shapes
import androidx.compose.ui.unit.dp

val Shapes = Shapes(
Expand Down
25 changes: 25 additions & 0 deletions shared/src/commonMain/kotlin/co/touchlab/kampkit/ui/theme/Theme.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package co.touchlab.kampkit.ui.theme

import androidx.compose.material3.darkColorScheme
import androidx.compose.material3.lightColorScheme

val DarkColorPalette = darkColorScheme(
primary = Purple200,
inversePrimary = Purple700,
secondary = Teal200
)

val LightColorPalette = lightColorScheme(
primary = Purple500,
inversePrimary = Purple700,
secondary = Teal200

// Other default colors to override
//
// background = Color.White,
// surface = Color.White,
// onPrimary = Color.White,
// onSecondary = Color.Black,
// onBackground = Color.Black,
// onSurface = Color.Black,
)
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package co.touchlab.kampkit.android.ui.theme
package co.touchlab.kampkit.ui.theme

import androidx.compose.material.Typography
import androidx.compose.material3.Typography
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.sp

// Set of Material typography styles to start with
val Typography = Typography(
body1 = TextStyle(
bodyLarge = TextStyle(
fontFamily = FontFamily.Default,
fontWeight = FontWeight.Normal,
fontSize = 16.sp
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package co.touchlab.kampkit.ui.theme

import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable

@Composable
actual fun KaMPKitTheme(
darkTheme: Boolean,
dynamicColor: Boolean,
content: @Composable () -> Unit
) {
MaterialTheme(
colorScheme = if (darkTheme) DarkColorPalette else LightColorPalette,
typography = Typography,
shapes = Shapes,
content = content
)
}

0 comments on commit 87166b0

Please sign in to comment.