123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231 |
- //
- // lastFileManager.m
- // Private-X
- //
- // Created by xd h on 2024/6/19.
- //
- #import "lastFileManager.h"
- #import <MJExtension.h>
- @implementation lastFileManager
- + (instancetype)shareManager {
- static lastFileManager *_instance;
- static dispatch_once_t onceToken;
- dispatch_once(&onceToken, ^{
- _instance = [[self alloc] init];
- });
- return _instance;
- }
- - (instancetype)init {
- if (self = [super init]) {
- _saveDays = 90;
- }
- return self;
- }
- - (void)setUid:(NSString *)uid
- {
- _uid = uid;
- _lastFileList = nil;
- _lastFileListArr = nil;
- }
- //- (NSString*)uid{
- // if(!_uid || _uid.length == 0){
- // return @"userName";
- // }
- //
- // return _uid;
- //}
- - (NSString *)getFullDirector {
- NSString *account = self.uid;
- if(!account){
- account = @"userName";
- }
-
- if (account.length != 0)
- {
- NSString *fileFolder = [HWDataManager documentPathForAccount:account fileFolder:KLastFileDirector];
- // 创建文件储存路径
- if (![[NSFileManager defaultManager] fileExistsAtPath:fileFolder]) {
- [[NSFileManager defaultManager] createDirectoryAtPath:fileFolder withIntermediateDirectories:YES attributes:nil error:nil];
- }
- return fileFolder;
- }else {
- HLog(@"创建文件失败!");
- return @"";
- }
- }
- #pragma mark- last file plist Path
- - (NSString *)getLastFilePlistPath {
- NSString *fileFolder = [self getFullDirector];
- return [fileFolder stringByAppendingPathComponent:@"lastFile.plist"];;
- }
- #pragma mark- last file plist
- - (NSMutableDictionary *)lastFileList {
- @synchronized (self) {
- if (!_lastFileList) { // 内存没有
- _lastFileList = [[NSDictionary dictionaryWithContentsOfFile:[self getLastFilePlistPath]] mutableCopy]; // 本地加载
- if (!_lastFileList) { // 本地没有,分配内存
- _lastFileList = [NSMutableDictionary dictionary];
- }
- }
- return _lastFileList;
- }
- }
- - (NSMutableArray*)lastFileListArr{
- @synchronized (self) {
- if (!_lastFileListArr) { // 内存没有
- NSMutableArray *dataArr = [NSMutableArray new];
- if (self.lastFileList) {
- NSDictionary *preLastFileList = [NSDictionary dictionaryWithDictionary:self.lastFileList];
- for (NSString *key in preLastFileList) {
- NSDictionary *dict = preLastFileList[key];
-
- lastFileModel *model = [lastFileModel mj_objectWithKeyValues:dict];
- if(model){
- NSTimeInterval preTime = model.lastPreTime;
- if (![self areTimes:preTime MoreThanDays:self.saveDays]) {
- [dataArr addObject:model];
- }
- else{
- [self.lastFileList removeObjectForKey:key];
- }
- }
- }
- }
-
- if(dataArr.count > 0){//排序
- NSArray *sortArr = [dataArr sortedArrayUsingComparator:^NSComparisonResult(lastFileModel* obj1, lastFileModel* obj2) {
- NSTimeInterval time1 = obj1.lastPreTime;
- NSTimeInterval time2 = obj2.lastPreTime;
- if (time1 < time2) {
- return NSOrderedDescending;
- }
- if (time1 > time2) {
- return NSOrderedAscending;
- }
- return NSOrderedSame;
- }];
-
- _lastFileListArr = [NSMutableArray arrayWithArray:sortArr];
- }
- else{
- _lastFileListArr = [NSMutableArray new];
- }
-
- }
- return _lastFileListArr;
- }
-
- }
- /** 增加配置信息 */
- - (BOOL)saveFileInfoWith:(lastFileModel *)lastFileMod with:(NSString*)fullPath {
-
- if(!lastFileMod || !fullPath){
- return NO;
- }
-
- BOOL flag = NO;
- @synchronized (self) {
- NSString *key = fullPath;
- //1.本地持久化
- NSMutableDictionary *dictM = self.lastFileList;
- //时间戳要刷新
- //lastFileMod.lastPreTime = [iTools getNowTimeStamp];
-
- NSDictionary *dict = [lastFileMod lastFileInfoFun];
- if(dict){
- [dictM setObject:dict forKey:key];
- }
- NSString * PlistPath = [self getLastFilePlistPath];
- flag = [dictM writeToFile:PlistPath atomically:YES];
-
- //2.写到内存
- BOOL isNewObj = YES;
- for (lastFileModel*preModel in self.lastFileListArr) {
- if([preModel.path isEqualToString:lastFileMod.path]){
- //isNewObj = NO;
- preModel.lastPreTime = lastFileMod.lastPreTime;
-
- //删除掉 后面在添加到首位
- [self.lastFileListArr removeObject:preModel];
- break;
- }
- }
-
- if(isNewObj){
- [self.lastFileListArr insertObject:lastFileMod atIndex:0];
- }
- }
-
- return flag;
-
- }
- /** 删除配置信息 */
- - (BOOL)deleteFileInfoWithUrl:(NSString *)fullPath {
- if(!fullPath){
- return NO;
- }
-
- BOOL flag = NO;
- @synchronized (self) {
- //1.本地持久化删除
- NSMutableDictionary *dictM = self.lastFileList;
- [dictM removeObjectForKey:fullPath];
- flag = [dictM writeToFile:[self getLastFilePlistPath] atomically:YES];
-
- //内存删除
- for (lastFileModel*preModel in self.lastFileListArr) {
- if([preModel.path isEqualToString:fullPath]){
- [self.lastFileListArr removeObject:preModel];
- break;
- }
- }
-
- }
- return flag;
- }
- /** 时间戳距离现在是否超过多少天 */
- - (BOOL)areTimes:(NSTimeInterval)timestamp1 MoreThanDays:(NSInteger)days {
- // 将时间戳转换为NSDate对象
- NSDate *date1 = [NSDate dateWithTimeIntervalSince1970:timestamp1];
- NSDate *date2 = [NSDate date];
-
- // 创建一个日历对象
- NSCalendar *calendar = [NSCalendar currentCalendar];
-
- // 计算两个日期之间的组件差异
- NSDateComponents *components = [calendar components:NSCalendarUnitDay
- fromDate:date1
- toDate:date2
- options:0];
-
- // 获取天数差
- NSInteger daysBetween = [components day];
-
- // // 如果日期是逆序的,我们需要取绝对值或调整计算逻辑
- // if (date1.compare(date2) == NSOrderedDescending) {
- // daysBetween = -daysBetween;
- // }
-
- // 检查天数差是否大于90
- return labs(daysBetween) > days;
- }
- @end
|