Skip to content

Commit e76da37

Browse files
authored
Refactor Message Templates (just Alert and Confirmation) (#332)
1 parent 2325cd2 commit e76da37

File tree

9 files changed

+403
-222
lines changed

9 files changed

+403
-222
lines changed

Leanplum-SDK.xcodeproj/project.pbxproj

Lines changed: 53 additions & 11 deletions
Large diffs are not rendered by default.

Leanplum-SDK/Classes/LPMessageTemplates.m

Lines changed: 5 additions & 211 deletions
Original file line numberDiff line numberDiff line change
@@ -30,111 +30,12 @@
3030
#import <QuartzCore/QuartzCore.h>
3131
#import <StoreKit/StoreKit.h>
3232
#import "LPCountAggregator.h"
33+
#import "LPAlertMessageTemplate.h"
34+
#import "LPConfirmMessageTemplate.h"
3335

3436
#define APP_NAME (([[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleDisplayName"]) ?: \
3537
([[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleName"]))
3638

37-
#define LPMT_ALERT_NAME @"Alert"
38-
#define LPMT_CONFIRM_NAME @"Confirm"
39-
#define LPMT_PUSH_ASK_TO_ASK @"Push Ask to Ask"
40-
#define LPMT_REGISTER_FOR_PUSH @"Register For Push"
41-
#define LPMT_CENTER_POPUP_NAME @"Center Popup"
42-
#define LPMT_INTERSTITIAL_NAME @"Interstitial"
43-
#define LPMT_WEB_INTERSTITIAL_NAME @"Web Interstitial"
44-
#define LPMT_OPEN_URL_NAME @"Open URL"
45-
#define LPMT_HTML_NAME @"HTML"
46-
#define LPMT_APP_RATING_NAME @"Request App Rating"
47-
#define LPMT_ICON_CHANGE_NAME @"Change App Icon"
48-
49-
#define LPMT_ARG_TITLE @"Title"
50-
#define LPMT_ARG_MESSAGE @"Message"
51-
#define LPMT_ARG_URL @"URL"
52-
#define LPMT_ARG_URL_CLOSE @"Close URL"
53-
#define LPMT_ARG_URL_OPEN @"Open URL"
54-
#define LPMT_ARG_URL_TRACK @"Track URL"
55-
#define LPMT_ARG_URL_ACTION @"Action URL"
56-
#define LPMT_ARG_URL_TRACK_ACTION @"Track Action URL"
57-
#define LPMT_ARG_DISMISS_ACTION @"Dismiss action"
58-
#define LPMT_ARG_ACCEPT_ACTION @"Accept action"
59-
#define LPMT_ARG_CANCEL_ACTION @"Cancel action"
60-
#define LPMT_ARG_CANCEL_TEXT @"Cancel text"
61-
#define LPMT_ARG_ACCEPT_TEXT @"Accept text"
62-
#define LPMT_ARG_DISMISS_TEXT @"Dismiss text"
63-
#define LPMT_HAS_DISMISS_BUTTON @"Has dismiss button"
64-
#define LPMT_ARG_HTML_TEMPLATE @"__file__Template"
65-
66-
#define LPMT_ARG_TITLE_TEXT @"Title.Text"
67-
#define LPMT_ARG_TITLE_COLOR @"Title.Color"
68-
#define LPMT_ARG_MESSAGE_TEXT @"Message.Text"
69-
#define LPMT_ARG_MESSAGE_COLOR @"Message.Color"
70-
#define LPMT_ARG_ACCEPT_BUTTON_TEXT @"Accept button.Text"
71-
#define LPMT_ARG_ACCEPT_BUTTON_BACKGROUND_COLOR @"Accept button.Background color"
72-
#define LPMT_ARG_ACCEPT_BUTTON_TEXT_COLOR @"Accept button.Text color"
73-
#define LPMT_ARG_CANCEL_BUTTON_TEXT @"Cancel button.Text"
74-
#define LPMT_ARG_CANCEL_BUTTON_BACKGROUND_COLOR @"Cancel button.Background color"
75-
#define LPMT_ARG_CANCEL_BUTTON_TEXT_COLOR @"Cancel button.Text color"
76-
#define LPMT_ARG_BACKGROUND_IMAGE @"Background image"
77-
#define LPMT_ARG_BACKGROUND_COLOR @"Background color"
78-
#define LPMT_ARG_LAYOUT_WIDTH @"Layout.Width"
79-
#define LPMT_ARG_LAYOUT_HEIGHT @"Layout.Height"
80-
#define LPMT_ARG_HTML_HEIGHT @"HTML Height"
81-
#define LPMT_ARG_HTML_WIDTH @"HTML Width"
82-
#define LPMT_ARG_HTML_ALIGN @"HTML Align"
83-
#define LPMT_ARG_HTML_Y_OFFSET @"HTML Y Offset"
84-
#define LPMT_ARG_HTML_TAP_OUTSIDE_TO_CLOSE @"Tap Outside to Close"
85-
#define LPMT_ARG_HTML_ALIGN_TOP @"Top"
86-
#define LPMT_ARG_HTML_ALIGN_BOTTOM @"Bottom"
87-
#define LPMT_ARG_APP_ICON @"__iOSAppIcon"
88-
89-
#define LPMT_DEFAULT_ALERT_MESSAGE @"Alert message goes here."
90-
#define LPMT_DEFAULT_CONFIRM_MESSAGE @"Confirmation message goes here."
91-
#define LPMT_DEFAULT_ASK_TO_ASK_MESSAGE @"Tap OK to receive important notifications from our app."
92-
#define LPMT_DEFAULT_POPUP_MESSAGE @"Popup message goes here."
93-
#define LPMT_DEFAULT_INTERSTITIAL_MESSAGE @"Interstitial message goes here."
94-
#define LPMT_DEFAULT_OK_BUTTON_TEXT @"OK"
95-
#define LPMT_DEFAULT_YES_BUTTON_TEXT @"Yes"
96-
#define LPMT_DEFAULT_NO_BUTTON_TEXT @"No"
97-
#define LPMT_DEFAULT_LATER_BUTTON_TEXT @"Maybe Later"
98-
#define LPMT_DEFAULT_URL @"http://www.example.com"
99-
#define LPMT_DEFAULT_CLOSE_URL @"http://leanplum/close"
100-
#define LPMT_DEFAULT_OPEN_URL @"http://leanplum/loadFinished"
101-
#define LPMT_DEFAULT_TRACK_URL @"http://leanplum/track"
102-
#define LPMT_DEFAULT_ACTION_URL @"http://leanplum/runAction"
103-
#define LPMT_DEFAULT_TRACK_ACTION_URL @"http://leanplum/runTrackedAction"
104-
#define LPMT_DEFAULT_HAS_DISMISS_BUTTON YES
105-
#define LPMT_DEFAULT_APP_ICON @"__iOSAppIcon-PrimaryIcon.png"
106-
107-
#define LPMT_ICON_FILE_PREFIX @"__iOSAppIcon-"
108-
#define LPMT_ICON_PRIMARY_NAME @"PrimaryIcon"
109-
110-
#define LPMT_POPUP_ANIMATION_LENGTH 0.35
111-
112-
#define LPMT_ACCEPT_BUTTON_WIDTH 50
113-
#define LPMT_ACCEPT_BUTTON_HEIGHT 15
114-
#define LPMT_ACCEPT_BUTTON_MARGIN 10
115-
#define LPMT_TWO_BUTTON_PADDING 13
116-
117-
#define LPMT_DEFAULT_CENTER_POPUP_WIDTH 300
118-
#define LPMT_DEFAULT_CENTER_POPUP_HEIGHT 250
119-
#define LPMT_DEFAULT_HTML_HEIGHT 0
120-
#define LPMT_DEFAULT_HTML_ALIGN LPMT_ARG_LAYOUT_ALIGN_TOP
121-
122-
#define LPMT_TITLE_LABEL_HEIGHT 30
123-
124-
#define LPMT_DISMISS_BUTTON_SIZE 32
125-
126-
#define LIGHT_GRAY (246.0/255.0)
127-
128-
#ifdef __IPHONE_6_0
129-
# define ALIGN_CENTER NSTextAlignmentCenter
130-
#else
131-
# define ALIGN_CENTER UITextAlignmentCenter
132-
#endif
133-
134-
#define LP_SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
135-
136-
#define LOG_LP_MESSAGE_EXCEPTION NSLog(@"Leanplum: Error in message template %@: %@\n%@", \
137-
context.actionName, exception, [exception callStackSymbols])
13839

13940
static NSString *DEFAULTS_ASKED_TO_PUSH = @"__Leanplum_asked_to_push";
14041
static NSString *DEFAULTS_LEANPLUM_ENABLED_PUSH = @"__Leanplum_enabled_push";
@@ -230,89 +131,10 @@ + (UIViewController *)visibleViewController
230131
// Please give us suggestions for other types of messages!
231132
- (void)defineActions
232133
{
233-
[Leanplum defineAction:LPMT_ALERT_NAME
234-
ofKind:kLeanplumActionKindMessage | kLeanplumActionKindAction
235-
withArguments:@[
236-
[LPActionArg argNamed:LPMT_ARG_TITLE withString:APP_NAME],
237-
[LPActionArg argNamed:LPMT_ARG_MESSAGE withString:LPMT_DEFAULT_ALERT_MESSAGE],
238-
[LPActionArg argNamed:LPMT_ARG_DISMISS_TEXT withString:LPMT_DEFAULT_OK_BUTTON_TEXT],
239-
[LPActionArg argNamed:LPMT_ARG_DISMISS_ACTION withAction:nil]
240-
]
241-
withResponder:^BOOL(LPActionContext *context) {
242-
@try {
243-
if (NSClassFromString(@"UIAlertController")) {
244-
UIAlertController *alert = [UIAlertController alertControllerWithTitle:NSLocalizedString([context stringNamed:LPMT_ARG_TITLE], nil) message:NSLocalizedString([context stringNamed:LPMT_ARG_MESSAGE], nil) preferredStyle:UIAlertControllerStyleAlert];
245-
UIAlertAction *action = [UIAlertAction actionWithTitle:NSLocalizedString([context stringNamed:LPMT_ARG_DISMISS_TEXT], nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
246-
[self alertDismissedWithButtonIndex:0];
247-
}];
248-
[alert addAction:action];
249-
250-
[[LPMessageTemplatesClass visibleViewController]
251-
presentViewController:alert animated:YES completion:nil];
252-
} else
253-
{
254-
UIAlertView *alert = [[UIAlertView alloc]
255-
initWithTitle:NSLocalizedString([context stringNamed:LPMT_ARG_TITLE], nil)
256-
message:NSLocalizedString([context stringNamed:LPMT_ARG_MESSAGE], nil)
257-
delegate:self
258-
cancelButtonTitle:NSLocalizedString([context stringNamed:LPMT_ARG_DISMISS_TEXT], nil)
259-
otherButtonTitles:nil];
260-
[alert show];
261-
}
262134

263-
[self->_contexts addObject:context];
264-
return YES;
265-
}
266-
@catch (NSException *exception) {
267-
LOG_LP_MESSAGE_EXCEPTION;
268-
return NO;
269-
}
270-
}];
271-
272-
[Leanplum defineAction:LPMT_CONFIRM_NAME
273-
ofKind:kLeanplumActionKindMessage | kLeanplumActionKindAction
274-
withArguments:@[
275-
[LPActionArg argNamed:LPMT_ARG_TITLE withString:APP_NAME],
276-
[LPActionArg argNamed:LPMT_ARG_MESSAGE withString:LPMT_DEFAULT_CONFIRM_MESSAGE],
277-
[LPActionArg argNamed:LPMT_ARG_ACCEPT_TEXT withString:LPMT_DEFAULT_YES_BUTTON_TEXT],
278-
[LPActionArg argNamed:LPMT_ARG_CANCEL_TEXT withString:LPMT_DEFAULT_NO_BUTTON_TEXT],
279-
[LPActionArg argNamed:LPMT_ARG_ACCEPT_ACTION withAction:nil],
280-
[LPActionArg argNamed:LPMT_ARG_CANCEL_ACTION withAction:nil],
281-
]
282-
withResponder:^BOOL(LPActionContext *context) {
283-
@try {
284-
if (NSClassFromString(@"UIAlertController")) {
285-
UIAlertController *alert = [UIAlertController alertControllerWithTitle:NSLocalizedString([context stringNamed:LPMT_ARG_TITLE], nil) message:NSLocalizedString([context stringNamed:LPMT_ARG_MESSAGE], nil) preferredStyle:UIAlertControllerStyleAlert];
286-
UIAlertAction *cancel = [UIAlertAction actionWithTitle:NSLocalizedString([context stringNamed:LPMT_ARG_CANCEL_TEXT], nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
287-
[self alertDismissedWithButtonIndex:0];
288-
}];
289-
[alert addAction:cancel];
290-
UIAlertAction *accept = [UIAlertAction actionWithTitle:NSLocalizedString([context stringNamed:LPMT_ARG_ACCEPT_TEXT], nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
291-
[self alertDismissedWithButtonIndex:1];
292-
}];
293-
[alert addAction:accept];
294-
295-
[[LPMessageTemplatesClass visibleViewController]
296-
presentViewController:alert animated:YES completion:nil];
297-
} else
298-
{
299-
UIAlertView *alert = [[UIAlertView alloc]
300-
initWithTitle:NSLocalizedString([context stringNamed:LPMT_ARG_TITLE], nil)
301-
message:NSLocalizedString([context stringNamed:LPMT_ARG_MESSAGE], nil)
302-
delegate:self
303-
cancelButtonTitle:NSLocalizedString([context stringNamed:LPMT_ARG_CANCEL_TEXT], nil)
304-
otherButtonTitles:NSLocalizedString([context stringNamed:LPMT_ARG_ACCEPT_TEXT], nil),nil];
305-
[alert show];
306-
}
307-
[self->_contexts addObject:context];
308-
return YES;
309-
}
310-
@catch (NSException *exception) {
311-
LOG_LP_MESSAGE_EXCEPTION;
312-
return NO;
313-
}
314-
}];
315-
135+
[[[LPAlertMessageTemplate alloc] init] defineActionWithContexts:_contexts];
136+
[[[LPConfirmMessageTemplate alloc] init] defineActionWithContexts:_contexts];
137+
316138
[Leanplum defineAction:LPMT_OPEN_URL_NAME
317139
ofKind:kLeanplumActionKindAction
318140
withArguments:@[[LPActionArg argNamed:LPMT_ARG_URL withString:LPMT_DEFAULT_URL]]
@@ -533,34 +355,6 @@ - (void)defineActions
533355
}
534356
}
535357

536-
#pragma mark Alert and Confirm Logic
537-
538-
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
539-
{
540-
[self alertDismissedWithButtonIndex:buttonIndex];
541-
}
542-
543-
- (void)alertDismissedWithButtonIndex:(NSInteger)buttonIndex
544-
{
545-
LPActionContext *context = _contexts.lastObject;
546-
@try {
547-
[_contexts removeLastObject];
548-
549-
if ([context.actionName isEqualToString:LPMT_ALERT_NAME]) {
550-
[context runActionNamed:LPMT_ARG_DISMISS_ACTION];
551-
} else {
552-
if (buttonIndex == 1) {
553-
[context runTrackedActionNamed:LPMT_ARG_ACCEPT_ACTION];
554-
} else {
555-
[context runActionNamed:LPMT_ARG_CANCEL_ACTION];
556-
}
557-
}
558-
}
559-
@catch (NSException *exception) {
560-
LOG_LP_MESSAGE_EXCEPTION;
561-
}
562-
}
563-
564358
#pragma mark Center Popup and Interstitial Logic
565359

566360
// Creates a 1x1 image with the specified color.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//
2+
// LPAlertMessageTemplate.h
3+
// LeanplumSDK-iOS
4+
//
5+
// Created by Mayank Sanganeria on 1/27/20.
6+
// Copyright © 2020 Leanplum. All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
#import "LPBaseMessageTemplate.h"
11+
12+
NS_ASSUME_NONNULL_BEGIN
13+
14+
@interface LPAlertMessageTemplate : LPBaseMessageTemplate
15+
16+
@end
17+
18+
NS_ASSUME_NONNULL_END
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//
2+
// LPAlertMessageTemplate.m
3+
// LeanplumSDK-iOS
4+
//
5+
// Created by Mayank Sanganeria on 1/27/20.
6+
// Copyright © 2020 Leanplum. All rights reserved.
7+
//
8+
9+
#import "LPAlertMessageTemplate.h"
10+
#import "Leanplum.h"
11+
12+
@implementation LPAlertMessageTemplate
13+
14+
-(void)defineActionWithContexts:(NSMutableArray *)contexts {
15+
[super defineActionWithContexts:contexts];
16+
[Leanplum defineAction:LPMT_ALERT_NAME
17+
ofKind:kLeanplumActionKindMessage | kLeanplumActionKindAction
18+
withArguments:@[
19+
[LPActionArg argNamed:LPMT_ARG_TITLE withString:APP_NAME],
20+
[LPActionArg argNamed:LPMT_ARG_MESSAGE withString:LPMT_DEFAULT_ALERT_MESSAGE],
21+
[LPActionArg argNamed:LPMT_ARG_DISMISS_TEXT withString:LPMT_DEFAULT_OK_BUTTON_TEXT],
22+
[LPActionArg argNamed:LPMT_ARG_DISMISS_ACTION withAction:nil]
23+
]
24+
withResponder:^BOOL(LPActionContext *context) {
25+
@try {
26+
if (NSClassFromString(@"UIAlertController")) {
27+
UIAlertController *alert = [UIAlertController alertControllerWithTitle:NSLocalizedString([context stringNamed:LPMT_ARG_TITLE], nil) message:NSLocalizedString([context stringNamed:LPMT_ARG_MESSAGE], nil) preferredStyle:UIAlertControllerStyleAlert];
28+
UIAlertAction *action = [UIAlertAction actionWithTitle:NSLocalizedString([context stringNamed:LPMT_ARG_DISMISS_TEXT], nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
29+
[self alertDismissedWithButtonIndex:0];
30+
}];
31+
[alert addAction:action];
32+
33+
[[self visibleViewController] presentViewController:alert animated:YES completion:nil];
34+
} else
35+
{
36+
UIAlertView *alert = [[UIAlertView alloc]
37+
initWithTitle:NSLocalizedString([context stringNamed:LPMT_ARG_TITLE], nil)
38+
message:NSLocalizedString([context stringNamed:LPMT_ARG_MESSAGE], nil)
39+
delegate:self
40+
cancelButtonTitle:NSLocalizedString([context stringNamed:LPMT_ARG_DISMISS_TEXT], nil)
41+
otherButtonTitles:nil];
42+
[alert show];
43+
}
44+
45+
[self.contexts addObject:context];
46+
return YES;
47+
}
48+
@catch (NSException *exception) {
49+
LOG_LP_MESSAGE_EXCEPTION;
50+
return NO;
51+
}
52+
}];
53+
54+
}
55+
56+
@end
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//
2+
// LPBaseMessageTemplate.h
3+
// LeanplumSDK-iOS
4+
//
5+
// Created by Mayank Sanganeria on 1/27/20.
6+
// Copyright © 2020 Leanplum. All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
#import "Leanplum.h"
11+
#import "LPMessageTemplateConstants.h"
12+
13+
NS_ASSUME_NONNULL_BEGIN
14+
15+
@interface LPBaseMessageTemplate : NSObject
16+
17+
@property (nonatomic, strong) NSMutableArray *contexts;
18+
19+
- (UIViewController *)visibleViewController;
20+
21+
- (void)defineActionWithContexts:(NSMutableArray *)contexts;
22+
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex;
23+
- (void)alertDismissedWithButtonIndex:(NSInteger)buttonIndex;
24+
25+
@end
26+
27+
NS_ASSUME_NONNULL_END

0 commit comments

Comments
 (0)