KMDataManager.swift 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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: - UserDefault
  31. extension KMDataManager {
  32. class func ud_object(forKey defaultName: String) -> Any? {
  33. return self.userD_.object(forKey: defaultName)
  34. }
  35. class func ud_set(_ value: Any?, forKey defaultName: String, sync: Bool = true) {
  36. self.userD_.set(value, forKey: defaultName)
  37. if sync {
  38. self.userD_.synchronize()
  39. }
  40. }
  41. class func ud_removeObject(forKey defaultName: String, sync: Bool = true) {
  42. self.userD_.removeObject(forKey: defaultName)
  43. }
  44. class func ud_string(forKey defaultName: String) -> String? {
  45. return self.userD_.string(forKey: defaultName)
  46. }
  47. class func ud_array(forKey defaultName: String) -> [Any]? {
  48. return self.userD_.array(forKey: defaultName)
  49. }
  50. class func ud_dictionary(forKey defaultName: String) -> [String : Any]? {
  51. return self.userD_.dictionary(forKey: defaultName)
  52. }
  53. class func ud_data(forKey defaultName: String) -> Data? {
  54. return self.userD_.data(forKey: defaultName)
  55. }
  56. class func ud_stringArray(forKey defaultName: String) -> [String]? {
  57. return self.userD_.stringArray(forKey: defaultName)
  58. }
  59. class func ud_integer(forKey defaultName: String) -> Int {
  60. return self.userD_.integer(forKey: defaultName)
  61. }
  62. class func ud_float(forKey defaultName: String) -> Float {
  63. return self.userD_.float(forKey: defaultName)
  64. }
  65. class func ud_double(forKey defaultName: String) -> Double {
  66. return self.userD_.double(forKey: defaultName)
  67. }
  68. class func ud_bool(forKey defaultName: String) -> Bool {
  69. return self.userD_.bool(forKey: defaultName)
  70. }
  71. class func ud_url(forKey defaultName: String) -> URL? {
  72. return self.userD_.url(forKey: defaultName)
  73. }
  74. class func ud_set(_ value: Int, forKey defaultName: String, sync: Bool = true) {
  75. self.userD_.set(value, forKey: defaultName)
  76. if sync {
  77. self.userD_.synchronize()
  78. }
  79. }
  80. class func ud_set(_ value: Float, forKey defaultName: String, sync: Bool = true) {
  81. self.userD_.set(value, forKey: defaultName)
  82. if sync {
  83. self.userD_.synchronize()
  84. }
  85. }
  86. class func ud_set(_ value: Double, forKey defaultName: String, sync: Bool = true) {
  87. self.userD_.set(value, forKey: defaultName)
  88. if sync {
  89. self.userD_.synchronize()
  90. }
  91. }
  92. class func ud_set(_ value: Bool, forKey defaultName: String, sync: Bool = true) {
  93. self.userD_.set(value, forKey: defaultName)
  94. if sync {
  95. self.userD_.synchronize()
  96. }
  97. }
  98. class func ud_set(_ url: URL?, forKey defaultName: String, sync: Bool = true) {
  99. self.userD_.set(url, forKey: defaultName)
  100. if sync {
  101. self.userD_.synchronize()
  102. }
  103. }
  104. // 私有
  105. private class var userD_: UserDefaults {
  106. get {
  107. return UserDefaults.standard
  108. }
  109. }
  110. }
  111. // MARK: - UserDefault 的补充
  112. extension KMDataManager {
  113. private static let userD_dirname_key_ = "KMUserDefaultExtension"
  114. private static let userD_filename_key_ = "KMUserDefaultExtension.plist"
  115. // 保存数据 [清空数据时传nil]
  116. class func udExtension_set(_ value: Any?, forKey defaultName: String) {
  117. // 存储文件路径
  118. let dir = self.fetchAppSupportOfBundleIdentifierDirectory().appendingPathComponent(self.userD_dirname_key_)
  119. if FileManager.default.fileExists(atPath: dir.path) == false {
  120. try?FileManager.default.createDirectory(at: dir, withIntermediateDirectories: false)
  121. }
  122. // 存储文件名称
  123. let fileUrl = dir.appendingPathComponent(self.userD_filename_key_)
  124. if FileManager.default.fileExists(atPath: fileUrl.path) == false {
  125. try?FileManager.default.createFile(atPath: fileUrl.path, contents: nil)
  126. }
  127. // 获取原始文件数据
  128. var info = NSMutableDictionary()
  129. if let data = try?NSDictionary(contentsOf: fileUrl, error: ()) {
  130. info.addEntries(from: data as? [AnyHashable : Any] ?? [:])
  131. }
  132. // 保存数据
  133. info.setObject(value as Any, forKey: defaultName as NSCopying)
  134. info.write(to: fileUrl, atomically: true)
  135. }
  136. // 获取数据
  137. class func udExtension_object(forKey defaultName: String) -> Any? {
  138. // 查找文件
  139. let dir = self.fetchAppSupportOfBundleIdentifierDirectory().appendingPathComponent(self.userD_dirname_key_)
  140. let fileUrl = dir.appendingPathComponent(self.userD_filename_key_)
  141. guard let dict = NSDictionary(contentsOfFile: fileUrl.path) else {
  142. return nil
  143. }
  144. // 获取数据
  145. if dict.count == 0 {
  146. return nil
  147. }
  148. return dict[defaultName]
  149. }
  150. }