KMDataManager.swift 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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,KMDocumentAIToolsToolbarItemIdentifier,KMDocumentViewDisplayToolbarItemIdentifier,KMDocumentScanOCRToolbarItemIdentifier, KMDocumentFormToolbarItemIdentifier, KMDocumentEditToolbarItemIdentifier, KMDocumentDigitalSignToolbarItemIdentifier,
  53. KMDocumentFillSginToolbarItemIdentifier, KMDocumentToolToolbarItemIdentifier,
  54. KMToolbarFlexibleSpaceItemIdentifier /* 插入可变宽度 */,
  55. KMDocumentPrintToolbarItemIdentifier,KMDocumentShareToolbarItemIdentifier,
  56. KMDocumentSearchToolbarItemIdentifier, KMRightControlToolbarItemIdentifier,
  57. KMDocumentPreviousBackToolbarItemIdentifier, KMDocumentFirstLastToolbarItemIdentifier,
  58. KMDocumentPageIndicatorToolbarItemIdentifier, KMDocumentPresentationToolbarItemIdentifier]
  59. var newDict = NSMutableDictionary(dictionary: [self.toolbar_default_key_ : defaultIdentifiers])
  60. let left = model?.leftCellIdentifiers ?? []
  61. newDict.setObject(left, forKey: self.toolbar_left_key_ as NSCopying)
  62. let center = model?.centerCellIdentifiers ?? []
  63. newDict.setObject(center, forKey: self.toolbar_center_key_ as NSCopying)
  64. let right = model?.rightCellIdentifiers ?? []
  65. newDict.setObject(right, forKey: self.toolbar_right_key_ as NSCopying)
  66. if newDict.write(to: fileUrl, atomically: true) {
  67. return true
  68. }
  69. } else { // 清空数据
  70. let newDict = NSMutableDictionary()
  71. if newDict.write(to: fileUrl, atomically: true) {
  72. return true
  73. }
  74. }
  75. return false
  76. }
  77. // 获取数据
  78. class func toolbar_getData() -> KMToolbarConfigModel? {
  79. let dir = self.fetchAppSupportOfBundleIdentifierDirectory().appendingPathComponent(self.toolbar_dirname_key_)
  80. let fileUrl = dir.appendingPathComponent(self.toolbar_filename_key_)
  81. guard let dict = NSDictionary(contentsOfFile: fileUrl.path) else {
  82. return nil
  83. }
  84. if dict.count == 0 {
  85. return nil
  86. }
  87. let model = KMToolbarConfigModel()
  88. model.leftCellIdentifiers = dict[self.toolbar_left_key_] as? [String]
  89. model.centerCellIdentifiers = dict[self.toolbar_center_key_] as? [String]
  90. model.rightCellIdentifiers = dict[self.toolbar_right_key_] as? [String]
  91. return model
  92. }
  93. }
  94. // MARK: - UserDefault
  95. extension KMDataManager {
  96. class func ud_object(forKey defaultName: String) -> Any? {
  97. return self.userD_.object(forKey: defaultName)
  98. }
  99. class func ud_set(_ value: Any?, forKey defaultName: String, sync: Bool = true) {
  100. self.userD_.set(value, forKey: defaultName)
  101. if sync {
  102. self.userD_.synchronize()
  103. }
  104. }
  105. class func ud_removeObject(forKey defaultName: String, sync: Bool = true) {
  106. self.userD_.removeObject(forKey: defaultName)
  107. }
  108. class func ud_string(forKey defaultName: String) -> String? {
  109. return self.userD_.string(forKey: defaultName)
  110. }
  111. class func ud_array(forKey defaultName: String) -> [Any]? {
  112. return self.userD_.array(forKey: defaultName)
  113. }
  114. class func ud_dictionary(forKey defaultName: String) -> [String : Any]? {
  115. return self.userD_.dictionary(forKey: defaultName)
  116. }
  117. class func ud_data(forKey defaultName: String) -> Data? {
  118. return self.userD_.data(forKey: defaultName)
  119. }
  120. class func ud_stringArray(forKey defaultName: String) -> [String]? {
  121. return self.userD_.stringArray(forKey: defaultName)
  122. }
  123. class func ud_integer(forKey defaultName: String) -> Int {
  124. return self.userD_.integer(forKey: defaultName)
  125. }
  126. class func ud_float(forKey defaultName: String) -> Float {
  127. return self.userD_.float(forKey: defaultName)
  128. }
  129. class func ud_double(forKey defaultName: String) -> Double {
  130. return self.userD_.double(forKey: defaultName)
  131. }
  132. class func ud_bool(forKey defaultName: String) -> Bool {
  133. return self.userD_.bool(forKey: defaultName)
  134. }
  135. class func ud_url(forKey defaultName: String) -> URL? {
  136. return self.userD_.url(forKey: defaultName)
  137. }
  138. class func ud_set(_ value: Int, forKey defaultName: String, sync: Bool = true) {
  139. self.userD_.set(value, forKey: defaultName)
  140. if sync {
  141. self.userD_.synchronize()
  142. }
  143. }
  144. class func ud_set(_ value: Float, forKey defaultName: String, sync: Bool = true) {
  145. self.userD_.set(value, forKey: defaultName)
  146. if sync {
  147. self.userD_.synchronize()
  148. }
  149. }
  150. class func ud_set(_ value: Double, forKey defaultName: String, sync: Bool = true) {
  151. self.userD_.set(value, forKey: defaultName)
  152. if sync {
  153. self.userD_.synchronize()
  154. }
  155. }
  156. class func ud_set(_ value: Bool, forKey defaultName: String, sync: Bool = true) {
  157. self.userD_.set(value, forKey: defaultName)
  158. if sync {
  159. self.userD_.synchronize()
  160. }
  161. }
  162. class func ud_set(_ url: URL?, forKey defaultName: String, sync: Bool = true) {
  163. self.userD_.set(url, forKey: defaultName)
  164. if sync {
  165. self.userD_.synchronize()
  166. }
  167. }
  168. // 私有
  169. private class var userD_: UserDefaults {
  170. get {
  171. return UserDefaults.standard
  172. }
  173. }
  174. }