|
@@ -11,154 +11,202 @@ public let KAutoSaveTimeValueChangedNoti = "KAutoSaveTimeValueChangedNoti"
|
|
|
|
|
|
class AutoSaveManager: NSObject {
|
|
|
//APP是否允许进行自动缓存
|
|
|
- var autoSaveEnabled = false
|
|
|
- var timeInterval: CGFloat = 15
|
|
|
- /*
|
|
|
- @interface AutoSaveManager : NSObject
|
|
|
-
|
|
|
- @property (nonatomic, copy, readonly) NSString *autoSaveFolder;
|
|
|
-
|
|
|
- @property (nonatomic, strong, readonly) NSMutableArray *originalPaths;//保存的原文件信息
|
|
|
-
|
|
|
- @property (nonatomic, strong, readonly) NSMutableArray *autoSavePaths;//对应保存的信息
|
|
|
-
|
|
|
- @property (nonatomic, strong) NSMutableArray *opendPaths;//所有打开的文件信息汇总
|
|
|
-
|
|
|
- @property (nonatomic, assign) BOOL;
|
|
|
-
|
|
|
- @property (nonatomic, assign) BOOL autoSaveAlertShow;//防止重复弹出
|
|
|
-
|
|
|
- @property (nonatomic, assign) BOOL autoSaveDidEndAction;
|
|
|
-
|
|
|
- @property (nonatomic, assign) BOOL isSaving;//当前是否正在保存
|
|
|
-
|
|
|
- @property (nonatomic, assign) CGFloat ;
|
|
|
-
|
|
|
- + (AutoSaveManager *)manager;
|
|
|
-
|
|
|
- - (void)clearCache;
|
|
|
-
|
|
|
- - (NSString *)autoSaveWithPath:(NSString *)filePath;
|
|
|
-
|
|
|
- - (void)removeAutoSavePath:(NSString *)filePath;
|
|
|
-
|
|
|
- @end
|
|
|
- */
|
|
|
+ var autoSaveEnabled = false {
|
|
|
+ didSet {
|
|
|
+ UserDefaults.standard.setValue(self.autoSaveEnabled, forKey: "ComAutoSaveKey")
|
|
|
+ UserDefaults.standard.synchronize()
|
|
|
+ }
|
|
|
+ }
|
|
|
+ var timeInterval: CGFloat = 15 {
|
|
|
+ didSet {
|
|
|
+ UserDefaults.standard.setValue(Float(self.timeInterval), forKey: "AutoSaveTimeIntervalKey")
|
|
|
+ UserDefaults.standard.synchronize()
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //防止重复弹出
|
|
|
+ var autoSaveAlertShow = false
|
|
|
+ var autoSaveDidEndAction = false
|
|
|
+ // /当前是否正在保存
|
|
|
+ var isSaving = false
|
|
|
+
|
|
|
+ private var _autoSaveFolder: String = ""
|
|
|
+ var autoSaveFolder: String {
|
|
|
+ get {
|
|
|
+ return self._autoSaveFolder
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private var _infoDict: NSMutableDictionary?
|
|
|
+ // 保存的原文件信息
|
|
|
+ private var _originalPaths: NSMutableArray?
|
|
|
+ var originalPaths: NSMutableArray? {
|
|
|
+ get {
|
|
|
+ return self._originalPaths
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 对应保存的信息
|
|
|
+ private var _autoSavePaths: NSMutableArray?
|
|
|
+ var autoSavePaths: NSMutableArray? {
|
|
|
+ get {
|
|
|
+ return self._autoSavePaths
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 所有打开的文件信息汇总
|
|
|
+ private var _opendPaths: NSMutableArray?
|
|
|
|
|
|
- static let manager = AutoSaveManager()
|
|
|
+ static let manager: AutoSaveManager = {
|
|
|
+ let man = AutoSaveManager()
|
|
|
+ man.autoSaveEnabled = UserDefaults.standard.bool(forKey: "ComAutoSaveKey")
|
|
|
+
|
|
|
+ let timeInterval = UserDefaults.standard.float(forKey: "AutoSaveTimeIntervalKey")
|
|
|
+ if timeInterval > 0 {
|
|
|
+ man.timeInterval = timeInterval.cgFloat
|
|
|
+ } else {
|
|
|
+ man.timeInterval = 15
|
|
|
+ }
|
|
|
+
|
|
|
+ man._loadData()
|
|
|
+
|
|
|
+ man.autoSaveDidEndAction = true
|
|
|
+ return man
|
|
|
+ }()
|
|
|
+
|
|
|
+ // MARK: - Public Method
|
|
|
+
|
|
|
+ func clearCache() {
|
|
|
+ if FileManager.default.fileExists(atPath: self.autoSaveFolder) {
|
|
|
+ try?FileManager.default.removeItem(atPath: self.autoSaveFolder)
|
|
|
+ }
|
|
|
+ if FileManager.default.fileExists(atPath: self.autoSaveFolder) == false {
|
|
|
+ try?FileManager.default.createDirectory(atPath: self.autoSaveFolder, withIntermediateDirectories: true)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ func autoSaveWithPath(_ filePath: String) -> String {
|
|
|
+ let autoSaveFolder = self.autoSaveFolder
|
|
|
+
|
|
|
+ let plistPath = String(format: "%@/autoSaveInfo.plist", autoSaveFolder)
|
|
|
+ var savePath = String(format: "%@/%@_%@.%@", autoSaveFolder, filePath.deletingPathExtension.lastPathComponent, "recovered", filePath.customPathExtension)
|
|
|
+
|
|
|
+ if FileManager.default.fileExists(atPath: plistPath) {
|
|
|
+ var dict = NSMutableDictionary(contentsOfFile: plistPath)
|
|
|
+ if let keys = dict?.allKeys as? [String], keys.contains(filePath) {
|
|
|
+ savePath = dict?.value(forKey: filePath) as? String ?? ""
|
|
|
+ }
|
|
|
+ if savePath.isEmpty == false && filePath.isEmpty == false {
|
|
|
+ if let values = dict?.allValues as? [String], values.contains(savePath) {
|
|
|
+ //不同路径下同名文件的保存覆盖的情况处理
|
|
|
+ var values = NSArray(array: values)
|
|
|
+
|
|
|
+ for key in dict?.allKeys ?? [] {
|
|
|
+ guard let _key = key as? String else {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ let value = dict?.object(forKey: key) as? String ?? ""
|
|
|
+ if savePath == value && _key != filePath {
|
|
|
+ var count = 1
|
|
|
+ savePath = String(format: "%@/%@_%@(%d).%@", autoSaveFolder, filePath.deletingPathExtension.lastPathComponent, "recovered", count, filePath.customPathExtension)
|
|
|
+ while values.contains(savePath) {
|
|
|
+ count += 1
|
|
|
+ savePath = String(format: "%@/%@_%@(%d).%@", autoSaveFolder, filePath.deletingPathExtension.lastPathComponent, "recovered", count, filePath.customPathExtension)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ dict?.setValue(savePath, forKey: filePath)
|
|
|
+ }
|
|
|
+ try?dict?.write(to: URL(fileURLWithPath: plistPath))
|
|
|
+ } else {
|
|
|
+ var dict = NSMutableDictionary()
|
|
|
+ if savePath.isEmpty == false && filePath.isEmpty == false {
|
|
|
+ dict.setValue(savePath, forKey: filePath)
|
|
|
+ }
|
|
|
+ try?dict.write(to: URL(fileURLWithPath: plistPath))
|
|
|
+ }
|
|
|
+ return savePath
|
|
|
+ }
|
|
|
+
|
|
|
+ func removeAutoSavePath(_ filePath: String) {
|
|
|
+ let autoSaveFolder = self.autoSaveFolder
|
|
|
+
|
|
|
+ let plistPath = String(format: "%@/autoSaveInfo.plist", autoSaveFolder)
|
|
|
+ if FileManager.default.fileExists(atPath: plistPath) {
|
|
|
+ var dict = NSMutableDictionary(contentsOfFile: plistPath)
|
|
|
+ if let savePath = dict?.value(forKey: filePath) as? String {
|
|
|
+ if FileManager.default.fileExists(atPath: savePath) {
|
|
|
+ try?FileManager.default.removeItem(atPath: savePath)
|
|
|
+ }
|
|
|
+ dict?.removeObject(forKey: filePath)
|
|
|
+ }
|
|
|
+
|
|
|
+ if let keys = dict?.allKeys, keys.count > 0 {
|
|
|
+ try?dict?.write(to: URL(fileURLWithPath: plistPath))
|
|
|
+ } else {
|
|
|
+ try?FileManager.default.removeItem(atPath: plistPath)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // MARK: - Private Methods
|
|
|
+
|
|
|
+ private func _loadData() {
|
|
|
+ var cachesPath = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true).first ?? ""
|
|
|
+
|
|
|
+ let filepath = "\(cachesPath)/\(Bundle.main.bundleIdentifier ?? "")"
|
|
|
+ if FileManager.default.fileExists(atPath: filepath) {
|
|
|
+ cachesPath = filepath
|
|
|
+ }
|
|
|
+ cachesPath = "\(cachesPath)/autoSaveFolder"
|
|
|
+ if FileManager.default.fileExists(atPath: cachesPath) == false {
|
|
|
+ try?FileManager.default.createDirectory(atPath: cachesPath, withIntermediateDirectories: true)
|
|
|
+ }
|
|
|
+ self._autoSaveFolder = cachesPath
|
|
|
+
|
|
|
+ KMPrint("autoSaveFolder===\(cachesPath)")
|
|
|
+
|
|
|
+ let plistPath = String(format: "%@/autoSaveInfo.plist", self.autoSaveFolder)
|
|
|
+ guard let dict = NSMutableDictionary(contentsOfFile: plistPath) else {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if (dict.allKeys.count == 0) {
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ self._infoDict = dict
|
|
|
+
|
|
|
+ if (self._autoSavePaths == nil) {
|
|
|
+ self._autoSavePaths = NSMutableArray()
|
|
|
+ }
|
|
|
+
|
|
|
+ if (self._originalPaths == nil) {
|
|
|
+ self._originalPaths = NSMutableArray()
|
|
|
+ }
|
|
|
+
|
|
|
+ self._originalPaths?.addObjects(from: dict.allKeys)
|
|
|
+
|
|
|
+ self._opendPaths = NSMutableArray()
|
|
|
+
|
|
|
+ var arr = (try?FileManager.default.contentsOfDirectory(atPath: self.autoSaveFolder)) ?? []
|
|
|
+
|
|
|
+ for fileName in arr {
|
|
|
+ let savedKey = "\(self.autoSaveFolder)/\(fileName)"
|
|
|
+ guard let values = dict.allValues as? [String] else {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ if values.contains(savedKey) {
|
|
|
+ self.autoSavePaths?.add(savedKey)
|
|
|
+ } else {
|
|
|
+ if fileName == "autoSaveInfo.plist" {
|
|
|
+
|
|
|
+ } else {
|
|
|
+ try?FileManager.default.removeItem(atPath: savedKey)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
/*
|
|
|
- @interface AutoSaveManager ()
|
|
|
-
|
|
|
- @property (nonatomic, strong) NSMutableDictionary *infoDict;
|
|
|
-
|
|
|
- @property (nonatomic, strong, readwrite) NSMutableArray *originalPaths;//保存的原文件信息
|
|
|
-
|
|
|
- @property (nonatomic, strong, readwrite) NSMutableArray *autoSavePaths;
|
|
|
-
|
|
|
- @property (nonatomic, copy, readwrite) NSString *autoSaveFolder;
|
|
|
-
|
|
|
- @end
|
|
|
-
|
|
|
- @implementation AutoSaveManager
|
|
|
-
|
|
|
- + (AutoSaveManager *)manager {
|
|
|
- static AutoSaveManager *__manager = nil;
|
|
|
- if (!__manager) {
|
|
|
- __manager = [[super allocWithZone:NULL] init];
|
|
|
- }
|
|
|
- return __manager;
|
|
|
- }
|
|
|
-
|
|
|
- + (instancetype)allocWithZone:(struct _NSZone *)zone {
|
|
|
- return AutoSaveManager.manager;
|
|
|
- }
|
|
|
-
|
|
|
- - (id)init {
|
|
|
- self = [super init];
|
|
|
- if (self) {
|
|
|
-
|
|
|
- self.autoSaveEnabled = [[NSUserDefaults standardUserDefaults] boolForKey:@"ComAutoSaveKey"];
|
|
|
-
|
|
|
- if ([[NSUserDefaults standardUserDefaults] floatForKey:@"AutoSaveTimeIntervalKey"]) {
|
|
|
- self.timeInterval = [[NSUserDefaults standardUserDefaults] floatForKey:@"AutoSaveTimeIntervalKey"];
|
|
|
- } else {
|
|
|
- self.timeInterval = 15;
|
|
|
- }
|
|
|
-
|
|
|
- [self loadData];
|
|
|
-
|
|
|
- self.autoSaveDidEndAction = YES;
|
|
|
-
|
|
|
- }
|
|
|
- return self;
|
|
|
- }
|
|
|
-
|
|
|
- - (void)loadData {
|
|
|
-
|
|
|
- NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
|
|
|
-
|
|
|
- if ([[NSFileManager defaultManager] fileExistsAtPath:[cachesPath stringByAppendingPathComponent:[NSBundle mainBundle].bundleIdentifier]]) {
|
|
|
- cachesPath = [cachesPath stringByAppendingPathComponent:[NSBundle mainBundle].bundleIdentifier];
|
|
|
- }
|
|
|
- cachesPath = [cachesPath stringByAppendingPathComponent:@"autoSaveFolder"];
|
|
|
- if (![[NSFileManager defaultManager] fileExistsAtPath:cachesPath]) {
|
|
|
- [[NSFileManager defaultManager] createDirectoryAtPath:cachesPath withIntermediateDirectories:YES attributes:nil error:nil];
|
|
|
- }
|
|
|
- self.autoSaveFolder = cachesPath;
|
|
|
-
|
|
|
- #if DEBUG
|
|
|
- NSLog(@"autoSaveFolder===%@",cachesPath);
|
|
|
- #endif
|
|
|
-
|
|
|
- NSString *plistPath = [NSString stringWithFormat:@"%@/autoSaveInfo.plist",self.autoSaveFolder];
|
|
|
- NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
|
|
|
- if (dict.allKeys.count == 0) {
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- self.infoDict = dict;
|
|
|
-
|
|
|
- if (!self.autoSavePaths) {
|
|
|
- self.autoSavePaths = [[NSMutableArray alloc] init];
|
|
|
- }
|
|
|
-
|
|
|
- if (!self.originalPaths) {
|
|
|
- self.originalPaths = [[NSMutableArray alloc] init];
|
|
|
- }
|
|
|
- [self.originalPaths addObjectsFromArray:dict.allKeys];
|
|
|
-
|
|
|
- self.opendPaths = [[NSMutableArray alloc] init];
|
|
|
-
|
|
|
- NSArray *arr = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:self.autoSaveFolder error:nil];
|
|
|
- for (NSString *fileName in arr) {
|
|
|
- NSString *savedKey = [self.autoSaveFolder stringByAppendingPathComponent:fileName];
|
|
|
- if ([dict.allValues containsObject:savedKey]) {
|
|
|
- [self.autoSavePaths addObject:savedKey];
|
|
|
- }else {
|
|
|
- if ([fileName isEqualToString:@"autoSaveInfo.plist"]) {
|
|
|
-
|
|
|
- } else {
|
|
|
- [[NSFileManager defaultManager] removeItemAtPath:savedKey error:nil];
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- #pragma mark - Setter
|
|
|
- - (void)setAutoSaveEnabled:(BOOL)autoSaveEnabled {
|
|
|
- _autoSaveEnabled = autoSaveEnabled;
|
|
|
-
|
|
|
- [[NSUserDefaults standardUserDefaults] setBool:_autoSaveEnabled forKey:@"ComAutoSaveKey"];
|
|
|
- [[NSUserDefaults standardUserDefaults] synchronize];
|
|
|
- }
|
|
|
-
|
|
|
- - (void)setTimeInterval:(CGFloat)timeInterval {
|
|
|
- _timeInterval = timeInterval;
|
|
|
-
|
|
|
- [[NSUserDefaults standardUserDefaults] setFloat:_timeInterval forKey:@"AutoSaveTimeIntervalKey"];
|
|
|
- [[NSUserDefaults standardUserDefaults] synchronize];
|
|
|
- }
|
|
|
|
|
|
- (NSString *)URLEncodedString:(NSString *)string {
|
|
|
CFStringRef stringRef = CFBridgingRetain(string);
|
|
@@ -191,87 +239,6 @@ class AutoSaveManager: NSObject {
|
|
|
}
|
|
|
return newPath;
|
|
|
}
|
|
|
-
|
|
|
- #pragma mark - Public Method
|
|
|
- - (void)clearCache {
|
|
|
- if ([[NSFileManager defaultManager] fileExistsAtPath:self.autoSaveFolder]) {
|
|
|
- [[NSFileManager defaultManager] removeItemAtPath:self.autoSaveFolder error:nil];
|
|
|
- }
|
|
|
- if (![[NSFileManager defaultManager] fileExistsAtPath:self.autoSaveFolder]) {
|
|
|
- [[NSFileManager defaultManager] createDirectoryAtPath:self.autoSaveFolder withIntermediateDirectories:YES attributes:nil error:nil];
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- - (NSString *)autoSaveWithPath:(NSString *)filePath {
|
|
|
-
|
|
|
- NSString *autoSaveFolder = self.autoSaveFolder;
|
|
|
-
|
|
|
- NSString *plistPath = [NSString stringWithFormat:@"%@/autoSaveInfo.plist",autoSaveFolder];
|
|
|
- NSString *savePath = [NSString stringWithFormat:@"%@/%@_%@.%@",autoSaveFolder,filePath.lastPathComponent.stringByDeletingPathExtension,@"recovered",filePath.pathExtension];
|
|
|
-
|
|
|
- if ([[NSFileManager defaultManager] fileExistsAtPath:plistPath]) {
|
|
|
- NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
|
|
|
- if ([dict.allKeys containsObject:filePath]) {
|
|
|
- savePath = [dict valueForKey:filePath];
|
|
|
- }
|
|
|
- if (savePath.length > 0 && filePath.length > 0) {
|
|
|
- if ([dict.allValues containsObject:savePath]) {
|
|
|
- //不同路径下同名文件的保存覆盖的情况处理
|
|
|
- NSArray *values = [[NSArray alloc] initWithArray:dict.allValues];
|
|
|
-
|
|
|
- for (NSString *key in dict.allKeys) {
|
|
|
- NSString *value = [dict objectForKey:key];
|
|
|
- if ([savePath isEqualToString:value] &&
|
|
|
- ![key isEqualToString:filePath]) {
|
|
|
- int count = 1;
|
|
|
- savePath = [NSString stringWithFormat:@"%@/%@_%@(%d).%@",autoSaveFolder,filePath.lastPathComponent.stringByDeletingPathExtension,@"recovered",count,filePath.pathExtension];
|
|
|
- while ([values containsObject:savePath]) {
|
|
|
- count ++;
|
|
|
- savePath = [NSString stringWithFormat:@"%@/%@_%@(%d).%@",autoSaveFolder,filePath.lastPathComponent.stringByDeletingPathExtension,@"recovered",count,filePath.pathExtension];
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- [dict setValue:savePath forKey:filePath];
|
|
|
- }
|
|
|
- [dict writeToURL:[NSURL fileURLWithPath:plistPath] error:nil];
|
|
|
- } else {
|
|
|
- NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
|
|
|
- if (savePath.length > 0 && filePath.length > 0) {
|
|
|
- [dict setValue:savePath forKey:filePath];
|
|
|
- }
|
|
|
- [dict writeToURL:[NSURL fileURLWithPath:plistPath] error:nil];
|
|
|
-
|
|
|
- }
|
|
|
- return savePath;
|
|
|
- }
|
|
|
-
|
|
|
- - (void)removeAutoSavePath:(NSString *)filePath {
|
|
|
-
|
|
|
- NSString *autoSaveFolder = self.autoSaveFolder;
|
|
|
-
|
|
|
- NSString *plistPath = [NSString stringWithFormat:@"%@/autoSaveInfo.plist",autoSaveFolder];
|
|
|
- if ([[NSFileManager defaultManager] fileExistsAtPath:plistPath]) {
|
|
|
- NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
|
|
|
- if ([dict valueForKey:filePath]) {
|
|
|
- NSString *savePath = [dict valueForKey:filePath];
|
|
|
- if ([[NSFileManager defaultManager] fileExistsAtPath:savePath]) {
|
|
|
- [[NSFileManager defaultManager] removeItemAtPath:savePath error:nil];
|
|
|
- }
|
|
|
- [dict removeObjectForKey:filePath];
|
|
|
- }
|
|
|
-
|
|
|
- if (dict.allKeys.count > 0) {
|
|
|
- [dict writeToURL:[NSURL fileURLWithPath:plistPath] error:nil];
|
|
|
- } else {
|
|
|
- [[NSFileManager defaultManager] removeItemAtPath:plistPath error:nil];
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- @end
|
|
|
-
|
|
|
*/
|
|
|
|
|
|
}
|