Skip to content

Commit

Permalink
fix: Dark theme injection in webviews
Browse files Browse the repository at this point in the history
  • Loading branch information
omerhabib26 committed Jul 30, 2024
1 parent 804fe2d commit 98733d6
Showing 1 changed file with 5 additions and 18 deletions.
23 changes: 5 additions & 18 deletions core/src/main/java/org/openedx/core/extension/ViewExt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package org.openedx.core.extension
import android.content.Context
import android.content.res.Resources
import android.graphics.Rect
import android.os.Build
import android.util.DisplayMetrics
import android.view.View
import android.view.ViewGroup
Expand Down Expand Up @@ -66,23 +65,11 @@ fun WebView.loadUrl(url: String, scope: CoroutineScope, cookieManager: AppCookie
}

fun WebView.applyDarkModeIfEnabled(isDarkTheme: Boolean) {
if (isDarkTheme && WebViewFeature.isFeatureSupported(WebViewFeature.FORCE_DARK)) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
settings.setAlgorithmicDarkeningAllowed(true)
} else {
// Switch WebView to dark mode; uses default dark theme
if (WebViewFeature.isFeatureSupported(WebViewFeature.FORCE_DARK)) {
WebSettingsCompat.setForceDark(
settings,
WebSettingsCompat.FORCE_DARK_ON
)
}
if (WebViewFeature.isFeatureSupported(WebViewFeature.FORCE_DARK_STRATEGY)) {
WebSettingsCompat.setForceDarkStrategy(
settings,
WebSettingsCompat.DARK_STRATEGY_PREFER_WEB_THEME_OVER_USER_AGENT_DARKENING
)
}
if (isDarkTheme && WebViewFeature.isFeatureSupported(WebViewFeature.ALGORITHMIC_DARKENING)) {
try {
WebSettingsCompat.setAlgorithmicDarkeningAllowed(settings, true)
} catch (e: Exception) {
e.printStackTrace()
}
}
}

0 comments on commit 98733d6

Please sign in to comment.