lastFileManager.m 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. for (NSString *key in self.lastFileList) {
  78. NSDictionary *dict = self.lastFileList[key];
  79. lastFileModel *model = [lastFileModel mj_objectWithKeyValues:dict];
  80. if(model){
  81. NSTimeInterval preTime = model.lastPreTime;
  82. if (![self areTimes:preTime MoreThanDays:self.saveDays]) {
  83. [dataArr addObject:model];
  84. }
  85. else{
  86. [self.lastFileList removeObjectForKey:key];
  87. }
  88. }
  89. }
  90. }
  91. if(dataArr.count > 0){//排序
  92. NSArray *sortArr = [dataArr sortedArrayUsingComparator:^NSComparisonResult(lastFileModel* obj1, lastFileModel* obj2) {
  93. NSTimeInterval time1 = obj1.lastPreTime;
  94. NSTimeInterval time2 = obj2.lastPreTime;
  95. if (time1 < time2) {
  96. return NSOrderedDescending;
  97. }
  98. if (time1 > time2) {
  99. return NSOrderedAscending;
  100. }
  101. return NSOrderedSame;
  102. }];
  103. _lastFileListArr = [NSMutableArray arrayWithArray:sortArr];
  104. }
  105. else{
  106. _lastFileListArr = [NSMutableArray new];
  107. }
  108. }
  109. return _lastFileListArr;
  110. }
  111. }
  112. /** 增加配置信息 */
  113. - (BOOL)saveFileInfoWith:(lastFileModel *)lastFileMod with:(NSString*)fullPath {
  114. if(!lastFileMod || !fullPath){
  115. return NO;
  116. }
  117. BOOL flag = NO;
  118. @synchronized (self) {
  119. NSString *key = fullPath;
  120. //1.本地持久化
  121. NSMutableDictionary *dictM = self.lastFileList;
  122. //时间戳要刷新
  123. //lastFileMod.lastPreTime = [iTools getNowTimeStamp];
  124. NSDictionary *dict = [lastFileMod lastFileInfoFun];
  125. if(dict){
  126. [dictM setObject:dict forKey:key];
  127. }
  128. NSString * PlistPath = [self getLastFilePlistPath];
  129. flag = [dictM writeToFile:PlistPath atomically:YES];
  130. //2.写到内存
  131. BOOL isNewObj = YES;
  132. for (lastFileModel*preModel in self.lastFileListArr) {
  133. if([preModel.path isEqualToString:lastFileMod.path]){
  134. //isNewObj = NO;
  135. preModel.lastPreTime = lastFileMod.lastPreTime;
  136. //删除掉 后面在添加到首位
  137. [self.lastFileListArr removeObject:preModel];
  138. break;
  139. }
  140. }
  141. if(isNewObj){
  142. [self.lastFileListArr insertObject:lastFileMod atIndex:0];
  143. }
  144. }
  145. return flag;
  146. }
  147. /** 删除配置信息 */
  148. - (BOOL)deleteFileInfoWithUrl:(NSString *)fullPath {
  149. if(!fullPath){
  150. return NO;
  151. }
  152. BOOL flag = NO;
  153. @synchronized (self) {
  154. //1.本地持久化删除
  155. NSMutableDictionary *dictM = self.lastFileList;
  156. [dictM removeObjectForKey:fullPath];
  157. flag = [dictM writeToFile:[self getLastFilePlistPath] atomically:YES];
  158. //内存删除
  159. for (lastFileModel*preModel in self.lastFileListArr) {
  160. if([preModel.path isEqualToString:fullPath]){
  161. [self.lastFileListArr removeObject:preModel];
  162. break;
  163. }
  164. }
  165. }
  166. return flag;
  167. }
  168. /** 时间戳距离现在是否超过多少天 */
  169. - (BOOL)areTimes:(NSTimeInterval)timestamp1 MoreThanDays:(NSInteger)days {
  170. // 将时间戳转换为NSDate对象
  171. NSDate *date1 = [NSDate dateWithTimeIntervalSince1970:timestamp1];
  172. NSDate *date2 = [NSDate date];
  173. // 创建一个日历对象
  174. NSCalendar *calendar = [NSCalendar currentCalendar];
  175. // 计算两个日期之间的组件差异
  176. NSDateComponents *components = [calendar components:NSCalendarUnitDay
  177. fromDate:date1
  178. toDate:date2
  179. options:0];
  180. // 获取天数差
  181. NSInteger daysBetween = [components day];
  182. // // 如果日期是逆序的,我们需要取绝对值或调整计算逻辑
  183. // if (date1.compare(date2) == NSOrderedDescending) {
  184. // daysBetween = -daysBetween;
  185. // }
  186. // 检查天数差是否大于90
  187. return labs(daysBetween) > days;
  188. }
  189. @end