Skip to content

Commit

Permalink
Extract platform specific UI from theme to UI Kit to Compose App root
Browse files Browse the repository at this point in the history
  • Loading branch information
kirich1409 committed Jun 22, 2024
1 parent 71fd173 commit 9b95eca
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 44 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package dev.androidbroadcast.news.compose

import android.app.Activity
import androidx.compose.runtime.Composable
import androidx.compose.runtime.SideEffect
import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.platform.LocalView
import androidx.core.view.WindowCompat
import dev.androidbroadcast.news.NewsTheme

@Composable
internal actual fun NewsPlatformTheme(
darkTheme: Boolean,
content: @Composable () -> Unit,
) {
val view = LocalView.current
if (!view.isInEditMode) {
val colorScheme = NewsTheme.colorScheme
SideEffect {
val window = (view.context as Activity).window
window.statusBarColor = colorScheme.primary.toArgb()
WindowCompat.getInsetsController(window, view).isAppearanceLightStatusBars = darkTheme
}
}
content()
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.androidbroadcast.news.compose

import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
Expand All @@ -9,16 +10,28 @@ import dev.androidbroadcast.news.NewsTheme
import dev.androidbroadcast.news.main.NewsMainScreen
import org.koin.compose.KoinContext

/**
* Setup theme for platform
*/
@Composable
internal expect fun NewsPlatformTheme(
darkTheme: Boolean = isSystemInDarkTheme(),
content: @Composable () -> Unit,
)

@Composable
public fun NewsApp() {
NewsTheme {
// A surface container using the 'background' color from the theme
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
KoinContext {
NewsMainScreen()
val darkTheme = isSystemInDarkTheme()
NewsTheme(darkTheme) {
NewsPlatformTheme(darkTheme) {
// A surface container using the 'background' color from the theme
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
KoinContext {
NewsMainScreen()
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package dev.androidbroadcast.news.compose

import androidx.compose.runtime.Composable

@Composable
internal actual fun NewsPlatformTheme(
darkTheme: Boolean,
content: @Composable () -> Unit,
) {
// Do nothing
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package dev.androidbroadcast.news.compose

import androidx.compose.runtime.Composable

@Composable
internal actual fun NewsPlatformTheme(
darkTheme: Boolean,
content: @Composable () -> Unit,
) {
// Do nothing
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,3 @@ internal actual fun dynamicColorScheme(darkTheme: Boolean): ColorScheme {
internal actual fun isPlatformWithDynamicSystemTheme(): Boolean {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.S
}

@Composable
internal actual fun platformThemeSetup(
darkTheme: Boolean,
colorScheme: ColorScheme,
) {
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
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,6 @@ internal expect fun isPlatformWithDynamicSystemTheme(): Boolean
@Composable
internal expect fun dynamicColorScheme(darkTheme: Boolean): ColorScheme

@Composable
internal expect fun platformThemeSetup(
darkTheme: Boolean,
colorScheme: ColorScheme,
)

@Composable
fun NewsTheme(
darkTheme: Boolean = isSystemInDarkTheme(),
Expand All @@ -58,8 +52,6 @@ fun NewsTheme(
else -> LightColorScheme
}

platformThemeSetup(darkTheme, colorScheme)

MaterialTheme(
colorScheme = colorScheme,
typography = Typography,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,3 @@ internal actual fun dynamicColorScheme(darkTheme: Boolean): ColorScheme {
}

internal actual fun isPlatformWithDynamicSystemTheme(): Boolean = false

@Composable
internal actual fun platformThemeSetup(
darkTheme: Boolean,
colorScheme: ColorScheme,
) {
// Do nothing
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,3 @@ internal actual fun dynamicColorScheme(darkTheme: Boolean): ColorScheme {
}

internal actual fun isPlatformWithDynamicSystemTheme(): Boolean = false

@Composable
internal actual fun platformThemeSetup(darkTheme: Boolean, colorScheme: ColorScheme) {
// Do nothing
}

0 comments on commit 9b95eca

Please sign in to comment.