KMRecommondInfo.m 12 KB

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