Skip to content

Commit

Permalink
Merge pull request #721 from hilmyveradin/issue_720_credits-dark-mode
Browse files Browse the repository at this point in the history
Fix Credits Dark Mode Issues and refactor webview code
  • Loading branch information
aaronbrethorst authored Apr 5, 2024
2 parents 47b6a80 + a1e0770 commit e8eb008
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
2 changes: 2 additions & 0 deletions OBAKit/Alerts/TransitAlertDetailViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ class TransitAlertDetailViewController: UIViewController, WKScriptMessageHandler

let view = DocumentWebView(frame: view.bounds, configuration: configuration)
view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
view.isOpaque = false
view.backgroundColor = ThemeColors.shared.systemBackground

if #available(iOS 16.4, *) {
view.isInspectable = true
Expand Down
2 changes: 2 additions & 0 deletions OBAKit/Settings/CreditsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ class CreditViewerController: UIViewController {
override func viewDidLoad() {
webView.frame = view.bounds
webView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
webView.isOpaque = false
webView.backgroundColor = ThemeColors.shared.systemBackground
view.addSubview(webView)

let mungedCredits = "<code>\(licenseText.replacingOccurrences(of: "\n", with: "<br>"))</code>"
Expand Down
29 changes: 16 additions & 13 deletions OBAKit/WebView/DocumentWebView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,23 @@ class DocumentWebView: WKWebView {
/// - Parameter htmlFragment: The content to render in the web view.
/// - Parameter actionButtonTitle: The title of the optional button shown at the bottom of the web view.
func setPageContent(_ htmlFragment: String, actionButtonTitle: String? = nil) {
var content = pageBody.replacingOccurrences(of: "{{{oba_page_content}}}", with: htmlFragment)
content = content.replacingOccurrences(of: "{{{accent_color}}}", with: accentHexColor)
content = content.replacingOccurrences(of: "{{{accent_foreground_color}}}", with: accentForegroundColor)

if let actionButtonTitle {
let buttonText = """
<div class="actions__button-container">
<button type="button" class="actions__button-container__button" onclick="window.webkit.messageHandlers.actionButtonClicked.postMessage({})">
\(actionButtonTitle)
</button>
</div>
"""
content = content.replacingOccurrences(of: "{{{oba_page_actions}}}", with: buttonText)
var content = pageBody
.replacingOccurrences(of: "{{{oba_page_content}}}", with: htmlFragment)
.replacingOccurrences(of: "{{{accent_color}}}", with: accentHexColor)
.replacingOccurrences(of: "{{{accent_foreground_color}}}", with: accentForegroundColor)

// Construct button HTML if actionButtonTitle is present and not empty
let buttonText: String? = actionButtonTitle.flatMap { title in
title.isEmpty ? nil : """
<div class="actions__button-container">
<button type="button" class="actions__button-container__button" onclick="window.webkit.messageHandlers.actionButtonClicked.postMessage({})">
\(title)
</button>
</div>
"""
}
// Use coalescing operator to remove action button placeholder if buttonText is nil
content = content.replacingOccurrences(of: "{{{oba_page_actions}}}", with: buttonText ?? "")

loadHTMLString(content, baseURL: nil)
}
Expand Down

0 comments on commit e8eb008

Please sign in to comment.