AutoSaveManager.swift 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. //
  2. // AutoSaveManager.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2023/11/8.
  6. //
  7. import Cocoa
  8. public let KAutoSaveTimeValueChangedNoti = "KAutoSaveTimeValueChangedNoti"
  9. @objcMembers class AutoSaveManager: NSObject {
  10. public static let kTimeValueChangedNotificationName = Notification.Name(KAutoSaveTimeValueChangedNoti)
  11. var timeInterval: CGFloat {
  12. get {
  13. let timeInterval = UserDefaults.standard.float(forKey: "AutoSaveTimeIntervalKey")
  14. if timeInterval > 0 {
  15. return timeInterval.cgFloat
  16. } else {
  17. return 15
  18. }
  19. }
  20. }
  21. //防止重复弹出
  22. var autoSaveAlertShow = false
  23. var autoSaveDidEndAction = false
  24. // /当前是否正在保存
  25. var isSaving = false
  26. var isSaveNoti = false
  27. private var _autoSaveFolder: String = ""
  28. var autoSaveFolder: String {
  29. get {
  30. return self._autoSaveFolder
  31. }
  32. }
  33. private var _infoDict: NSMutableDictionary?
  34. // 保存的原文件信息
  35. private var _originalPaths: NSMutableArray?
  36. var originalPaths: NSMutableArray? {
  37. get {
  38. return self._originalPaths
  39. }
  40. }
  41. // 对应保存的信息
  42. private var _autoSavePaths: NSMutableArray?
  43. var autoSavePaths: NSMutableArray? {
  44. get {
  45. return self._autoSavePaths
  46. }
  47. }
  48. // 所有打开的文件信息汇总
  49. var opendPaths: NSMutableArray? {
  50. get {
  51. return self._opendPaths
  52. }
  53. }
  54. private var _opendPaths: NSMutableArray?
  55. static let manager: AutoSaveManager = {
  56. let man = AutoSaveManager()
  57. man._loadData()
  58. man.autoSaveDidEndAction = true
  59. return man
  60. }()
  61. // MARK: - Public Method
  62. func updateTimeInterval () {
  63. }
  64. func clearCache() {
  65. if FileManager.default.fileExists(atPath: self.autoSaveFolder) {
  66. try?FileManager.default.removeItem(atPath: self.autoSaveFolder)
  67. }
  68. if FileManager.default.fileExists(atPath: self.autoSaveFolder) == false {
  69. try?FileManager.default.createDirectory(atPath: self.autoSaveFolder, withIntermediateDirectories: true)
  70. }
  71. }
  72. func autoSaveWithPath(_ filePath: String) -> String {
  73. let autoSaveFolder = self.autoSaveFolder
  74. let plistPath = String(format: "%@/autoSaveInfo.plist", autoSaveFolder)
  75. var savePath = String(format: "%@/%@_%@.%@", autoSaveFolder, filePath.deletingPathExtension.lastPathComponent, "recovered", filePath.customPathExtension)
  76. if FileManager.default.fileExists(atPath: plistPath) {
  77. let dict = NSMutableDictionary(contentsOfFile: plistPath)
  78. if let keys = dict?.allKeys as? [String], keys.contains(filePath) {
  79. savePath = dict?.value(forKey: filePath) as? String ?? ""
  80. }
  81. if savePath.isEmpty == false && filePath.isEmpty == false {
  82. if let values = dict?.allValues as? [String], values.contains(savePath) {
  83. //不同路径下同名文件的保存覆盖的情况处理
  84. let values = NSArray(array: values)
  85. for key in dict?.allKeys ?? [] {
  86. guard let _key = key as? String else {
  87. continue
  88. }
  89. let value = dict?.object(forKey: key) as? String ?? ""
  90. if savePath == value && _key != filePath {
  91. var count = 1
  92. savePath = String(format: "%@/%@_%@(%d).%@", autoSaveFolder, filePath.deletingPathExtension.lastPathComponent, "recovered", count, filePath.customPathExtension)
  93. while values.contains(savePath) {
  94. count += 1
  95. savePath = String(format: "%@/%@_%@(%d).%@", autoSaveFolder, filePath.deletingPathExtension.lastPathComponent, "recovered", count, filePath.customPathExtension)
  96. }
  97. }
  98. }
  99. }
  100. dict?.setValue(savePath, forKey: filePath)
  101. }
  102. try?dict?.write(to: URL(fileURLWithPath: plistPath))
  103. } else {
  104. let dict = NSMutableDictionary()
  105. if savePath.isEmpty == false && filePath.isEmpty == false {
  106. dict.setValue(savePath, forKey: filePath)
  107. }
  108. try?dict.write(to: URL(fileURLWithPath: plistPath))
  109. }
  110. return savePath
  111. }
  112. func removeAutoSavePath(_ filePath: String) {
  113. let autoSaveFolder = self.autoSaveFolder
  114. let plistPath = String(format: "%@/autoSaveInfo.plist", autoSaveFolder)
  115. if FileManager.default.fileExists(atPath: plistPath) {
  116. let dict = NSMutableDictionary(contentsOfFile: plistPath)
  117. if let savePath = dict?.value(forKey: filePath) as? String {
  118. if FileManager.default.fileExists(atPath: savePath) {
  119. try?FileManager.default.removeItem(atPath: savePath)
  120. }
  121. dict?.removeObject(forKey: filePath)
  122. }
  123. if let keys = dict?.allKeys, keys.count > 0 {
  124. try?dict?.write(to: URL(fileURLWithPath: plistPath))
  125. } else {
  126. try?FileManager.default.removeItem(atPath: plistPath)
  127. }
  128. }
  129. }
  130. // MARK: - Private Methods
  131. private func _loadData() {
  132. var cachesPath = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true).first ?? ""
  133. let filepath = "\(cachesPath)/\(Bundle.main.bundleIdentifier ?? "")"
  134. if FileManager.default.fileExists(atPath: filepath) {
  135. cachesPath = filepath
  136. }
  137. cachesPath = "\(cachesPath)/autoSaveFolder"
  138. if FileManager.default.fileExists(atPath: cachesPath) == false {
  139. try?FileManager.default.createDirectory(atPath: cachesPath, withIntermediateDirectories: true)
  140. }
  141. self._autoSaveFolder = cachesPath
  142. KMPrint("autoSaveFolder===\(cachesPath)")
  143. let plistPath = String(format: "%@/autoSaveInfo.plist", self.autoSaveFolder)
  144. guard let dict = NSMutableDictionary(contentsOfFile: plistPath) else {
  145. return
  146. }
  147. if (dict.allKeys.count == 0) {
  148. return
  149. }
  150. self._infoDict = dict
  151. if (self._autoSavePaths == nil) {
  152. self._autoSavePaths = NSMutableArray()
  153. }
  154. if (self._originalPaths == nil) {
  155. self._originalPaths = NSMutableArray()
  156. }
  157. self._originalPaths?.addObjects(from: dict.allKeys)
  158. self._opendPaths = NSMutableArray()
  159. let arr = (try?FileManager.default.contentsOfDirectory(atPath: self.autoSaveFolder)) ?? []
  160. for fileName in arr {
  161. let savedKey = "\(self.autoSaveFolder)/\(fileName)"
  162. guard let values = dict.allValues as? [String] else {
  163. continue
  164. }
  165. if values.contains(savedKey) {
  166. self.autoSavePaths?.add(savedKey)
  167. } else {
  168. if fileName == "autoSaveInfo.plist" {
  169. } else {
  170. try?FileManager.default.removeItem(atPath: savedKey)
  171. }
  172. }
  173. }
  174. }
  175. }