KMRecommondManager.m 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. //
  2. // KMRecommondManager.m
  3. // PDF Reader Pro Edition
  4. //
  5. // Created by Niehaoyu on 2023/4/11.
  6. //
  7. #import "KMRecommondManager.h"
  8. @interface KMRecommondManager ()
  9. @property (nonatomic, strong, readwrite) KMRecommond *pdfProRecommond;
  10. @property (nonatomic, strong, readwrite) KMRecommond *othersRecommond;
  11. @property (nonatomic, strong, readwrite) KMRecommond *advertisimentRecommond;
  12. @property (nonatomic, assign) BOOL isLoading;
  13. @end
  14. @implementation KMRecommondManager
  15. + (KMRecommondManager *)manager {
  16. static KMRecommondManager *__manager = nil;
  17. if (!__manager) {
  18. __manager = [[KMRecommondManager alloc] init];
  19. }
  20. return __manager;
  21. }
  22. - (id)init {
  23. self = [super init];
  24. if (self) {
  25. }
  26. return self;
  27. }
  28. - (NSString *)appNameFromType:(KMRecommondAPP)appType {
  29. if (appType == KMRecommondAPP_PDFReaderProMacLite) {
  30. return @"PDFReaderProMac";
  31. } else if (appType == KMRecommondAPP_PDFReaderProMacDMG) {
  32. return @"PDFReaderProMacDMG";
  33. } else if (appType == KMRecommondAPP_PDFReaderProMacPro) {
  34. return @"PDFReaderProMacPro";
  35. } else if (appType == KMRecommondAPP_RecordIt) {
  36. return @"RecordIt";
  37. } else if (appType == KMRecommondAPP_RecordItPro) {
  38. return @"RecordItPro";
  39. } else if (appType == KMRecommondAPP_RecordItDMG) {
  40. return @"RecordItDMG";
  41. } else if (appType == KMRecommondAPP_FilmageScreen) {
  42. return @"FilmageScreen";
  43. } else if (appType == KMRecommondAPP_FilmageScreenPro) {
  44. return @"FilmageScreenPro";
  45. } else if (appType == KMRecommondAPP_FilmageScreenDMG) {
  46. return @"FilmageScreenDMG";
  47. } else if (appType == KMRecommondAPP_FilmageEditorMac) {
  48. return @"FilmageEditorMac";
  49. } else if (appType == KMRecommondAPP_FilmageEditorMacDMG) {
  50. return @"FilmageEditorMacDMG";
  51. } else if (appType == KMRecommondAPP_FilmageConverterMac) {
  52. return @"FilmageConverterMac";
  53. } else if (appType == KMRecommondAPP_FilmageConverterMacPro) {
  54. return @"FilmageConverterMacPro";
  55. } else if (appType == KMRecommondAPP_FilmageConverterMacDMG) {
  56. return @"FilmageConverterMacDMG";
  57. } else if (appType == KMRecommondAPP_FilmagePlayerMac) {
  58. return @"FilmagePlayerMac";
  59. } else if (appType == KMRecommondAPP_FilmagePlayerMacPro) {
  60. return @"FilmagePlayerMacPro";
  61. } else if (appType == KMRecommondAPP_FilmagePlayerMacDMG) {
  62. return @"FilmagePlayerMacDMG";
  63. } else if (appType == KMRecommondAPP_PDFReaderProIOS) {
  64. return @"PDFReaderProIOS";
  65. } else if (appType == KMRecommondAPP_PDFReaderProIOSPro) {
  66. return @"PDFReaderProIOSPro";
  67. } else if (appType == KMRecommondAPP_PDFReaderProAndroid) {
  68. return @"PDFReaderProAndroid";
  69. } else if (appType == KMRecommondAPP_PDFMasterMac) {
  70. return @"PDFMasterMac";
  71. } else if (appType == KMRecommondAPP_PDFMasterMacPro) {
  72. return @"PDFMasterMacPro";
  73. } else if (appType == KMRecommondAPP_PDFMasterMacDMG) {
  74. return @"PDFMasterMacDMG";
  75. }
  76. return @"";
  77. }
  78. - (void)loadDataWithAppType:(KMRecommondAPP)appType completionHandler:(void (^)(NSError * _Nonnull))handler {
  79. if ([self appNameFromType:appType].length > 0) {
  80. [self loadDataWithAppName:[self appNameFromType:appType] completionHandler:handler];
  81. }
  82. }
  83. - (void)loadDataWithAppName:(NSString *)appName completionHandler:(void (^)(NSError *error))handler {
  84. if (self.isLoading) {
  85. return;
  86. }
  87. if (self.pdfProRecommond ||
  88. self.othersRecommond ||
  89. self.advertisimentRecommond) {
  90. if (handler) {
  91. handler(nil);
  92. }
  93. [[NSNotificationCenter defaultCenter] postNotificationName:KMRecommondDatasUpdateNoti object:nil];
  94. return;
  95. }
  96. __weak typeof(self) weakSelf = self;
  97. self.isLoading = YES;
  98. //Send Request
  99. NSString *urlString = [kRecommondInfoURL stringByAppendingString:[NSString stringWithFormat:@"/api/advertise-new?app_name=%@",appName?:@""]];
  100. NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
  101. configuration.requestCachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
  102. [configuration setURLCache:[NSURLCache sharedURLCache]];
  103. NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]];
  104. [request setHTTPMethod:@"GET"];
  105. [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
  106. NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration];
  107. NSURLSessionDataTask *task = [session dataTaskWithRequest:request
  108. completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
  109. self.isLoading = NO;
  110. if (!error) {
  111. NSDictionary *datas = [NSJSONSerialization JSONObjectWithData:data
  112. options:NSJSONReadingMutableContainers error:nil];
  113. if ([datas isKindOfClass:[NSDictionary class]] && datas[@"list"]) {
  114. NSArray *listArray = datas[@"list"];
  115. for (NSDictionary *listDict in listArray) {
  116. if ([listDict[@"status"] intValue] == 1) {
  117. NSString *detailString = listDict[@"detail"];
  118. NSData *jsonData = [detailString dataUsingEncoding:NSUTF8StringEncoding];
  119. NSDictionary *detailDic = [NSJSONSerialization JSONObjectWithData:jsonData
  120. options:NSJSONReadingMutableContainers
  121. error:nil];
  122. if (detailDic[@"recommondContent"]) {
  123. if (detailDic[@"recommondContent"][@"recommondContentPDFPro"]) {
  124. weakSelf.pdfProRecommond = [[KMRecommond alloc] initWithDict:detailDic[@"recommondContent"][@"recommondContentPDFPro"]];
  125. }
  126. if (detailDic[@"recommondContent"][@"recommondContentOther"]) {
  127. weakSelf.othersRecommond = [[KMRecommond alloc] initWithDict:detailDic[@"recommondContent"][@"recommondContentOther"]];
  128. }
  129. }
  130. if (detailDic[@"advertisement"]) {
  131. for (NSDictionary *adverDict in detailDic[@"advertisement"]) {
  132. weakSelf.advertisimentRecommond = [[KMRecommond alloc] initWithDict:detailDic[@"advertisement"]];
  133. }
  134. }
  135. }
  136. }
  137. }
  138. }
  139. if (handler) {
  140. handler(error);
  141. }
  142. [[NSNotificationCenter defaultCenter] postNotificationName:KMRecommondDatasUpdateNoti object:nil];
  143. }];
  144. [task resume];
  145. }
  146. @end