Skip to content

Commit

Permalink
Handle Ten X Theme (#231)
Browse files Browse the repository at this point in the history
* Check config for Ten X theme and apply darker theme

* Cleanup

* Remove empty lines for Code Climate
  • Loading branch information
chickdan authored Oct 16, 2020
1 parent 7200775 commit 7516976
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions DEV-Simple/views/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ struct UserData: Codable {
}

class ViewController: UIViewController {

@IBOutlet weak var backButton: UIBarButtonItem!
@IBOutlet weak var forwardButton: UIBarButtonItem!
@IBOutlet weak var refreshButton: UIBarButtonItem!
Expand All @@ -31,11 +30,9 @@ class ViewController: UIViewController {
if !UIAccessibility.isInvertColorsEnabled {
return WKWebView()
}

guard let path = Bundle.main.path(forResource: "invertedImages", ofType: "css") else {
return WKWebView()
}

let cssString = try? String(contentsOfFile: path).components(separatedBy: .newlines).joined()
let source = """
var style = document.createElement('style');
Expand All @@ -46,7 +43,6 @@ class ViewController: UIViewController {
let userScript = WKUserScript(source: source,
injectionTime: .atDocumentEnd,
forMainFrameOnly: true)

let userContentController = WKUserContentController()
userContentController.addUserScript(userScript)

Expand All @@ -55,7 +51,6 @@ class ViewController: UIViewController {

let webView = WKWebView(frame: .zero,
configuration: configuration)

webView.accessibilityIgnoresInvertColors = true
return webView
}()
Expand Down Expand Up @@ -131,7 +126,6 @@ class ViewController: UIViewController {
guard let reachability = note.object as? Reachability else {
return
}

switch reachability.status {
case .wifi:
if errorBanner.isDisplaying {
Expand Down Expand Up @@ -185,7 +179,6 @@ class ViewController: UIViewController {
guard let self = self else {
return
}

// Wait a split second if first launch (Hack, probably a race condition)
self.webView.load(serverURL ?? "https://dev.to")
}
Expand Down Expand Up @@ -222,14 +215,16 @@ class ViewController: UIViewController {
print("Error getting user data: \(error)")
return
}

if let jsonString = result as? String {
do {
let user = try JSONDecoder().decode(UserData.self, from: Data(jsonString.utf8))
let notificationSubscription = "user-notifications-\(String(user.userID))"
try? self.pushNotifications.addDeviceInterest(interest: notificationSubscription)
if user.configBodyClass.contains("night-theme") {
self.applyDarkTheme()
} else if user.configBodyClass.contains("ten-x-hacker-theme") {
self.applyDarkTheme()
self.applyDarkerTheme()
}
} catch {
print("Error info: \(error)")
Expand All @@ -251,6 +246,11 @@ class ViewController: UIViewController {
activityIndicator.color = UIColor.white
}

private func applyDarkerTheme() {
navigationToolBar.barTintColor = UIColor.black
view.backgroundColor = UIColor.black
}

func modifyShellDesign() {
let javascript = "document.getElementById('page-content').getAttribute('data-current-page')"
webView.evaluateJavaScript(javascript) { [weak self] result, error in
Expand Down Expand Up @@ -351,7 +351,6 @@ extension ViewController: WKNavigationDelegate {
decisionHandler(.allow)
return
}

let policy = navigationPolicy(url: url, navigationType: navigationAction.navigationType)
decisionHandler(policy)
}
Expand Down

0 comments on commit 7516976

Please sign in to comment.