8
8
import Foundation
9
9
import UIKit
10
10
11
+ typealias PaddingParser = HtmlContentParser . InAppDisplaySettingsParser . PaddingParser
12
+ typealias Padding = PaddingParser . Padding
13
+
11
14
enum InAppContentParseResult {
12
15
case success( content: IterableInAppContent )
13
16
case failure( reason: String )
@@ -41,7 +44,7 @@ private protocol ContentFromJsonParser {
41
44
}
42
45
43
46
struct HtmlContentParser {
44
- static func getPadding( fromInAppSettings settings: [ AnyHashable : Any ] ? ) -> UIEdgeInsets {
47
+ static func getPadding( fromInAppSettings settings: [ AnyHashable : Any ] ? ) -> Padding {
45
48
InAppDisplaySettingsParser . PaddingParser. getPadding ( fromInAppSettings: settings)
46
49
}
47
50
@@ -101,6 +104,65 @@ struct HtmlContentParser {
101
104
case bottom
102
105
}
103
106
107
+ enum PaddingValue : Equatable {
108
+ case percent( value: Int )
109
+ case autoExpand
110
+
111
+ func toCGFloat( ) -> CGFloat {
112
+ switch self {
113
+ case . percent( value: let value) :
114
+ return CGFloat ( value)
115
+ case . autoExpand:
116
+ return CGFloat ( - 1 )
117
+ }
118
+ }
119
+
120
+ static func from( cgFloat: CGFloat ) -> PaddingValue {
121
+ switch cgFloat {
122
+ case - 1 :
123
+ return . autoExpand
124
+ default :
125
+ return . percent( value: Int ( cgFloat) )
126
+ }
127
+ }
128
+ }
129
+
130
+ struct Padding : Equatable {
131
+ static let zero = Padding ( top: . percent( value: 0 ) ,
132
+ left: 0 ,
133
+ bottom: . percent( value: 0 ) ,
134
+ right: 0 )
135
+ let top : PaddingValue
136
+ let left : Int
137
+ let bottom : PaddingValue
138
+ let right : Int
139
+
140
+ func adjusted( ) -> Padding {
141
+ if left + right >= 100 {
142
+ return Padding ( top: top,
143
+ left: 0 ,
144
+ bottom: bottom,
145
+ right: 0 )
146
+ } else {
147
+ return self
148
+ }
149
+ }
150
+
151
+ func toEdgeInsets( ) -> UIEdgeInsets {
152
+ UIEdgeInsets ( top: top. toCGFloat ( ) ,
153
+ left: CGFloat ( left) ,
154
+ bottom: bottom. toCGFloat ( ) ,
155
+ right: CGFloat ( right) )
156
+ }
157
+
158
+ static func from( edgeInsets: UIEdgeInsets ) -> Padding {
159
+ Padding ( top: PaddingValue . from ( cgFloat: edgeInsets. top) ,
160
+ left: Int ( edgeInsets. left) ,
161
+ bottom: PaddingValue . from ( cgFloat: edgeInsets. bottom) ,
162
+ right: Int ( edgeInsets. right) )
163
+ }
164
+ }
165
+
104
166
enum PaddingKey : String , JsonKeyRepresentable {
105
167
var jsonKey : String {
106
168
rawValue
@@ -114,50 +176,75 @@ struct HtmlContentParser {
114
176
115
177
/// `settings` json looks like the following
116
178
/// {"bottom": {"displayOption": "AutoExpand"}, "left": {"percentage": 60}, "right": {"percentage": 60}, "top": {"displayOption": "AutoExpand"}}
117
- static func getPadding( fromInAppSettings settings: [ AnyHashable : Any ] ? ) -> UIEdgeInsets {
118
- UIEdgeInsets ( top: getEdgePadding ( fromInAppSettings: settings, edge: . top) ,
119
- left: getEdgePadding ( fromInAppSettings: settings, edge: . left) ,
120
- bottom: getEdgePadding ( fromInAppSettings: settings, edge: . bottom) ,
121
- right: getEdgePadding ( fromInAppSettings: settings, edge: . right) )
179
+ static func getPadding( fromInAppSettings settings: [ AnyHashable : Any ] ? ) -> Padding {
180
+ Padding ( top: getEdgePaddingValue ( fromInAppSettings: settings, edge: . top) ,
181
+ left: getEdgePadding ( fromInAppSettings: settings, edge: . left) ,
182
+ bottom: getEdgePaddingValue ( fromInAppSettings: settings, edge: . bottom) ,
183
+ right: getEdgePadding ( fromInAppSettings: settings, edge: . right) )
122
184
}
123
185
124
186
/// json comes in as
125
187
/// `{"displayOption": "AutoExpand"}`
126
188
/// or `{"percentage": 60}`
127
- /// returns `-1` for `AutoExpand` type
128
- static func decodePadding( _ value: Any ? ) -> Int {
189
+ static func decodePaddingValue( _ value: Any ? ) -> PaddingValue {
129
190
guard let dict = value as? [ AnyHashable : Any ] else {
130
- return 0
191
+ return . percent ( value : 0 )
131
192
}
132
193
133
194
if let displayOption = dict. getValue ( for: PaddingKey . displayOption) as? String , displayOption == Self . displayOptionAutoExpand {
134
- return - 1
195
+ return . autoExpand
135
196
} else {
136
197
if let percentage = dict. getValue ( for: PaddingKey . percentage) as? NSNumber {
137
- return percentage . intValue
198
+ return . percent ( value : Int ( truncating : percentage ) )
138
199
}
139
200
201
+ return . percent( value: 0 )
202
+ }
203
+ }
204
+
205
+ /// json comes in as
206
+ /// `{"percentage": 60}`
207
+ static func decodePadding( _ value: Any ? ) -> Int {
208
+ guard let dict = value as? [ AnyHashable : Any ] else {
140
209
return 0
141
210
}
211
+
212
+ if let percentage = dict. getValue ( for: PaddingKey . percentage) as? NSNumber {
213
+ return Int ( truncating: percentage)
214
+ }
215
+
216
+ return 0
142
217
}
143
-
144
- static func location( fromPadding padding: UIEdgeInsets ) -> IterableMessageLocation {
145
- if padding. top == 0 , padding. bottom == 0 {
218
+
219
+ static func location( fromPadding padding: Padding ) -> IterableMessageLocation {
220
+ if case . percent( let topPadding) = padding. top,
221
+ case . percent( let bottomPadding) = padding. bottom,
222
+ topPadding == 0 ,
223
+ bottomPadding == 0 {
146
224
return . full
147
- } else if padding. top == 0 , padding. bottom < 0 {
225
+ } else if case . autoExpand = padding. bottom,
226
+ case . percent( let topPadding) = padding. top,
227
+ topPadding == 0 {
148
228
return . top
149
- } else if padding. top < 0 , padding. bottom == 0 {
229
+ } else if case . autoExpand = padding. top,
230
+ case . percent( let bottomPadding) = padding. bottom,
231
+ bottomPadding == 0 {
150
232
return . bottom
151
233
} else {
152
234
return . center
153
235
}
154
236
}
155
-
237
+
238
+ private static func getEdgePaddingValue( fromInAppSettings settings: [ AnyHashable : Any ] ? ,
239
+ edge: PaddingEdge ) -> PaddingValue {
240
+ settings? . getValue ( for: edge)
241
+ . map ( decodePaddingValue ( _: ) ) ?? . percent( value: 0 )
242
+ }
243
+
156
244
private static func getEdgePadding( fromInAppSettings settings: [ AnyHashable : Any ] ? ,
157
- edge: PaddingEdge ) -> CGFloat {
245
+ edge: PaddingEdge ) -> Int {
158
246
settings? . getValue ( for: edge)
159
- . map ( decodePadding ( _: ) )
160
- . map { CGFloat ( $0) } ?? . zero
247
+ . map ( decodePadding ( _: ) ) ?? 0
161
248
}
162
249
}
163
250
}
@@ -174,12 +261,12 @@ extension HtmlContentParser: ContentFromJsonParser {
174
261
}
175
262
176
263
let inAppDisplaySettings = json [ JsonKey . InApp. inAppDisplaySettings] as? [ AnyHashable : Any ]
177
- let edgeInsets = getPadding ( fromInAppSettings: inAppDisplaySettings)
264
+ let padding = getPadding ( fromInAppSettings: inAppDisplaySettings)
178
265
179
266
let shouldAnimate = inAppDisplaySettings. map ( Self . parseShouldAnimate ( fromInAppSettings: ) ) ?? false
180
267
let backgroundColor = inAppDisplaySettings. flatMap ( Self . parseBackgroundColor ( fromInAppSettings: ) )
181
268
182
- return . success( content: IterableHtmlInAppContent ( edgeInsets: edgeInsets ,
269
+ return . success( content: IterableHtmlInAppContent ( edgeInsets: padding . toEdgeInsets ( ) ,
183
270
html: html,
184
271
shouldAnimate: shouldAnimate,
185
272
backgroundColor: backgroundColor) )
0 commit comments