Skip to content

Commit a18138a

Browse files
authored
[SDK-446] onRunActionNamed (#441)
1 parent 0ffc9ac commit a18138a

24 files changed

+163
-10
lines changed

Example/Tests/Classes/LPActionContextTest.m

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,4 +135,32 @@ - (void)test_asciiEncodedFileURL {
135135
XCTAssert([encodedUrl isEqualToString:@"file:///Users/mayank/Library/Developer/CoreSimulator/Devices/24394C0B-8820-4369-B3AA-9BF26F62A798/data/Containers/Data/Application/29AF4C33-1C94-46DF-A8A5-4B4CD5A3A364/Library/Caches/Leanplum_Resources/lp_public_sf_ui_font.css"]);
136136
}
137137

138+
- (void)test_setActionNamedResponder {
139+
NSString *acceptAction = @"Accept action";
140+
NSDictionary *args = @{
141+
acceptAction: @"Test"
142+
};
143+
144+
LPActionContext *messageContext = [LPActionContext
145+
actionContextWithName:@"action"
146+
args:args
147+
messageId:@"1"];
148+
149+
XCTestExpectation *onRunActionNamedExpectation = [self expectationWithDescription:@"onRunActionNamedExpectation"];
150+
151+
__weak LPActionContext *weakMessageContext = messageContext;
152+
153+
[messageContext setActionNamedResponder:^BOOL(LPActionContext * _Nonnull context) {
154+
XCTAssertEqual([context actionName], acceptAction);
155+
XCTAssertNotNil([context parentContext]);
156+
XCTAssertEqual([[context parentContext] actionName], [weakMessageContext actionName]);
157+
XCTAssertEqual([[context parentContext] messageId], [weakMessageContext messageId]);
158+
[onRunActionNamedExpectation fulfill];
159+
return YES;
160+
}];
161+
[messageContext runActionNamed:acceptAction];
162+
163+
[self waitForExpectationsWithTimeout:6 handler:nil];
164+
}
165+
138166
@end

Example/Tests/Classes/LeanplumTest.m

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ + (void)trackGeofence:(LPGeofenceEventType *)event withValue:(double)value andIn
6464

6565
@end
6666

67+
@interface LPActionContext (Test)
68+
69+
@property(strong, nonatomic) LeanplumActionBlock actionNamedResponder;
70+
71+
@end
72+
6773
@interface LeanplumTest : XCTestCase
6874

6975
@end
@@ -2407,4 +2413,66 @@ - (void) test_forceContentUpdateVariants
24072413
XCTAssertTrue([[LPVarCache sharedCache] variants].count == 4);
24082414
}
24092415

2416+
- (void)test_onAction {
2417+
NSString *actionName = @"TestAction";
2418+
LPActionContext *context = [LPActionContext
2419+
actionContextWithName:actionName
2420+
args:nil
2421+
messageId:@"1"];
2422+
2423+
XCTestExpectation *onActionExpectation = [self expectationWithDescription:@"onActionExpectation"];
2424+
2425+
[Leanplum onAction:actionName invoke:^BOOL(LPActionContext * _Nonnull context) {
2426+
XCTAssertEqual([context actionName], actionName);
2427+
[onActionExpectation fulfill];
2428+
return NO;
2429+
}];
2430+
[Leanplum triggerAction:context];
2431+
2432+
[self waitForExpectationsWithTimeout:6 handler:nil];
2433+
}
2434+
2435+
/**
2436+
* Leanplum triggerAction uses the same context instance to call all LeanplumActionBlock responders
2437+
* Those include the defineAction:actionResponder and the onAction:actionResponder blocks
2438+
*/
2439+
- (void)test_onAction_sameContext {
2440+
NSString *actionName = @"TestAction";
2441+
LPActionContext *messageContext = [LPActionContext
2442+
actionContextWithName:actionName
2443+
args:nil
2444+
messageId:@"1"];
2445+
2446+
__weak LPActionContext *weakContext = messageContext;
2447+
2448+
XCTestExpectation *onActionExpectation = [self expectationWithDescription:@"onActionExpectation"];
2449+
2450+
[Leanplum onAction:actionName invoke:^BOOL(LPActionContext * _Nonnull context) {
2451+
XCTAssertEqual(context, weakContext);
2452+
2453+
[context setActionNamedResponder:^BOOL(LPActionContext * _Nonnull context) {
2454+
return NO;
2455+
}];
2456+
2457+
[onActionExpectation fulfill];
2458+
return NO;
2459+
}];
2460+
2461+
XCTestExpectation *onActionExpectation2 = [self expectationWithDescription:@"onActionExpectation2"];
2462+
2463+
[Leanplum onAction:actionName invoke:^BOOL(LPActionContext * _Nonnull context) {
2464+
XCTAssertEqual(context, weakContext);
2465+
2466+
// The responder is already set to the context from the previous onAction block
2467+
XCTAssertNotNil([context actionNamedResponder]);
2468+
2469+
[onActionExpectation2 fulfill];
2470+
return NO;
2471+
}];
2472+
2473+
[Leanplum triggerAction:messageContext];
2474+
2475+
[self waitForExpectationsWithTimeout:6 handler:nil];
2476+
}
2477+
24102478
@end

Leanplum-SDK/Classes/Features/Actions/LPActionContext-Internal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
//
77

88
#import "LeanplumInternal.h"
9+
#import "LPActionContext.h"
910

1011
NS_ASSUME_NONNULL_BEGIN
1112

