Skip to content

Commit 0cd0ed7

Browse files
authored
update theme style implementation and use AppCompatDelegate for night… (#584)
* update theme style implementation and use AppCompatDelegate for night mode * change default theme style to DARK * update theme style default to dark in SettingsTest * fix theme style default value in SettingsTest * refactor MainActivity to streamline theme style initialization * add ThemeInstrumentedTest to verify theme settings and functionality
1 parent 782221a commit 0cd0ed7

File tree

6 files changed

+96
-29
lines changed

6 files changed

+96
-29
lines changed

app/src/androidTest/kotlin/com/vrem/wifianalyzer/MainInstrumentedTest.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,9 @@ class MainInstrumentedTest {
115115
fun settings() {
116116
SettingsInstrumentedTest().run()
117117
}
118+
119+
@Test
120+
fun theme() {
121+
ThemeInstrumentedTest().run()
122+
}
118123
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* WiFiAnalyzer
3+
* Copyright (C) 2026 VREM Software Development <[email protected]>
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>
17+
*/
18+
package com.vrem.wifianalyzer
19+
20+
import androidx.appcompat.app.AppCompatDelegate
21+
import androidx.test.espresso.Espresso.onView
22+
import androidx.test.espresso.Espresso.pressBack
23+
import androidx.test.espresso.action.ViewActions.click
24+
import androidx.test.espresso.assertion.ViewAssertions.matches
25+
import androidx.test.espresso.matcher.ViewMatchers.isDisplayed
26+
import androidx.test.espresso.matcher.ViewMatchers.withText
27+
import org.junit.Assert.assertEquals
28+
29+
internal class ThemeInstrumentedTest : Runnable {
30+
override fun run() {
31+
verifyThemeSettings()
32+
33+
listOf(
34+
"Dark" to AppCompatDelegate.MODE_NIGHT_YES,
35+
"Light" to AppCompatDelegate.MODE_NIGHT_NO,
36+
"System" to AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM,
37+
"Black" to AppCompatDelegate.MODE_NIGHT_YES,
38+
).forEach { (themeName, expectedNightMode) ->
39+
changeThemeAndVerify(themeName, expectedNightMode)
40+
}
41+
}
42+
43+
private fun verifyThemeSettings() {
44+
selectMenuItem(R.id.nav_drawer_settings, "Settings")
45+
scrollToAndVerify("Theme")
46+
pressBack()
47+
}
48+
49+
private fun changeThemeAndVerify(
50+
themeName: String,
51+
expectedNightMode: Int,
52+
) {
53+
selectMenuItem(R.id.nav_drawer_settings, "Settings")
54+
scrollToAndVerify("Theme")
55+
onView(withText("Theme")).perform(click())
56+
pauseShort()
57+
onView(withText(themeName)).check(matches(isDisplayed()))
58+
onView(withText(themeName)).perform(click())
59+
pauseShort()
60+
assertEquals(
61+
"Theme $themeName should set night mode to $expectedNightMode",
62+
expectedNightMode,
63+
AppCompatDelegate.getDefaultNightMode(),
64+
)
65+
}
66+
}

app/src/main/kotlin/com/vrem/wifianalyzer/MainActivity.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import android.view.Menu
2626
import android.view.MenuItem
2727
import android.view.View
2828
import androidx.appcompat.app.AppCompatActivity
29+
import androidx.appcompat.app.AppCompatDelegate
2930
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
3031
import androidx.core.view.GravityCompat
3132
import androidx.drawerlayout.widget.DrawerLayout
@@ -61,7 +62,11 @@ class MainActivity :
6162

6263
val settings = mainContext.settings
6364
settings.initializeDefaultValues()
64-
setTheme(settings.themeStyle().themeNoActionBar)
65+
66+
settings.themeStyle().apply {
67+
AppCompatDelegate.setDefaultNightMode(nightMode)
68+
setTheme(themeNoActionBar)
69+
}
6570

6671
mainReload = MainReload(settings)
6772

app/src/main/kotlin/com/vrem/wifianalyzer/settings/ThemeStyle.kt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,17 @@ package com.vrem.wifianalyzer.settings
2020
import android.graphics.Color
2121
import androidx.annotation.ColorInt
2222
import androidx.annotation.StyleRes
23+
import androidx.appcompat.app.AppCompatDelegate
2324
import com.vrem.wifianalyzer.R
2425

2526
enum class ThemeStyle(
2627
@param:StyleRes val theme: Int,
2728
@param:StyleRes val themeNoActionBar: Int,
2829
@param:ColorInt val colorGraphText: Int,
30+
val nightMode: Int,
2931
) {
30-
DARK(R.style.ThemeDark, R.style.ThemeDarkNoActionBar, Color.WHITE),
31-
LIGHT(R.style.ThemeLight, R.style.ThemeLightNoActionBar, Color.BLACK),
32-
SYSTEM(R.style.ThemeSystem, R.style.ThemeSystemNoActionBar, Color.GRAY),
33-
BLACK(R.style.ThemeBlack, R.style.ThemeBlackNoActionBar, Color.WHITE),
32+
DARK(R.style.ThemeSystem, R.style.ThemeSystemNoActionBar, Color.WHITE, AppCompatDelegate.MODE_NIGHT_YES),
33+
LIGHT(R.style.ThemeSystem, R.style.ThemeSystemNoActionBar, Color.BLACK, AppCompatDelegate.MODE_NIGHT_NO),
34+
SYSTEM(R.style.ThemeSystem, R.style.ThemeSystemNoActionBar, Color.GRAY, AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM),
35+
BLACK(R.style.ThemeBlack, R.style.ThemeBlackNoActionBar, Color.WHITE, AppCompatDelegate.MODE_NIGHT_YES),
3436
}

app/src/main/res/values/styles.xml

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,6 @@
1818

1919
<resources>
2020

21-
<!-- Dark theme -->
22-
<style name="ThemeDark" parent="Theme.AppCompat">
23-
<item name="android:windowAnimationStyle">@style/WindowAnimationTransition</item>
24-
<item name="android:actionMenuTextColor">@color/selected</item>
25-
</style>
26-
<style name="ThemeDarkNoActionBar" parent="ThemeDark">
27-
<item name="windowActionBar">false</item>
28-
<item name="windowNoTitle">true</item>
29-
</style>
30-
31-
<!-- Light theme -->
32-
<style name="ThemeLight" parent="Theme.AppCompat.Light.DarkActionBar">
33-
<item name="android:windowAnimationStyle">@style/WindowAnimationTransition</item>
34-
<item name="android:actionMenuTextColor">@color/selected</item>
35-
</style>
36-
<style name="ThemeLightNoActionBar" parent="ThemeLight">
37-
<item name="windowActionBar">false</item>
38-
<item name="windowNoTitle">true</item>
39-
</style>
40-
4121
<!-- Black theme -->
4222
<style name="ThemeBlack" parent="Theme.AppCompat">
4323
<item name="android:windowAnimationStyle">@style/WindowAnimationTransition</item>

app/src/test/kotlin/com/vrem/wifianalyzer/settings/ThemeStyleTest.kt

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package com.vrem.wifianalyzer.settings
1919

2020
import android.graphics.Color
21+
import androidx.appcompat.app.AppCompatDelegate
2122
import com.vrem.wifianalyzer.R
2223
import org.assertj.core.api.Assertions.assertThat
2324
import org.junit.Test
@@ -32,16 +33,16 @@ class ThemeStyleTest {
3233

3334
@Test
3435
fun theme() {
35-
assertThat(ThemeStyle.LIGHT.theme).isEqualTo(R.style.ThemeLight)
36-
assertThat(ThemeStyle.DARK.theme).isEqualTo(R.style.ThemeDark)
36+
assertThat(ThemeStyle.LIGHT.theme).isEqualTo(R.style.ThemeSystem)
37+
assertThat(ThemeStyle.DARK.theme).isEqualTo(R.style.ThemeSystem)
3738
assertThat(ThemeStyle.SYSTEM.theme).isEqualTo(R.style.ThemeSystem)
3839
assertThat(ThemeStyle.BLACK.theme).isEqualTo(R.style.ThemeBlack)
3940
}
4041

4142
@Test
4243
fun themeNoActionBar() {
43-
assertThat(ThemeStyle.DARK.themeNoActionBar).isEqualTo(R.style.ThemeDarkNoActionBar)
44-
assertThat(ThemeStyle.LIGHT.themeNoActionBar).isEqualTo(R.style.ThemeLightNoActionBar)
44+
assertThat(ThemeStyle.DARK.themeNoActionBar).isEqualTo(R.style.ThemeSystemNoActionBar)
45+
assertThat(ThemeStyle.LIGHT.themeNoActionBar).isEqualTo(R.style.ThemeSystemNoActionBar)
4546
assertThat(ThemeStyle.SYSTEM.themeNoActionBar).isEqualTo(R.style.ThemeSystemNoActionBar)
4647
assertThat(ThemeStyle.BLACK.themeNoActionBar).isEqualTo(R.style.ThemeBlackNoActionBar)
4748
}
@@ -54,6 +55,14 @@ class ThemeStyleTest {
5455
assertThat(ThemeStyle.BLACK.colorGraphText).isEqualTo(Color.WHITE)
5556
}
5657

58+
@Test
59+
fun nightMode() {
60+
assertThat(ThemeStyle.DARK.nightMode).isEqualTo(AppCompatDelegate.MODE_NIGHT_YES)
61+
assertThat(ThemeStyle.LIGHT.nightMode).isEqualTo(AppCompatDelegate.MODE_NIGHT_NO)
62+
assertThat(ThemeStyle.SYSTEM.nightMode).isEqualTo(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
63+
assertThat(ThemeStyle.BLACK.nightMode).isEqualTo(AppCompatDelegate.MODE_NIGHT_YES)
64+
}
65+
5766
@Test
5867
fun themeStyleOrdinal() {
5968
assertThat(ThemeStyle.DARK.ordinal).isEqualTo(0)

0 commit comments

Comments
 (0)