// // KMRecommondInfo.m // PDF Reader Pro Edition // // Created by Niehaoyu on 2023/4/11. // #import "KMRecommondInfo.h" #import @interface KMRecommond () @property (nonatomic, strong, readwrite) NSMutableArray *recommondInfoArrM; @property (nonatomic, copy) NSDictionary *infoDict; @end @implementation KMRecommond - (id)init { self = [super init]; if (self) { self.recommondInfoArrM = [[NSMutableArray alloc] init]; } return self; } - (id)initWithDict:(NSDictionary *)dict { self = [super init]; if (self) { self.infoDict = dict; self.recommondInfoArrM = [[NSMutableArray alloc] init]; if (self.infoDict[@"content"]) { for (NSDictionary *contentDict in self.infoDict[@"content"]) { KMRecommondInfo *info = [[KMRecommondInfo alloc] initWithDict:contentDict]; [self.recommondInfoArrM addObject:info]; } } } return self; } #pragma mark - Setter and Getter - (NSString *)versionKey { if (!_versionKey) { if (self.infoDict[@"version"]) { _versionKey = self.infoDict[@"version"]; } } return _versionKey; } - (NSString *)name { if (!_name) { if (self.infoDict[@"name"]) { NSDictionary *valueDict = self.infoDict[@"name"]; _name = [valueDict objectForKey:[KMRecommondInfo languageKey]]; } } return _name; } @end #pragma mark - KMRecommondInfo @interface KMRecommondInfo () @property (nonatomic, copy) NSDictionary *infoDict; @end @implementation KMRecommondInfo + (NSString *)languageKey { NSString *language = [[NSBundle mainBundle] preferredLocalizations][0]; if ([language isEqualToString:@"zh_CN"]) { return @"zh_CN"; } else if ([language isEqualToString:@"zh_TW"]) { return @"zh_TW"; } return @"en"; } + (NSString *)cacheDirs { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); NSString *cachesDir = [paths objectAtIndex:0]; if ([[NSFileManager defaultManager] fileExistsAtPath:[cachesDir stringByAppendingPathComponent:[NSBundle mainBundle].bundleIdentifier]]) { cachesDir = [cachesDir stringByAppendingPathComponent:[NSBundle mainBundle].bundleIdentifier]; } cachesDir = [cachesDir stringByAppendingPathComponent:@"Advertisement/ImageCache"]; if (![[NSFileManager defaultManager] fileExistsAtPath:cachesDir]) { [[NSFileManager defaultManager] createDirectoryAtPath:cachesDir withIntermediateDirectories:YES attributes:nil error:nil]; } return cachesDir; } + (BOOL)isDarkMode { BOOL isDarkMode = NO; // if (@available(macOS 10.14, *)) { // NSAppearanceName appearanceName = [[NSApp effectiveAppearance] bestMatchFromAppearancesWithNames:@[NSAppearanceNameAqua, NSAppearanceNameDarkAqua]]; // if ([appearanceName isEqualToString:NSAppearanceNameDarkAqua]) { // isDarkMode = YES; // } // } return isDarkMode; } - (id)initWithDict:(NSDictionary *)dict { self = [super init]; if (self) { self.infoDict = dict; } return self; } - (id)init { self = [super init]; if (self) { } return self; } - (BOOL)show { if ([self.infoDict[@"show"] boolValue]) { double startDateKey = [self.infoDict[@"startTime"] doubleValue]; double endDateKey = [self.infoDict[@"endTime"] doubleValue]; if (startDateKey > 1000000000) { startDateKey = startDateKey/1000; } if (endDateKey > 1000000000) { endDateKey = endDateKey/1000; } NSDate *startDate = [NSDate dateWithTimeIntervalSince1970:startDateKey]; NSDate *endDate = [NSDate dateWithTimeIntervalSince1970:endDateKey]; if ([startDate compare:[NSDate date]] == NSOrderedAscending && [endDate compare:[NSDate date]] == NSOrderedDescending) { return YES; }; } return NO; } - (KMRecommondShowType)showType { KMRecommondShowType type = KMRecommondShowType_None; if ([self.infoDict[@"subscriptionType"] isEqualToString:@"0"]) { type = KMRecommondShowType_All; } else if ([self.infoDict[@"subscriptionType"] isEqualToString:@"2"]) { type = KMRecommondShowType_Pro; } else if ([self.infoDict[@"subscriptionType"] isEqualToString:@"1"]) { type = KMRecommondShowType_Lite; } return type; } - (NSDate *)startDate { if ([self.infoDict[@"endTime"] boolValue]) { double startDateKey = [self.infoDict[@"endTime"] doubleValue]; if (startDateKey > 1000000000) { startDateKey = startDateKey/1000; } NSDate *startDate = [NSDate dateWithTimeIntervalSince1970:startDateKey]; return startDate; } return nil; } - (NSDate *)endDate { if ([self.infoDict[@"startTime"] boolValue]) { double startDateKey = [self.infoDict[@"startTime"] doubleValue]; if (startDateKey > 1000000000) { startDateKey = startDateKey/1000; } NSDate *startDate = [NSDate dateWithTimeIntervalSince1970:startDateKey]; return startDate; } return nil; } - (NSString *)versionKey { if (self.infoDict[@"version"]) { return self.infoDict[@"version"]; } return @""; } - (BOOL)showCloseBtn { if ([self.infoDict[@"showCloseBtn"] boolValue]) { return YES; } return NO; } - (NSString *)title { if (self.infoDict[@"name"]) { NSDictionary *valueDict = self.infoDict[@"name"]; return [valueDict objectForKey:[KMRecommondInfo languageKey]]?:@""; } return @""; } - (NSString *)tooltips { if (self.infoDict[@"tooltip"]) { NSDictionary *valueDict = self.infoDict[@"tooltip"]; return [valueDict objectForKey:[KMRecommondInfo languageKey]]?:@""; } return @""; } - (NSString *)linkURL { if (self.infoDict[@"linkURL"]) { NSDictionary *valueDict = self.infoDict[@"linkURL"]; return [valueDict objectForKey:[KMRecommondInfo languageKey]]?:@""; } return @""; } - (NSImage *)normalImage { NSString *imageName = [self versionKey]?:@""; if ([KMRecommondInfo isDarkMode]) { NSURL *url = [NSURL URLWithString:self.infoDict[@"image"][@"normal_Dark"]]; imageName = [imageName stringByAppendingString:[NSString stringWithFormat:@"%@.%@",@"-normal_Dark",url.path.pathExtension]]; } else { NSURL *url = [NSURL URLWithString:self.infoDict[@"image"][@"normal_Light"]]; imageName = [imageName stringByAppendingString:[NSString stringWithFormat:@"%@.%@",@"-normal_Light",url.path.pathExtension]]; } NSString *imgSavePath = [[KMRecommondInfo cacheDirs] stringByAppendingPathComponent:imageName]; if ([[NSFileManager defaultManager] fileExistsAtPath:imgSavePath]) { return [[NSImage alloc] initWithContentsOfFile:imgSavePath]; } if ([KMRecommondInfo isDarkMode]) { NSURL *url = [NSURL URLWithString:self.infoDict[@"image"][@"normal_Dark"]]; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ NSData *data = [[NSData alloc] initWithContentsOfURL:url]; [data writeToFile:imgSavePath atomically:YES]; [[NSNotificationCenter defaultCenter] postNotificationName:@"KMRecommondInfoUpdateNoti" object:@{@"unique":[self versionKey]}]; }); } else { NSURL *url = [NSURL URLWithString:self.infoDict[@"image"][@"normal_Light"]]; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ NSData *data = [[NSData alloc] initWithContentsOfURL:url]; [data writeToFile:imgSavePath atomically:YES]; [[NSNotificationCenter defaultCenter] postNotificationName:@"KMRecommondInfoUpdateNoti" object:@{@"unique":[self versionKey]}]; }); } return nil; } - (NSImage *)hoverImage { NSString *imageName = [self versionKey]?:@""; if ([KMRecommondInfo isDarkMode]) { NSURL *url = [NSURL URLWithString:self.infoDict[@"image"][@"hover_Dark"]]; imageName = [imageName stringByAppendingString:[NSString stringWithFormat:@"%@.%@",@"-hover_Dark",url.path.pathExtension]]; } else { NSURL *url = [NSURL URLWithString:self.infoDict[@"image"][@"hover_Light"]]; imageName = [imageName stringByAppendingString:[NSString stringWithFormat:@"%@.%@",@"-hover_Light",url.path.pathExtension]]; } NSString *imgSavePath = [[KMRecommondInfo cacheDirs] stringByAppendingPathComponent:imageName]; if ([[NSFileManager defaultManager] fileExistsAtPath:imgSavePath]) { return [[NSImage alloc] initWithContentsOfFile:imgSavePath]; } if ([KMRecommondInfo isDarkMode]) { NSURL *url = [NSURL URLWithString:self.infoDict[@"image"][@"hover_Dark"]]; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ NSData *data = [[NSData alloc] initWithContentsOfURL:url]; [data writeToFile:imgSavePath atomically:YES]; [[NSNotificationCenter defaultCenter] postNotificationName:@"KMRecommondInfoUpdateNoti" object:@{@"unique":[self versionKey]}]; }); } else { NSURL *url = [NSURL URLWithString:self.infoDict[@"image"][@"hover_Light"]]; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ NSData *data = [[NSData alloc] initWithContentsOfURL:url]; [data writeToFile:imgSavePath atomically:YES]; [[NSNotificationCenter defaultCenter] postNotificationName:@"KMRecommondInfoUpdateNoti" object:@{@"unique":[self versionKey]}]; }); } return nil; } - (NSImage *)iconImage { NSString *imageName = [self versionKey]?:@""; NSURL *url = [NSURL URLWithString:[self.infoDict[@"image"] objectForKey:[KMRecommondInfo languageKey]]]; imageName = [imageName stringByAppendingString:[NSString stringWithFormat:@"%@.%@",@"-iconImage",url.path.pathExtension]]; NSString *imgSavePath = [[KMRecommondInfo cacheDirs] stringByAppendingPathComponent:imageName]; if ([[NSFileManager defaultManager] fileExistsAtPath:imgSavePath]) { return [[NSImage alloc] initWithContentsOfFile:imgSavePath]; } dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ NSData *data = [[NSData alloc] initWithContentsOfURL:url]; [data writeToFile:imgSavePath atomically:YES]; [[NSNotificationCenter defaultCenter] postNotificationName:@"KMRecommondInfoUpdateNoti" object:@{@"unique":[self versionKey]}]; }); return nil; } - (NSString *)firebaseEvent { if (self.infoDict[@"Firebase"]) { return self.infoDict[@"Firebase"][@"Event"]?:@""; } return nil; } - (NSString *)firebasePropertyKey { if (self.infoDict[@"Firebase"]) { return self.infoDict[@"Firebase"][@"PropertyKey"]?:@""; } return nil; } - (NSString *)firebasePropertyValue { if (self.infoDict[@"Firebase"]) { return self.infoDict[@"Firebase"][@"PropertyValue"]?:@""; } return nil; } #pragma mark - Public Method - (void)updateValue:(NSString *)value forKey:(NSString *)key { if (self.infoDict[key]) { NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithDictionary:self.infoDict]; [dict setValue:value forKey:key]; self.infoDict = dict; } } @end