123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261 |
- //
- // KMDataManager.swift
- // PDF Reader Pro
- //
- // Created by tangchao on 2024/2/23.
- //
- import Cocoa
- // 数据持久化 【UserDefault、本地存储、数据库】
- let kDocumentDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
- let kAppSupportDirectory = FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).first!
- let kAppSupportOfBundleIdentifierDirectory = kAppSupportDirectory.appendingPathComponent(MainBundle.bundleIdentifier ?? kBundleIdentifier)
- class KMDataManager: NSObject {
- // 单例
- static let `default` = KMDataManager()
-
- var isTabbingWin = false //多页签提示页是否展示中
-
- var toolbarConfigDataUpdated = false
- }
- // MARK: - Paths
- extension KMDataManager {
- class func fetchAppSupportOfBundleIdentifierDirectory(makeIfNecessary: Bool = true) -> URL {
- let url = kAppSupportOfBundleIdentifierDirectory
- if makeIfNecessary {
- if FileManager.default.fileExists(atPath: url.path) == false {
- try?FileManager.default.createDirectory(at: url, withIntermediateDirectories: false)
- }
- }
- return url
- }
- }
- // MARK: - ToolBar
- extension KMDataManager {
- private static let toolbar_dirname_key_ = "PreferenceData"
- private static let toolbar_filename_key_ = "ToolbarConfigData.plist"
-
- private static let toolbar_default_key_ = "default"
- private static let toolbar_left_key_ = "left"
- private static let toolbar_center_key_ = "center"
- private static let toolbar_right_key_ = "right"
-
- // 保存数据 [清空数据时传nil]
- class func toolbar_saveData(_ model: KMToolbarConfigModel?) -> Bool {
- let dir = self.fetchAppSupportOfBundleIdentifierDirectory().appendingPathComponent(self.toolbar_dirname_key_)
- if FileManager.default.fileExists(atPath: dir.path) == false {
- try?FileManager.default.createDirectory(at: dir, withIntermediateDirectories: false)
- }
- let fileUrl = dir.appendingPathComponent(self.toolbar_filename_key_)
- if FileManager.default.fileExists(atPath: fileUrl.path) == false {
- try?FileManager.default.createFile(atPath: fileUrl.path, contents: nil)
- }
-
- if let data = model {
- let defaultIdentifiers = [KMLeftControlToolbarItemIdentifier, KMDocumentZoomViewToolbarItemIdentifier,
- KMDocumentPreviousPageToolbarItemIdentifier,KMDocumentHomeToolbarItemIdentifier,
- KMToolbarFlexibleSpaceItemIdentifier /* 插入可变宽度 */,
- KMDocumentAnnotationToolbarItemIdentifier, KMDocumentPageToolbarItemIdentifier, KMDocumentConversonToolbarItemIdentifier,KMDocumentRedactToolbarItemIdentifier,KMDocumentAIToolsToolbarItemIdentifier,KMDocumentViewDisplayToolbarItemIdentifier,KMDocumentScanOCRToolbarItemIdentifier, KMDocumentFormToolbarItemIdentifier, KMDocumentEditToolbarItemIdentifier, KMDocumentDigitalSignToolbarItemIdentifier,
- KMDocumentFillSginToolbarItemIdentifier, KMDocumentToolToolbarItemIdentifier,
- KMToolbarFlexibleSpaceItemIdentifier /* 插入可变宽度 */,
- KMDocumentPrintToolbarItemIdentifier,KMDocumentShareToolbarItemIdentifier,
- KMDocumentSearchToolbarItemIdentifier, KMRightControlToolbarItemIdentifier,
- KMDocumentPreviousBackToolbarItemIdentifier, KMDocumentFirstLastToolbarItemIdentifier,
- KMDocumentPageIndicatorToolbarItemIdentifier, KMDocumentPresentationToolbarItemIdentifier]
-
-
- var newDict = NSMutableDictionary(dictionary: [self.toolbar_default_key_ : defaultIdentifiers])
-
- let left = model?.leftCellIdentifiers ?? []
- newDict.setObject(left, forKey: self.toolbar_left_key_ as NSCopying)
- let center = model?.centerCellIdentifiers ?? []
- newDict.setObject(center, forKey: self.toolbar_center_key_ as NSCopying)
- let right = model?.rightCellIdentifiers ?? []
- newDict.setObject(right, forKey: self.toolbar_right_key_ as NSCopying)
- if newDict.write(to: fileUrl, atomically: true) {
- return true
- }
- } else { // 清空数据
- let newDict = NSMutableDictionary()
- if newDict.write(to: fileUrl, atomically: true) {
- return true
- }
- }
- return false
- }
-
- // 获取数据
- class func toolbar_getData() -> KMToolbarConfigModel? {
- let dir = self.fetchAppSupportOfBundleIdentifierDirectory().appendingPathComponent(self.toolbar_dirname_key_)
- let fileUrl = dir.appendingPathComponent(self.toolbar_filename_key_)
- guard let dict = NSDictionary(contentsOfFile: fileUrl.path) else {
- return nil
- }
- if dict.count == 0 {
- return nil
- }
-
- let model = KMToolbarConfigModel()
- model.leftCellIdentifiers = dict[self.toolbar_left_key_] as? [String]
- model.centerCellIdentifiers = dict[self.toolbar_center_key_] as? [String]
- model.rightCellIdentifiers = dict[self.toolbar_right_key_] as? [String]
- return model
- }
- }
- // MARK: - UserDefault
- extension KMDataManager {
- class func ud_object(forKey defaultName: String) -> Any? {
- return self.userD_.object(forKey: defaultName)
- }
- class func ud_set(_ value: Any?, forKey defaultName: String, sync: Bool = true) {
- self.userD_.set(value, forKey: defaultName)
- if sync {
- self.userD_.synchronize()
- }
- }
- class func ud_removeObject(forKey defaultName: String, sync: Bool = true) {
- self.userD_.removeObject(forKey: defaultName)
- }
- class func ud_string(forKey defaultName: String) -> String? {
- return self.userD_.string(forKey: defaultName)
- }
- class func ud_array(forKey defaultName: String) -> [Any]? {
- return self.userD_.array(forKey: defaultName)
- }
- class func ud_dictionary(forKey defaultName: String) -> [String : Any]? {
- return self.userD_.dictionary(forKey: defaultName)
- }
- class func ud_data(forKey defaultName: String) -> Data? {
- return self.userD_.data(forKey: defaultName)
- }
- class func ud_stringArray(forKey defaultName: String) -> [String]? {
- return self.userD_.stringArray(forKey: defaultName)
- }
- class func ud_integer(forKey defaultName: String) -> Int {
- return self.userD_.integer(forKey: defaultName)
- }
- class func ud_float(forKey defaultName: String) -> Float {
- return self.userD_.float(forKey: defaultName)
- }
- class func ud_double(forKey defaultName: String) -> Double {
- return self.userD_.double(forKey: defaultName)
- }
- class func ud_bool(forKey defaultName: String) -> Bool {
- return self.userD_.bool(forKey: defaultName)
- }
- class func ud_url(forKey defaultName: String) -> URL? {
- return self.userD_.url(forKey: defaultName)
- }
- class func ud_set(_ value: Int, forKey defaultName: String, sync: Bool = true) {
- self.userD_.set(value, forKey: defaultName)
- if sync {
- self.userD_.synchronize()
- }
- }
- class func ud_set(_ value: Float, forKey defaultName: String, sync: Bool = true) {
- self.userD_.set(value, forKey: defaultName)
- if sync {
- self.userD_.synchronize()
- }
- }
- class func ud_set(_ value: Double, forKey defaultName: String, sync: Bool = true) {
- self.userD_.set(value, forKey: defaultName)
- if sync {
- self.userD_.synchronize()
- }
- }
- class func ud_set(_ value: Bool, forKey defaultName: String, sync: Bool = true) {
- self.userD_.set(value, forKey: defaultName)
- if sync {
- self.userD_.synchronize()
- }
- }
- class func ud_set(_ url: URL?, forKey defaultName: String, sync: Bool = true) {
- self.userD_.set(url, forKey: defaultName)
- if sync {
- self.userD_.synchronize()
- }
- }
-
- // 私有
-
- private class var userD_: UserDefaults {
- get {
- return UserDefaults.standard
- }
- }
- }
- // MARK: - UserDefault 的补充
- extension KMDataManager {
- private static let userD_dirname_key_ = "KMUserDefaultExtension"
- private static let userD_filename_key_ = "KMUserDefaultExtension.plist"
-
- // 保存数据 [清空数据时传nil]
- class func udExtension_set(_ value: Any?, forKey defaultName: String) {
- // 存储文件路径
- let dir = self.fetchAppSupportOfBundleIdentifierDirectory().appendingPathComponent(self.userD_dirname_key_)
- if FileManager.default.fileExists(atPath: dir.path) == false {
- try?FileManager.default.createDirectory(at: dir, withIntermediateDirectories: false)
- }
- // 存储文件名称
- let fileUrl = dir.appendingPathComponent(self.userD_filename_key_)
- if FileManager.default.fileExists(atPath: fileUrl.path) == false {
- try?FileManager.default.createFile(atPath: fileUrl.path, contents: nil)
- }
-
- // 获取原始文件数据
- var info = NSMutableDictionary()
- if let data = try?NSDictionary(contentsOf: fileUrl, error: ()) {
- info.addEntries(from: data as? [AnyHashable : Any] ?? [:])
- }
-
- // 保存数据
- info.setObject(value as Any, forKey: defaultName as NSCopying)
- info.write(to: fileUrl, atomically: true)
- }
-
- // 获取数据
- class func udExtension_object(forKey defaultName: String) -> Any? {
- // 查找文件
- let dir = self.fetchAppSupportOfBundleIdentifierDirectory().appendingPathComponent(self.userD_dirname_key_)
- let fileUrl = dir.appendingPathComponent(self.userD_filename_key_)
- guard let dict = NSDictionary(contentsOfFile: fileUrl.path) else {
- return nil
- }
- // 获取数据
- if dict.count == 0 {
- return nil
- }
-
- return dict[defaultName]
- }
- }
|