-
Notifications
You must be signed in to change notification settings - Fork 4
/
ViewControllerMixinable.swift
122 lines (115 loc) · 4.29 KB
/
ViewControllerMixinable.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
//
// ViewControllerMixinable.swift
// Mixin
//
// Created by one on 30/11/2017.
// Copyright © 2017 one. All rights reserved.
//
import UIKit
@objc public protocol UIViewControllerLifeCycle: class {
@objc optional func loadView()
@objc optional func loadViewIfNeeded()
@objc optional func viewDidLoad()
@objc optional func prepare(for segue: UIStoryboardSegue, sender: Any?)
@objc optional func viewWillAppear(_ animated: Bool)
@objc optional func viewDidAppear(_ animated: Bool)
@objc optional func viewWillDisappear(_ animated: Bool)
@objc optional func viewDidDisappear(_ animated: Bool)
@objc optional func viewWillLayoutSubviews()
@objc optional func viewDidLayoutSubviews()
@objc optional func didReceiveMemoryWarning()
}
public protocol ViewControllerMixinable: class {
typealias LifeCycle = UIViewControllerLifeCycle
var viewControllerLifeCycles: [LifeCycle?] { get }
}
public class MixinableViewControllerLifeCycle<T: ViewControllerMixinable>: NSObject, UIViewControllerLifeCycle {
unowned let mixin: T
init(mixin: T) {
self.mixin = mixin
super.init()
}
}
public class BlockViewControllerLifeCycle: NSObject, UIViewControllerLifeCycle {
typealias LoadView = () -> Void
typealias LoadViewIfNeeded = () -> Void
typealias ViewDidLoad = () -> Void
typealias Prepare = (_ segue: UIStoryboardSegue, _ sender: Any?) -> Void
typealias ViewWillAppear = (_ animated: Bool) -> Void
typealias ViewDidAppear = (_ animated: Bool) -> Void
typealias ViewWillDisappear = (_ animated: Bool) -> Void
typealias ViewDidDisappear = (_ animated: Bool) -> Void
typealias ViewWillLayoutSubviews = () -> Void
typealias ViewDidLayoutSubviews = () -> Void
typealias DidReceiveMemoryWarning = () -> Void
private let _loadView: LoadView?
private let _loadViewIfNeeded: LoadViewIfNeeded?
private let _viewDidLoad: ViewDidLoad?
private let _prepare: Prepare?
private let _viewWillAppear: ViewWillAppear?
private let _viewDidAppear: ViewDidAppear?
private let _viewWillDisappear: ViewWillDisappear?
private let _viewDidDisappear: ViewDidDisappear?
private let _viewWillLayoutSubviews: ViewWillLayoutSubviews?
private let _viewDidLayoutSubviews: ViewDidLayoutSubviews?
private let _didReceiveMemoryWarning: DidReceiveMemoryWarning?
init(
loadView: LoadView? = nil,
loadViewIfNeeded: LoadViewIfNeeded? = nil,
viewDidLoad: ViewDidLoad? = nil,
prepare: Prepare? = nil,
viewWillAppear: ViewWillAppear? = nil,
viewDidAppear: ViewDidAppear? = nil,
viewWillDisappear: ViewWillDisappear? = nil,
viewDidDisappear: ViewDidDisappear? = nil,
viewWillLayoutSubviews: ViewWillLayoutSubviews? = nil,
viewDidLayoutSubviews: ViewDidLayoutSubviews? = nil,
didReceiveMemoryWarning: DidReceiveMemoryWarning? = nil
) {
_loadView = loadView
_loadViewIfNeeded = loadViewIfNeeded
_viewDidLoad = viewDidLoad
_prepare = prepare
_viewWillAppear = viewWillAppear
_viewDidAppear = viewDidAppear
_viewWillDisappear = viewWillDisappear
_viewDidDisappear = viewDidDisappear
_viewWillLayoutSubviews = viewWillLayoutSubviews
_viewDidLayoutSubviews = viewDidLayoutSubviews
_didReceiveMemoryWarning = didReceiveMemoryWarning
super.init()
}
public func loadView() {
_loadView?()
}
public func loadViewIfNeeded() {
_loadViewIfNeeded?()
}
public func viewDidLoad() {
_viewDidLoad?()
}
public func prepare(for segue: UIStoryboardSegue, sender: Any?) {
_prepare?(segue, sender)
}
public func viewWillAppear(_ animated: Bool) {
_viewWillAppear?(animated)
}
public func viewDidAppear(_ animated: Bool) {
_viewDidAppear?(animated)
}
public func viewWillDisappear(_ animated: Bool) {
_viewWillDisappear?(animated)
}
public func viewDidDisappear(_ animated: Bool) {
_viewDidDisappear?(animated)
}
public func viewWillLayoutSubviews() {
_viewWillLayoutSubviews?()
}
public func viewDidLayoutSubviews() {
_viewDidLayoutSubviews?()
}
public func didReceiveMemoryWarning() {
_didReceiveMemoryWarning?()
}
}