// // KMVerificationMessageViewController.m // PDF Reader Pro Edition // // Created by 王帅 on 2018/5/9. // #import "KMVerificationMessageViewController.h" #import #import "VerificationManager.h" #import "AIInfoManager.h" #import "KMVerificationWindowController.h" #import "KMPurchaseCompareWindowController.h" #import "KMUnlockAlertViewController.h" #define kbottomGap 0 @interface KMVerificationMessageViewController ()< NSPopoverDelegate> @property (strong) IBOutlet NSView *contendView; @property (strong) IBOutlet NSButton *freeTrialButton; @property (strong) IBOutlet NSButton *upgradButton; @property (nonatomic, strong) NSTrackingArea *trackingArea; @property (nonatomic, strong) NSPopover *popover; @property (nonatomic, assign) BOOL isPopoverShow; @property (nonatomic, assign) BOOL checkValue; @property (nonatomic, assign) BOOL stopPopOverHide; @property (nonatomic, strong) KMUnlockAlertViewController *testVC; @property (nonatomic, strong) KMTrialGuideWindowController *trialGuideWC; @property (nonatomic, strong) FreeTrialApplyWindowController *freeTrialApplyWC; @end @implementation KMVerificationMessageViewController #pragma mark Init Methods - (instancetype)init { if (self = [super initWithNibName:@"KMVerificationMessageViewController" bundle:nil]) { #if VERSION_DMG [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceActivateStatusChangeNotification:) name:kDeviceActivateStatusChangeNotification object:nil]; #else [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(IAPResultReceived:) name:KMIAPSubscriptionLoadedNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(IAPPurchaseSuccess:) name:KMIAPProductPurchasedNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(IAPRestoreFinish:) name:KMIAPProductRestoreFinishedNotification object:nil]; #endif } return self; } - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; // if (self.trackingArea) { // [self.upgradButton removeTrackingArea:self.trackingArea]; // } } #pragma mark Setter Methods - (void)loadView { [super loadView]; // Do view setup here. self.freeTrialButton.wantsLayer = YES; self.freeTrialButton.title = [NSString stringWithFormat:@" %@ ",NSLocalizedString(@"Free Trial", nil)]; [self.freeTrialButton setTitleColor:[NSColor whiteColor]]; [self.freeTrialButton setAlignment:NSTextAlignmentCenter]; self.upgradButton.wantsLayer = YES; self.upgradButton.layer.masksToBounds = YES; self.upgradButton.layer.cornerRadius = 4.0; self.upgradButton.font = [NSFont SFProTextSemiboldFont:11]; self.upgradButton.layer.backgroundColor = [NSColor colorWithDeviceRed:121.0/255.0 green:196.0/255.0 blue:66.0/255.0 alpha:1.0].CGColor; [self.upgradButton setAlignment:NSTextAlignmentCenter]; self.freeTrialButton.wantsLayer = YES; self.freeTrialButton.layer.masksToBounds = YES; self.freeTrialButton.layer.cornerRadius = 4.0; self.freeTrialButton.font = [NSFont SFProTextSemiboldFont:11]; NSString *tTips = NSLocalizedString(@"Upgrade to Pro", nil); #if VERSION_DMG tTips = [NSString stringWithFormat:@"%@\n%@", NSLocalizedString(@"Upgrade to Permanent License with one time purchase", nil), NSLocalizedString(@"Complete the purchase in your web browser to get a license", nil)]; #else tTips = [NSString stringWithFormat:@"%@\n%@", NSLocalizedString(@"Subscribe to All Access Pack to enjoy more expanded features.", nil), NSLocalizedString(@"Choose your favorite payment and enjoy flexible upgrades.", nil)]; #endif self.upgradButton.toolTip = tTips; self.upgradButton.title = [NSString stringWithFormat:@" %@ ",NSLocalizedString(@"Upgrade", nil)]; [self.upgradButton setTitleColor:[NSColor whiteColor]]; [self.upgradButton setAlignment:NSTextAlignmentCenter]; [self reloadData]; } - (void)reloadData { /* 未试用:申请试用 + 解锁完整版 试用中:试用剩余天数 + 解锁完整版 试用过期:解锁完整版 已激活:隐藏 */ #if VERSION_DMG if (self.trackingArea) { [self.upgradButton removeTrackingArea:self.trackingArea]; } self.freeTrialButton.hidden = self.upgradButton.hidden = YES; if (ActivityStatusNone == [VerificationManager manager].status) { self.freeTrialButton.hidden = self.upgradButton.hidden = NO; self.freeTrialButton.title = [NSString stringWithFormat:@" %@ ",NSLocalizedString(@"Free Trial", nil)]; self.freeTrialButton.layer.backgroundColor = [NSColor colorWithRed:0.0/255.0 green:207.0/255.0 blue:133.0/255.0 alpha:1].CGColor; [self.freeTrialButton setTitleColor:[NSColor whiteColor]]; self.upgradButton.title = [NSString stringWithFormat:@" %@ ",NSLocalizedString(@"Upgrade", nil)]; [self.upgradButton setAlignment:NSTextAlignmentCenter]; [self.upgradButton setTitleColor:[NSColor whiteColor]]; } else if (ActivityStatusTrial == [VerificationManager manager].status) { self.freeTrialButton.hidden = self.upgradButton.hidden = NO; NSDate *date = [NSDate date]; NSDate *expireDate = [VerificationManager manager].detailInfo.expireDate; NSTimeInterval time = [expireDate timeIntervalSinceDate:date]; int leftDays = ((int)time)/(3600*24); NSString *needShowString = [NSString stringWithFormat:NSLocalizedString(@"%d days left", nil),leftDays]; self.freeTrialButton.title = needShowString; [self.freeTrialButton setTitleColor:[NSColor redColor]]; self.freeTrialButton.layer.backgroundColor = [NSColor clearColor].CGColor; self.upgradButton.layer.backgroundColor = [NSColor colorWithRed:255.0/255.0 green:94.0/255.0 blue:44.0/255.0 alpha:1].CGColor; self.upgradButton.title = [NSString stringWithFormat:@" %@ ",NSLocalizedString(@"Upgrade", nil)]; [self.upgradButton setTitleColor:[NSColor whiteColor]]; [self.upgradButton setAlignment:NSTextAlignmentCenter]; // if (!self.trackingArea) { // self.trackingArea = [[NSTrackingArea alloc] initWithRect:self.upgradButton.bounds options:NSTrackingMouseEnteredAndExited | NSTrackingInVisibleRect | NSTrackingActiveAlways owner:self userInfo:nil]; // } // [self.upgradButton addTrackingArea:self.trackingArea]; } else if (ActivityStatusTrialExpire == [VerificationManager manager].status) { self.upgradButton.hidden = NO; self.upgradButton.title = [NSString stringWithFormat:@" %@ ",NSLocalizedString(@"Upgrade", nil)]; [self.upgradButton setAlignment:NSTextAlignmentCenter]; self.upgradButton.layer.backgroundColor = [NSColor clearColor].CGColor; [self.upgradButton setTitleColor:[NSColor whiteColor]]; // if (!self.trackingArea) { // self.trackingArea = [[NSTrackingArea alloc] initWithRect:self.upgradButton.bounds options:NSTrackingMouseEnteredAndExited | NSTrackingInVisibleRect | NSTrackingActiveAlways owner:self userInfo:nil]; // } // [self.upgradButton addTrackingArea:self.trackingArea]; } else if (ActivityStatusVerifExpire == [VerificationManager manager].status) { self.upgradButton.hidden = NO; self.upgradButton.title = [NSString stringWithFormat:@" %@ ",NSLocalizedString(@"Upgrade", nil)]; [self.upgradButton setTitleColor:[NSColor whiteColor]]; self.upgradButton.layer.backgroundColor = [NSColor clearColor].CGColor; [self.upgradButton setAlignment:NSTextAlignmentCenter]; } else if (ActivityStatusVerification == [VerificationManager manager].status) { } else { } #else #endif [self.freeTrialButton sizeToFit]; [self.freeTrialButton setAlignment:NSTextAlignmentCenter]; [self.upgradButton sizeToFit]; [self.upgradButton setAlignment:NSTextAlignmentCenter]; CGFloat totalWidth = 8; if (self.freeTrialButton.hidden == NO) { CGFloat buttonWidth = self.freeTrialButton.frame.size.width; buttonWidth += 10; self.freeTrialButton.frame = CGRectMake(totalWidth, kbottomGap, buttonWidth, self.view.frame.size.height -2 *kbottomGap); totalWidth += buttonWidth; totalWidth += 8; } if (self.upgradButton.hidden == NO) { NSRect btnRect = [self.upgradButton.attributedTitle boundingRectWithSize:CGSizeMake(MAXFLOAT, CGRectGetHeight(self.upgradButton.frame)) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading]; CGFloat buttonWidth = btnRect.size.width + 40; self.upgradButton.frame = CGRectMake(totalWidth, kbottomGap, buttonWidth, self.view.frame.size.height -2 *kbottomGap); totalWidth += buttonWidth; totalWidth += 8; } if (self.view.superview) { self.view.frame = CGRectMake(MAX(0, CGRectGetWidth(self.view.superview.frame)-totalWidth - 8), (CGRectGetHeight(self.view.superview.frame)-CGRectGetHeight(self.view.frame))/2., totalWidth, self.view.frame.size.height); } else { if (self.upgradButton.hidden) { self.view.frame = CGRectMake(0,0, CGRectGetMaxX(self.freeTrialButton.frame) + 20 ,self.view.frame.size.height); } else { self.view.frame = CGRectMake(0,0, CGRectGetMaxX(self.upgradButton.frame) ,self.view.frame.size.height); } } self.contendView.wantsLayer = true; self.contendView.layer.backgroundColor = NSColor.clearColor.CGColor; if (self.frameUpdateHandle) { self.frameUpdateHandle(self.view.frame); } [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(popoverShowNoti:) name:@"KMVerificationMessagePopShowNoti" object:nil]; } #pragma mark - Button Actions - (IBAction)freeTrialButtonAction:(NSButton *)sender { if (ActivityStatusNone != [VerificationManager manager].status) { return; } __weak typeof(self) weakSelf = self; if (!self.trialGuideWC) { self.trialGuideWC = [KMTrialGuideWindowController currentWC]; } self.trialGuideWC.noEmailBlock = ^{ [weakSelf.trialGuideWC close]; [weakSelf showFreeTrialApplyWC]; }; self.trialGuideWC.shouldMinSizeAnimate = true; [self.trialGuideWC.window center]; CGRect originalRect = self.trialGuideWC.window.frame; originalRect.size.width = 888; originalRect.size.height = 520; originalRect.origin.x = ([NSScreen mainScreen].frame.size.width - originalRect.size.width)/2.0; originalRect.origin.y = ([NSScreen mainScreen].frame.size.height - originalRect.size.height)/2.0; self.trialGuideWC.originalFrame = originalRect; CGRect rect = [self.view convertRect:self.upgradButton.frame toView:self.view.window.contentView]; rect.origin.x = CGRectGetMaxX(self.view.window.frame) - self.upgradButton.frame.size.width - self.freeTrialButton.frame.size.width/2.0 - 15; rect.origin.y = CGRectGetMaxY(self.view.window.frame) - self.upgradButton.frame.size.height - 5; rect.size.width = 0; rect.size.height = 0; self.trialGuideWC.startFrame = rect; [self.trialGuideWC.window setFrame:rect display:YES]; [self.trialGuideWC showWindow:nil]; [self.trialGuideWC animateScaling]; } - (void)showFreeTrialApplyWC { if (self.freeTrialApplyWC != nil) { self.freeTrialApplyWC = nil; } if (self.freeTrialApplyWC == nil) { self.freeTrialApplyWC = [[FreeTrialApplyWindowController alloc] init]; } [self.freeTrialApplyWC.window center]; [self.freeTrialApplyWC showWindow:nil]; } - (IBAction)upgradeButtonAction:(id)sender { BOOL isFree = NO; __weak typeof(self) weakSelf = self; #if VERSION_DMG if (ActivityStatusNone == [VerificationManager manager].status || ActivityStatusTrialExpire == [VerificationManager manager].status) { // [[NSNotificationCenter defaultCenter] postNotificationName:@"KMVerificationMessagePopShowNoti" object:nil]; // // if (!self.testVC) { // self.testVC = [[KMUnlockAlertViewController alloc] init]; // if (ActivityStatusNone == [VerificationManager manager].status) { // //弹出 试用弹窗UI优化-新用户试用路径 新UI 情况一 右上角按钮 // KMPurchaseFirstTrialWindowController *firstTrialWC = [KMPurchaseFirstTrialWindowController currentFirstTrialWC]; // firstTrialWC.buttonActionBlock = ^(KMPurchaseFirstTrialWindowController * _Nonnull ftWC, KMButton * _Nonnull btn, NSString * _Nonnull email, NSString * _Nonnull name) { // if (btn.tag == 10001) { // if (ActivityStatusNone == [VerificationManager manager].status || // ActivityStatusTrialExpire == [VerificationManager manager].status) { // // [[FMTrackEventManager defaultManager] trackEventWithEvent:@"Tbr" withProperties:@{@"Tbr_Btn":@"Btn_Tbr_SecondTrial"}]; // [[VerificationManager manager] trialForDays:7 // email:email?:[VerificationManager manager].email // name:name?:[VerificationManager manager].accountName // complention:^(ActivityStatus status, NSDictionary *info, NSError *error) { // if (error || // status != ActivityStatusTrial) { // NSAlert *alert = [NSAlert alertWithMessageText:NSLocalizedString(@"Activation Error", nil) // defaultButton:NSLocalizedString(@"Try Again", nil) // alternateButton:nil // otherButton:nil // informativeTextWithFormat:NSLocalizedString(@"Failed to sign up for the free trial. Please make sure your internet connection is available and try again later.", nil), nil]; // [alert runModal]; // } else { // KMVerificationWindowController *vc = [KMVerificationWindowController verificationWithType:KMVerificationTypeRepeatTrialSuccess]; // [vc showWindow:nil]; // } // }]; // [ftWC close]; // } else { // [ftWC close]; // KMVerificationWindowController *vc = [KMVerificationWindowController verificationWithType:KMVerificationTypeActivate]; // vc.callback = ^{ // [[KMPurchaseCompareWindowController sharedInstance] showWindow:nil]; // }; // [vc showWindow:nil]; // } // } else if (btn.tag == 10002) { // NSURL *url = [NSURL URLWithString:@"https://www.pdfreaderpro.com/store/pdftecheditor?utm_source=app_dmg&utm_medium=dmg_puw_free_trial"]; // [[NSWorkspace sharedWorkspace] openURL:url]; // [[FMTrackEventManager defaultManager] trackEventWithEvent:@"PUW" withProperties:@{@"PUW_Btn":@"PUW_Btn_UpgradeBuyNow"}]; // } else if (btn.tag == 10003) { // KMVerificationWindowController *vc = [KMVerificationWindowController verificationWithType:KMVerificationTypeActivate];//KMVerificationTypeActivate // [vc showWindow:nil]; // [ftWC close]; // } // }; // [firstTrialWC.window center]; // [firstTrialWC showWindow:nil]; // } else if (ActivityStatusTrialExpire == [VerificationManager manager].status || // ActivityStatusTrial == [VerificationManager manager].status) { //// if ([VerificationManager manager].secondTrialEnabled) { //// if (!self.checkValue) { //// [[KMPurchaseCompareWindowController sharedInstance] showWindow:nil]; //// } //// } else { // [[NSNotificationCenter defaultCenter] postNotificationName:@"KMVerificationMessagePopShowNoti" object:nil]; // // if (!self.testVC) { // self.testVC = [[KMUnlockAlertViewController alloc] init]; // } // [self.testVC addtrackingAreaInfo]; // self.testVC.callback = ^(NSInteger index) { // if (index == 1) { // KMVerificationWindowController *vc = [KMVerificationWindowController verificationWithType:KMVerificationTypeActivate]; // [vc showWindow:nil]; // } else if (index == 2) { // NSURL *url = [NSURL URLWithString:@"https://www.pdfreaderpro.com/store/pdftecheditor"]; // // [[NSWorkspace sharedWorkspace] openURL:url]; // [[FMTrackEventManager defaultManager] trackEventWithEvent:@"PUW" withProperties:@{@"PUW_Btn":@"Btn_PUW_BuyNow-Expire"}]; //// KMPurchaseEmbeddedWindowController *embeddedWC = [KMPurchaseEmbeddedWindowController currentFirstTrialWC:@"com.brother.pdfreaderpro.mac.product_3"]; //// [embeddedWC showWindow:nil]; //// [[embeddedWC window] center]; // } // }; // self.testVC.mouseEventCall = ^(BOOL mouseEnter) { // if (mouseEnter) { // self.stopPopOverHide = YES; // if (ActivityStatusTrialExpire == [VerificationManager manager].status || // ActivityStatusTrial == [VerificationManager manager].status) { // if (!self.popover.isShown) { // [self buttonAction:self.button]; // } // } // } else { // self.stopPopOverHide = NO; // if (self.popover) { // [self.popover close]; // } // [self.testVC removeTrackingAreaInfo]; // } // }; // NSPopover *popover = [[NSPopover alloc] init]; // popover.delegate = self; // popover.contentViewController = self.testVC; // popover.animates = YES; // popover.behavior = NSPopoverBehaviorTransient; // [popover showRelativeToRect:[sender bounds] ofView:sender preferredEdge:NSMinYEdge]; // self.popover = popover; // } // [self.testVC addtrackingAreaInfo]; // self.testVC.callback = ^(NSInteger index) { // if (index == 1) { // KMVerificationWindowController *vc = [KMVerificationWindowController verificationWithType:KMVerificationTypeActivate]; // [vc showWindow:nil]; // } else if (index == 2) { // NSURL *url = [NSURL URLWithString:@"https://www.pdfreaderpro.com/store/pdftecheditor"]; // // [[NSWorkspace sharedWorkspace] openURL:url]; // } // }; // self.testVC.mouseEventCall = ^(BOOL mouseEnter) { // if (mouseEnter) { // weakSelf.stopPopOverHide = YES; // if (ActivityStatusTrialExpire == [VerificationManager manager].status || // ActivityStatusTrial == [VerificationManager manager].status) { // if (!weakSelf.popover.isShown) { // [weakSelf upgradeButtonAction:weakSelf.upgradButton]; // } // } // } else { // weakSelf.stopPopOverHide = NO; // if (weakSelf.popover) { // [weakSelf.popover close]; // } // [weakSelf.testVC removeTrackingAreaInfo]; // } // }; // NSPopover *popover = [[NSPopover alloc] init]; // popover.delegate = self; // popover.contentViewController = self.testVC; // popover.animates = YES; // popover.behavior = NSPopoverBehaviorTransient; // [popover showRelativeToRect:[sender bounds] ofView:sender preferredEdge:NSMinYEdge]; // self.popover = popover; if (IAPProductsManager.defaultManager.isAvailableAllFunction == NO) { [[KMPurchaseCompareWindowController sharedInstance] showWindow:nil]; } } else if (ActivityStatusTrial == [VerificationManager manager].status) { [[KMPurchaseCompareWindowController sharedInstance] showWindow:nil]; } else if (ActivityStatusVerifExpire == [VerificationManager manager].status) { if (IAPProductsManager.defaultManager.isAvailableAllFunction == NO) { [[KMPurchaseCompareWindowController sharedInstance] showWindow:nil]; } // KMVerificationWindowController *vc = [KMVerificationWindowController verificationWithType:KMVerificationTypeActivateExpired]; // [vc showWindow:nil]; } else if (ActivityStatusVerification == [VerificationManager manager].status) { if ([VerificationManager manager].needUpgradeLicense) { NSURL *url = [NSURL URLWithString:@"https://www.pdfreaderpro.com/store/pdftecheditor?product_code=product_2"]; [[NSWorkspace sharedWorkspace] openURL:url]; } } else { if ([VerificationManager manager].needUpgradeLicense) { NSURL *url = [NSURL URLWithString:@"https://www.pdfreaderpro.com/store/pdftecheditor?product_code=product_2"]; [[NSWorkspace sharedWorkspace] openURL:url]; // [[FMTrackEventManager defaultManager] trackEventWithEvent:@"Tbr" withProperties:@{@"Tbr_Btn":@"Btn_Tbr_PDFtoOffice"}]; } else { KMVerificationWindowController *vc = [KMVerificationWindowController verificationWithType:KMVerificationTypeNormal]; [vc showWindow:nil]; } } #else isFree = YES; #if VERSION_FREE if ([IAPProductsManager defaultManager].isAvailableAllFunction) { isFree = NO; } if (isFree) { [[KMPurchaseCompareWindowController sharedInstance] showWindow:nil]; } #else if (![IAPProductsManager defaultManager].isAvailableAdvancedPDFToOffice) { // KMToolCompareWindowController *vc = [KMToolCompareWindowController toolCompareWithType:KMCompareWithToolType_Convert setSelectIndex:0]; KMToolCompareWindowController *vc = [KMToolCompareWindowController toolCompareWithToolType:KMCompareWithToolTypeConvert selectNum:0]; [vc.window center]; [vc showWindow:nil]; } #endif #endif } #pragma mark - Private Method - (NSAttributedString *)numberConvertColor:(NSColor*)color String:(NSString *)string { NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string]; NSString *temp = nil; for (NSUInteger i = 0; i