Skip to content

Commit 89ec570

Browse files
committed
parse and use margin data from loadHTMLContent
1 parent 458eedb commit 89ec570

File tree

1 file changed

+53
-10
lines changed

1 file changed

+53
-10
lines changed

iOS_SDK/OneSignalSDK/Source/OSInAppMessageViewController.m

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,14 @@ @interface OSInAppMessageViewController ()
9393
// BOOL to track if the message content has loaded before tags have finished loading for liquid templating
9494
@property (nonatomic, nullable) NSString *pendingHTMLContent;
9595

96+
@property (nonatomic) double leftMarginModifier;
97+
98+
@property (nonatomic) double rightMarginModifier;
99+
100+
@property (nonatomic) double topMarginModifier;
101+
102+
@property (nonatomic) double bottomMarginModifier;
103+
96104
@end
97105

98106
@implementation OSInAppMessageViewController
@@ -101,6 +109,10 @@ - (instancetype _Nonnull)initWithMessage:(OSInAppMessage *)inAppMessage delegate
101109
if (self = [super init]) {
102110
self.message = inAppMessage;
103111
self.delegate = delegate;
112+
self.topMarginModifier = 1.0;
113+
self.bottomMarginModifier = 1.0;
114+
self.leftMarginModifier = 1.0;
115+
self.rightMarginModifier = 1.0;
104116
}
105117

106118
return self;
@@ -232,8 +244,14 @@ - (OSResultSuccessBlock)messageContentOnSuccess {
232244
[[OneSignal sessionManager] onInAppMessageReceived:self.message.messageId];
233245

234246
let baseUrl = [NSURL URLWithString:OS_IAM_WEBVIEW_BASE_URL];
235-
self.pendingHTMLContent = data[@"html"];
236-
self.maxDisplayTime = [data[@"display_duration"] doubleValue];
247+
NSMutableDictionary *fakeData = [[NSMutableDictionary alloc] initWithDictionary:data];
248+
fakeData[@"styles"] = @{
249+
@"top_margin" : @"0",
250+
@"bottom_margin" : @"0",
251+
@"left_margin" : @"0",
252+
@"right_margin" : @"0",
253+
};
254+
[self parseContentData:fakeData];
237255
if (self.waitForTags) {
238256
return;
239257
}
@@ -242,6 +260,27 @@ - (OSResultSuccessBlock)messageContentOnSuccess {
242260
};
243261
}
244262

263+
- (void)parseContentData:(NSDictionary *)data {
264+
self.pendingHTMLContent = data[@"html"];
265+
self.maxDisplayTime = [data[@"display_duration"] doubleValue];
266+
if (data[@"styles"]) {
267+
// We are currently only allowing default margin or no margin.
268+
// If we receive a number that isn't 0 we want to use default margin for now.
269+
if (data[@"styles"][@"top_margin"]) {
270+
self.topMarginModifier = [data[@"styles"][@"top_margin"] doubleValue] == 0 ? 0 : 1.0;
271+
}
272+
if (data[@"styles"][@"bottom_margin"]) {
273+
self.bottomMarginModifier = [data[@"styles"][@"bottom_margin"] doubleValue] == 0 ? 0 : 1.0;
274+
}
275+
if (data[@"styles"][@"left_margin"]) {
276+
self.leftMarginModifier = [data[@"styles"][@"left_margin"] doubleValue] == 0 ? 0 : 1.0;
277+
}
278+
if (data[@"styles"][@"right_margin"]) {
279+
self.rightMarginModifier = [data[@"styles"][@"right_margin"] doubleValue] == 0 ? 0 : 1.0;
280+
}
281+
}
282+
}
283+
245284
- (void)setWaitForTags:(BOOL)waitForTags {
246285
_waitForTags = waitForTags;
247286
if (!waitForTags && self.pendingHTMLContent) {
@@ -316,18 +355,18 @@ - (void)addConstraintsForMessage {
316355
// Height constraint based on the IAM being full screen or any others with a specific height
317356
// NOTE: full screen IAM payload has no height, so match screen height minus margins
318357
if (self.message.position == OSInAppMessageDisplayPositionFullScreen)
319-
self.heightConstraint = [self.messageView.heightAnchor constraintEqualToAnchor:height multiplier:1.0 constant:-2.0f * marginSpacing];
358+
self.heightConstraint = [self.messageView.heightAnchor constraintEqualToAnchor:height multiplier:1.0 constant:(self.topMarginModifier * -marginSpacing) + (self.bottomMarginModifier * -marginSpacing)];
320359
else
321360
self.heightConstraint = [self.messageView.heightAnchor constraintEqualToConstant:self.message.height.doubleValue];
322361

323362
// The aspect ratio for each type (ie. Banner) determines the height normally
324363
// However the actual height of the HTML content takes priority.
325364
// Makes sure our webview is never taller than our screen.
326-
[self.messageView.heightAnchor constraintLessThanOrEqualToAnchor:height multiplier:1.0 constant:-2.0f * marginSpacing].active = true;
365+
[self.messageView.heightAnchor constraintLessThanOrEqualToAnchor:height multiplier:1.0 constant:(self.topMarginModifier * -marginSpacing) + (self.bottomMarginModifier * -marginSpacing)].active = true;
327366

328367
// Pins the message view to the left & right
329-
let leftConstraint = [self.messageView.leadingAnchor constraintEqualToAnchor:leading constant:marginSpacing];
330-
let rightConstraint = [self.messageView.trailingAnchor constraintEqualToAnchor:trailing constant:-marginSpacing];
368+
let leftConstraint = [self.messageView.leadingAnchor constraintEqualToAnchor:leading constant:marginSpacing * self.leftMarginModifier];
369+
let rightConstraint = [self.messageView.trailingAnchor constraintEqualToAnchor:trailing constant:-marginSpacing * self.rightMarginModifier];
331370

332371
// Ensure the message view is always centered horizontally
333372
[self.messageView.centerXAnchor constraintEqualToAnchor:center].active = true;
@@ -350,8 +389,10 @@ - (void)addConstraintsForMessage {
350389
self.view.window.frame = CGRectMake(0, 0, bannerWidth, bannerHeight);
351390

352391
self.initialYConstraint = [self.messageView.bottomAnchor constraintEqualToAnchor:self.view.topAnchor constant:-8.0f];
353-
self.finalYConstraint = [self.messageView.topAnchor constraintEqualToAnchor:top constant:marginSpacing];
354-
self.panVerticalConstraint = [self.messageView.topAnchor constraintEqualToAnchor:top constant:marginSpacing];
392+
self.finalYConstraint = [self.messageView.topAnchor constraintEqualToAnchor:top
393+
constant:marginSpacing * self.topMarginModifier];
394+
self.panVerticalConstraint = [self.messageView.topAnchor constraintEqualToAnchor:top
395+
constant:marginSpacing * self.topMarginModifier];
355396
break;
356397
case OSInAppMessageDisplayPositionBottom:
357398
if (@available(iOS 11, *)) {
@@ -362,8 +403,10 @@ - (void)addConstraintsForMessage {
362403
self.view.window.frame = CGRectMake(0, bannerMessageY, bannerWidth, bannerHeight);
363404

364405
self.initialYConstraint = [self.messageView.topAnchor constraintEqualToAnchor:self.view.bottomAnchor constant:8.0f];
365-
self.finalYConstraint = [self.messageView.bottomAnchor constraintEqualToAnchor:bottom constant:-marginSpacing];
366-
self.panVerticalConstraint = [self.messageView.bottomAnchor constraintEqualToAnchor:bottom constant:-marginSpacing];
406+
self.finalYConstraint = [self.messageView.bottomAnchor constraintEqualToAnchor:bottom
407+
constant:-marginSpacing * self.bottomMarginModifier];
408+
self.panVerticalConstraint = [self.messageView.bottomAnchor constraintEqualToAnchor:bottom
409+
constant:-marginSpacing * self.bottomMarginModifier];
367410
break;
368411
case OSInAppMessageDisplayPositionFullScreen:
369412
case OSInAppMessageDisplayPositionCenterModal:

0 commit comments

Comments
 (0)