Skip to content

Commit

Permalink
Merge branch 'master' of github.com:mixpanel/mixpanel-iphone
Browse files Browse the repository at this point in the history
  • Loading branch information
samgreen committed Oct 17, 2015
2 parents 7f2005c + 91f43cc commit 5a8bde0
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 10 deletions.
2 changes: 2 additions & 0 deletions HelloMixpanel/HelloMixpanel.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,9 @@
2808F9981610EFE3005772B7 /* Supporting Files */,
9003ECED95D33B569C44DB53 /* Pods */,
);
indentWidth = 4;
sourceTree = "<group>";
tabWidth = 4;
};
2808F98E1610EFE3005772B7 /* Products */ = {
isa = PBXGroup;
Expand Down
52 changes: 52 additions & 0 deletions HelloMixpanel/HelloMixpanelTests/HelloMixpanelTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -1141,6 +1141,58 @@ - (void)testNoDoubleShowNotification
}
}

- (void)testNoShowNotificationOnAlertController
{
UIViewController *topVC = [self topViewController];

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Alert" message:nil preferredStyle:UIAlertControllerStyleAlert];
[alertController addAction:[UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:nil]];

__block BOOL waitForBlock = YES;
[topVC presentViewController:alertController animated:NO completion:^{
waitForBlock = NO;
}];

while(waitForBlock) {
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
}

NSDictionary *o = @{@"id": @3,
@"message_id": @1,
@"title": @"title",
@"type": @"takeover",
@"body": @"body",
@"cta": @"cta",
@"cta_url": @"maps://",
@"image_url": @"http://cdn.mxpnl.com/site_media/images/engage/inapp_messages/mini/icon_coin.png"};
MPNotification *notif = [MPNotification notificationWithJSONObject:o];
[self.mixpanel showNotificationWithObject:notif];

//wait for notifs to be shown from main queue
[self waitForAsyncQueue];

topVC = [self topViewController];
XCTAssertFalse([topVC isKindOfClass:[MPNotificationViewController class]], @"Notification was presented");

// Dismiss the alert and try to present notification again
waitForBlock = YES;
[topVC dismissViewControllerAnimated:YES completion:^{
waitForBlock = NO;
}];

while(waitForBlock) {
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
}

[self.mixpanel showNotificationWithObject:notif];

//wait for notifs to be shown from main queue
[self waitForAsyncQueue];

topVC = [self topViewController];
XCTAssertTrue([topVC isKindOfClass:[MPNotificationViewController class]], @"Notification wasn't presented");
}

