// // KMRecommondManager.m // PDF Reader Pro Edition // // Created by Niehaoyu on 2023/4/11. // #import "KMRecommondManager.h" @interface KMRecommondManager () @property (nonatomic, strong, readwrite) KMRecommond *pdfProRecommond; @property (nonatomic, strong, readwrite) KMRecommond *othersRecommond; @property (nonatomic, strong, readwrite) KMRecommond *advertisimentRecommond; @property (nonatomic, assign) BOOL isLoading; @end @implementation KMRecommondManager + (KMRecommondManager *)manager { static KMRecommondManager *__manager = nil; if (!__manager) { __manager = [[KMRecommondManager alloc] init]; } return __manager; } - (id)init { self = [super init]; if (self) { } return self; } - (NSString *)appNameFromType:(KMRecommondAPP)appType { if (appType == KMRecommondAPP_PDFReaderProMacLite) { return @"PDFReaderProMac"; } else if (appType == KMRecommondAPP_PDFReaderProMacDMG) { return @"PDFReaderProMacDMG"; } else if (appType == KMRecommondAPP_PDFReaderProMacPro) { return @"PDFReaderProMacPro"; } else if (appType == KMRecommondAPP_RecordIt) { return @"RecordIt"; } else if (appType == KMRecommondAPP_RecordItPro) { return @"RecordItPro"; } else if (appType == KMRecommondAPP_RecordItDMG) { return @"RecordItDMG"; } else if (appType == KMRecommondAPP_FilmageScreen) { return @"FilmageScreen"; } else if (appType == KMRecommondAPP_FilmageScreenPro) { return @"FilmageScreenPro"; } else if (appType == KMRecommondAPP_FilmageScreenDMG) { return @"FilmageScreenDMG"; } else if (appType == KMRecommondAPP_FilmageEditorMac) { return @"FilmageEditorMac"; } else if (appType == KMRecommondAPP_FilmageEditorMacDMG) { return @"FilmageEditorMacDMG"; } else if (appType == KMRecommondAPP_FilmageConverterMac) { return @"FilmageConverterMac"; } else if (appType == KMRecommondAPP_FilmageConverterMacPro) { return @"FilmageConverterMacPro"; } else if (appType == KMRecommondAPP_FilmageConverterMacDMG) { return @"FilmageConverterMacDMG"; } else if (appType == KMRecommondAPP_FilmagePlayerMac) { return @"FilmagePlayerMac"; } else if (appType == KMRecommondAPP_FilmagePlayerMacPro) { return @"FilmagePlayerMacPro"; } else if (appType == KMRecommondAPP_FilmagePlayerMacDMG) { return @"FilmagePlayerMacDMG"; } else if (appType == KMRecommondAPP_PDFReaderProIOS) { return @"PDFReaderProIOS"; } else if (appType == KMRecommondAPP_PDFReaderProIOSPro) { return @"PDFReaderProIOSPro"; } else if (appType == KMRecommondAPP_PDFReaderProAndroid) { return @"PDFReaderProAndroid"; } else if (appType == KMRecommondAPP_PDFMasterMac) { return @"PDFMasterMac"; } else if (appType == KMRecommondAPP_PDFMasterMacPro) { return @"PDFMasterMacPro"; } else if (appType == KMRecommondAPP_PDFMasterMacDMG) { return @"PDFMasterMacDMG"; } return @""; } - (void)loadDataWithAppType:(KMRecommondAPP)appType completionHandler:(void (^)(NSError * _Nonnull))handler { if ([self appNameFromType:appType].length > 0) { [self loadDataWithAppName:[self appNameFromType:appType] completionHandler:handler]; } } - (void)loadDataWithAppName:(NSString *)appName completionHandler:(void (^)(NSError *error))handler { if (self.isLoading) { return; } if (self.pdfProRecommond || self.othersRecommond || self.advertisimentRecommond) { if (handler) { handler(nil); } [[NSNotificationCenter defaultCenter] postNotificationName:KMRecommondDatasUpdateNoti object:nil]; return; } __weak typeof(self) weakSelf = self; self.isLoading = YES; //Send Request NSString *urlString = [kRecommondInfoURL stringByAppendingString:[NSString stringWithFormat:@"/api/advertise-new?app_name=%@",appName?:@""]]; NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; configuration.requestCachePolicy = NSURLRequestReloadIgnoringLocalCacheData; [configuration setURLCache:[NSURLCache sharedURLCache]]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]]; [request setHTTPMethod:@"GET"]; [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration]; NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { self.isLoading = NO; if (!error) { NSDictionary *datas = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil]; if ([datas isKindOfClass:[NSDictionary class]] && datas[@"list"]) { NSArray *listArray = datas[@"list"]; for (NSDictionary *listDict in listArray) { if ([listDict[@"status"] intValue] == 1) { NSString *detailString = listDict[@"detail"]; NSData *jsonData = [detailString dataUsingEncoding:NSUTF8StringEncoding]; NSDictionary *detailDic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil]; if (detailDic[@"recommondContent"]) { if (detailDic[@"recommondContent"][@"recommondContentPDFPro"]) { weakSelf.pdfProRecommond = [[KMRecommond alloc] initWithDict:detailDic[@"recommondContent"][@"recommondContentPDFPro"]]; } if (detailDic[@"recommondContent"][@"recommondContentOther"]) { weakSelf.othersRecommond = [[KMRecommond alloc] initWithDict:detailDic[@"recommondContent"][@"recommondContentOther"]]; } } if (detailDic[@"advertisement"]) { for (NSDictionary *adverDict in detailDic[@"advertisement"]) { weakSelf.advertisimentRecommond = [[KMRecommond alloc] initWithDict:detailDic[@"advertisement"]]; } } } } } } if (handler) { handler(error); } [[NSNotificationCenter defaultCenter] postNotificationName:KMRecommondDatasUpdateNoti object:nil]; }]; [task resume]; } @end