KMEmailSubWindowController.m 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. //
  2. // KMEmailSubWindowController.m
  3. // PDF Reader Pro Edition
  4. //
  5. // Created by 王帅 on 2019/4/18.
  6. //
  7. #import "KMEmailSubWindowController.h"
  8. #import <PDF_Reader_Pro-Swift.h>
  9. @interface KMEmailSubWindowController ()
  10. @property (nonatomic,assign) IBOutlet NSTextField *textLabel;
  11. @property (nonatomic,assign) IBOutlet NSTextField *textField;
  12. @property (nonatomic,assign) IBOutlet NSButton *button;
  13. @property (nonatomic,assign) IBOutlet NSProgressIndicator *indicator;
  14. @end
  15. @implementation KMEmailSubWindowController
  16. #pragma mark - Init Methods
  17. - (instancetype)init {
  18. if (self = [super initWithWindowNibName:@"KMEmailSubWindowController"]) {
  19. }
  20. return self;
  21. }
  22. #pragma mark - Window Methods
  23. - (void)windowDidLoad {
  24. [super windowDidLoad];
  25. // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
  26. [self.window setBackgroundColor:[NSColor whiteColor]];
  27. [self.window setMovableByWindowBackground:YES];
  28. self.textLabel.stringValue = NSLocalizedString(@"Subscribe to get the latest news and special offers!", nil);
  29. self.textField.placeholderString = NSLocalizedString(@"Enter your email", nil);
  30. self.button.title = NSLocalizedString(@"Subscribe Now", nil);
  31. self.button.wantsLayer = YES;
  32. self.button.layer.masksToBounds = YES;
  33. self.button.layer.cornerRadius = 2;
  34. self.button.layer.backgroundColor = [NSColor colorWithRed:46.0/255.0 green:114.0/255.0 blue:230.0/255.0 alpha:1.0].CGColor;
  35. [self.button setTitleColorWithColor:[NSColor whiteColor] font:nil];
  36. }
  37. #pragma mark - Button Actions
  38. - (IBAction)buttonAction:(id)sender {
  39. if (![self isValidateEmail:self.textField.stringValue]) {
  40. NSAlert *alert = [[NSAlert alloc] init];
  41. [alert setAlertStyle:NSAlertStyleCritical];
  42. [alert setMessageText:@""];
  43. [alert setInformativeText:NSLocalizedString(@"Please enter valid email", nil)];
  44. [alert addButtonWithTitle:NSLocalizedString(@"OK", nil)];
  45. [alert runModal];
  46. return;
  47. }
  48. self.button.enabled = NO;
  49. self.textField.enabled = NO;
  50. self.indicator.hidden = NO;
  51. [self.indicator startAnimation:nil];
  52. [self subscriptionWithEmail:self.textField.stringValue completionHandler:^(NSError *error) {
  53. self.button.enabled = YES;
  54. self.textField.enabled = YES;
  55. self.indicator.hidden = YES;
  56. [self.indicator stopAnimation:nil];
  57. if (!error) {
  58. [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"KMEmailSubscriptionShowKey"];
  59. [[NSUserDefaults standardUserDefaults] synchronize];
  60. [self.window close];
  61. }
  62. }];
  63. }
  64. #pragma mark - Private Methods
  65. - (BOOL)isValidateEmail:(NSString *)email {
  66. NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
  67. NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES%@",emailRegex];
  68. return [emailTest evaluateWithObject:email];
  69. }
  70. - (void)subscriptionWithEmail:(NSString *)email completionHandler:(void (^)(NSError *error))handler {
  71. NSString *string = @"https://store.pdfreaderpro.com/api/member/subscribe";
  72. NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:string]];
  73. [request setHTTPMethod:@"POST"];
  74. [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
  75. NSDictionary *body = @{@"member" : @{@"email" : email}};
  76. [request setHTTPBody:[NSJSONSerialization dataWithJSONObject:body options:NSJSONWritingPrettyPrinted error:nil]];
  77. NSURLSession *session = [NSURLSession sharedSession];
  78. NSURLSessionDataTask *task = [session dataTaskWithRequest:request
  79. completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
  80. if (!error) {
  81. NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data
  82. options:NSJSONReadingMutableContainers error:nil];
  83. NSDictionary *dataDic = dic[@"member"];
  84. if (dataDic) {
  85. } else {
  86. error = [NSError errorWithDomain:@"" code:0 userInfo:dic];
  87. }
  88. }
  89. dispatch_async(dispatch_get_main_queue(), ^{
  90. if (handler) {
  91. handler(error);
  92. }
  93. });
  94. }];
  95. [task resume];
  96. }
  97. #pragma mark - Public Methods
  98. + (BOOL)isAllowsShow {
  99. if ([[NSUserDefaults standardUserDefaults] objectForKey:@"KMEmailSubscriptionShowKey"]) {
  100. return NO;
  101. }
  102. return YES;
  103. }
  104. + (KMEmailSubWindowController *)windowController {
  105. static KMEmailSubWindowController *singleton = nil;
  106. static dispatch_once_t pred;
  107. dispatch_once(&pred, ^{
  108. singleton = [[super allocWithZone:NULL] init];
  109. });
  110. return singleton;
  111. }
  112. + (instancetype)allocWithZone:(struct _NSZone *)zone {
  113. return [self windowController];
  114. }
  115. @end