KMRecommondInfo.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. //
  2. // KMRecommondInfo.m
  3. // PDF Reader Pro Edition
  4. //
  5. // Created by Niehaoyu on 2023/4/11.
  6. //
  7. #import "KMRecommondInfo.h"
  8. #import <Cocoa/Cocoa.h>
  9. @interface KMRecommond ()
  10. @property (nonatomic, strong, readwrite) NSMutableArray <KMRecommondInfo *> *recommondInfoArrM;
  11. @property (nonatomic, copy) NSDictionary *infoDict;
  12. @end
  13. @implementation KMRecommond
  14. - (id)init {
  15. self = [super init];
  16. if (self) {
  17. self.recommondInfoArrM = [[NSMutableArray alloc] init];
  18. }
  19. return self;
  20. }
  21. - (id)initWithDict:(NSDictionary *)dict {
  22. self = [super init];
  23. if (self) {
  24. self.infoDict = dict;
  25. self.recommondInfoArrM = [[NSMutableArray alloc] init];
  26. if (self.infoDict[@"content"]) {
  27. for (NSDictionary *contentDict in self.infoDict[@"content"]) {
  28. KMRecommondInfo *info = [[KMRecommondInfo alloc] initWithDict:contentDict];
  29. [self.recommondInfoArrM addObject:info];
  30. }
  31. }
  32. }
  33. return self;
  34. }
  35. #pragma mark - Setter and Getter
  36. - (NSString *)versionKey {
  37. if (!_versionKey) {
  38. if (self.infoDict[@"version"]) {
  39. _versionKey = self.infoDict[@"version"];
  40. }
  41. }
  42. return _versionKey;
  43. }
  44. - (NSString *)name {
  45. if (!_name) {
  46. if (self.infoDict[@"name"]) {
  47. NSDictionary *valueDict = self.infoDict[@"name"];
  48. _name = [valueDict objectForKey:[KMRecommondInfo languageKey]];
  49. }
  50. }
  51. return _name;
  52. }
  53. @end
  54. #pragma mark - KMRecommondInfo
  55. @interface KMRecommondInfo ()
  56. @property (nonatomic, copy) NSDictionary *infoDict;
  57. @end
  58. @implementation KMRecommondInfo
  59. + (NSString *)languageKey {
  60. NSString *language = [[NSBundle mainBundle] preferredLocalizations][0];
  61. if ([language isEqualToString:@"zh_CN"]) {
  62. return @"zh_CN";
  63. } else if ([language isEqualToString:@"zh_TW"]) {
  64. return @"zh_TW";
  65. }
  66. return @"en";
  67. }
  68. + (NSString *)cacheDirs {
  69. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  70. NSString *cachesDir = [paths objectAtIndex:0];
  71. if ([[NSFileManager defaultManager] fileExistsAtPath:[cachesDir stringByAppendingPathComponent:[NSBundle mainBundle].bundleIdentifier]]) {
  72. cachesDir = [cachesDir stringByAppendingPathComponent:[NSBundle mainBundle].bundleIdentifier];
  73. }
  74. cachesDir = [cachesDir stringByAppendingPathComponent:@"Advertisement/ImageCache"];
  75. if (![[NSFileManager defaultManager] fileExistsAtPath:cachesDir]) {
  76. [[NSFileManager defaultManager] createDirectoryAtPath:cachesDir withIntermediateDirectories:YES attributes:nil error:nil];
  77. }
  78. return cachesDir;
  79. }
  80. + (BOOL)isDarkMode {
  81. BOOL isDarkMode = NO;
  82. // if (@available(macOS 10.14, *)) {
  83. // NSAppearanceName appearanceName = [[NSApp effectiveAppearance] bestMatchFromAppearancesWithNames:@[NSAppearanceNameAqua, NSAppearanceNameDarkAqua]];
  84. // if ([appearanceName isEqualToString:NSAppearanceNameDarkAqua]) {
  85. // isDarkMode = YES;
  86. // }
  87. // }
  88. return isDarkMode;
  89. }
  90. - (id)initWithDict:(NSDictionary *)dict {
  91. self = [super init];
  92. if (self) {
  93. self.infoDict = dict;
  94. }
  95. return self;
  96. }
  97. - (id)init {
  98. self = [super init];
  99. if (self) {
  100. }
  101. return self;
  102. }
  103. - (BOOL)show {
  104. if ([self.infoDict[@"show"] boolValue]) {
  105. double startDateKey = [self.infoDict[@"startTime"] doubleValue];
  106. double endDateKey = [self.infoDict[@"endTime"] doubleValue];
  107. if (startDateKey > 1000000000) {
  108. startDateKey = startDateKey/1000;
  109. }
  110. if (endDateKey > 1000000000) {
  111. endDateKey = endDateKey/1000;
  112. }
  113. NSDate *startDate = [NSDate dateWithTimeIntervalSince1970:startDateKey];
  114. NSDate *endDate = [NSDate dateWithTimeIntervalSince1970:endDateKey];
  115. if ([startDate compare:[NSDate date]] == NSOrderedAscending &&
  116. [endDate compare:[NSDate date]] == NSOrderedDescending) {
  117. return YES;
  118. };
  119. }
  120. return NO;
  121. }
  122. - (KMRecommondShowType)showType {
  123. KMRecommondShowType type = KMRecommondShowType_None;
  124. if ([self.infoDict[@"subscriptionType"] isEqualToString:@"0"]) {
  125. type = KMRecommondShowType_All;
  126. } else if ([self.infoDict[@"subscriptionType"] isEqualToString:@"2"]) {
  127. type = KMRecommondShowType_Pro;
  128. } else if ([self.infoDict[@"subscriptionType"] isEqualToString:@"1"]) {
  129. type = KMRecommondShowType_Lite;
  130. }
  131. return type;
  132. }
  133. - (NSDate *)startDate {
  134. if ([self.infoDict[@"endTime"] boolValue]) {
  135. double startDateKey = [self.infoDict[@"endTime"] doubleValue];
  136. if (startDateKey > 1000000000) {
  137. startDateKey = startDateKey/1000;
  138. }
  139. NSDate *startDate = [NSDate dateWithTimeIntervalSince1970:startDateKey];
  140. return startDate;
  141. }
  142. return nil;
  143. }
  144. - (NSDate *)endDate {
  145. if ([self.infoDict[@"startTime"] boolValue]) {
  146. double startDateKey = [self.infoDict[@"startTime"] doubleValue];
  147. if (startDateKey > 1000000000) {
  148. startDateKey = startDateKey/1000;
  149. }
  150. NSDate *startDate = [NSDate dateWithTimeIntervalSince1970:startDateKey];
  151. return startDate;
  152. }
  153. return nil;
  154. }
  155. - (NSString *)versionKey {
  156. if (self.infoDict[@"version"]) {
  157. return self.infoDict[@"version"];
  158. }
  159. return @"";
  160. }
  161. - (BOOL)showCloseBtn {
  162. if ([self.infoDict[@"showCloseBtn"] boolValue]) {
  163. return YES;
  164. }
  165. return NO;
  166. }
  167. - (NSString *)title {
  168. if (self.infoDict[@"name"]) {
  169. NSDictionary *valueDict = self.infoDict[@"name"];
  170. return [valueDict objectForKey:[KMRecommondInfo languageKey]]?:@"";
  171. }
  172. return @"";
  173. }
  174. - (NSString *)tooltips {
  175. if (self.infoDict[@"tooltip"]) {
  176. NSDictionary *valueDict = self.infoDict[@"tooltip"];
  177. return [valueDict objectForKey:[KMRecommondInfo languageKey]]?:@"";
  178. }
  179. return @"";
  180. }
  181. - (NSString *)linkURL {
  182. if (self.infoDict[@"linkURL"]) {
  183. NSDictionary *valueDict = self.infoDict[@"linkURL"];
  184. return [valueDict objectForKey:[KMRecommondInfo languageKey]]?:@"";
  185. }
  186. return @"";
  187. }
  188. - (NSImage *)normalImage {
  189. NSString *imageName = [self versionKey]?:@"";
  190. if ([KMRecommondInfo isDarkMode]) {
  191. NSURL *url = [NSURL URLWithString:self.infoDict[@"image"][@"normal_Dark"]];
  192. imageName = [imageName stringByAppendingString:[NSString stringWithFormat:@"%@.%@",@"-normal_Dark",url.path.pathExtension]];
  193. } else {
  194. NSURL *url = [NSURL URLWithString:self.infoDict[@"image"][@"normal_Light"]];
  195. imageName = [imageName stringByAppendingString:[NSString stringWithFormat:@"%@.%@",@"-normal_Light",url.path.pathExtension]];
  196. }
  197. NSString *imgSavePath = [[KMRecommondInfo cacheDirs] stringByAppendingPathComponent:imageName];
  198. if ([[NSFileManager defaultManager] fileExistsAtPath:imgSavePath]) {
  199. return [[NSImage alloc] initWithContentsOfFile:imgSavePath];
  200. }
  201. if ([KMRecommondInfo isDarkMode]) {
  202. NSURL *url = [NSURL URLWithString:self.infoDict[@"image"][@"normal_Dark"]];
  203. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  204. NSData *data = [[NSData alloc] initWithContentsOfURL:url];
  205. [data writeToFile:imgSavePath atomically:YES];
  206. [[NSNotificationCenter defaultCenter] postNotificationName:@"KMRecommondInfoUpdateNoti" object:@{@"unique":[self versionKey]}];
  207. });
  208. } else {
  209. NSURL *url = [NSURL URLWithString:self.infoDict[@"image"][@"normal_Light"]];
  210. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  211. NSData *data = [[NSData alloc] initWithContentsOfURL:url];
  212. [data writeToFile:imgSavePath atomically:YES];
  213. [[NSNotificationCenter defaultCenter] postNotificationName:@"KMRecommondInfoUpdateNoti" object:@{@"unique":[self versionKey]}];
  214. });
  215. }
  216. return nil;
  217. }
  218. - (NSImage *)hoverImage {
  219. NSString *imageName = [self versionKey]?:@"";
  220. if ([KMRecommondInfo isDarkMode]) {
  221. NSURL *url = [NSURL URLWithString:self.infoDict[@"image"][@"hover_Dark"]];
  222. imageName = [imageName stringByAppendingString:[NSString stringWithFormat:@"%@.%@",@"-hover_Dark",url.path.pathExtension]];
  223. } else {
  224. NSURL *url = [NSURL URLWithString:self.infoDict[@"image"][@"hover_Light"]];
  225. imageName = [imageName stringByAppendingString:[NSString stringWithFormat:@"%@.%@",@"-hover_Light",url.path.pathExtension]];
  226. }
  227. NSString *imgSavePath = [[KMRecommondInfo cacheDirs] stringByAppendingPathComponent:imageName];
  228. if ([[NSFileManager defaultManager] fileExistsAtPath:imgSavePath]) {
  229. return [[NSImage alloc] initWithContentsOfFile:imgSavePath];
  230. }
  231. if ([KMRecommondInfo isDarkMode]) {
  232. NSURL *url = [NSURL URLWithString:self.infoDict[@"image"][@"hover_Dark"]];
  233. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  234. NSData *data = [[NSData alloc] initWithContentsOfURL:url];
  235. [data writeToFile:imgSavePath atomically:YES];
  236. [[NSNotificationCenter defaultCenter] postNotificationName:@"KMRecommondInfoUpdateNoti" object:@{@"unique":[self versionKey]}];
  237. });
  238. } else {
  239. NSURL *url = [NSURL URLWithString:self.infoDict[@"image"][@"hover_Light"]];
  240. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  241. NSData *data = [[NSData alloc] initWithContentsOfURL:url];
  242. [data writeToFile:imgSavePath atomically:YES];
  243. [[NSNotificationCenter defaultCenter] postNotificationName:@"KMRecommondInfoUpdateNoti" object:@{@"unique":[self versionKey]}];
  244. });
  245. }
  246. return nil;
  247. }
  248. - (NSImage *)iconImage {
  249. NSString *imageName = [self versionKey]?:@"";
  250. NSURL *url = [NSURL URLWithString:[self.infoDict[@"image"] objectForKey:[KMRecommondInfo languageKey]]];
  251. imageName = [imageName stringByAppendingString:[NSString stringWithFormat:@"%@.%@",@"-iconImage",url.path.pathExtension]];
  252. NSString *imgSavePath = [[KMRecommondInfo cacheDirs] stringByAppendingPathComponent:imageName];
  253. if ([[NSFileManager defaultManager] fileExistsAtPath:imgSavePath]) {
  254. return [[NSImage alloc] initWithContentsOfFile:imgSavePath];
  255. }
  256. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  257. NSData *data = [[NSData alloc] initWithContentsOfURL:url];
  258. [data writeToFile:imgSavePath atomically:YES];
  259. [[NSNotificationCenter defaultCenter] postNotificationName:@"KMRecommondInfoUpdateNoti" object:@{@"unique":[self versionKey]}];
  260. });
  261. return nil;
  262. }
  263. - (NSString *)firebaseEvent {
  264. if (self.infoDict[@"Firebase"]) {
  265. return self.infoDict[@"Firebase"][@"Event"]?:@"";
  266. }
  267. return nil;
  268. }
  269. - (NSString *)firebasePropertyKey {
  270. if (self.infoDict[@"Firebase"]) {
  271. return self.infoDict[@"Firebase"][@"PropertyKey"]?:@"";
  272. }
  273. return nil;
  274. }
  275. - (NSString *)firebasePropertyValue {
  276. if (self.infoDict[@"Firebase"]) {
  277. return self.infoDict[@"Firebase"][@"PropertyValue"]?:@"";
  278. }
  279. return nil;
  280. }
  281. #pragma mark - Public Method
  282. - (void)updateValue:(NSString *)value forKey:(NSString *)key {
  283. if (self.infoDict[key]) {
  284. NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithDictionary:self.infoDict];
  285. [dict setValue:value forKey:key];
  286. self.infoDict = dict;
  287. }
  288. }
  289. @end