Skip to content

Commit 5490ddf

Browse files
authored
Merge pull request #10 from odaridavid/fix-code-sample-system-theme
fix code sample to follow system theme
2 parents 53464db + 60e4227 commit 5490ddf

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

app/build.gradle

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ android {
1313

1414
def versionMajor = 1
1515
def versionMinor = 1
16-
def versionPatch = 0
16+
def versionPatch = 1
1717

1818
applicationId "com.github.odaridavid.designpatterns"
1919
minSdkVersion 21
@@ -31,6 +31,8 @@ android {
3131
manifestPlaceholders = [crashlyticsEnabled: true]
3232
}
3333
debug {
34+
applicationIdSuffix ".debug"
35+
versionNameSuffix "-debug"
3436
manifestPlaceholders = [crashlyticsEnabled: false]
3537
}
3638
}

app/src/main/java/com/github/odaridavid/designpatterns/helpers/Extensions.kt

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,13 @@ import android.app.Activity
1717
import android.content.Context
1818
import android.content.Intent
1919
import android.content.SharedPreferences
20+
import android.content.res.Configuration
21+
import android.os.Build
22+
import android.os.PowerManager
2023
import android.widget.Toast
2124
import androidx.preference.PreferenceManager
2225
import com.github.odaridavid.designpatterns.R
26+
import com.github.odaridavid.designpatterns.helpers.SdkUtils.versionUntil
2327
import us.feras.mdv.MarkdownView
2428

2529

@@ -43,11 +47,33 @@ internal fun getThemedCss(context: Context): String {
4347
return when (getCurrentTheme(context, sp)) {
4448
ThemeUtils.THEME_DARK -> DARK_KOTLIN_CSS_PATH
4549
ThemeUtils.THEME_LIGHT -> LIGHT_KOTLIN_CSS_PATH
46-
ThemeUtils.THEME_SYSTEM -> LIGHT_KOTLIN_CSS_PATH
50+
ThemeUtils.THEME_SYSTEM -> {
51+
if (versionUntil(Build.VERSION_CODES.P)) {
52+
val powerManager = context.getSystemService(Context.POWER_SERVICE) as PowerManager
53+
onPowerSaverModeChange(powerManager)
54+
} else {
55+
onUiModeConfigChange(context)
56+
}
57+
}
4758
else -> LIGHT_KOTLIN_CSS_PATH
4859
}
4960
}
5061

62+
private fun onUiModeConfigChange(context: Context): String {
63+
return when (context.resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK) {
64+
Configuration.UI_MODE_NIGHT_NO -> LIGHT_KOTLIN_CSS_PATH
65+
Configuration.UI_MODE_NIGHT_YES -> DARK_KOTLIN_CSS_PATH
66+
else -> LIGHT_KOTLIN_CSS_PATH
67+
}
68+
}
69+
70+
private fun onPowerSaverModeChange(powerManager: PowerManager): String {
71+
return if (powerManager.isPowerSaveMode)
72+
DARK_KOTLIN_CSS_PATH
73+
else
74+
LIGHT_KOTLIN_CSS_PATH
75+
}
76+
5177
internal fun getCurrentTheme(context: Context, sp: SharedPreferences?): String {
5278
return sp?.getString(context.getString(R.string.key_theme_preference), ThemeUtils.THEME_LIGHT)
5379
?: "default"

whatsnew/whatsnew-en-GB.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
- Immediate In App Update Available
2-
- Adds a Dark theme variant
1+
- Fix code sample not switching to a dark theme on system-related theme change.
2+
- Minor bug fixes and improvements.

0 commit comments

Comments
 (0)