Skip to content

Commit

Permalink
Simplify enabling algorithmic darkening on webviews
Browse files Browse the repository at this point in the history
  • Loading branch information
LunarX committed Jun 21, 2024
1 parent 5f7770f commit f56f26b
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import androidx.viewbinding.ViewBinding
import androidx.webkit.WebSettingsCompat
import androidx.webkit.WebViewFeature
import com.infomaniak.lib.core.utils.FormatterFileSize.formatShortFileSize
import com.infomaniak.lib.core.utils.context
import com.infomaniak.lib.core.utils.isNightModeEnabled
Expand Down Expand Up @@ -285,9 +283,7 @@ class ThreadAdapter(
}

private fun WebView.applyWebViewContent(uid: String, bodyWebView: String, type: String) {
if (WebViewFeature.isFeatureSupported(WebViewFeature.ALGORITHMIC_DARKENING)) {
WebSettingsCompat.setAlgorithmicDarkeningAllowed(settings, threadAdapterState.isThemeTheSameMap[uid]!!)
}
enableAlgorithmicDarkening(threadAdapterState.isThemeTheSameMap[uid]!!)

var styledBody = if (type == TEXT_PLAIN) createHtmlForPlainText(bodyWebView) else bodyWebView
styledBody = processMailDisplay(styledBody, uid, isForPrinting)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ import androidx.core.view.isVisible
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.navigation.fragment.findNavController
import androidx.webkit.WebSettingsCompat
import androidx.webkit.WebViewFeature
import com.infomaniak.lib.core.utils.FilePicker
import com.infomaniak.lib.core.utils.SnackbarUtils.showSnackbar
import com.infomaniak.lib.core.utils.getBackNavigationResult
Expand Down Expand Up @@ -263,10 +261,8 @@ class NewMessageFragment : Fragment() {
toolbar.setNavigationOnClickListener { activity?.onBackPressedDispatcher?.onBackPressed() }
changeToolbarColorOnScroll(toolbar, compositionNestedScrollView)

if (WebViewFeature.isFeatureSupported(WebViewFeature.ALGORITHMIC_DARKENING)) {
WebSettingsCompat.setAlgorithmicDarkeningAllowed(signatureWebView.settings, true)
WebSettingsCompat.setAlgorithmicDarkeningAllowed(quoteWebView.settings, true)
}
signatureWebView.enableAlgorithmicDarkening(true)
quoteWebView.enableAlgorithmicDarkening(true)

attachmentsRecyclerView.adapter = AttachmentAdapter(
shouldDisplayCloseButton = true,
Expand Down
8 changes: 2 additions & 6 deletions app/src/main/java/com/infomaniak/mail/utils/WebViewUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ import android.content.Context
import android.webkit.JavascriptInterface
import android.webkit.WebSettings
import android.webkit.WebView
import androidx.webkit.WebSettingsCompat
import androidx.webkit.WebViewFeature
import com.infomaniak.mail.R
import com.infomaniak.mail.utils.HtmlFormatter.Companion.getCustomDarkMode
import com.infomaniak.mail.utils.HtmlFormatter.Companion.getCustomStyle
Expand All @@ -33,6 +31,7 @@ import com.infomaniak.mail.utils.HtmlFormatter.Companion.getJsBridgeScript
import com.infomaniak.mail.utils.HtmlFormatter.Companion.getPrintMailStyle
import com.infomaniak.mail.utils.HtmlFormatter.Companion.getResizeScript
import com.infomaniak.mail.utils.HtmlFormatter.Companion.getSignatureMarginStyle
import com.infomaniak.mail.utils.extensions.enableAlgorithmicDarkening
import com.infomaniak.mail.utils.extensions.readRawResource

class WebViewUtils(context: Context) {
Expand Down Expand Up @@ -152,10 +151,7 @@ class WebViewUtils(context: Context) {
}

fun WebView.toggleWebViewTheme(isThemeTheSame: Boolean) {
if (WebViewFeature.isFeatureSupported(WebViewFeature.ALGORITHMIC_DARKENING)) {
WebSettingsCompat.setAlgorithmicDarkeningAllowed(settings, isThemeTheSame)
}

enableAlgorithmicDarkening(isThemeTheSame)
if (isThemeTheSame) addBackgroundJs() else removeBackgroundJs()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.LiveData
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager2.widget.ViewPager2
import androidx.webkit.WebSettingsCompat
import androidx.webkit.WebViewFeature
import com.airbnb.lottie.LottieAnimationView
import com.airbnb.lottie.LottieProperty
import com.airbnb.lottie.SimpleColorFilter
Expand Down Expand Up @@ -619,3 +621,9 @@ fun <T> List<T>.indexOfFirstOrNull(predicate: (T) -> Boolean): Int? {
val index = indexOfFirst(predicate)
return if (index == -1) null else index
}

fun WebView.enableAlgorithmicDarkening(isEnabled: Boolean) {
if (WebViewFeature.isFeatureSupported(WebViewFeature.ALGORITHMIC_DARKENING)) {
WebSettingsCompat.setAlgorithmicDarkeningAllowed(settings, isEnabled)
}
}

0 comments on commit f56f26b

Please sign in to comment.