Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
//
// ABMKMigrationMessagingService.m
// Audible
//
// Created by Jain, Sunayna on 6/9/17.
// Copyright © 2017 Audible, Inc. All rights reserved.
//
#import "ABMKMigrationMessagingService.h"
#import "ABMigrationInfo.h"
#import "ABCustomerInformation.h"
#import "ABMarketPlace.h"
#import "ABMKAccountMigrationConstants.h"
#import <AmazoniOSCommons/AMZMacros.h>
@implementation ABMKMigrationMessagingService
+ (UIAlertController *)migrationAlertForCustomerInfo:(id<ABCustomerInformation>)customerInfo {
ABMigrationStatus status = customerInfo.migrationInfo.migrationStatus;
switch (status) {
case ABMigrationStatusEligible:
return [self alertForEligibleUserWithCustomerInfo:customerInfo];
case ABMigrationStatusAlreadyMigrated:
if ([self isCorrectMarketPlace]) {
return [self alertForSuccessfullyMigratedUserWithCustomerInfo:customerInfo];
} else {
return [self alertForLoggingIntoCorrectMarketplace];
}
default:
return nil;
break;
}
}
+ (BOOL)isCorrectMarketPlace {
ABMarketPlace *currentMarketplace = [ABMarketPlace currentMarketplace];
return currentMarketplace.marketPlaceIdentifier == kAudibleCAMarketplace;
}
+ (UIAlertController *)alertForEligibleUserWithCustomerInfo: (id<ABCustomerInformation>)customerInfo {
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSString *customerID = customerInfo.customerId;
NSString *neverShowMigrateDialogKey = $(ABMKNeverShowMigrateDialogKey, @"-", customerID);
BOOL neverShowMigrateDialogValue = [userDefaults valueForKey:neverShowMigrateDialogKey];
NSString *lastMigrateDialogShownDateKey = $(ABMKLastMigrateDialogShownDateKey, @"-", customerID);
NSDate *lastMigrateDialogShownDateValue = [userDefaults valueForKey:lastMigrateDialogShownDateKey];
if (neverShowMigrateDialogValue) {
return nil;
}
if (!lastMigrateDialogShownDateValue) {
return [self firstAlertForEligibleUserForCustomerInfo:customerInfo];
}
if ([[NSDate date] timeIntervalSinceDate:lastMigrateDialogShownDateValue] >= 7*24*60*60) {
return [self subsequentAlertsForEligibleUserForCustomerInfo:customerInfo];
}
return nil;
}
+ (UIAlertController *)firstAlertForEligibleUserForCustomerInfo: (id<ABCustomerInformation>)customerInfo {
NSString *title = @"Transfer to Audible Canada";
NSString *description = @"As a customer with a Canadian billing address, you are eligible to transfer your account to Audible Canada. Benefits include Canada-originated content, no more currency fees, and more.\nTo learn more or transfer, you can visit Audible website, call Customer Service, or have us send you more information via email.";
void (^saveUserDefaultsBlock)(void) = ^{
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSString *lastMigrateDialogShownDateKey = $(ABMKLastMigrateDialogShownDateKey, @"-", customerInfo.customerId);
[userDefaults setValue:[NSDate date] forKey:lastMigrateDialogShownDateKey];
[userDefaults synchronize];
};
UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:description preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *emailAction = [UIAlertAction actionWithTitle:@"Email me information" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action __unused) {
[alert dismissViewControllerAnimated:YES completion:nil];
NSLog(@"email option selected");
saveUserDefaultsBlock();
}];
UIAlertAction *notRightNowAction = [UIAlertAction actionWithTitle:@"Not right now" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action __unused) {
[alert dismissViewControllerAnimated:YES completion:nil];
NSLog(@"not right now selected");
saveUserDefaultsBlock();
}];
[alert addAction:emailAction];
[alert addAction:notRightNowAction];
return alert;
}
+ (UIAlertController *)subsequentAlertsForEligibleUserForCustomerInfo: (id<ABCustomerInformation>)customerInfo {
NSString *title = @"Transfer to Audible Canada";
NSString *description = @"As a customer with a Canadian billing address, you are eligible to transfer your account to Audible Canada. Benefits include Canada-originated content, no more currency fees, and more.\nFind out more about the benefits and transfer process on Audible's website.";
void (^saveDateBlock)(void) = ^{
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSString *lastMigrateDialogShownDateKey = $(ABMKLastMigrateDialogShownDateKey, @"-", customerInfo.customerId);
[userDefaults setValue:[NSDate date] forKey:lastMigrateDialogShownDateKey];
[userDefaults synchronize];
};
void (^saveDateAndNeverShowAgainBlock)(void) = ^{
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSString *lastMigrateDialogShownDateKey = $(ABMKLastMigrateDialogShownDateKey, @"-", customerInfo.customerId);
[userDefaults setValue:[NSDate date] forKey:lastMigrateDialogShownDateKey];
NSString *neverShowMigrateDialogKey = $(ABMKNeverShowMigrateDialogKey, @"-", customerInfo.customerId);
[userDefaults setValue:[NSNumber numberWithBool:YES] forKey:neverShowMigrateDialogKey];
[userDefaults synchronize];
};
UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:description preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *emailAction = [UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action __unused) {
[alert dismissViewControllerAnimated:YES completion:nil];
NSLog(@"ok selected");
saveDateBlock();
}];
UIAlertAction *notNowAction = [UIAlertAction actionWithTitle:@"Not now" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action __unused) {
[alert dismissViewControllerAnimated:YES completion:nil];
NSLog(@"not now selected");
saveDateBlock();
}];
UIAlertAction *neverShowAgain = [UIAlertAction actionWithTitle:@"Don't show me this again" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action __unused) {
[alert dismissViewControllerAnimated:YES completion:nil];
NSLog(@"never show again selected");
saveDateAndNeverShowAgainBlock();
}];
[alert addAction:emailAction];
[alert addAction:notNowAction];
[alert addAction:neverShowAgain];
return alert;
}
+ (UIAlertController *)alertForSuccessfullyMigratedUserWithCustomerInfo: (id<ABCustomerInformation>)customerInfo {
NSString *title = @"You are now in Canada marketplace";
NSString *description = nil;
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSString *customerID = customerInfo.customerId;
NSString *shownSuccessfulMigrationDialogKey = $(ABMKShownSuccessfulMigrationDialogKey, @"-", customerID);
BOOL shownSuccessfulMigrationDialogValue = [userDefaults valueForKey: shownSuccessfulMigrationDialogKey];
if (shownSuccessfulMigrationDialogValue) {
return nil;
}
NSDate *migrationDate = customerInfo.migrationInfo.migrationDate;
if ([[NSDate date] timeIntervalSinceDate:migrationDate] >= 10*60) {
description = @"1";
} else {
description = @"2";
}
UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:description preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action __unused) {
[alert dismissViewControllerAnimated:YES completion:nil];
[userDefaults setValue:[NSNumber numberWithBool:YES] forKey:shownSuccessfulMigrationDialogKey];
[userDefaults synchronize];
}];
[alert addAction:okAction];
return alert;
}
+ (UIAlertController *)alertForLoggingIntoCorrectMarketplace {
NSString *title = @"Switch to the Canada Marketplace";
NSString *description = @"You have migrated your account to Canada, but you're still in the US marketplace. Login to the Canada marketplace to see your books";
UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:description preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *loginAction = [UIAlertAction actionWithTitle:@"Login to Canada Marketplace" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action __unused) {
[alert dismissViewControllerAnimated:YES completion:nil];
}];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action __unused) {
[alert dismissViewControllerAnimated:YES completion:nil];
}];
[alert addAction:loginAction];
[alert addAction:cancelAction];
return alert;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment