Skip to content

Commit add88db

Browse files
committed
DarkTheme.kt: Add utils to check whether dark theme should be applied
* Add for existing `DarkThemeValue.Version2` * And `DarkModeSetting` from the ProtoBuf message
1 parent 64bf1dd commit add88db

File tree

2 files changed

+58
-0
lines changed
  • ui/theming
    • common/src/main/kotlin/com/edricchan/studybuddy/ui/theming/common/night
    • compose/src/main/kotlin/com/edricchan/studybuddy/ui/theming/compose/night

2 files changed

+58
-0
lines changed

ui/theming/common/src/main/kotlin/com/edricchan/studybuddy/ui/theming/common/night/DarkTheme.kt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package com.edricchan.studybuddy.ui.theming.common.night
33
import android.content.Context
44
import android.content.res.Configuration
55
import android.content.res.Resources
6+
import com.edricchan.studybuddy.core.settings.appearance.DarkThemeValue
7+
import com.edricchan.studybuddy.core.settings.appearance.proto.DarkModeSetting
68

79
/** Checks if the receiver [Resources] has dark theme enabled. */
810
val Resources.isDarkThemeEnabled: Boolean
@@ -14,3 +16,33 @@ val Resources.isDarkThemeEnabled: Boolean
1416
* @see [Resources.isDarkThemeEnabled]
1517
*/
1618
val Context.isDarkThemeEnabled: Boolean get() = resources.isDarkThemeEnabled
19+
20+
/**
21+
* Whether dark theme should be applied based on the value of [themeValue].
22+
* @param themeValue The current [DarkThemeValue.Version2].
23+
* @param isSystemInDarkTheme Whether the system is using dark theme.
24+
*/
25+
@Suppress("DEPRECATION")
26+
fun Context.shouldApplyDarkTheme(
27+
themeValue: DarkThemeValue.Version2,
28+
isSystemInDarkTheme: Boolean = isDarkThemeEnabled
29+
): Boolean = when (themeValue) {
30+
DarkThemeValue.V2Never -> false
31+
DarkThemeValue.V2Always -> true
32+
DarkThemeValue.V2FollowSystem -> isSystemInDarkTheme
33+
}
34+
35+
/**
36+
* Whether dark theme should be applied based on the value of [themeSetting].
37+
* @param themeSetting The current [DarkModeSetting].
38+
* @param isSystemInDarkTheme Whether the system is using dark theme.
39+
*/
40+
fun shouldApplyDarkTheme(
41+
themeSetting: DarkModeSetting,
42+
isSystemInDarkTheme: Boolean
43+
): Boolean = when (themeSetting) {
44+
DarkModeSetting.AlwaysOff -> false
45+
DarkModeSetting.AlwaysOn -> true
46+
DarkModeSetting.FollowSystem -> isSystemInDarkTheme
47+
else -> true
48+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.edricchan.studybuddy.ui.theming.compose.night
2+
3+
import android.content.Context
4+
import androidx.compose.foundation.isSystemInDarkTheme
5+
import androidx.compose.runtime.Composable
6+
import androidx.compose.ui.platform.LocalContext
7+
import com.edricchan.studybuddy.core.settings.appearance.DarkThemeValue
8+
import com.edricchan.studybuddy.core.settings.appearance.proto.DarkModeSetting
9+
import com.edricchan.studybuddy.ui.theming.common.night.shouldApplyDarkTheme as commonShouldApplyDarkTheme
10+
11+
@Composable
12+
fun shouldApplyDarkTheme(
13+
context: Context = LocalContext.current,
14+
themeValue: DarkThemeValue.Version2
15+
): Boolean = context.commonShouldApplyDarkTheme(
16+
themeValue = themeValue,
17+
isSystemInDarkTheme = isSystemInDarkTheme()
18+
)
19+
20+
@Composable
21+
fun shouldApplyDarkTheme(
22+
themeSetting: DarkModeSetting
23+
): Boolean = commonShouldApplyDarkTheme(
24+
themeSetting = themeSetting,
25+
isSystemInDarkTheme = isSystemInDarkTheme()
26+
)

0 commit comments

Comments
 (0)