Skip to content

Commit c23da4b

Browse files
committed
ViewController: add more declarative methods
1 parent 8ae44e8 commit c23da4b

File tree

1 file changed

+245
-4
lines changed

1 file changed

+245
-4
lines changed

Classes/Controllers/ViewController.swift

Lines changed: 245 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,27 +100,64 @@ open class ViewController: UIViewController, DeclarativeProtocol, DeclarativePro
100100

101101
open func buildUI() {}
102102

103-
public var isAppearedOnce = false
103+
public var isWillAppearedOnce = false
104+
public var isDidAppearedOnce = false
105+
106+
var _viewDidLoad = {}
107+
var _viewDidLayoutSubviews = {}
108+
var _viewDidAppear: (Bool) -> Void = { _ in }
109+
var _viewDidAppearFirstTime: (Bool) -> Void = { _ in }
110+
var _viewWillAppear: (Bool) -> Void = { _ in }
111+
var _viewWillAppearFirstTime: (Bool) -> Void = { _ in }
112+
var _viewWillDisappear: (Bool) -> Void = { _ in }
113+
var _viewDidDisappear: (Bool) -> Void = { _ in }
114+
115+
open override func viewDidLoad() {
116+
super.viewDidLoad()
117+
_viewDidLoad()
118+
}
119+
120+
open override func viewDidLayoutSubviews() {
121+
super.viewDidLayoutSubviews()
122+
_viewDidLayoutSubviews()
123+
}
104124

