6
6
import UIKit
7
7
import PlaygroundSupport
8
8
9
- public class BaseViewController : UIViewController , PlaygroundLiveViewSafeAreaContainer ,
10
- ImagePickerViewControllerDelegate , ToolBarButtonViewDelegate {
11
-
12
- @IBOutlet weak var backgroundImageView : UIImageView !
13
- @IBOutlet weak var showcaseImageCoontainerView : UIView !
9
+ public class BaseViewController : UIViewController ,
10
+ PlaygroundLiveViewSafeAreaContainer ,
11
+ ImagePickerViewControllerDelegate ,
12
+ ToolBarButtonViewDelegate ,
13
+ PlaygroundLiveViewMessageHandler {
14
+
15
+ @IBOutlet private weak var backgroundImageView : UIImageView !
16
+ @IBOutlet private weak var showcaseImageContainerView : UIView !
14
17
@IBOutlet weak var showcaseImageView : ShowcaseImageView !
15
-
16
- @IBOutlet weak var toolBarContainerView : UIView !
18
+
19
+ @IBOutlet private weak var toolBarContainerView : UIView !
20
+
21
+ private weak var loadingIndicatorView : UIView !
17
22
18
23
var sourceImage : UIImage ?
19
24
20
25
var imagePickerController : ImagePickerViewController !
21
-
26
+
27
+ var editorConnected = false
28
+
22
29
public override func viewDidLoad( ) {
23
30
super. viewDidLoad ( )
24
31
32
+ setupLogoImageView ( )
33
+ setupToolBarBlurView ( )
34
+ setupProcessingIndicator ( )
35
+
25
36
backgroundImageView. contentMode = . scaleAspectFill
26
37
backgroundImageView. image = UIImage ( named: " background-v2 " )
27
38
28
39
showcaseImageView. cornerRadius = 8.0
29
40
30
- setupLogoImageView ( )
31
- setupToolBarBlurView ( )
41
+ setLoadingIndicatorHidden ( true , animated: false )
32
42
}
33
43
34
44
public override func prepare( for segue: UIStoryboardSegue , sender: Any ? ) {
@@ -46,24 +56,24 @@ public class BaseViewController: UIViewController, PlaygroundLiveViewSafeAreaCon
46
56
}
47
57
}
48
58
49
- func setupLogoImageView( ) {
59
+ private func setupLogoImageView( ) {
50
60
let logoImageView = UIImageView ( )
51
61
logoImageView. image = UIImage ( named: " logo " )
52
62
logoImageView. contentMode = . scaleAspectFit
53
-
63
+
54
64
logoImageView. translatesAutoresizingMaskIntoConstraints = false
55
- self . showcaseImageCoontainerView . insertSubview ( logoImageView, belowSubview: showcaseImageView)
65
+ self . showcaseImageContainerView . insertSubview ( logoImageView, belowSubview: showcaseImageView)
56
66
57
- logoImageView. centerXAnchor. constraint ( equalTo: showcaseImageCoontainerView . centerXAnchor) . isActive = true
58
- logoImageView. centerYAnchor. constraint ( equalTo: showcaseImageCoontainerView . centerYAnchor) . isActive = true
67
+ logoImageView. centerXAnchor. constraint ( equalTo: showcaseImageContainerView . centerXAnchor) . isActive = true
68
+ logoImageView. centerYAnchor. constraint ( equalTo: showcaseImageContainerView . centerYAnchor) . isActive = true
59
69
logoImageView. widthAnchor. constraint ( equalToConstant: 420 ) . isActive = true
60
- logoImageView. heightAnchor. constraint ( equalTo: showcaseImageCoontainerView . heightAnchor) . isActive = true
70
+ logoImageView. heightAnchor. constraint ( equalTo: showcaseImageContainerView . heightAnchor) . isActive = true
61
71
}
62
-
63
- func setupToolBarBlurView( ) {
72
+
73
+ private func setupToolBarBlurView( ) {
64
74
let blurEffect = UIBlurEffect ( style: . dark)
65
75
let blurView = UIVisualEffectView ( effect: blurEffect)
66
-
76
+
67
77
blurView. translatesAutoresizingMaskIntoConstraints = false
68
78
self . view. insertSubview ( blurView, aboveSubview: self . backgroundImageView)
69
79
@@ -72,11 +82,72 @@ public class BaseViewController: UIViewController, PlaygroundLiveViewSafeAreaCon
72
82
blurView. topAnchor. constraint ( equalTo: self . toolBarContainerView. topAnchor, constant: - 12 ) . isActive = true
73
83
blurView. bottomAnchor. constraint ( equalTo: self . view. bottomAnchor) . isActive = true
74
84
}
75
-
85
+
86
+ private func setupProcessingIndicator( ) {
87
+ let indicatorBackgroundView = UIView ( )
88
+
89
+ indicatorBackgroundView. backgroundColor = UIColor . black. withAlphaComponent ( 0.6 )
90
+ indicatorBackgroundView. translatesAutoresizingMaskIntoConstraints = false
91
+
92
+ self . showcaseImageContainerView. addSubview ( indicatorBackgroundView)
93
+
94
+ indicatorBackgroundView. topAnchor. constraint ( equalTo: self . view. topAnchor) . isActive = true
95
+ indicatorBackgroundView. leadingAnchor. constraint ( equalTo: self . view. leadingAnchor) . isActive = true
96
+ indicatorBackgroundView. trailingAnchor. constraint ( equalTo: self . view. trailingAnchor) . isActive = true
97
+ indicatorBackgroundView. bottomAnchor. constraint ( equalTo: self . view. bottomAnchor) . isActive = true
98
+
99
+ let indicatorContainerView = UIView ( )
100
+
101
+ indicatorContainerView. backgroundColor = . clear
102
+ indicatorContainerView. translatesAutoresizingMaskIntoConstraints = false
103
+ indicatorBackgroundView. addSubview ( indicatorContainerView)
104
+
105
+ indicatorContainerView. topAnchor. constraint ( equalTo: indicatorBackgroundView. topAnchor) . isActive = true
106
+ indicatorContainerView. leadingAnchor. constraint ( equalTo: indicatorBackgroundView. leadingAnchor) . isActive = true
107
+ indicatorContainerView. trailingAnchor. constraint ( equalTo: indicatorBackgroundView. trailingAnchor) . isActive = true
108
+ indicatorContainerView. bottomAnchor. constraint ( equalTo: showcaseImageContainerView. bottomAnchor) . isActive = true
109
+
110
+ let progressIndicator = UIActivityIndicatorView ( style: . whiteLarge)
111
+ progressIndicator. translatesAutoresizingMaskIntoConstraints = false
112
+ indicatorContainerView. addSubview ( progressIndicator)
113
+ progressIndicator. centerXAnchor. constraint ( equalTo: indicatorContainerView. centerXAnchor) . isActive = true
114
+ progressIndicator. centerYAnchor. constraint ( equalTo: indicatorContainerView. centerYAnchor) . isActive = true
115
+
116
+ progressIndicator. startAnimating ( )
117
+
118
+ self . loadingIndicatorView = indicatorBackgroundView
119
+ }
120
+
76
121
func updateShowcaseImage( image: UIImage ) {
77
122
showcaseImageView. image = image
78
123
}
79
124
125
+ private func setLoadingIndicatorHidden( _ hidden: Bool , animated: Bool ) {
126
+ if loadingIndicatorView. isHidden == hidden {
127
+ return
128
+ }
129
+ if animated {
130
+ if loadingIndicatorView. isHidden && !hidden {
131
+ loadingIndicatorView. alpha = 0.0
132
+ loadingIndicatorView. isHidden = false
133
+ }
134
+ UIView . animate ( withDuration: 0.25 , delay: hidden ? 0.25 : 0 , animations: {
135
+ self . loadingIndicatorView. alpha = hidden ? 0.0 : 1.0
136
+ } , completion: { ( complete) in
137
+ self . loadingIndicatorView. isHidden = hidden
138
+ } )
139
+ } else {
140
+ loadingIndicatorView. isHidden = hidden
141
+ }
142
+ }
143
+
144
+ func send( _ message: PlaygroundSupport . PlaygroundValue , showLoadingView: Bool ) {
145
+ if editorConnected && showLoadingView {
146
+ setLoadingIndicatorHidden ( false , animated: true )
147
+ }
148
+ send ( message)
149
+ }
150
+
80
151
func didSelectImage( image: UIImage , pickerController: ImagePickerViewController ) {
81
152
sourceImage = image
82
153
updateShowcaseImage ( image: image)
@@ -86,4 +157,28 @@ public class BaseViewController: UIViewController, PlaygroundLiveViewSafeAreaCon
86
157
87
158
}
88
159
160
+ public func liveViewMessageConnectionOpened( ) {
161
+ editorConnected = true
162
+ }
163
+
164
+ public func liveViewMessageConnectionClosed( ) {
165
+ editorConnected = false
166
+ setLoadingIndicatorHidden ( true , animated: true )
167
+ }
168
+
169
+ public func receive( _ message: PlaygroundValue ) {
170
+ guard let message = EventMessage . from ( playgroundValue: message) else {
171
+ return
172
+ }
173
+ switch message {
174
+ case . imageProcessingResponse( let image) :
175
+ setLoadingIndicatorHidden ( true , animated: true )
176
+ if let image = image {
177
+ self . updateShowcaseImage ( image: image)
178
+ }
179
+ default :
180
+ break
181
+ }
182
+ }
183
+
89
184
}
0 commit comments