@@ -42,10 +42,10 @@ class IterableHtmlMessageViewController: UIViewController {
42
42
. map { $0. shouldAnimate } ?? false
43
43
}
44
44
45
- var backgroundColor : UIColor {
45
+ var backgroundColor : UIColor ? {
46
46
messageMetadata
47
47
. flatMap { $0. message. content as? IterableHtmlInAppContent }
48
- . map { $0. backgroundColor } ?? IterableHtmlInAppContent . defaultBackgroundColor ( )
48
+ . flatMap { $0. backgroundColor }
49
49
}
50
50
51
51
var location : IterableMessageLocation {
@@ -85,15 +85,7 @@ class IterableHtmlMessageViewController: UIViewController {
85
85
86
86
location = parameters. location
87
87
88
- if parameters. isModal {
89
- view. backgroundColor = UIColor . clear
90
- } else {
91
- if #available( iOS 14 , * ) {
92
- view. backgroundColor = UIColor . systemBackground
93
- } else {
94
- view. backgroundColor = UIColor . white
95
- }
96
- }
88
+ view. backgroundColor = InAppCalculations . initialViewBackgroundColor ( isModal: parameters. isModal)
97
89
98
90
webView. set ( position: ViewPosition ( width: view. frame. width, height: view. frame. height, center: view. center) )
99
91
webView. loadHTMLString ( parameters. html, baseURL: URL ( string: " " ) )
@@ -119,8 +111,8 @@ class IterableHtmlMessageViewController: UIViewController {
119
111
120
112
override func viewDidLayoutSubviews( ) {
121
113
super. viewDidLayoutSubviews ( )
122
-
123
- resizeWebView ( )
114
+
115
+ resizeWebView ( animate : false )
124
116
}
125
117
126
118
override open func viewWillDisappear( _ animated: Bool ) {
@@ -174,23 +166,8 @@ class IterableHtmlMessageViewController: UIViewController {
174
166
return webView as WebViewProtocol
175
167
}
176
168
177
- private static func trackClickOnDismiss( internalAPI: IterableAPIInternal ? ,
178
- params: Parameters ,
179
- futureClickedURL: Promise < URL , IterableError > ,
180
- withURL url: URL ,
181
- andDestinationURL destinationURL: String ) {
182
- ITBInfo ( )
183
- futureClickedURL. resolve ( with: url)
184
- if let messageMetadata = params. messageMetadata {
185
- internalAPI? . trackInAppClick ( messageMetadata. message,
186
- location: messageMetadata. location,
187
- inboxSessionId: params. inboxSessionId,
188
- clickedUrl: destinationURL)
189
- }
190
- }
191
-
192
169
/// Resizes the webview based upon the insetPadding, height etc
193
- private func resizeWebView( ) {
170
+ private func resizeWebView( animate : Bool ) {
194
171
let parentPosition = ViewPosition ( width: view. bounds. width,
195
172
height: view. bounds. height,
196
173
center: view. center)
@@ -201,52 +178,48 @@ class IterableHtmlMessageViewController: UIViewController {
201
178
paddingRight: parameters. padding. right,
202
179
location: location)
203
180
. onSuccess { [ weak self] position in
204
- if self ? . parameters . isModal == true && self ? . parameters . shouldAnimate == true {
205
- self ? . animateWhileEntering ( position: position )
181
+ if animate {
182
+ self ? . animateWhileEntering ( position)
206
183
} else {
207
184
self ? . webView. set ( position: position)
208
185
}
209
186
}
210
187
}
211
188
212
- private func animateWhileEntering( position: ViewPosition ) {
213
- Self . animate ( duration: parameters. animationDuration) { [ weak self] in
214
- self ? . setInitialValuesForAnimation ( position: position)
215
- } finalValues: { [ weak self] in
216
- self ? . setFinalValuesForAnimation ( position: position)
217
- }
189
+ private func animateWhileEntering( _ position: ViewPosition ) {
190
+ ITBInfo ( )
191
+ createAnimationDetail ( withPosition: position) . map { applyAnimation ( animationDetail: $0) } ?? ( webView. set ( position: position) )
218
192
}
219
-
220
- private func setInitialValuesForAnimation( position: ViewPosition ) {
221
- view. backgroundColor = UIColor . clear
222
- let startPosition = InAppCalculations . calculateAnimationStartPosition ( for: position,
223
- location: location,
224
- safeAreaInsets: InAppCalculations . safeAreaInsets ( for: view) )
225
- let startAlpha = InAppCalculations . calculateAnimationStartAlpha ( location: location)
226
- webView. set ( position: startPosition)
227
- webView. view. alpha = startAlpha
193
+
194
+ private func animateWhileLeaving( _ position: ViewPosition ) {
195
+ let animation = createAnimationDetail ( withPosition: position) . map ( InAppCalculations . swapAnimation ( animationDetail: ) )
196
+ let dismisser = InAppCalculations . createDismisser ( for: self ,
197
+ isModal: parameters. isModal,
198
+ isInboxMessage: parameters. messageMetadata? . location == . inbox)
199
+ animation. map { applyAnimation ( animationDetail: $0, completion: dismisser) } ?? ( dismisser ( ) )
228
200
}
229
-
230
- private func setFinalValuesForAnimation( position: ViewPosition ) {
231
- view. backgroundColor = parameters. backgroundColor
232
- webView. set ( position: position)
233
- webView. view. alpha = 1.0
201
+
202
+ private func createAnimationDetail( withPosition position: ViewPosition ) -> InAppCalculations . AnimationDetail ? {
203
+ let input = InAppCalculations . AnimationInput ( position: position,
204
+ isModal: parameters. isModal,
205
+ shouldAnimate: parameters. shouldAnimate,
206
+ location: location,
207
+ safeAreaInsets: InAppCalculations . safeAreaInsets ( for: view) ,
208
+ backgroundColor: parameters. backgroundColor)
209
+ return InAppCalculations . calculateAnimationDetail ( animationInput: input)
234
210
}
235
211
236
- private func animateWhileLeaving( position: ViewPosition , url: URL , destinationUrl: String ) {
237
- ITBInfo ( )
212
+ private func applyAnimation( animationDetail: InAppCalculations . AnimationDetail , completion: ( ( ) -> Void ) ? = nil ) {
238
213
Self . animate ( duration: parameters. animationDuration) { [ weak self] in
239
- self ? . setFinalValuesForAnimation ( position: position)
214
+ self ? . webView. set ( position: animationDetail. initial. position)
215
+ self ? . webView. view. alpha = animationDetail. initial. alpha
216
+ self ? . view. backgroundColor = animationDetail. initial. bgColor
240
217
} finalValues: { [ weak self] in
241
- self ? . setInitialValuesForAnimation ( position: position)
242
- } completion: { [ weak self, weak internalApi = internalAPI, futureClickedURL = futureClickedURL, parameters = parameters] in
243
- self ? . dismiss ( animated: false , completion: {
244
- Self . trackClickOnDismiss ( internalAPI: internalApi,
245
- params: parameters,
246
- futureClickedURL: futureClickedURL,
247
- withURL: url,
248
- andDestinationURL: destinationUrl)
249
- } )
218
+ self ? . webView. set ( position: animationDetail. final. position)
219
+ self ? . webView. view. alpha = animationDetail. final. alpha
220
+ self ? . view. backgroundColor = animationDetail. final. bgColor
221
+ } completion: {
222
+ completion ? ( )
250
223
}
251
224
}
252
225
@@ -288,7 +261,7 @@ class IterableHtmlMessageViewController: UIViewController {
288
261
extension IterableHtmlMessageViewController : WKNavigationDelegate {
289
262
func webView( _: WKWebView , didFinish _: WKNavigation ! ) {
290
263
ITBInfo ( )
291
- resizeWebView ( )
264
+ resizeWebView ( animate : true )
292
265
presenter? . webViewDidFinish ( )
293
266
}
294
267
@@ -312,30 +285,31 @@ extension IterableHtmlMessageViewController: WKNavigationDelegate {
312
285
313
286
linkClicked = true
314
287
clickedLink = destinationUrl
315
-
316
- if parameters. isModal {
317
- if parameters. shouldAnimate {
318
- animateWhileLeaving ( position: webView. position, url: url, destinationUrl: destinationUrl)
319
- } else {
320
- let animated = parameters. messageMetadata? . location == . inbox
321
- dismiss ( animated: animated) { [ weak internalAPI, futureClickedURL, parameters, url, destinationUrl] in
322
- Self . trackClickOnDismiss ( internalAPI: internalAPI,
323
- params: parameters,
324
- futureClickedURL: futureClickedURL,
325
- withURL: url,
326
- andDestinationURL: destinationUrl)
327
- }
328
- }
329
- } else {
330
- Self . trackClickOnDismiss ( internalAPI: internalAPI,
331
- params: parameters,
332
- futureClickedURL: futureClickedURL,
333
- withURL: url,
334
- andDestinationURL: destinationUrl)
335
-
336
- navigationController? . popViewController ( animated: true )
337
- }
338
-
288
+
289
+ Self . trackClickOnDismiss ( internalAPI: internalAPI,
290
+ params: parameters,
291
+ futureClickedURL: futureClickedURL,
292
+ withURL: url,
293
+ andDestinationURL: destinationUrl)
294
+
295
+ animateWhileLeaving ( webView. position)
296
+
339
297
decisionHandler ( . cancel)
340
298
}
299
+
300
+ private static func trackClickOnDismiss( internalAPI: IterableAPIInternal ? ,
301
+ params: Parameters ,
302
+ futureClickedURL: Promise < URL , IterableError > ,
303
+ withURL url: URL ,
304
+ andDestinationURL destinationURL: String ) {
305
+ ITBInfo ( )
306
+ futureClickedURL. resolve ( with: url)
307
+ if let messageMetadata = params. messageMetadata {
308
+ internalAPI? . trackInAppClick ( messageMetadata. message,
309
+ location: messageMetadata. location,
310
+ inboxSessionId: params. inboxSessionId,
311
+ clickedUrl: destinationURL)
312
+ }
313
+ }
314
+
341
315
}
0 commit comments