Skip to content

Commit 5bb8fd3

Browse files
committed
Make view controller declarative
1 parent a3e5820 commit 5bb8fd3

File tree

5 files changed

+432
-11
lines changed

5 files changed

+432
-11
lines changed

Classes/Controllers/FormViewController.swift

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,9 @@ open class FormViewController: ViewController {
44
public lazy var scrollView = ScrollView().edgesToSuperview(top: 0, leading: 0, trailing: 0).bottomToSuperview($bottom)
55
public lazy var stackView = VStack().edgesToSuperview().width(to: .width, of: scrollView)
66

7-
@State
8-
var bottom: CGFloat = 0
9-
107
open override func buildUI() {
118
super.buildUI()
12-
view.body { scrollView }
9+
_view.body { scrollView }
1310
scrollView.body { stackView }
1411
$keyboardHeight.listen { height in
1512
self.bottom = -1 * height

Classes/Controllers/ViewController.swift

Lines changed: 75 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import Foundation
22
import UIKit
33

4-
open class ViewController: UIViewController {
4+
open class ViewController: UIViewController, DeclarativeProtocol, DeclarativeProtocolInternal, _Toucheable {
55
open override var preferredStatusBarStyle: UIStatusBarStyle { statusBarStyle.rawValue }
66
/// UIKitPlus reimplementation of `preferredStatusBarStyle`
7-
open var statusBarStyle: StatusBarStyle { .default }
8-
9-
@State public var keyboardHeight: CGFloat = 0
7+
open var statusBarStyle: StatusBarStyle { _statusBarStyle ?? .default }
108

119
public init (@ViewBuilder block: ViewBuilder.SingleView) {
1210
super.init(nibName: nil, bundle: nil)
@@ -35,10 +33,71 @@ open class ViewController: UIViewController {
3533
NotificationCenter.default.removeObserver(self)
3634
}
3735

38-
open func buildUI() {
39-
view.backgroundColor = .white
36+
// MARK: DeclarativeProtocol
37+
38+
public var declarativeView: View { _view }
39+
public lazy var properties = Properties<View>()
40+
lazy var _properties = PropertiesInternal()
41+
42+
@State public var height: CGFloat = 0
43+
@State public var width: CGFloat = 0
44+
@State public var top: CGFloat = 0
45+
@State public var leading: CGFloat = 0
46+
@State public var left: CGFloat = 0
47+
@State public var trailing: CGFloat = 0
48+
@State public var right: CGFloat = 0
49+
@State public var bottom: CGFloat = 0
50+
@State public var centerX: CGFloat = 0
51+
@State public var centerY: CGFloat = 0
52+
53+
var __height: State<CGFloat> { _height }
54+
var __width: State<CGFloat> { _width }
55+
var __top: State<CGFloat> { _top }
56+
var __leading: State<CGFloat> { _leading }
57+
var __left: State<CGFloat> { _left }
58+
var __trailing: State<CGFloat> { _trailing }
59+
var __right: State<CGFloat> { _right }
60+
var __bottom: State<CGFloat> { _bottom }
61+
var __centerX: State<CGFloat> { _centerX }
62+
var __centerY: State<CGFloat> { _centerY }
63+
64+
lazy var _view = View().background(.white).edgesToSuperview()
65+
66+
open override func loadView() {
67+
view = _view
68+
}
69+
70+
// MARK: Toucheable
71+
72+
var _touchesBegan: TouchClosure?
73+
var _touchesMoved: TouchClosure?
74+
var _touchesEnded: TouchClosure?
75+
var _touchesCancelled: TouchClosure?
76+
77+
override open func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
78+
super.touchesBegan(touches, with: event)
79+
_touchesBegan?(touches, event)
4080
}
4181

82+
open override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
83+
super.touchesMoved(touches, with: event)
84+
_touchesMoved?(touches, event)
85+
}
86+
87+
override open func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
88+
super.touchesEnded(touches, with: event)
89+
_touchesEnded?(touches, event)
90+
}
91+
92+
open override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
93+
super.touchesCancelled(touches, with: event)
94+
_touchesCancelled?(touches, event)
95+
}
96+
97+
// MARK: Lifecycle
98+
99+
open func buildUI() {}
100+
42101
public var isAppearedOnce = false
43102

44103
open override func viewDidAppear(_ animated: Bool) {
@@ -61,6 +120,8 @@ open class ViewController: UIViewController {
61120

62121
open func viewDidAppearFirstTime(_ animated: Bool) {}
63122

123+
@State public var keyboardHeight: CGFloat = 0
124+
64125
private var isSubscribedToKeyboardNotifications = false
65126

66127
private func subscribeToKeyboardNotifications() {
@@ -113,6 +174,14 @@ open class ViewController: UIViewController {
113174
}
114175
return self
115176
}
177+
178+
var _statusBarStyle: StatusBarStyle?
179+
180+
@discardableResult
181+
public func statusBarStyle(_ value: StatusBarStyle) -> Self {
182+
_statusBarStyle = value
183+
return self
184+
}
116185
}
117186

118187
// MARK: Keyboard Notifications

Classes/Extensions/UIViewController+Body.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ import UIKit
22

33
extension UIViewController {
44
open func body(@ViewBuilder block: ViewBuilder.SingleView) {
5-
view.body { block().viewBuilderItems }
5+
if let s = self as? ViewController {
6+
s._view.body { block().viewBuilderItems }
7+
} else {
8+
view.body { block().viewBuilderItems }
9+
}
610
}
711
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import UIKit
2+
3+
public protocol BackgroundColorable {
4+
@discardableResult
5+
func background(_ color: UIColor) -> Self
6+
7+
@discardableResult
8+
func background(_ number: Int) -> Self
9+
10+
@discardableResult
11+
func background(_ state: State<UIColor>) -> Self
12+
13+
@discardableResult
14+
func background<V>(_ expressable: ExpressableState<V, UIColor>) -> Self
15+
16+
@discardableResult
17+
func background(_ identifier: FontIdentifier, _ size: CGFloat) -> Self
18+
}
19+
20+
protocol _BackgroundColorable: BackgroundColorable {
21+
func _setBackgroundColor(_ v: UIColor)
22+
}
23+
24+
extension BackgroundColorable {
25+
@discardableResult
26+
public func background(_ number: Int) -> Self {
27+
background(number.color)
28+
}
29+
30+
@discardableResult
31+
public func background(_ state: State<UIColor>) -> Self {
32+
background(state.wrappedValue)
33+
state.listen {
34+
self.background($0)
35+
}
36+
return self
37+
}
38+
39+
@discardableResult
40+
public func background<V>(_ expressable: ExpressableState<V, UIColor>) -> Self {
41+
background(expressable.value())
42+
expressable.state.listen {
43+
self.background(expressable.value())
44+
}
45+
return self
46+
}
47+
}
48+
49+
@available(iOS 13.0, *)
50+
extension BackgroundColorable {
51+
@discardableResult
52+
public func background(_ color: UIColor) -> Self {
53+
guard let s = self as? _BackgroundColorable else { return self }
54+
s._setBackgroundColor(color)
55+
return self
56+
}
57+
}
58+
59+
// for iOS lower than 13
60+
extension _BackgroundColorable {
61+
@discardableResult
62+
public func background(_ color: UIColor) -> Self {
63+
_setBackgroundColor(color)
64+
return self
65+
}
66+
}

0 commit comments

Comments
 (0)