105125
open override func viewDidAppear(_ animated: Bool) {
106126
super.viewDidAppear(animated)
107-
if !isAppearedOnce {
108-
isAppearedOnce = true
127+
if !isDidAppearedOnce {
128+
isDidAppearedOnce = true
109129
viewDidAppearFirstTime(animated)
110130
}
131+
_viewDidAppear(animated)
111132
}
112133

113134
open override func viewWillAppear(_ animated: Bool) {
114135
super.viewWillAppear(animated)
115136
isSubscribedToKeyboardNotifications = true
137+
if !isWillAppearedOnce {
138+
isWillAppearedOnce = true
139+
viewWillAppearFirstTime(animated)
140+
}
141+
_viewWillAppear(animated)
116142
}
117143

118144
open override func viewWillDisappear(_ animated: Bool) {
119145
super.viewWillDisappear(animated)
120146
isSubscribedToKeyboardNotifications = false
147+
_viewWillDisappear(animated)
121148
}
122149

123-
open func viewDidAppearFirstTime(_ animated: Bool) {}
150+
open override func viewDidDisappear(_ animated: Bool) {
151+
super.viewDidDisappear(animated)
152+
_viewDidDisappear(animated)
153+
}
154+
155+
open func viewWillAppearFirstTime(_ animated: Bool) {
156+
_viewDidAppearFirstTime(animated)
157+
}
158+
open func viewDidAppearFirstTime(_ animated: Bool) {
159+
_viewWillAppearFirstTime(animated)
160+
}
124161

125162
@State public var keyboardHeight: CGFloat = 0
126163

@@ -184,6 +221,210 @@ open class ViewController: UIViewController, DeclarativeProtocol, DeclarativePro
184221
_statusBarStyle = value
185222
return self
186223
}
224+
225+
@discardableResult
226+
public func navigationItem(_ closure: (Self, UINavigationItem) -> Void) -> Self {
227+
closure(self, navigationItem)
228+
return self
229+
}
230+
231+
@discardableResult
232+
public func navigationController(_ closure: (Self, UINavigationController?) -> Void) -> Self {
233+
closure(self, navigationController)
234+
return self
235+
}
236+
}
237+
238+
// MARK: Lifecycle
239+
240+
extension ViewController {
241+
/// didLoad
242+
@discardableResult
243+
public func onViewDidLoad(_ closure: @escaping () -> Void) -> Self {
244+
_viewDidLoad = closure
245+
return self
246+
}
247+
248+
@discardableResult
249+
public func onViewDidLoad(_ closure: @escaping (Self) -> Void) -> Self {
250+
_viewDidLoad = {
251+
closure(self)
252+
}
253+
return self
254+
}
255+
256+
/// didLayoutSubviews
257+
@discardableResult
258+
public func onViewDidLayoutSubviews(_ closure: @escaping () -> Void) -> Self {
259+
_viewDidLayoutSubviews = closure
260+
return self
261+
}
262+
263+
@discardableResult
264+
public func onViewDidLayoutSubviews(_ closure: @escaping (Self) -> Void) -> Self {
265+
_viewDidLayoutSubviews = {
266+
closure(self)
267+
}
268+
return self
269+
}
270+
271+
/// didAppear
272+
@discardableResult
273+
public func onViewDidAppear(_ closure: @escaping () -> Void) -> Self {
274+
_viewDidAppear = { a in
275+
closure()
276+
}
277+
return self
278+
}
279+
280+
@discardableResult
281+
public func onViewDidAppear(_ closure: @escaping (Self) -> Void) -> Self {
282+
_viewDidAppear = { a in
283+
closure(self)
284+
}
285+
return self
286+
}
287+
288+
@discardableResult
289+
public func onViewDidAppear(_ closure: @escaping (Self, Bool) -> Void) -> Self {
290+
_viewDidAppear = { a in
291+
closure(self, a)
292+
}
293+
return self
294+
}
295+
296+
/// didAppearFirstTime
297+
@discardableResult
298+
public func onViewDidAppearFirstTime(_ closure: @escaping () -> Void) -> Self {
299+
_viewDidAppearFirstTime = { a in
300+
closure()
301+
}
302+
return self
303+
}
304+
305+
@discardableResult
306+
public func onViewDidAppearFirstTime(_ closure: @escaping (Self) -> Void) -> Self {
307+
_viewDidAppearFirstTime = { a in
308+
closure(self)
309+
}
310+
return self
311+
}
312+
313+
@discardableResult
314+
public func onViewDidAppearFirstTime(_ closure: @escaping (Self, Bool) -> Void) -> Self {
315+
_viewDidAppearFirstTime = { a in
316+
closure(self, a)
317+
}
318+
return self
319+
}
320+
321+
/// willAppear
322+
@discardableResult
323+
public func onViewWillAppear(_ closure: @escaping () -> Void) -> Self {
324+
_viewWillAppear = { a in
325+
closure()
326+
}
327+
return self
328+
}
329+
330+
@discardableResult
331+
public func onViewWillAppear(_ closure: @escaping (Self) -> Void) -> Self {
332+
_viewWillAppear = { a in
333+
closure(self)
334+
}
335+
return self
336+
}
337+
338+
@discardableResult
339+
public func onViewWillAppear(_ closure: @escaping (Self, Bool) -> Void) -> Self {
340+
_viewWillAppear = { a in
341+
closure(self, a)
342+
}
343+
return self
344+
}
345+
346+
/// willAppearFirstTime
347+
@discardableResult
348+
public func onViewWillAppearFirstTime(_ closure: @escaping () -> Void) -> Self {
349+
_viewWillAppearFirstTime = { a in
350+
closure()
351+
}
352+
return self
353+
}
354+
355+
@discardableResult
356+
public func onViewWillAppearFirstTime(_ closure: @escaping (Self) -> Void) -> Self {
357+
_viewWillAppearFirstTime = { a in
358+
closure(self)
359+
}
360+
return self
361+
}
362+
363+
@discardableResult
364+
public func onViewWillAppearFirstTime(_ closure: @escaping (Self, Bool) -> Void) -> Self {
365+
_viewWillAppearFirstTime = { a in
366+
closure(self, a)
367+
}
368+
return self
369+
}
370+
371+
/// willDisappear
372+
@discardableResult
373+
public func onViewWillDisappear(_ closure: @escaping () -> Void) -> Self {
374+
_viewWillDisappear = { a in
375+
closure()
376+
}
377+
return self
378+
}
379+
380+
@discardableResult
381+
public func onViewWillDisappear(_ closure: @escaping (Self) -> Void) -> Self {
382+
_viewWillDisappear = { a in
383+
closure(self)
384+
}
385+
return self
386+
}
387+
388+
@discardableResult
389+
public func onViewWillDisappear(_ closure: @escaping (Self, Bool) -> Void) -> Self {
390+
_viewWillDisappear = { a in
391+
closure(self, a)
392+
}
393+
return self
394+
}
395+
396+
/// didDisappear
397+
@discardableResult
398+
public func onViewDidDisappear(_ closure: @escaping () -> Void) -> Self {
399+
_viewDidDisappear = { a in
400+
closure()
401+
}
402+
return self
403+
}
404+
405+
@discardableResult
406+
public func onViewDidDisappear(_ closure: @escaping (Self) -> Void) -> Self {
407+
_viewDidDisappear = { a in
408+
closure(self)
409+
}
410+
return self
411+
}
412+
413+
@discardableResult
414+
public func onViewDidDisappear(_ closure: @escaping (Self, Bool) -> Void) -> Self {
415+
_viewDidDisappear = { a in
416+
closure(self, a)
417+
}
418+
return self
419+
}
420+
}
421+
422+
// MARK: Titleable
423+
424+
extension ViewController: _Titleable {
425+
func _setTitle(_ v: String?) {
426+
title = v
427+
}
187428
}
188429

189430
// MARK: Keyboard Notifications

0 commit comments

Comments
 (0)