@@ -26,7 +27,6 @@ NS_ASSUME_NONNULL_BEGIN
2627
@property (readonly, strong) NSString *name;
2728
@property (readonly, strong) NSString *originalMessageId;
2829
@property (readonly, strong) NSNumber *priority;
29-
@property (readonly, strong, nullable) LPActionContext *parentContext;
3030
@property (readonly) int contentVersion;
3131
@property (readonly, strong, nullable) NSString *key;
3232
@property (assign) BOOL preventRealtimeUpdating;

Leanplum-SDK/Classes/Features/Actions/LPActionContext.m

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ @interface LPActionContext (PrivateProperties)
3030
@interface LPActionContext()
3131

3232
@property (nonatomic, strong) LPCountAggregator *countAggregator;
33+
@property (strong, nonatomic) LeanplumActionBlock actionNamedResponder;
3334

3435
@end
3536

@@ -498,6 +499,29 @@ - (NSString *)eventWithParentEventNamesFromEvent:(NSString *)event
498499
LP_END_TRY
499500
}
500501

502+
- (void)setActionNamedResponder:(LeanplumActionBlock)block
503+
{
504+
if (block) {
505+
_actionNamedResponder = [block copy];
506+
} else {
507+
_actionNamedResponder = nil;
508+
}
509+
}
510+
511+
- (void)triggerActionNamedResponder:(NSString *)name withArgs:(NSDictionary *)args
512+
{
513+
if ([self actionNamedResponder]) {
514+
LPActionContext *actionContext = [LPActionContext
515+
actionContextWithName:name
516+
args:args messageId:_messageId];
517+
518+
__weak LPActionContext *weakSelf = self;
519+
actionContext->_parentContext = weakSelf;
520+
521+
self.actionNamedResponder(actionContext);
522+
}
523+
}
524+
501525
- (void)runActionNamed:(NSString *)name
502526
{
503527
LP_TRY
@@ -506,6 +530,9 @@ - (void)runActionNamed:(NSString *)name
506530
return;
507531
}
508532
NSDictionary *args = [self getChildArgs:name];
533+
534+
[self triggerActionNamedResponder:name withArgs:args];
535+
509536
if (!args) {
510537
return;
511538
}

Leanplum-SDK/Classes/LPActionContext.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
// specific language governing permissions and limitations
2222
// under the License.
2323

24-
#import <Foundation/Foundation.h>
2524
#import <UIKit/UIKit.h>
25+
#import "Leanplum.h"
2626

2727
NS_ASSUME_NONNULL_BEGIN
2828

@@ -41,6 +41,11 @@ NS_SWIFT_NAME(ActionContext)
4141
*/
4242
@property (readonly, strong, nullable) NSDictionary *args;
4343

44+
/**
45+
* The parent ActionContext if present
46+
*/
47+
@property (readonly, nonatomic, strong, nullable) LPActionContext *parentContext;
48+
4449
- (id)objectNamed:(NSString *)name
4550
NS_SWIFT_NAME(object(name:));
4651

@@ -77,6 +82,11 @@ NS_SWIFT_NAME(htmlTemplate(name:));
7782
- (void)runActionNamed:(NSString *)name
7883
NS_SWIFT_NAME(runAction(name:));
7984

85+
/**
86+
* Executes a block when action is run.
87+
*/
88+
- (void)setActionNamedResponder:(LeanplumActionBlock)block;
89+
8090
/**
8191
* Runs and tracks an event for the action given by the "name" key.
8292
* This will track an event if no action is set.

Leanplum-SDK/Classes/Leanplum.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
#import <UserNotifications/UserNotifications.h>
2727
#import "LPInbox.h"
2828
#import "LPActionArg.h"
29-
#import "LPActionContext.h"
3029
#import "LPVar.h"
3130
#import "LPMessageArchiveData.h"
3231
#import "LPEnumConstants.h"
@@ -68,9 +67,6 @@
6867
#import "LPLogManager.h"
6968
#import "LPRequestSenderTimer.h"
7069

71-
// Prevent circular reference
72-
@class LPDeferrableAction;
73-
7470
NS_ASSUME_NONNULL_BEGIN
7571

7672
#define _LP_DEFINE_HELPER(name,val,type) LPVar* name; \
@@ -133,6 +129,8 @@ name = [LPVar define:[@#name stringByReplacingOccurrencesOfString:@"_" withStrin
133129
}
134130
/**@}*/
135131

132+
// Prevent circular reference
133+
@class LPDeferrableAction;
136134
@class LPActionContext;
137135
@class SKPaymentTransaction;
138136
@class NSExtensionContext;

Leanplum-SDK/Classes/MessageTemplates/LPAlertMessageTemplate.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//
88

99
#import "LPAlertMessageTemplate.h"
10+
#import "LPActionContext.h"
1011

1112
@implementation LPAlertMessageTemplate
1213

Leanplum-SDK/Classes/MessageTemplates/LPAppRatingMessageTemplate.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//
88

99
#import "LPAppRatingMessageTemplate.h"
10+
#import "LPActionContext.h"
1011

1112
@implementation LPAppRatingMessageTemplate
1213

Leanplum-SDK/Classes/MessageTemplates/LPCenterPopupMessageTemplate.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//
88

99
#import "LPCenterPopupMessageTemplate.h"
10+
#import "LPActionContext.h"
1011

1112
@implementation LPCenterPopupMessageTemplate
1213

Leanplum-SDK/Classes/MessageTemplates/LPConfirmMessageTemplate.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//
88

99
#import "LPConfirmMessageTemplate.h"
10+
#import "LPActionContext.h"
1011

1112
@implementation LPConfirmMessageTemplate
1213

0 commit comments

Comments
 (0)