Skip to content

Commit 4a8d88b

Browse files
committed
Merge master
2 parents cd7676c + 523f927 commit 4a8d88b

File tree

3 files changed

+74
-23
lines changed

3 files changed

+74
-23
lines changed

DEV-Simple.xcodeproj/project.pbxproj

100755100644
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
73D76751218BA66B00BD13B6 /* DEV_SimpleTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 73D76750218BA66B00BD13B6 /* DEV_SimpleTests.swift */; };
1717
73D7675C218BA66B00BD13B6 /* DEV_SimpleUITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 73D7675B218BA66B00BD13B6 /* DEV_SimpleUITests.swift */; };
1818
73E8ACC72192165E009CE057 /* BrowserViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 73E8ACC62192165E009CE057 /* BrowserViewController.swift */; };
19+
BD35609425719BFE00555C25 /* ThemeManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = BD35609325719BFE00555C25 /* ThemeManager.swift */; };
1920
BD35609225718C4C00555C25 /* Cartfile in Resources */ = {isa = PBXBuildFile; fileRef = BD35609125718C4C00555C25 /* Cartfile */; };
2021
BD5C906C21B63CCC000D9F79 /* NetworkReachability.swift in Sources */ = {isa = PBXBuildFile; fileRef = BD5C906B21B63CCC000D9F79 /* NetworkReachability.swift */; };
2122
CCEA9AFC219B403100AEF5BE /* ViewControllerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = CCEA9AFB219B403100AEF5BE /* ViewControllerTests.swift */; };
@@ -75,6 +76,7 @@
7576
73D7675D218BA66B00BD13B6 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
7677
73D767692190CE3900BD13B6 /* DEV-Simple.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = "DEV-Simple.entitlements"; sourceTree = "<group>"; };
7778
73E8ACC62192165E009CE057 /* BrowserViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BrowserViewController.swift; sourceTree = "<group>"; };
79+
BD35609325719BFE00555C25 /* ThemeManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ThemeManager.swift; sourceTree = "<group>"; };
7880
BD35609125718C4C00555C25 /* Cartfile */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = Cartfile; sourceTree = SOURCE_ROOT; };
7981
BD5C906B21B63CCC000D9F79 /* NetworkReachability.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NetworkReachability.swift; sourceTree = "<group>"; };
8082
CCEA9AFB219B403100AEF5BE /* ViewControllerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewControllerTests.swift; sourceTree = "<group>"; };
@@ -132,6 +134,7 @@
132134
1B032B1721E409FE00A2108A /* Globals.swift */,
133135
DC65E65325656AF600EE3446 /* Notification+DEV.swift */,
134136
BD5C906B21B63CCC000D9F79 /* NetworkReachability.swift */,
137+
BD35609325719BFE00555C25 /* ThemeManager.swift */,
135138
);
136139
path = Utilities;
137140
sourceTree = "<group>";
@@ -405,6 +408,7 @@
405408
73D7673C218BA66700BD13B6 /* AppDelegate.swift in Sources */,
406409
1B032B1821E409FE00A2108A /* Globals.swift in Sources */,
407410
BD5C906C21B63CCC000D9F79 /* NetworkReachability.swift in Sources */,
411+
BD35609425719BFE00555C25 /* ThemeManager.swift in Sources */,
408412
);
409413
runOnlyForDeploymentPostprocessing = 0;
410414
};
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
//
2+
// ThemeManager.swift
3+
// DEV-Simple
4+
//
5+
// Created by Daniel Chick on 11/27/20.
6+
// Copyright © 2020 DEV. All rights reserved.
7+
//
8+
9+
import UIKit
10+
import ForemWebView
11+
12+
class ThemeManager {
13+
private(set) static var useLightIcons = false
14+
15+
static func applyTheme(to viewController: ViewController) {
16+
guard let webView = viewController.webView else { return }
17+
18+
let theme = webView.userData?.theme() ?? .base
19+
let config = getThemeColor(for: theme)
20+
21+
viewController.navigationToolBar.barTintColor = config.barTintColor
22+
viewController.view.backgroundColor = config.backgroundColor
23+
24+
viewController.navigationToolBar.isTranslucent = !useLightIcons
25+
viewController.safariButton.tintColor = useLightIcons ? UIColor.white : ThemeColors.darkBackgroundColor
26+
viewController.backButton.tintColor = useLightIcons ? UIColor.white : ThemeColors.darkBackgroundColor
27+
viewController.forwardButton.tintColor = useLightIcons ? UIColor.white : ThemeColors.darkBackgroundColor
28+
viewController.refreshButton.tintColor = useLightIcons ? UIColor.white : ThemeColors.darkBackgroundColor
29+
viewController.activityIndicator.color = useLightIcons ? UIColor.white : ThemeColors.darkBackgroundColor
30+
}
31+
32+
private static func getThemeColor(for theme: ForemWebViewTheme) -> ThemeConfig {
33+
switch theme {
34+
case .night:
35+
useLightIcons = true
36+
return ThemeConfig.night
37+
case .hacker:
38+
useLightIcons = true
39+
return ThemeConfig.hacker
40+
case .pink:
41+
useLightIcons = true
42+
return ThemeConfig.pink
43+
case .minimal:
44+
useLightIcons = false
45+
return ThemeConfig.minimal
46+
default:
47+
useLightIcons = false
48+
return ThemeConfig.base
49+
}
50+
}
51+
}
52+
53+
private struct ThemeConfig {
54+
let barTintColor: UIColor
55+
let backgroundColor: UIColor
56+
}
57+
58+
extension ThemeConfig {
59+
private static let devPink = UIColor(red: 250/255, green: 70/255, blue: 129/255, alpha: 1)
60+
61+
static let night = ThemeConfig(barTintColor: ThemeColors.darkBackgroundColor,
62+
backgroundColor: ThemeColors.darkBackgroundColor)
63+
static let hacker = ThemeConfig(barTintColor: .black, backgroundColor: .black)
64+
static let pink = ThemeConfig(barTintColor: devPink, backgroundColor: devPink)
65+
static let base = ThemeConfig(barTintColor: .white, backgroundColor: .white)
66+
static let minimal = ThemeConfig(barTintColor: .white, backgroundColor: .white)
67+
}

