// // KMEmailSubWindowController.m // PDF Reader Pro Edition // // Created by 王帅 on 2019/4/18. // #import "KMEmailSubWindowController.h" #import @interface KMEmailSubWindowController () @property (nonatomic,assign) IBOutlet NSTextField *textLabel; @property (nonatomic,assign) IBOutlet NSTextField *textField; @property (nonatomic,assign) IBOutlet NSButton *button; @property (nonatomic,assign) IBOutlet NSProgressIndicator *indicator; @end @implementation KMEmailSubWindowController #pragma mark - Init Methods - (instancetype)init { if (self = [super initWithWindowNibName:@"KMEmailSubWindowController"]) { } return self; } #pragma mark - Window Methods - (void)windowDidLoad { [super windowDidLoad]; // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file. [self.window setBackgroundColor:[NSColor whiteColor]]; [self.window setMovableByWindowBackground:YES]; self.textLabel.stringValue = NSLocalizedString(@"Subscribe to get the latest news and special offers!", nil); self.textField.placeholderString = NSLocalizedString(@"Enter your email", nil); self.button.title = NSLocalizedString(@"Subscribe Now", nil); self.button.wantsLayer = YES; self.button.layer.masksToBounds = YES; self.button.layer.cornerRadius = 2; self.button.layer.backgroundColor = [NSColor colorWithRed:46.0/255.0 green:114.0/255.0 blue:230.0/255.0 alpha:1.0].CGColor; [self.button setTitleColorWithColor:[NSColor whiteColor] font:nil]; } #pragma mark - Button Actions - (IBAction)buttonAction:(id)sender { if (![self isValidateEmail:self.textField.stringValue]) { NSAlert *alert = [[NSAlert alloc] init]; [alert setAlertStyle:NSAlertStyleCritical]; [alert setMessageText:@""]; [alert setInformativeText:NSLocalizedString(@"Please enter valid email", nil)]; [alert addButtonWithTitle:NSLocalizedString(@"OK", nil)]; [alert runModal]; return; } self.button.enabled = NO; self.textField.enabled = NO; self.indicator.hidden = NO; [self.indicator startAnimation:nil]; [self subscriptionWithEmail:self.textField.stringValue completionHandler:^(NSError *error) { self.button.enabled = YES; self.textField.enabled = YES; self.indicator.hidden = YES; [self.indicator stopAnimation:nil]; if (!error) { [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"KMEmailSubscriptionShowKey"]; [[NSUserDefaults standardUserDefaults] synchronize]; [self.window close]; } }]; } #pragma mark - Private Methods - (BOOL)isValidateEmail:(NSString *)email { NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"; NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES%@",emailRegex]; return [emailTest evaluateWithObject:email]; } - (void)subscriptionWithEmail:(NSString *)email completionHandler:(void (^)(NSError *error))handler { NSString *string = @"https://store.pdfreaderpro.com/api/member/subscribe"; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:string]]; [request setHTTPMethod:@"POST"]; [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; NSDictionary *body = @{@"member" : @{@"email" : email}}; [request setHTTPBody:[NSJSONSerialization dataWithJSONObject:body options:NSJSONWritingPrettyPrinted error:nil]]; NSURLSession *session = [NSURLSession sharedSession]; NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { if (!error) { NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil]; NSDictionary *dataDic = dic[@"member"]; if (dataDic) { } else { error = [NSError errorWithDomain:@"" code:0 userInfo:dic]; } } dispatch_async(dispatch_get_main_queue(), ^{ if (handler) { handler(error); } }); }]; [task resume]; } #pragma mark - Public Methods + (BOOL)isAllowsShow { if ([[NSUserDefaults standardUserDefaults] objectForKey:@"KMEmailSubscriptionShowKey"]) { return NO; } return YES; } + (KMEmailSubWindowController *)windowController { static KMEmailSubWindowController *singleton = nil; static dispatch_once_t pred; dispatch_once(&pred, ^{ singleton = [[super allocWithZone:NULL] init]; }); return singleton; } + (instancetype)allocWithZone:(struct _NSZone *)zone { return [self windowController]; } @end