Skip to content

Commit e05aad5

Browse files
authored
fix: Give example on how to follow theme colors when used with Service (#15)
1 parent fde1ca5 commit e05aad5

File tree

5 files changed

+50
-38
lines changed

5 files changed

+50
-38
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ on:
66
- '.idea/**'
77
- 'preview/**'
88
- 'jitpack.yml'
9-
- '*.md'
9+
- '**/*.md'
1010
- '.gitignore'
1111
workflow_dispatch:
1212

app-service-hilt/Readme.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,13 @@ This is mostly just a sample project on how to incorporate hilt into viewmodels
3131

3232
![compose-floating-window](https://github.com/user-attachments/assets/2201f599-137d-48ba-8c79-66eb86461fa3)
3333

34+
Note:
35+
- Service is not aware of the App's theme colors. There is a need to pass the Default Themeing in this app's case `ComposeFloatingWindowTheme` and you can decide if you'll changed it to fix theme or use preference to update the theme
36+
37+
```kotlin
38+
val darkMode by model.darkMode.collectAsStateWithLifecycle(false)
39+
40+
ComposeFloatingWindowTheme(darkTheme = darkMode) {
41+
```
42+
43+
![Theme floating window](https://github.com/user-attachments/assets/22b446ce-34b5-4ba0-955c-b270f3c30f90)

app-service-hilt/src/main/java/com/github/only52607/compose/window/hilt/MainActivity.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import androidx.compose.runtime.LaunchedEffect
1717
import androidx.compose.runtime.getValue
1818
import androidx.compose.runtime.mutableStateOf
1919
import androidx.compose.runtime.remember
20-
import androidx.compose.runtime.rememberCoroutineScope
2120
import androidx.compose.ui.Alignment
2221
import androidx.compose.ui.Modifier
2322
import androidx.compose.ui.platform.LocalContext
@@ -28,7 +27,6 @@ import com.github.only52607.compose.window.hilt.ui.DialogPermission
2827
import com.github.only52607.compose.window.hilt.ui.theme.ComposeFloatingWindowTheme
2928
import dagger.hilt.android.AndroidEntryPoint
3029
import kotlinx.coroutines.flow.distinctUntilChanged
31-
import kotlinx.coroutines.launch
3230
import javax.inject.Inject
3331

3432
@AndroidEntryPoint

app-service-hilt/src/main/java/com/github/only52607/compose/window/hilt/ui/FloatingWindowContent.kt

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ import androidx.compose.runtime.getValue
1313
import androidx.compose.ui.Modifier
1414
import androidx.compose.ui.unit.dp
1515
import androidx.lifecycle.compose.collectAsStateWithLifecycle
16-
import androidx.lifecycle.viewmodel.compose.viewModel
1716
import com.github.only52607.compose.window.LocalFloatingWindow
1817
import com.github.only52607.compose.window.dragFloatingWindow
18+
import com.github.only52607.compose.window.hilt.ui.theme.ComposeFloatingWindowTheme
1919

2020
@Composable
2121
fun FloatingWindowContent(
@@ -25,30 +25,33 @@ fun FloatingWindowContent(
2525

2626
val darkMode by model.darkMode.collectAsStateWithLifecycle(false)
2727

28-
if (model.dialogVisible) {
29-
SystemAlertDialog(
30-
onDismissRequest = { model.dismissDialog() },
31-
confirmButton = {
32-
TextButton(onClick = { model.dismissDialog() }) {
33-
Text(text = "OK")
28+
ComposeFloatingWindowTheme(darkTheme = darkMode) {
29+
30+
if (model.dialogVisible) {
31+
SystemAlertDialog(
32+
onDismissRequest = { model.dismissDialog() },
33+
confirmButton = {
34+
TextButton(onClick = { model.dismissDialog() }) {
35+
Text(text = "OK")
36+
}
37+
},
38+
text = {
39+
Text(
40+
text = "This is now ${if (darkMode) "Dark" else "Light"} mode",
41+
)
3442
}
43+
)
44+
}
45+
FloatingActionButton(
46+
modifier = Modifier.dragFloatingWindow(),
47+
onClick = {
48+
model.showDialog(!darkMode)
3549
},
36-
text = {
37-
Text(
38-
text = "This is now ${if (darkMode) "Dark" else "Light"} mode",
39-
)
40-
}
41-
)
42-
}
43-
FloatingActionButton(
44-
modifier = Modifier.dragFloatingWindow(),
45-
onClick = {
46-
model.showDialog(!darkMode)
47-
},
48-
elevation = FloatingActionButtonDefaults.elevation(
49-
defaultElevation = 0.dp
50-
)
51-
) {
52-
Icon(Icons.Filled.Call, "Call")
50+
elevation = FloatingActionButtonDefaults.elevation(
51+
defaultElevation = 0.dp
52+
)
53+
) {
54+
Icon(Icons.Filled.Call, "Call")
55+
}
5356
}
5457
}

app-service-hilt/src/main/java/com/github/only52607/compose/window/hilt/ui/theme/Theme.kt

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ import androidx.compose.material3.dynamicLightColorScheme
1111
import androidx.compose.material3.lightColorScheme
1212
import androidx.compose.runtime.Composable
1313
import androidx.compose.runtime.SideEffect
14+
import androidx.compose.ui.graphics.Color
1415
import androidx.compose.ui.graphics.toArgb
1516
import androidx.compose.ui.platform.LocalContext
1617
import androidx.compose.ui.platform.LocalView
1718
import androidx.core.view.WindowCompat
18-
import androidx.compose.ui.graphics.Color
1919

2020
private val surfaceDark = Color(0xFF101417)
2121
private val onSurfaceDark = Color(0xFFE0E3E8)
@@ -55,25 +55,26 @@ private val LightColorScheme = lightColorScheme(
5555
@Composable
5656
fun ComposeFloatingWindowTheme(
5757
darkTheme: Boolean = isSystemInDarkTheme(),
58-
// Dynamic color is available on Android 12+
59-
dynamicColor: Boolean = true,
6058
content: @Composable () -> Unit
6159
) {
62-
val colorScheme = when {
63-
dynamicColor && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> {
64-
val context = LocalContext.current
65-
if (darkTheme) dynamicDarkColorScheme(context) else dynamicLightColorScheme(context)
66-
}
60+
val context = LocalContext.current
61+
val dynamicColor = Build.VERSION.SDK_INT >= Build.VERSION_CODES.S
6762

63+
val colorScheme = when {
64+
dynamicColor && darkTheme -> dynamicDarkColorScheme(context)
65+
dynamicColor && !darkTheme -> dynamicLightColorScheme(context)
6866
darkTheme -> DarkColorScheme
6967
else -> LightColorScheme
7068
}
7169
val view = LocalView.current
7270
if (!view.isInEditMode) {
7371
SideEffect {
74-
val window = (view.context as Activity).window
75-
window.statusBarColor = colorScheme.primary.toArgb()
76-
WindowCompat.getInsetsController(window, view).isAppearanceLightStatusBars = darkTheme
72+
if (view.context is Activity) {
73+
val window = (view.context as Activity).window
74+
window.statusBarColor = colorScheme.primary.toArgb()
75+
WindowCompat.getInsetsController(window, view).isAppearanceLightStatusBars =
76+
darkTheme
77+
}
7778
}
7879
}
7980

0 commit comments

Comments
 (0)