Skip to content

Commit 2555b4b

Browse files
author
Łukasz Śliwiński
committed
Initial commit.
1 parent d3e3155 commit 2555b4b

25 files changed

+926
-96
lines changed

.swiftlint.yml

+8-2
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,11 @@ excluded: # paths to ignore during linting. Takes precedence over `included`.
1717
- Tests
1818
- Example
1919

20-
disabled_rules: # rule identifiers to exclude from running
21-
- trailing_whitespace
20+
identifier_name:
21+
min_length: # only min_length
22+
error: 4 # only error
23+
excluded: # excluded via string array
24+
- id
25+
26+
nesting:
27+
type_level: 2

Example/AppDelegate.swift

+5-9
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
//
2-
// AppDelegate.swift
3-
// Example
4-
//
5-
// Created by Łukasz Śliwiński on 30 Apr 2020.
6-
// Copyright © 2020 plum. All rights reserved.
7-
//
1+
// Copyright © 2020 plum. All rights reserved.
82

93
import UIKit
104

@@ -28,8 +22,10 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
2822
/// - application: The UIApplication
2923
/// - launchOptions: The LaunchOptions
3024
/// - Returns: The launch result
31-
func application(_ application: UIApplication,
32-
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
25+
func application(
26+
_ application: UIApplication,
27+
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
28+
) -> Bool {
3329
// Initialize UIWindow
3430
self.window = .init(frame: UIScreen.main.bounds)
3531
// Set RootViewController

Example/Stylesheet.swift

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// Copyright © 2020 plum. All rights reserved.
2+
3+
import UIKit
4+
import ObservableThemeKit
5+
6+
// MARK: - Stylesheet
7+
8+
/// For the sake of the example Stylesheet represents what kind of the Style type, the Theme could hold
9+
struct Stylesheet {
10+
11+
fileprivate static var shared: Observable<Stylesheet> = .init(Stylesheet(appearance: .light))
12+
13+
let appearance: Appearance
14+
let primaryColor: UIColor
15+
let backgroundColor: UIColor
16+
17+
init(appearance: Appearance) {
18+
self.appearance = appearance
19+
20+
switch appearance {
21+
case .light:
22+
self.primaryColor = .black
23+
self.backgroundColor = .white
24+
case .dark:
25+
self.primaryColor = .white
26+
self.backgroundColor = .darkGray
27+
}
28+
}
29+
30+
func font(with type: FontType) -> UIFont {
31+
switch type {
32+
case .header:
33+
return UIFont.systemFont(ofSize: 16, weight: .semibold)
34+
case .normal:
35+
return UIFont.systemFont(ofSize: 14, weight: .regular)
36+
case .small:
37+
return UIFont.systemFont(ofSize: 10, weight: .regular)
38+
}
39+
}
40+
}
41+
42+
// MARK: - Stylesheet helpers
43+
44+
extension Stylesheet {
45+
46+
/// Change shared stylesheet using the new appearance
47+
static func changeAppearance(to appearance: Appearance) {
48+
Self.shared.wrappedValue = Stylesheet(appearance: appearance)
49+
}
50+
51+
/// Currently used appearance in the stylesheet
52+
static var appearance: Appearance {
53+
return Self.shared.wrappedValue.appearance
54+
}
55+
56+
/// Currently set stylesheet
57+
static var current: Stylesheet {
58+
return Self.shared.wrappedValue
59+
}
60+
}
61+
62+
// MARK: - Stylesheet types
63+
64+
extension Stylesheet {
65+
66+
enum Appearance {
67+
case light
68+
case dark
69+
}
70+
71+
enum FontType {
72+
case header
73+
case normal
74+
case small
75+
}
76+
}
77+
78+
// MARK: - Theme default implementation
79+
80+
extension Theme {
81+
82+
/// Default stylesheet for the convenience
83+
static var stylesheet: Observable<Stylesheet> {
84+
return Stylesheet.shared
85+
}
86+
}

Example/ViewController.swift

+62-24
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,81 @@
1-
//
2-
// ViewController.swift
3-
// Example
4-
//
5-
// Created by Łukasz Śliwiński on 30 Apr 2020.
6-
// Copyright © 2020 plum. All rights reserved.
7-
//
1+
// Copyright © 2020 plum. All rights reserved.
82

9-
import UIKit
103
import ObservableThemeKit
4+
import UIKit
115

126
// MARK: - ViewController
137

148
/// The ViewController
159
class ViewController: UIViewController {
1610

1711
// MARK: Properties
18-
19-
/// The Label
20-
lazy var label: UILabel = {
21-
let label = UILabel()
22-
label.text = "🚀\nObservableThemeKit\nExample"
23-
label.font = .systemFont(ofSize: 25, weight: .semibold)
24-
label.numberOfLines = 0
25-
label.lineBreakMode = .byWordWrapping
26-
label.textAlignment = .center
27-
return label
12+
13+
/// Theme
14+
@ObservableTheme var theme: ViewTheme
15+
16+
/// Button
17+
lazy var button: UIButton = {
18+
let button = UIButton()
19+
button.setTitle("Change appearance", for: .normal)
20+
button.addTarget(self, action: #selector(handleButtonTap), for: .touchUpInside)
21+
return button
2822
}()
29-
23+
3024
// MARK: View-Lifecycle
31-
25+
3226
/// View did load
3327
override func viewDidLoad() {
3428
super.viewDidLoad()
35-
self.view.backgroundColor = .white
29+
self.setupAppearance()
30+
31+
self.$theme.observe(
32+
owner: self,
33+
handler: { (owner, _) in
34+
owner.setupAppearance()
35+
}
36+
)
3637
}
37-
38-
/// LoadView
38+
39+
/// Load view
3940
override func loadView() {
40-
self.view = self.label
41+
self.view = self.button
4142
}
4243

44+
// MARK: Helpers
45+
46+
/// Setup appearance
47+
private func setupAppearance() {
48+
self.button.setTitleColor(theme.labelColor, for: .normal)
49+
self.button.titleLabel?.font = theme.buttonFont
50+
self.view.backgroundColor = theme.backgroundColor
51+
}
52+
53+
/// Handle button tap
54+
@objc private func handleButtonTap() {
55+
if Stylesheet.appearance == .light {
56+
Stylesheet.changeAppearance(to: .dark)
57+
} else {
58+
Stylesheet.changeAppearance(to: .light)
59+
}
60+
}
61+
}
62+
63+
// MARK: - ViewTheme
64+
65+
extension ViewController {
66+
67+
/// Theme for the ViewController
68+
struct ViewTheme: Theme {
69+
static let `default`: ViewController.ViewTheme = .init(stylesheet: Self.stylesheet.wrappedValue)
70+
71+
let labelColor: UIColor
72+
let backgroundColor: UIColor
73+
let buttonFont: UIFont
74+
75+
init(stylesheet: Stylesheet) {
76+
self.labelColor = stylesheet.primaryColor
77+
self.backgroundColor = stylesheet.backgroundColor
78+
self.buttonFont = stylesheet.font(with: .header)
79+
}
80+
}
4381
}

0 commit comments

Comments
 (0)