KMEmailSubWindowController.m 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. //
  2. // KMEmailSubWindowController.m
  3. // Cisdem PDFMaster
  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. //
  76. // NSDictionary *body = @{@"member" : @{@"email" : email}};
  77. // [request setHTTPBody:[NSJSONSerialization dataWithJSONObject:body options:NSJSONWritingPrettyPrinted error:nil]];
  78. //
  79. // NSURLSession *session = [NSURLSession sharedSession];
  80. // NSURLSessionDataTask *task = [session dataTaskWithRequest:request
  81. // completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
  82. // if (!error) {
  83. // NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data
  84. // options:NSJSONReadingMutableContainers error:nil];
  85. // NSDictionary *dataDic = dic[@"member"];
  86. // if (dataDic) {
  87. //
  88. // } else {
  89. // error = [NSError errorWithDomain:@"" code:0 userInfo:dic];
  90. // }
  91. // }
  92. // dispatch_async(dispatch_get_main_queue(), ^{
  93. // if (handler) {
  94. // handler(error);
  95. // }
  96. // });
  97. // }];
  98. // [task resume];
  99. }
  100. #pragma mark - Public Methods
  101. + (BOOL)isAllowsShow {
  102. if ([[NSUserDefaults standardUserDefaults] objectForKey:@"KMEmailSubscriptionShowKey"]) {
  103. return NO;
  104. }
  105. return YES;
  106. }
  107. + (KMEmailSubWindowController *)windowController {
  108. static KMEmailSubWindowController *singleton = nil;
  109. static dispatch_once_t pred;
  110. dispatch_once(&pred, ^{
  111. singleton = [[super allocWithZone:NULL] init];
  112. });
  113. return singleton;
  114. }
  115. + (instancetype)allocWithZone:(struct _NSZone *)zone {
  116. return [self windowController];
  117. }
  118. @end