KMDataManager.swift 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. //
  2. // KMDataManager.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2024/2/23.
  6. //
  7. import Cocoa
  8. // 数据持久化 【UserDefault、本地存储、数据库】
  9. let kDocumentDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
  10. let kAppSupportDirectory = FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).first!
  11. let kAppSupportOfBundleIdentifierDirectory = kAppSupportDirectory.appendingPathComponent(MainBundle.bundleIdentifier ?? kBundleIdentifier)
  12. class KMDataManager: NSObject {
  13. // 单例
  14. static let `default` = KMDataManager()
  15. var isTabbingWin = false //多页签提示页是否展示中
  16. var toolbarConfigDataUpdated = false
  17. }
  18. // MARK: - Paths
  19. extension KMDataManager {
  20. class func fetchAppSupportOfBundleIdentifierDirectory(makeIfNecessary: Bool = true) -> URL {
  21. let url = kAppSupportOfBundleIdentifierDirectory
  22. if makeIfNecessary {
  23. if FileManager.default.fileExists(atPath: url.path) == false {
  24. try?FileManager.default.createDirectory(at: url, withIntermediateDirectories: false)
  25. }
  26. }
  27. return url
  28. }
  29. }
  30. // MARK: - ToolBar
  31. extension KMDataManager {
  32. private static let toolbar_dirname_key_ = "PreferenceData"
  33. private static let toolbar_filename_key_ = "ToolbarConfigData.plist"
  34. private static let toolbar_default_key_ = "default"
  35. private static let toolbar_left_key_ = "left"
  36. private static let toolbar_center_key_ = "center"
  37. private static let toolbar_right_key_ = "right"
  38. // 保存数据 [清空数据时传nil]
  39. class func toolbar_saveData(_ model: KMToolbarConfigModel?) -> Bool {
  40. let dir = self.fetchAppSupportOfBundleIdentifierDirectory().appendingPathComponent(self.toolbar_dirname_key_)
  41. if FileManager.default.fileExists(atPath: dir.path) == false {
  42. try?FileManager.default.createDirectory(at: dir, withIntermediateDirectories: false)
  43. }
  44. let fileUrl = dir.appendingPathComponent(self.toolbar_filename_key_)
  45. if FileManager.default.fileExists(atPath: fileUrl.path) == false {
  46. try?FileManager.default.createFile(atPath: fileUrl.path, contents: nil)
  47. }
  48. if let data = model {
  49. let defaultIdentifiers = [KMLeftControlToolbarItemIdentifier, KMDocumentZoomViewToolbarItemIdentifier,
  50. KMDocumentPreviousPageToolbarItemIdentifier,KMDocumentHomeToolbarItemIdentifier,
  51. KMToolbarFlexibleSpaceItemIdentifier /* 插入可变宽度 */,
  52. KMDocumentAnnotationToolbarItemIdentifier, KMDocumentPageToolbarItemIdentifier, KMDocumentConversonToolbarItemIdentifier,KMDocumentRedactToolbarItemIdentifier,
  53. // KMDocumentAIToolsToolbarItemIdentifier,
  54. KMDocumentViewDisplayToolbarItemIdentifier,KMDocumentScanOCRToolbarItemIdentifier, KMDocumentFormToolbarItemIdentifier, KMDocumentEditToolbarItemIdentifier, KMDocumentDigitalSignToolbarItemIdentifier,
  55. KMDocumentFillSginToolbarItemIdentifier, KMDocumentToolToolbarItemIdentifier,
  56. KMToolbarFlexibleSpaceItemIdentifier /* 插入可变宽度 */,
  57. KMDocumentPrintToolbarItemIdentifier,KMDocumentShareToolbarItemIdentifier,
  58. KMDocumentSearchToolbarItemIdentifier, KMRightControlToolbarItemIdentifier,
  59. KMDocumentPreviousBackToolbarItemIdentifier, KMDocumentFirstLastToolbarItemIdentifier,
  60. KMDocumentPageIndicatorToolbarItemIdentifier, KMDocumentPresentationToolbarItemIdentifier]
  61. var newDict = NSMutableDictionary(dictionary: [self.toolbar_default_key_ : defaultIdentifiers])
  62. let left = model?.leftCellIdentifiers ?? []
  63. newDict.setObject(left, forKey: self.toolbar_left_key_ as NSCopying)
  64. let center = model?.centerCellIdentifiers ?? []
  65. newDict.setObject(center, forKey: self.toolbar_center_key_ as NSCopying)
  66. let right = model?.rightCellIdentifiers ?? []
  67. newDict.setObject(right, forKey: self.toolbar_right_key_ as NSCopying)
  68. if newDict.write(to: fileUrl, atomically: true) {
  69. return true
  70. }
  71. } else { // 清空数据
  72. let newDict = NSMutableDictionary()
  73. if newDict.write(to: fileUrl, atomically: true) {
  74. return true
  75. }
  76. }
  77. return false
  78. }
  79. // 获取数据
  80. class func toolbar_getData() -> KMToolbarConfigModel? {
  81. let dir = self.fetchAppSupportOfBundleIdentifierDirectory().appendingPathComponent(self.toolbar_dirname_key_)
  82. let fileUrl = dir.appendingPathComponent(self.toolbar_filename_key_)
  83. guard let dict = NSDictionary(contentsOfFile: fileUrl.path) else {
  84. return nil
  85. }
  86. if dict.count == 0 {
  87. return nil
  88. }
  89. let model = KMToolbarConfigModel()
  90. model.leftCellIdentifiers = dict[self.toolbar_left_key_] as? [String]
  91. model.centerCellIdentifiers = dict[self.toolbar_center_key_] as? [String]
  92. model.rightCellIdentifiers = dict[self.toolbar_right_key_] as? [String]
  93. return model
  94. }
  95. }
  96. // MARK: - UserDefault
  97. extension KMDataManager {
  98. class func ud_object(forKey defaultName: String) -> Any? {
  99. return self.userD_.object(forKey: defaultName)
  100. }
  101. class func ud_set(_ value: Any?, forKey defaultName: String, sync: Bool = true) {
  102. self.userD_.set(value, forKey: defaultName)
  103. if sync {
  104. self.userD_.synchronize()
  105. }
  106. }
  107. class func ud_removeObject(forKey defaultName: String, sync: Bool = true) {
  108. self.userD_.removeObject(forKey: defaultName)
  109. }
  110. class func ud_string(forKey defaultName: String) -> String? {
  111. return self.userD_.string(forKey: defaultName)
  112. }
  113. class func ud_array(forKey defaultName: String) -> [Any]? {
  114. return self.userD_.array(forKey: defaultName)
  115. }
  116. class func ud_dictionary(forKey defaultName: String) -> [String : Any]? {
  117. return self.userD_.dictionary(forKey: defaultName)
  118. }
  119. class func ud_data(forKey defaultName: String) -> Data? {
  120. return self.userD_.data(forKey: defaultName)
  121. }
  122. class func ud_stringArray(forKey defaultName: String) -> [String]? {
  123. return self.userD_.stringArray(forKey: defaultName)
  124. }
  125. class func ud_integer(forKey defaultName: String) -> Int {
  126. return self.userD_.integer(forKey: defaultName)
  127. }
  128. class func ud_float(forKey defaultName: String) -> Float {
  129. return self.userD_.float(forKey: defaultName)
  130. }
  131. class func ud_double(forKey defaultName: String) -> Double {
  132. return self.userD_.double(forKey: defaultName)
  133. }
  134. class func ud_bool(forKey defaultName: String) -> Bool {
  135. return self.userD_.bool(forKey: defaultName)
  136. }
  137. class func ud_url(forKey defaultName: String) -> URL? {
  138. return self.userD_.url(forKey: defaultName)
  139. }
  140. class func ud_set(_ value: Int, forKey defaultName: String, sync: Bool = true) {
  141. self.userD_.set(value, forKey: defaultName)
  142. if sync {
  143. self.userD_.synchronize()
  144. }
  145. }
  146. class func ud_set(_ value: Float, forKey defaultName: String, sync: Bool = true) {
  147. self.userD_.set(value, forKey: defaultName)
  148. if sync {
  149. self.userD_.synchronize()
  150. }
  151. }
  152. class func ud_set(_ value: Double, forKey defaultName: String, sync: Bool = true) {
  153. self.userD_.set(value, forKey: defaultName)
  154. if sync {
  155. self.userD_.synchronize()
  156. }
  157. }
  158. class func ud_set(_ value: Bool, forKey defaultName: String, sync: Bool = true) {
  159. self.userD_.set(value, forKey: defaultName)
  160. if sync {
  161. self.userD_.synchronize()
  162. }
  163. }
  164. class func ud_set(_ url: URL?, forKey defaultName: String, sync: Bool = true) {
  165. self.userD_.set(url, forKey: defaultName)
  166. if sync {
  167. self.userD_.synchronize()
  168. }
  169. }
  170. // 私有
  171. private class var userD_: UserDefaults {
  172. get {
  173. return UserDefaults.standard
  174. }
  175. }
  176. }
  177. // MARK: - UserDefault 的补充
  178. extension KMDataManager {
  179. private static let userD_dirname_key_ = "KMUserDefaultExtension"
  180. private static let userD_filename_key_ = "KMUserDefaultExtension.plist"
  181. // 保存数据 [清空数据时传nil]
  182. class func udExtension_set(_ value: Any?, forKey defaultName: String) {
  183. // 存储文件路径
  184. let dir = self.fetchAppSupportOfBundleIdentifierDirectory().appendingPathComponent(self.userD_dirname_key_)
  185. if FileManager.default.fileExists(atPath: dir.path) == false {
  186. try?FileManager.default.createDirectory(at: dir, withIntermediateDirectories: false)
  187. }
  188. // 存储文件名称
  189. let fileUrl = dir.appendingPathComponent(self.userD_filename_key_)
  190. if FileManager.default.fileExists(atPath: fileUrl.path) == false {
  191. try?FileManager.default.createFile(atPath: fileUrl.path, contents: nil)
  192. }
  193. // 获取原始文件数据
  194. var info = NSMutableDictionary()
  195. if let data = try?NSDictionary(contentsOf: fileUrl, error: ()) {
  196. info.addEntries(from: data as? [AnyHashable : Any] ?? [:])
  197. }
  198. // 保存数据
  199. info.setObject(value as Any, forKey: defaultName as NSCopying)
  200. info.write(to: fileUrl, atomically: true)
  201. }
  202. // 获取数据
  203. class func udExtension_object(forKey defaultName: String) -> Any? {
  204. // 查找文件
  205. let dir = self.fetchAppSupportOfBundleIdentifierDirectory().appendingPathComponent(self.userD_dirname_key_)
  206. let fileUrl = dir.appendingPathComponent(self.userD_filename_key_)
  207. guard let dict = NSDictionary(contentsOfFile: fileUrl.path) else {
  208. return nil
  209. }
  210. // 获取数据
  211. if dict.count == 0 {
  212. return nil
  213. }
  214. return dict[defaultName]
  215. }
  216. }