123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- //
- // KMEmailSubWindowController.m
- // PDF Reader Pro Edition
- //
- // Created by 王帅 on 2019/4/18.
- //
- #import "KMEmailSubWindowController.h"
- #import <PDF_Reader_Pro-Swift.h>
- @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
|