|
@@ -12,17 +12,14 @@ public let KAutoSaveTimeValueChangedNoti = "KAutoSaveTimeValueChangedNoti"
|
|
|
@objcMembers class AutoSaveManager: NSObject {
|
|
|
public static let kTimeValueChangedNotificationName = Notification.Name(KAutoSaveTimeValueChangedNoti)
|
|
|
|
|
|
- //APP是否允许进行自动缓存
|
|
|
- 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 timeInterval: CGFloat {
|
|
|
+ get {
|
|
|
+ let timeInterval = UserDefaults.standard.float(forKey: "AutoSaveTimeIntervalKey")
|
|
|
+ if timeInterval > 0 {
|
|
|
+ return timeInterval.cgFloat
|
|
|
+ } else {
|
|
|
+ return 15
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -65,15 +62,7 @@ public let KAutoSaveTimeValueChangedNoti = "KAutoSaveTimeValueChangedNoti"
|
|
|
|
|
|
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
|
|
@@ -82,6 +71,10 @@ public let KAutoSaveTimeValueChangedNoti = "KAutoSaveTimeValueChangedNoti"
|
|
|
|
|
|
// MARK: - Public Method
|
|
|
|
|
|
+ func updateTimeInterval () {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
func clearCache() {
|
|
|
if FileManager.default.fileExists(atPath: self.autoSaveFolder) {
|
|
|
try?FileManager.default.removeItem(atPath: self.autoSaveFolder)
|
|
@@ -98,14 +91,14 @@ public let KAutoSaveTimeValueChangedNoti = "KAutoSaveTimeValueChangedNoti"
|
|
|
var savePath = String(format: "%@/%@_%@.%@", autoSaveFolder, filePath.deletingPathExtension.lastPathComponent, "recovered", filePath.customPathExtension)
|
|
|
|
|
|
if FileManager.default.fileExists(atPath: plistPath) {
|
|
|
- var dict = NSMutableDictionary(contentsOfFile: plistPath)
|
|
|
+ let 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)
|
|
|
+ let values = NSArray(array: values)
|
|
|
|
|
|
for key in dict?.allKeys ?? [] {
|
|
|
guard let _key = key as? String else {
|
|
@@ -126,7 +119,7 @@ public let KAutoSaveTimeValueChangedNoti = "KAutoSaveTimeValueChangedNoti"
|
|
|
}
|
|
|
try?dict?.write(to: URL(fileURLWithPath: plistPath))
|
|
|
} else {
|
|
|
- var dict = NSMutableDictionary()
|
|
|
+ let dict = NSMutableDictionary()
|
|
|
if savePath.isEmpty == false && filePath.isEmpty == false {
|
|
|
dict.setValue(savePath, forKey: filePath)
|
|
|
}
|
|
@@ -140,7 +133,7 @@ public let KAutoSaveTimeValueChangedNoti = "KAutoSaveTimeValueChangedNoti"
|
|
|
|
|
|
let plistPath = String(format: "%@/autoSaveInfo.plist", autoSaveFolder)
|
|
|
if FileManager.default.fileExists(atPath: plistPath) {
|
|
|
- var dict = NSMutableDictionary(contentsOfFile: plistPath)
|
|
|
+ let 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)
|
|
@@ -195,7 +188,7 @@ public let KAutoSaveTimeValueChangedNoti = "KAutoSaveTimeValueChangedNoti"
|
|
|
|
|
|
self._opendPaths = NSMutableArray()
|
|
|
|
|
|
- var arr = (try?FileManager.default.contentsOfDirectory(atPath: self.autoSaveFolder)) ?? []
|
|
|
+ let arr = (try?FileManager.default.contentsOfDirectory(atPath: self.autoSaveFolder)) ?? []
|
|
|
|
|
|
for fileName in arr {
|
|
|
let savedKey = "\(self.autoSaveFolder)/\(fileName)"
|
|
@@ -213,40 +206,5 @@ public let KAutoSaveTimeValueChangedNoti = "KAutoSaveTimeValueChangedNoti"
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- /*
|
|
|
-
|
|
|
- - (NSString *)URLEncodedString:(NSString *)string {
|
|
|
- CFStringRef stringRef = CFBridgingRetain(string);
|
|
|
- #pragma clang diagnostic push
|
|
|
- #pragma clang diagnostic ignored "-Wdeprecated-declarations"
|
|
|
- CFStringRef encoded = CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
|
|
|
- stringRef,
|
|
|
- NULL,
|
|
|
- CFSTR("!*'\"();:@&=+$,/?%#[]% "),
|
|
|
- kCFStringEncodingUTF8);
|
|
|
- #pragma clang diagnostic pop
|
|
|
- CFRelease(stringRef);
|
|
|
- return CFBridgingRelease(encoded);
|
|
|
- }
|
|
|
-
|
|
|
- - (NSString *)getValidFilePath:(NSString *)oldPath {
|
|
|
- NSFileManager *fileManager = [NSFileManager defaultManager];
|
|
|
- NSDictionary *fileDic = [fileManager attributesOfItemAtPath:oldPath error:nil];
|
|
|
- NSString *fileType = [fileDic objectForKey:NSFileType];
|
|
|
- int i =1;
|
|
|
- NSString *newPath = oldPath;
|
|
|
- while ([fileManager fileExistsAtPath:newPath]) {
|
|
|
- if ([fileType isEqualToString:NSFileTypeDirectory]) {
|
|
|
- newPath = [oldPath stringByAppendingFormat:@"(%d)",i];
|
|
|
- } else {
|
|
|
- NSString *fileExtension = [oldPath pathExtension];
|
|
|
- newPath = [[oldPath stringByDeletingPathExtension] stringByAppendingFormat:@"(%d).%@",i,fileExtension];
|
|
|
- }
|
|
|
- i++;
|
|
|
- }
|
|
|
- return newPath;
|
|
|
- }
|
|
|
- */
|
|
|
|
|
|
}
|