Skip to content

Commit 702c589

Browse files
authored
Disable Force Dark Mode when WebView version doesn't support it (#4653)
Task/Issue URL: https://app.asana.com/0/1157893581871903/1207572180893002/f ### Description We enabled [✓ Android: Force dark mode in sites that don’t support it](https://app.asana.com/0/276630244458377/1206630026838095/f) This was causing the app crash in webview versions that didn’t support it. Fixed in[✓ setAlgorithmicDarkeningAllowed crashing the app](https://app.asana.com/0/1125189844152671/1207572075253654/f) After this fix, devices with webview versions that don’t support algorithmic darkening won’t see the feature. This PR removes this option in devices that don’t support it ### Steps to test this PR _WebView not supported_ - [x] Install this branch in an emulator with an old webview version - [x] Open Settings / Appearance - [x] Ensure Dark theme is selected - [x] Verify that Force Dark Mode is not visible _WebView supported_ - [x] Install this branch in an emulator with a new webview version - [x] Open Settings / Appearance - [x] Ensure Dark theme is selected - [x] Verify that Force Dark Mode is enabled - [x] Verify that turning it on enabled force dark mode
1 parent 20bb974 commit 702c589

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

app/src/main/java/com/duckduckgo/app/appearance/AppearanceActivity.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package com.duckduckgo.app.appearance
1818

1919
import android.os.Bundle
2020
import android.widget.CompoundButton
21+
import androidx.core.view.isVisible
2122
import androidx.lifecycle.Lifecycle
2223
import androidx.lifecycle.flowWithLifecycle
2324
import androidx.lifecycle.lifecycleScope
@@ -97,6 +98,7 @@ class AppearanceActivity : DuckDuckGoActivity() {
9798
binding.changeAppIcon.setImageResource(it.appIcon.icon)
9899
binding.experimentalNightMode.quietlySetIsChecked(viewState.forceDarkModeEnabled, forceDarkModeToggleListener)
99100
binding.experimentalNightMode.isEnabled = viewState.canForceDarkMode
101+
binding.experimentalNightMode.isVisible = viewState.supportsForceDarkMode
100102
}
101103
}.launchIn(lifecycleScope)
102104

app/src/main/java/com/duckduckgo/app/appearance/AppearanceViewModel.kt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package com.duckduckgo.app.appearance
1818

1919
import androidx.lifecycle.ViewModel
2020
import androidx.lifecycle.viewModelScope
21+
import androidx.webkit.WebViewFeature
2122
import com.duckduckgo.anvil.annotations.ContributesViewModel
2223
import com.duckduckgo.app.icon.api.AppIcon
2324
import com.duckduckgo.app.pixels.AppPixelName
@@ -51,6 +52,7 @@ class AppearanceViewModel @Inject constructor(
5152
val appIcon: AppIcon = AppIcon.DEFAULT,
5253
val forceDarkModeEnabled: Boolean = false,
5354
val canForceDarkMode: Boolean = false,
55+
val supportsForceDarkMode: Boolean = true,
5456
)
5557

5658
sealed class Command {
@@ -69,7 +71,8 @@ class AppearanceViewModel @Inject constructor(
6971
theme = themingDataStore.theme,
7072
appIcon = settingsDataStore.appIcon,
7173
forceDarkModeEnabled = settingsDataStore.experimentalWebsiteDarkMode,
72-
canForceDarkMode = themingDataStore.theme != DuckDuckGoTheme.LIGHT,
74+
canForceDarkMode = canForceDarkMode(),
75+
supportsForceDarkMode = WebViewFeature.isFeatureSupported(WebViewFeature.ALGORITHMIC_DARKENING),
7376
),
7477
)
7578
}
@@ -79,6 +82,10 @@ class AppearanceViewModel @Inject constructor(
7982
return command.receiveAsFlow()
8083
}
8184

85+
private fun canForceDarkMode(): Boolean {
86+
return themingDataStore.theme != DuckDuckGoTheme.LIGHT
87+
}
88+
8289
fun userRequestedToChangeTheme() {
8390
viewModelScope.launch { command.send(Command.LaunchThemeSettings(viewState.value.theme)) }
8491
pixel.fire(AppPixelName.SETTINGS_THEME_OPENED)
@@ -98,7 +105,7 @@ class AppearanceViewModel @Inject constructor(
98105
viewModelScope.launch(dispatcherProvider.io()) {
99106
themingDataStore.theme = selectedTheme
100107
withContext(dispatcherProvider.main()) {
101-
viewState.emit(currentViewState().copy(theme = selectedTheme, forceDarkModeEnabled = selectedTheme != DuckDuckGoTheme.LIGHT))
108+
viewState.emit(currentViewState().copy(theme = selectedTheme, forceDarkModeEnabled = canForceDarkMode()))
102109
command.send(Command.UpdateTheme)
103110
}
104111
}

0 commit comments

Comments
 (0)