123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337 |
- //
- // KMRecommondInfo.m
- // PDF Reader Pro Edition
- //
- // Created by Niehaoyu on 2023/4/11.
- //
- #import "KMRecommondInfo.h"
- #import <Cocoa/Cocoa.h>
- @interface KMRecommond ()
- @property (nonatomic, strong, readwrite) NSMutableArray <KMRecommondInfo *> *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
|