- (void)testNoShowSurveyOnPresentingVC
{
NSDictionary *o = @{@"id": @3,
Expand Down
2 changes: 2 additions & 0 deletions Mixpanel/MPNotificationViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

@interface MPMiniNotificationViewController : MPNotificationViewController

@property (nonatomic, strong) UIColor *backgroundColor;

- (void)showWithAnimation;

@end
Expand Down
13 changes: 8 additions & 5 deletions Mixpanel/MPNotificationViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -275,12 +275,15 @@ - (void)viewDidLoad
self.bodyLabel.lineBreakMode = NSLineBreakByWordWrapping;
self.bodyLabel.numberOfLines = 0;

UIColor *backgroundColor = [UIColor mp_applicationPrimaryColor];
if (!backgroundColor) {
backgroundColor = [UIColor mp_darkEffectColor];
if (!self.backgroundColor) {
self.backgroundColor = [UIColor mp_applicationPrimaryColor];
if (!self.backgroundColor) {
self.backgroundColor = [UIColor mp_darkEffectColor];
}
}
backgroundColor = [backgroundColor colorWithAlphaComponent:0.95f];
self.view.backgroundColor = backgroundColor;

UIColor *backgroundColorWithAlphaComponent = [self.backgroundColor colorWithAlphaComponent:0.95f];
self.view.backgroundColor = backgroundColorWithAlphaComponent;

if (self.notification != nil) {
if (self.notification.image != nil) {
Expand Down
13 changes: 13 additions & 0 deletions Mixpanel/Mixpanel.h
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,19 @@
*/
@property (atomic) CGFloat miniNotificationPresentationTime;

/*!
@property
@abstract
If set, determines the background color of mini notifications.
@discussion
If this isn't set, we default to either the color of the UINavigationBar of the top
UINavigationController that is showing when the notification is presented, the
UINavigationBar default color for the app or the UITabBar default color.
*/
@property (atomic) UIColor* miniNotificationBackgroundColor;

/*!
@property
Expand Down
29 changes: 24 additions & 5 deletions Mixpanel/Mixpanel.m
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ - (instancetype)initWithToken:(NSString *)apiToken launchOptions:(NSDictionary *
self.checkForVariantsOnActive = YES;
self.checkForSurveysOnActive = YES;
self.miniNotificationPresentationTime = 6.0;
self.miniNotificationBackgroundColor = nil;

self.distinctId = [self defaultDistinctId];
self.superProperties = [NSMutableDictionary dictionary];
Expand Down Expand Up @@ -1197,6 +1198,22 @@ + (UIViewController *)topPresentedViewController
return controller;
}

+ (BOOL)canPresentFromViewController:(UIViewController *)viewController
{
// This fixes the NSInternalInconsistencyException caused when we try present a
// survey on a viewcontroller that is itself being presented.
if ([viewController isBeingPresented] || [viewController isBeingDismissed]) {
return NO;
}

Class UIAlertControllerClass = NSClassFromString(@"UIAlertController");
if (UIAlertControllerClass && [viewController isKindOfClass:UIAlertControllerClass]) {
return NO;
}

return YES;
}

- (void)checkForDecideResponseWithCompletion:(void (^)(NSArray *surveys, NSArray *notifications, NSSet *variants, NSSet *eventBindings))completion
{
[self checkForDecideResponseWithCompletion:completion useCache:YES];
Expand Down Expand Up @@ -1390,10 +1407,7 @@ - (void)presentSurveyWithRootViewController:(MPSurvey *)survey
{
UIViewController *presentingViewController = [Mixpanel topPresentedViewController];

// This fixes the NSInternalInconsistencyException caused when we try present a
// survey on a viewcontroller that is itself being presented.
if (![presentingViewController isBeingPresented] && ![presentingViewController isBeingDismissed]) {

if ([[self class] canPresentFromViewController:presentingViewController]) {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MPSurvey" bundle:[NSBundle bundleForClass:Mixpanel.class]];
MPSurveyNavigationController *controller = [storyboard instantiateViewControllerWithIdentifier:@"MPSurveyNavigationController"];
controller.survey = survey;
Expand Down Expand Up @@ -1596,6 +1610,10 @@ - (void)showNotificationWithObject:(MPNotification *)notification
if (shown && ![notification.title isEqualToString:@"$ignore"]) {
[self markNotificationShown:notification];
}

if (!shown) {
self.currentlyShowingNotification = nil;
}
}
});
}
Expand All @@ -1604,7 +1622,7 @@ - (BOOL)showTakeoverNotificationWithObject:(MPNotification *)notification
{
UIViewController *presentingViewController = [Mixpanel topPresentedViewController];

if (![presentingViewController isBeingPresented] && ![presentingViewController isBeingDismissed]) {
if ([[self class] canPresentFromViewController:presentingViewController]) {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MPNotification" bundle:[NSBundle bundleForClass:Mixpanel.class]];
MPTakeoverNotificationViewController *controller = [storyboard instantiateViewControllerWithIdentifier:@"MPNotificationViewController"];

Expand All @@ -1625,6 +1643,7 @@ - (BOOL)showMiniNotificationWithObject:(MPNotification *)notification
MPMiniNotificationViewController *controller = [[MPMiniNotificationViewController alloc] init];
controller.notification = notification;
controller.delegate = self;
controller.backgroundColor = self.miniNotificationBackgroundColor;
self.notificationViewController = controller;

[controller showWithAnimation];
Expand Down

0 comments on commit 5a8bde0

Please sign in to comment.