Skip to content

Commit 10ae2fe

Browse files
committed
[FIXED] Compose in dark mode
1 parent f87c556 commit 10ae2fe

File tree

1 file changed

+58
-6
lines changed

1 file changed

+58
-6
lines changed

example/src/main/java/dev/hossain/ynaash/example/ui/demoprismjs/PrismJsComposeDemoActivity.kt

Lines changed: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,33 @@
11
package dev.hossain.ynaash.example.ui.demoprismjs
22

3+
import android.os.Build
34
import android.os.Bundle
45
import androidx.activity.compose.setContent
56
import androidx.activity.enableEdgeToEdge
67
import androidx.appcompat.app.AppCompatActivity
7-
import androidx.compose.foundation.layout.*
8+
import androidx.compose.foundation.isSystemInDarkTheme
9+
import androidx.compose.foundation.layout.Arrangement
10+
import androidx.compose.foundation.layout.Column
11+
import androidx.compose.foundation.layout.WindowInsets
12+
import androidx.compose.foundation.layout.fillMaxSize
13+
import androidx.compose.foundation.layout.fillMaxWidth
14+
import androidx.compose.foundation.layout.height
15+
import androidx.compose.foundation.layout.padding
16+
import androidx.compose.foundation.layout.systemBars
17+
import androidx.compose.foundation.layout.windowInsetsPadding
818
import androidx.compose.foundation.rememberScrollState
919
import androidx.compose.foundation.verticalScroll
10-
import androidx.compose.material3.*
20+
import androidx.compose.material3.Card
21+
import androidx.compose.material3.MaterialTheme
22+
import androidx.compose.material3.Surface
23+
import androidx.compose.material3.Text
24+
import androidx.compose.material3.darkColorScheme
25+
import androidx.compose.material3.dynamicDarkColorScheme
26+
import androidx.compose.material3.dynamicLightColorScheme
27+
import androidx.compose.material3.lightColorScheme
1128
import androidx.compose.runtime.Composable
1229
import androidx.compose.ui.Modifier
30+
import androidx.compose.ui.platform.LocalContext
1331
import androidx.compose.ui.tooling.preview.Preview
1432
import androidx.compose.ui.unit.dp
1533
import dev.hossain.ynaash.compose.SyntaxHighlighter
@@ -30,7 +48,7 @@ class PrismJsComposeDemoActivity : AppCompatActivity() {
3048
supportActionBar?.title = "PrismJS Compose Demo"
3149

3250
setContent {
33-
MaterialTheme {
51+
AppTheme {
3452
Surface(
3553
modifier = Modifier.fillMaxSize(),
3654
color = MaterialTheme.colorScheme.background
@@ -42,6 +60,32 @@ class PrismJsComposeDemoActivity : AppCompatActivity() {
4260
}
4361
}
4462

63+
@Composable
64+
fun AppTheme(
65+
darkTheme: Boolean = isSystemInDarkTheme(),
66+
content: @Composable () -> Unit
67+
) {
68+
val context = LocalContext.current
69+
70+
val colorScheme = when {
71+
// Dynamic color is available on Android 12+
72+
Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && darkTheme -> {
73+
dynamicDarkColorScheme(context)
74+
}
75+
Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && !darkTheme -> {
76+
dynamicLightColorScheme(context)
77+
}
78+
// Use default dark/light colorScheme for older Android versions
79+
darkTheme -> darkColorScheme()
80+
else -> lightColorScheme()
81+
}
82+
83+
MaterialTheme(
84+
colorScheme = colorScheme,
85+
content = content
86+
)
87+
}
88+
4589
@Composable
4690
fun PrismJsComposeDemoScreen() {
4791
Column(
@@ -113,10 +157,18 @@ fun PrismJsComposeDemoScreen() {
113157
}
114158
}
115159

116-
@Preview(showBackground = true)
160+
@Preview(showBackground = true, name = "Light Mode")
161+
@Composable
162+
fun PrismJsComposeDemoScreenPreviewLight() {
163+
AppTheme {
164+
PrismJsComposeDemoScreen()
165+
}
166+
}
167+
168+
@Preview(showBackground = true, name = "Dark Mode", uiMode = android.content.res.Configuration.UI_MODE_NIGHT_YES)
117169
@Composable
118-
fun PrismJsComposeDemoScreenPreview() {
119-
MaterialTheme {
170+
fun PrismJsComposeDemoScreenPreviewDark() {
171+
AppTheme {
120172
PrismJsComposeDemoScreen()
121173
}
122174
}

0 commit comments

Comments
 (0)