DEV-Simple/ViewController.swift

100755100644
Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ class ViewController: UIViewController {
2424
@IBOutlet weak var activityIndicator: UIActivityIndicatorView!
2525
@IBOutlet weak var navigationToolBar: UIToolbar!
2626

27-
var useDarkMode = false
2827
var pushNotificationSubscription = ""
2928

3029
lazy var errorBanner: NotificationBanner = {
@@ -124,27 +123,8 @@ class ViewController: UIViewController {
124123

125124
backButton.isEnabled = webView.canGoBack
126125
forwardButton.isEnabled = webView.canGoForward
127-
switch webView.userData?.theme() {
128-
case .night:
129-
useDarkMode = true
130-
navigationToolBar.barTintColor = ThemeColors.darkBackgroundColor
131-
view.backgroundColor = ThemeColors.darkBackgroundColor
132-
case .hacker:
133-
useDarkMode = true
134-
navigationToolBar.barTintColor = UIColor.black
135-
view.backgroundColor = UIColor.black
136-
default:
137-
useDarkMode = false
138-
navigationToolBar.barTintColor = UIColor.white
139-
view.backgroundColor = UIColor.white
140-
}
141126

142-
navigationToolBar.isTranslucent = !useDarkMode
143-
safariButton.tintColor = useDarkMode ? UIColor.white : ThemeColors.darkBackgroundColor
144-
backButton.tintColor = useDarkMode ? UIColor.white : ThemeColors.darkBackgroundColor
145-
forwardButton.tintColor = useDarkMode ? UIColor.white : ThemeColors.darkBackgroundColor
146-
refreshButton.tintColor = useDarkMode ? UIColor.white : ThemeColors.darkBackgroundColor
147-
activityIndicator.color = useDarkMode ? UIColor.white : ThemeColors.darkBackgroundColor
127+
ThemeManager.applyTheme(to: self)
148128
setNeedsStatusBarAppearanceUpdate()
149129
}
150130

@@ -181,10 +161,10 @@ class ViewController: UIViewController {
181161
}
182162

183163
override var preferredStatusBarStyle: UIStatusBarStyle {
184-
if !useDarkMode && traitCollection.userInterfaceStyle == .dark {
164+
if !ThemeManager.useLightIcons && traitCollection.userInterfaceStyle == .dark {
185165
return UIStatusBarStyle.init(rawValue: ThemeColors.statusBarStyleDarkContentRawValue)!
186166
}
187-
return useDarkMode ? .lightContent : .default
167+
return ThemeManager.useLightIcons ? .lightContent : .default
188168
}
189169

190170
private func setupObservers() {

0 commit comments

Comments
 (0)