Skip to content

Commit

Permalink
Making UI Kit KMP module
Browse files Browse the repository at this point in the history
  • Loading branch information
kirich1409 committed Jun 19, 2024
1 parent 92a1614 commit 6166ce6
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 97 deletions.
2 changes: 2 additions & 0 deletions core/uikit/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ kotlin {
}
}

jvm()

sourceSets {
commonMain.dependencies {
api(compose.ui)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,102 +1,7 @@
package dev.androidbroadcast.news

import android.app.Activity
import android.os.Build
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.material3.ColorScheme
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Shapes
import androidx.compose.material3.Typography
import androidx.compose.material3.darkColorScheme
import androidx.compose.material3.dynamicDarkColorScheme
import androidx.compose.material3.dynamicLightColorScheme
import androidx.compose.material3.lightColorScheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.ReadOnlyComposable
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

private val DarkColorScheme =
darkColorScheme(
primary = Purple80,
secondary = PurpleGrey80,
tertiary = Pink80
)

private val LightColorScheme =
lightColorScheme(
primary = Purple40,
secondary = PurpleGrey40,
tertiary = Pink40
/* Other default colors to override
background = Color(0xFFFFFBFE),
surface = Color(0xFFFFFBFE),
onPrimary = Color.White,
onSecondary = Color.White,
onTertiary = Color.White,
onBackground = Color(0xFF1C1B1F),
onSurface = Color(0xFF1C1B1F),
*/
)

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

darkTheme -> DarkColorScheme
else -> LightColorScheme
}
val view = LocalView.current
if (!view.isInEditMode) {
SideEffect {
val window = (view.context as Activity).window
window.statusBarColor = colorScheme.primary.toArgb()
WindowCompat.getInsetsController(window, view).isAppearanceLightStatusBars = darkTheme
}
}

MaterialTheme(
colorScheme = colorScheme,
typography = Typography,
content = content
)
}

object NewsTheme {
/**
* Retrieves the current [ColorScheme] at the call site's position in the hierarchy.
*/
val colorScheme: ColorScheme
@Composable
@ReadOnlyComposable
get() = MaterialTheme.colorScheme

/**
* Retrieves the current [Typography] at the call site's position in the hierarchy.
*/
val typography: Typography
@Composable
@ReadOnlyComposable
get() = MaterialTheme.typography

/**
* Retrieves the current [Shapes] at the call site's position in the hierarchy.
*/
val shapes: Shapes
@Composable
@ReadOnlyComposable
get() = MaterialTheme.shapes
internal actual fun isPlatformWithDynamicSystemTheme(): Boolean {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.S
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package dev.androidbroadcast.news

import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.material3.ColorScheme
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Shapes
import androidx.compose.material3.Typography
import androidx.compose.material3.darkColorScheme
import androidx.compose.material3.dynamicDarkColorScheme
import androidx.compose.material3.dynamicLightColorScheme
import androidx.compose.material3.lightColorScheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.ReadOnlyComposable
import androidx.compose.ui.platform.LocalContext

private val DarkColorScheme =
darkColorScheme(
primary = Purple80,
secondary = PurpleGrey80,
tertiary = Pink80
)

private val LightColorScheme =
lightColorScheme(
primary = Purple40,
secondary = PurpleGrey40,
tertiary = Pink40
/* Other default colors to override
background = Color(0xFFFFFBFE),
surface = Color(0xFFFFFBFE),
onPrimary = Color.White,
onSecondary = Color.White,
onTertiary = Color.White,
onBackground = Color(0xFF1C1B1F),
onSurface = Color(0xFF1C1B1F),
*/
)

internal expect fun isPlatformWithDynamicSystemTheme(): Boolean

@Composable
fun NewsTheme(
darkTheme: Boolean = isSystemInDarkTheme(),
// Dynamic color is available on Android 12+
dynamicColor: Boolean = true,
content: @Composable () -> Unit
) {
val colorScheme =
when {
dynamicColor && isPlatformWithDynamicSystemTheme() -> {
val context = LocalContext.current
if (darkTheme) dynamicDarkColorScheme(context) else dynamicLightColorScheme(context)
}

darkTheme -> DarkColorScheme
else -> LightColorScheme
}
// val view = LocalView.current
// if (!view.isInEditMode) {
// SideEffect {
// val window = (view.context as Activity).window
// window.statusBarColor = colorScheme.primary.toArgb()
// WindowCompat.getInsetsController(window, view).isAppearanceLightStatusBars = darkTheme
// }
// }

MaterialTheme(
colorScheme = colorScheme,
typography = Typography,
content = content
)
}

object NewsTheme {
/**
* Retrieves the current [ColorScheme] at the call site's position in the hierarchy.
*/
val colorScheme: ColorScheme
@Composable
@ReadOnlyComposable
get() = MaterialTheme.colorScheme

/**
* Retrieves the current [Typography] at the call site's position in the hierarchy.
*/
val typography: Typography
@Composable
@ReadOnlyComposable
get() = MaterialTheme.typography

/**
* Retrieves the current [Shapes] at the call site's position in the hierarchy.
*/
val shapes: Shapes
@Composable
@ReadOnlyComposable
get() = MaterialTheme.shapes
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package dev.androidbroadcast.news

import android.os.Build

internal actual fun isPlatformWithDynamicSystemTheme(): Boolean {
return false
}

0 comments on commit 6166ce6

Please sign in to comment.