lastFileManager.m 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. //
  2. // lastFileManager.m
  3. // 双子星云手机
  4. //
  5. // Created by xd h on 2024/6/19.
  6. //
  7. #import "lastFileManager.h"
  8. #import <MJExtension.h>
  9. @implementation lastFileManager
  10. + (instancetype)shareManager {
  11. static lastFileManager *_instance;
  12. static dispatch_once_t onceToken;
  13. dispatch_once(&onceToken, ^{
  14. _instance = [[self alloc] init];
  15. });
  16. return _instance;
  17. }
  18. - (instancetype)init {
  19. if (self = [super init]) {
  20. _saveDays = 90;
  21. }
  22. return self;
  23. }
  24. - (void)setUid:(NSString *)uid
  25. {
  26. _uid = uid;
  27. _lastFileList = nil;
  28. _lastFileListArr = nil;
  29. }
  30. //- (NSString*)uid{
  31. // if(!_uid || _uid.length == 0){
  32. // return @"userName";
  33. // }
  34. //
  35. // return _uid;
  36. //}
  37. - (NSString *)getFullDirector {
  38. NSString *account = self.uid;
  39. if(!account){
  40. account = @"userName";
  41. }
  42. if (account.length != 0)
  43. {
  44. NSString *fileFolder = [HWDataManager documentPathForAccount:account fileFolder:KLastFileDirector];
  45. // 创建文件储存路径
  46. if (![[NSFileManager defaultManager] fileExistsAtPath:fileFolder]) {
  47. [[NSFileManager defaultManager] createDirectoryAtPath:fileFolder withIntermediateDirectories:YES attributes:nil error:nil];
  48. }
  49. return fileFolder;
  50. }else {
  51. HLog(@"创建文件失败!");
  52. return @"";
  53. }
  54. }
  55. #pragma mark- last file plist Path
  56. - (NSString *)getLastFilePlistPath {
  57. NSString *fileFolder = [self getFullDirector];
  58. return [fileFolder stringByAppendingPathComponent:@"lastFile.plist"];;
  59. }
  60. #pragma mark- last file plist
  61. - (NSMutableDictionary *)lastFileList {
  62. @synchronized (self) {
  63. if (!_lastFileList) { // 内存没有
  64. _lastFileList = [[NSDictionary dictionaryWithContentsOfFile:[self getLastFilePlistPath]] mutableCopy]; // 本地加载
  65. if (!_lastFileList) { // 本地没有,分配内存
  66. _lastFileList = [NSMutableDictionary dictionary];
  67. }
  68. }
  69. return _lastFileList;
  70. }
  71. }
  72. - (NSMutableArray*)lastFileListArr{
  73. @synchronized (self) {
  74. if (!_lastFileListArr) { // 内存没有
  75. NSMutableArray *dataArr = [NSMutableArray new];
  76. if (self.lastFileList) {
  77. NSDictionary *preLastFileList = [NSDictionary dictionaryWithDictionary:self.lastFileList];
  78. for (NSString *key in preLastFileList) {
  79. NSDictionary *dict = preLastFileList[key];
  80. lastFileModel *model = [lastFileModel mj_objectWithKeyValues:dict];
  81. if(model){
  82. NSTimeInterval preTime = model.lastPreTime;
  83. if (![self areTimes:preTime MoreThanDays:self.saveDays]) {
  84. [dataArr addObject:model];
  85. }
  86. else{
  87. [self.lastFileList removeObjectForKey:key];
  88. }
  89. }
  90. }
  91. }
  92. if(dataArr.count > 0){//排序
  93. NSArray *sortArr = [dataArr sortedArrayUsingComparator:^NSComparisonResult(lastFileModel* obj1, lastFileModel* obj2) {
  94. NSTimeInterval time1 = obj1.lastPreTime;
  95. NSTimeInterval time2 = obj2.lastPreTime;
  96. if (time1 < time2) {
  97. return NSOrderedDescending;
  98. }
  99. if (time1 > time2) {
  100. return NSOrderedAscending;
  101. }
  102. return NSOrderedSame;
  103. }];
  104. _lastFileListArr = [NSMutableArray arrayWithArray:sortArr];
  105. }
  106. else{
  107. _lastFileListArr = [NSMutableArray new];
  108. }
  109. }
  110. return _lastFileListArr;
  111. }
  112. }
  113. /** 增加配置信息 */
  114. - (BOOL)saveFileInfoWith:(lastFileModel *)lastFileMod with:(NSString*)fullPath {
  115. if(!lastFileMod || !fullPath){
  116. return NO;
  117. }
  118. BOOL flag = NO;
  119. @synchronized (self) {
  120. NSString *key = fullPath;
  121. //1.本地持久化
  122. NSMutableDictionary *dictM = self.lastFileList;
  123. //时间戳要刷新
  124. //lastFileMod.lastPreTime = [iTools getNowTimeStamp];
  125. NSDictionary *dict = [lastFileMod lastFileInfoFun];
  126. if(dict){
  127. [dictM setObject:dict forKey:key];
  128. }
  129. NSString * PlistPath = [self getLastFilePlistPath];
  130. flag = [dictM writeToFile:PlistPath atomically:YES];
  131. //2.写到内存
  132. BOOL isNewObj = YES;
  133. for (lastFileModel*preModel in self.lastFileListArr) {
  134. if([preModel.path isEqualToString:lastFileMod.path]){
  135. //isNewObj = NO;
  136. preModel.lastPreTime = lastFileMod.lastPreTime;
  137. //删除掉 后面在添加到首位
  138. [self.lastFileListArr removeObject:preModel];
  139. break;
  140. }
  141. }
  142. if(isNewObj){
  143. [self.lastFileListArr insertObject:lastFileMod atIndex:0];
  144. }
  145. }
  146. return flag;
  147. }
  148. /** 删除配置信息 */
  149. - (BOOL)deleteFileInfoWithUrl:(NSString *)fullPath {
  150. if(!fullPath){
  151. return NO;
  152. }
  153. BOOL flag = NO;
  154. @synchronized (self) {
  155. //1.本地持久化删除
  156. NSMutableDictionary *dictM = self.lastFileList;
  157. [dictM removeObjectForKey:fullPath];
  158. flag = [dictM writeToFile:[self getLastFilePlistPath] atomically:YES];
  159. //内存删除
  160. for (lastFileModel*preModel in self.lastFileListArr) {
  161. if([preModel.path isEqualToString:fullPath]){
  162. [self.lastFileListArr removeObject:preModel];
  163. break;
  164. }
  165. }
  166. }
  167. return flag;
  168. }
  169. /** 时间戳距离现在是否超过多少天 */
  170. - (BOOL)areTimes:(NSTimeInterval)timestamp1 MoreThanDays:(NSInteger)days {
  171. // 将时间戳转换为NSDate对象
  172. NSDate *date1 = [NSDate dateWithTimeIntervalSince1970:timestamp1];
  173. NSDate *date2 = [NSDate date];
  174. // 创建一个日历对象
  175. NSCalendar *calendar = [NSCalendar currentCalendar];
  176. // 计算两个日期之间的组件差异
  177. NSDateComponents *components = [calendar components:NSCalendarUnitDay
  178. fromDate:date1
  179. toDate:date2
  180. options:0];
  181. // 获取天数差
  182. NSInteger daysBetween = [components day];
  183. // // 如果日期是逆序的,我们需要取绝对值或调整计算逻辑
  184. // if (date1.compare(date2) == NSOrderedDescending) {
  185. // daysBetween = -daysBetween;
  186. // }
  187. // 检查天数差是否大于90
  188. return labs(daysBetween) > days;
  189. }
  190. @end