Skip to content

Commit

Permalink
Merge pull request #378 from Kommunicate-io/CM-1660
Browse files Browse the repository at this point in the history
[CM-1660] Dark Mode Support Part - 3 (Dynamic Change)
  • Loading branch information
kandpal025 authored Dec 11, 2023
2 parents 971fccd + 1f2e056 commit feceb01
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
30 changes: 29 additions & 1 deletion Sources/Kommunicate/Classes/FaqViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,15 @@ public class FaqViewController: UIViewController, Localizable {
let webConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.allowsBackForwardNavigationGestures = true
webView.scrollView.backgroundColor = UIColor.kmDynamicColor(light: .white, dark: .backgroundDarkColor())
webView.backgroundColor = UIColor.kmDynamicColor(light: .white, dark: .backgroundDarkColor())
webView.navigationDelegate = self
webView.configuration.userContentController.addUserScript(self.getZoomDisableScript())
if UIColor.isKMDarkModeEnabled {
injectDynamicStyles()
}
view = webView
}


private func getZoomDisableScript() -> WKUserScript {
let source: String = "var meta = document.createElement('meta');" +
Expand All @@ -44,6 +48,30 @@ public class FaqViewController: UIViewController, Localizable {
return WKUserScript(source: source, injectionTime: .atDocumentEnd, forMainFrameOnly: true)
}

private func injectDynamicStyles() {
let dynamicStylesScript = """
function prefersDarkMode() {
return window.matchMedia('(prefers-color-scheme: dark)').matches;
}
function applyDynamicStyles() {
var initialBackgroundColor = prefersDarkMode() ? '#1C1C1C' : '#ffffff';
document.body.style.backgroundColor = initialBackgroundColor;
if (prefersDarkMode()) {
document.body.style.backgroundColor = '#1C1C1C';
} else {
document.body.style.backgroundColor = '#ffffff';
}
}
applyDynamicStyles();
"""
let dynamicStylesScriptInjection = WKUserScript(source: dynamicStylesScript, injectionTime: .atDocumentEnd, forMainFrameOnly: true)

webView.configuration.userContentController.addUserScript(dynamicStylesScriptInjection)
}

override public func viewDidLoad() {
super.viewDidLoad()
webView.load(URLRequest(url: url))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class ConversationClosedView: UIView {
private let conversationResolvedLabel: UILabel = {
let label = UILabel(frame: .zero)
label.numberOfLines = 1
label.textColor = .text(.brownishGreyTwo)
label.textColor = UIColor.kmDynamicColor(light: .text(.brownishGreyTwo), dark: UIColor.text(.warmGrey))
label.font = Style.Font.normal(size: 15).font()
label.backgroundColor = .clear
label.text = LocalizedText.conversationResolved
Expand Down Expand Up @@ -99,7 +99,7 @@ class ConversationClosedView: UIView {
action: #selector(restartConversationTapped),
for: .touchUpInside
)
backgroundColor = .background(.mediumGrey)
backgroundColor = UIColor.kmDynamicColor(light: UIColor.background(.mediumGrey), dark: UIColor.appBarDarkColor())
setupLayout()
}

Expand Down

0 comments on commit feceb01

Please sign in to comment.