Skip to content

Commit 8235c9d

Browse files
tapashmajumderroninopf
authored andcommitted
Merge pull request #450 from Iterable/feature/mob-2216-padding
[MOB-2216] - Strongly typed padding struct instead of UIEdgeInsets.
1 parent e1202ff commit 8235c9d

File tree

8 files changed

+216
-86
lines changed

8 files changed

+216
-86
lines changed

swift-sdk/Internal/InAppCalculations.swift

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -102,17 +102,6 @@ struct InAppCalculations {
102102
}
103103
}
104104

105-
static func adjustedPadding(from padding: UIEdgeInsets) -> UIEdgeInsets {
106-
var insetPadding = padding
107-
if insetPadding.left + insetPadding.right >= 100 {
108-
ITBError("Can't display an in-app with padding > 100%. Defaulting to 0 for padding left/right")
109-
insetPadding.left = 0
110-
insetPadding.right = 0
111-
}
112-
113-
return insetPadding
114-
}
115-
116105
static func calculateWebViewPosition(safeAreaInsets: UIEdgeInsets,
117106
parentPosition: ViewPosition,
118107
paddingLeft: CGFloat,

swift-sdk/Internal/InAppContentParser.swift

Lines changed: 109 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
import Foundation
99
import UIKit
1010

11+
typealias PaddingParser = HtmlContentParser.InAppDisplaySettingsParser.PaddingParser
12+
typealias Padding = PaddingParser.Padding
13+
1114
enum InAppContentParseResult {
1215
case success(content: IterableInAppContent)
1316
case failure(reason: String)
@@ -41,7 +44,7 @@ private protocol ContentFromJsonParser {
4144
}
4245

4346
struct HtmlContentParser {
44-
static func getPadding(fromInAppSettings settings: [AnyHashable: Any]?) -> UIEdgeInsets {
47+
static func getPadding(fromInAppSettings settings: [AnyHashable: Any]?) -> Padding {
4548
InAppDisplaySettingsParser.PaddingParser.getPadding(fromInAppSettings: settings)
4649
}
4750

@@ -101,6 +104,65 @@ struct HtmlContentParser {
101104
case bottom
102105
}
103106

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+
104166
enum PaddingKey: String, JsonKeyRepresentable {
105167
var jsonKey: String {
106168
rawValue
@@ -114,50 +176,75 @@ struct HtmlContentParser {
114176

115177
/// `settings` json looks like the following
116178
/// {"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))
122184
}
123185

124186
/// json comes in as
125187
/// `{"displayOption": "AutoExpand"}`
126188
/// or `{"percentage": 60}`
127-
/// returns `-1` for `AutoExpand` type
128-
static func decodePadding(_ value: Any?) -> Int {
189+
static func decodePaddingValue(_ value: Any?) -> PaddingValue {
129190
guard let dict = value as? [AnyHashable: Any] else {
130-
return 0
191+
return .percent(value: 0)
131192
}
132193

133194
if let displayOption = dict.getValue(for: PaddingKey.displayOption) as? String, displayOption == Self.displayOptionAutoExpand {
134-
return -1
195+
return .autoExpand
135196
} else {
136197
if let percentage = dict.getValue(for: PaddingKey.percentage) as? NSNumber {
137-
return percentage.intValue
198+
return .percent(value: Int(truncating: percentage))
138199
}
139200

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 {
140209
return 0
141210
}
211+
212+
if let percentage = dict.getValue(for: PaddingKey.percentage) as? NSNumber {
213+
return Int(truncating: percentage)
214+
}
215+
216+
return 0
142217
}
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 {
146224
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 {
148228
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 {
150232
return .bottom
151233
} else {
152234
return .center
153235
}
154236
}
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+
156244
private static func getEdgePadding(fromInAppSettings settings: [AnyHashable: Any]?,
157-
edge: PaddingEdge) -> CGFloat {
245+
edge: PaddingEdge) -> Int {
158246
settings?.getValue(for: edge)
159-
.map(decodePadding(_:))
160-
.map { CGFloat($0) } ?? .zero
247+
.map(decodePadding(_:)) ?? 0
161248
}
162249
}
163250
}
@@ -174,12 +261,12 @@ extension HtmlContentParser: ContentFromJsonParser {
174261
}
175262

176263
let inAppDisplaySettings = json[JsonKey.InApp.inAppDisplaySettings] as? [AnyHashable: Any]
177-
let edgeInsets = getPadding(fromInAppSettings: inAppDisplaySettings)
264+
let padding = getPadding(fromInAppSettings: inAppDisplaySettings)
178265

179266
let shouldAnimate = inAppDisplaySettings.map(Self.parseShouldAnimate(fromInAppSettings:)) ?? false
180267
let backgroundColor = inAppDisplaySettings.flatMap(Self.parseBackgroundColor(fromInAppSettings:))
181268

182-
return .success(content: IterableHtmlInAppContent(edgeInsets: edgeInsets,
269+
return .success(content: IterableHtmlInAppContent(edgeInsets: padding.toEdgeInsets(),
183270
html: html,
184271
shouldAnimate: shouldAnimate,
185272
backgroundColor: backgroundColor))

swift-sdk/Internal/InAppDisplayer.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class InAppDisplayer: InAppDisplayerProtocol {
4242
*/
4343
@discardableResult static func showIterableHtmlMessage(_ htmlString: String,
4444
messageMetadata: IterableInAppMessageMetadata? = nil,
45-
padding: UIEdgeInsets = .zero) -> ShowResult {
45+
padding: Padding = .zero) -> ShowResult {
4646
guard !InAppPresenter.isPresenting else {
4747
return .notShown("In-app notification is being presented.")
4848
}
@@ -131,7 +131,7 @@ class InAppDisplayer: InAppDisplayerProtocol {
131131

132132
return showIterableHtmlMessage(content.html,
133133
messageMetadata: metadata,
134-
padding: content.edgeInsets)
134+
padding: content.padding)
135135
}
136136

137137
// deprecated - will be removed in version 6.3.x or above

swift-sdk/Internal/InAppManager.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ class InAppManager: NSObject, IterableInternalInAppManagerProtocol {
173173
}
174174

175175
let parameters = IterableHtmlMessageViewController.Parameters(html: content.html,
176-
padding: content.edgeInsets,
176+
padding: content.padding,
177177
messageMetadata: IterableInAppMessageMetadata(message: message, location: .inbox),
178178
isModal: inboxMode == .popup,
179179
inboxSessionId: inboxSessionId)

swift-sdk/Internal/IterableHtmlMessageViewController.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,21 @@ enum IterableMessageLocation: Int {
1616
class IterableHtmlMessageViewController: UIViewController {
1717
struct Parameters {
1818
let html: String
19-
let padding: UIEdgeInsets
19+
let padding: Padding
2020
let messageMetadata: IterableInAppMessageMetadata?
2121
let isModal: Bool
2222

2323
let inboxSessionId: String?
2424
let animationDuration = 0.67
2525

2626
init(html: String,
27-
padding: UIEdgeInsets = .zero,
27+
padding: Padding = .zero,
2828
messageMetadata: IterableInAppMessageMetadata? = nil,
2929
isModal: Bool,
3030
inboxSessionId: String? = nil) {
3131
ITBInfo()
3232
self.html = html
33-
self.padding = InAppCalculations.adjustedPadding(from: padding)
33+
self.padding = padding.adjusted()
3434
self.messageMetadata = messageMetadata
3535
self.isModal = isModal
3636
self.inboxSessionId = inboxSessionId
@@ -174,8 +174,8 @@ class IterableHtmlMessageViewController: UIViewController {
174174
IterableHtmlMessageViewController.calculateWebViewPosition(webView: webView,
175175
safeAreaInsets: InAppCalculations.safeAreaInsets(for: view),
176176
parentPosition: parentPosition,
177-
paddingLeft: parameters.padding.left,
178-
paddingRight: parameters.padding.right,
177+
paddingLeft: CGFloat(parameters.padding.left),
178+
paddingRight: CGFloat(parameters.padding.right),
179179
location: location)
180180
.onSuccess { [weak self] position in
181181
if animate {

swift-sdk/IterableMessaging.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,12 @@ public extension Notification.Name {
107107
}
108108
}
109109

110+
extension IterableHtmlInAppContent {
111+
var padding: Padding {
112+
Padding.from(edgeInsets: edgeInsets)
113+
}
114+
}
115+
110116
@objcMembers public final class IterableInboxMetadata: NSObject {
111117
public let title: String?
112118
public let subtitle: String?

0 commit comments

Comments
 (0)