// // KMBatesManager.swift // PDF Reader Pro // // Created by tangchao on 2022/12/28. // import Cocoa class KMBatesManager: NSObject { let kBatesFolderPath = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.applicationSupportDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).last?.stringByAppendingPathComponent(Bundle.main.bundleIdentifier!).stringByAppendingPathComponent("bates") let kBatesPlistPath = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.applicationSupportDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).last?.stringByAppendingPathComponent(Bundle.main.bundleIdentifier!).stringByAppendingPathComponent("bates").stringByAppendingPathComponent("bates.plist") static let defaultManager = KMBatesManager() var datas: Array = [] var defaultModel: KMBatesModel = KMBatesModel() override init() { super.init() #if DEBUG print("kBatesPlistPath = \(kBatesPlistPath ?? "")") #endif if (FileManager.default.fileExists(atPath: kBatesPlistPath!)) { let dataDict = NSDictionary(contentsOfFile: kBatesPlistPath!) if (dataDict == nil) { return } for keyIndex in 0 ..< (dataDict?.allKeys.count)! { let key: String = dataDict?.allKeys[keyIndex] as! String let modelDict: NSDictionary = dataDict?.object(forKey: key) as! NSDictionary let model = parseDictionary(dict: modelDict) model.tag = key self.datas.append(model) } /// 根据id进行排序(升序) self.datas.sort(){$0.tag > $1.tag} } defaultModel.name = self.fetchBatesAvailableName() } //MARK: - Get func fetchBatesAvailableName() -> String { var availableIndex = 0 for item in datas { if item.name.hasPrefix("Bates") { if let index = Int(item.name.dropFirst("Bates".count)), index >= availableIndex { availableIndex = index + 1 } } } return "Bates\(availableIndex)" } //MARK: - 增删改查 func addTemplate(_ model: KMBatesModel) -> Bool { if (!FileManager.default.fileExists(atPath: kBatesFolderPath!)) { let create: ()? = try?FileManager.default.createDirectory(atPath: kBatesFolderPath!, withIntermediateDirectories: false) if (create == nil) { return false } } if (!FileManager.default.fileExists(atPath: kBatesPlistPath!)) { let create = try?FileManager.default.createFile(atPath: kBatesPlistPath!, contents: nil) if (create == nil) { return false } } let dict = NSDictionary(contentsOfFile: kBatesPlistPath!) var newDict:NSMutableDictionary! if (dict != nil) { newDict = NSMutableDictionary(dictionary: dict!) } else { newDict = NSMutableDictionary() } let modelDict = self.parseModel(model: model) let tag = model.tag newDict.addEntries(from: [tag : modelDict]) let result = newDict.write(toFile: kBatesPlistPath!, atomically: true) if (result) { if (self.datas.count < 1) { self.datas.append(model) } else { self.datas.insert(model, at: 0) } } return result } func deleteTemplate(_ model: KMBatesModel) -> Bool { if (model.tag.isEmpty) { return false } if (!FileManager.default.fileExists(atPath: kBatesPlistPath!)) { return false } let key: String = model.tag let dictionary = NSDictionary(contentsOfFile: kBatesPlistPath!) var newDictionary: NSMutableDictionary! if (dictionary != nil) { newDictionary = NSMutableDictionary(dictionary: dictionary!) } else { newDictionary = NSMutableDictionary() } newDictionary.removeObject(forKey: key) let result = newDictionary.write(toFile: kBatesPlistPath!, atomically: true) if (result) { if (self.datas.contains(model)) { self.datas.removeObject(model) } } return result } func deleteAllTemplate() -> Bool { if (!FileManager.default.fileExists(atPath: kBatesPlistPath!)) { return false } let dictionary = NSDictionary(contentsOfFile: kBatesPlistPath!) var newDictionary: NSMutableDictionary! if (dictionary != nil) { newDictionary = NSMutableDictionary(dictionary: dictionary!) } else { newDictionary = NSMutableDictionary() } newDictionary.removeAllObjects() let result = newDictionary.write(toFile: kBatesPlistPath!, atomically: true) if (result) { self.datas.removeAll() } return result } func updateTemplate(_ model: KMBatesModel) -> Bool { if (!FileManager.default.fileExists(atPath: kBatesFolderPath!)) { let create = try?FileManager.default.createDirectory(atPath: kBatesFolderPath!, withIntermediateDirectories: false) if (create == nil) { return false } } if (!FileManager.default.fileExists(atPath: kBatesPlistPath!)) { let create = try?FileManager.default.createFile(atPath: kBatesPlistPath!, contents: nil) if (create == nil) { return false } } var flagModel: KMBatesModel! for model_ in self.datas { if (model_.tag == model.tag) { flagModel = model_ break } } if (flagModel == nil) { return false } let dict = NSDictionary(contentsOfFile: kBatesPlistPath!) var newDict:NSMutableDictionary! if (dict != nil) { newDict = NSMutableDictionary(dictionary: dict!) } else { newDict = NSMutableDictionary() } let modelDict = self.parseModel(model: model) newDict.setObject(modelDict, forKey: flagModel.tag as NSCopying) let result = newDict.write(toFile: kBatesPlistPath!, atomically: true) if (result) { if let index = self.datas.firstIndex(of: flagModel) { self.datas[index] = model } } return result } func updateModel(_ model: KMBatesModel, with dict: NSDictionary) { /// 字体相关 model.fontName = dict["fontName"] as! String model.fontsize = dict["fontsize"] as! CGFloat if let value = dict.object(forKey: "color") { model.color = NSColor.km_init(hex: value as! String) } /// 页边距 model.leftMargin = dict["leftMargin"] as! Int model.rightMargin = dict["rightMargin"] as! Int model.bottomMargin = dict["bottomMargin"] as! Int model.topMargin = dict["topMargin"] as! Int /// 内容 model.topLeftString = dict["topLeftString"] as! String model.topCenterString = dict["topCenterString"] as! String model.topRightString = dict["topRightString"] as! String model.bottomLeftString = dict["bottomLeftString"] as! String model.bottomCenterString = dict["bottomCenterString"] as! String model.bottomRightString = dict["bottomRightString"] as! String model.prefixString = dict["prefixString"] as! String model.suffixString = dict["suffixString"] as! String model.digits = dict["digits"] as! Int model.startString = dict["startString"] as! String model.tag = dict["tag"] as! String if let value = dict.object(forKey: "name") { model.name = value as! String } } //MARK: - Parse func parseModel(model: KMBatesModel) -> Dictionary { var dict: [String : Any] = [:] /// 字体相关 dict["fontName"] = model.fontName dict["fontsize"] = model.fontsize dict["color"] = model.color.toHex() /// 页边距 dict["leftMargin"] = model.leftMargin dict["rightMargin"] = model.rightMargin dict["bottomMargin"] = model.bottomMargin dict["topMargin"] = model.topMargin /// 内容 dict["topLeftString"] = model.topLeftString dict["topCenterString"] = model.topCenterString dict["topRightString"] = model.topRightString dict["bottomLeftString"] = model.bottomLeftString dict["bottomCenterString"] = model.bottomCenterString dict["bottomRightString"] = model.bottomRightString dict["prefixString"] = model.prefixString dict["suffixString"] = model.suffixString dict["digits"] = model.digits dict["startString"] = model.startString dict["tag"] = model.tag dict["name"] = model.name return dict } private func parseDictionary(dict: NSDictionary) -> KMBatesModel { let model = KMBatesModel() /// 字体相关 model.fontName = dict["fontName"] as! String model.fontsize = dict["fontsize"] as! CGFloat if let value = dict.object(forKey: "color") { model.color = NSColor.km_init(hex: value as! String) } /// 页边距 model.leftMargin = dict["leftMargin"] as! Int model.rightMargin = dict["rightMargin"] as! Int model.bottomMargin = dict["bottomMargin"] as! Int model.topMargin = dict["topMargin"] as! Int /// 内容 model.topLeftString = dict["topLeftString"] as! String model.topCenterString = dict["topCenterString"] as! String model.topRightString = dict["topRightString"] as! String model.bottomLeftString = dict["bottomLeftString"] as! String model.bottomCenterString = dict["bottomCenterString"] as! String model.bottomRightString = dict["bottomRightString"] as! String model.prefixString = dict["prefixString"] as! String model.suffixString = dict["suffixString"] as! String model.digits = dict["digits"] as! Int model.startString = dict["startString"] as! String if let value = dict["tag"] { model.tag = value as! String } if let value = dict["name"] { model.name = value as! String } return model } //MARK: - Compare class func compareIsChangedModel(_ model: KMBatesModel, withDict dict: NSDictionary) -> Bool { if let value = dict["fontName"] { if model.fontName != (value as! String) { return true } } if let value = dict["fontsize"] { if model.fontsize != (value as! CGFloat) { return true } } if let value = dict["color"] { if model.color.toHex() != (value as! String) { return true } } if let value = dict["leftMargin"] { if model.leftMargin != (value as! Int) { return true } } if let value = dict["rightMargin"] { if model.rightMargin != (value as! Int) { return true } } if let value = dict["bottomMargin"] { if model.bottomMargin != (value as! Int) { return true } } if let value = dict["topMargin"] { if model.topMargin != (value as! Int) { return true } } if let value = dict["topLeftString"] { if model.topLeftString != (value as! String) { return true } } if let value = dict["topCenterString"] { if model.topCenterString != (value as! String) { return true } } if let value = dict["topRightString"] { if model.topRightString != (value as! String) { return true } } if let value = dict["bottomLeftString"] { if model.bottomLeftString != (value as! String) { return true } } if let value = dict["bottomCenterString"] { if model.bottomCenterString != (value as! String) { return true } } if let value = dict["bottomRightString"] { if model.bottomRightString != (value as! String) { return true } } if let value = dict["prefixString"] { if model.prefixString != (value as! String) { return true } } if let value = dict["suffixString"] { if model.suffixString != (value as! String) { return true } } if let value = dict["digits"] { if model.digits != (value as! Int) { return true } } if let value = dict["startString"] { if model.startString != (value as! String) { return true } } return false } } //Class extension KMBatesManager { class func parseModel(model: KMBatesModel, _ pageCount: UInt) -> [String] { var topLeftString: String = "" if (!model.topLeftString.isEmpty) { var string = KMBatesManager.parsePageFormat(formatString: model.topLeftString, startPage: model.startString, pageCount: "\(pageCount)") string = KMBatesManager.parseDateFormat(formatString: string) topLeftString = string } var topCenterString: String = "" if (!model.topCenterString.isEmpty) { var string = KMBatesManager.parsePageFormat(formatString: model.topCenterString, startPage: model.startString, pageCount: "\(pageCount)") string = KMBatesManager.parseDateFormat(formatString: string) topCenterString = string } var topRightString: String = "" if (!model.topRightString.isEmpty) { var string = KMBatesManager.parsePageFormat(formatString: model.topRightString, startPage: model.startString, pageCount: "\(pageCount)") string = KMBatesManager.parseDateFormat(formatString: string) topRightString = string } var bottomLeftString: String = "" if (!model.bottomLeftString.isEmpty) { var string = KMBatesManager.parsePageFormat(formatString: model.bottomLeftString, startPage: model.startString, pageCount: "\(pageCount)") string = KMBatesManager.parseDateFormat(formatString: string) bottomLeftString = string } var bottomCenterString: String = "" if (!model.bottomCenterString.isEmpty) { var string = KMBatesManager.parsePageFormat(formatString: model.bottomCenterString, startPage: model.startString, pageCount: "\(pageCount)") string = KMBatesManager.parseDateFormat(formatString: string) bottomCenterString = string } var bottomRightString: String = "" if (!model.bottomRightString.isEmpty) { var string = KMBatesManager.parsePageFormat(formatString: model.bottomRightString, startPage: model.startString, pageCount: "\(pageCount)") string = KMBatesManager.parseDateFormat(formatString: string) bottomRightString = string } return [topLeftString, topCenterString, topRightString, bottomLeftString, bottomCenterString, bottomRightString] } class func parsePageFormat(formatString: String, startPage: String, pageCount: String) -> String { var result = formatString for pageFormat in self.getPageFormats() { let string = "<<\(pageFormat)>>" if (result.contains(string)) { var tempString = "" if (string == "<<1>>") { tempString.append("<<\(startPage)>>") } else if (string == "<<1 of n>>") { tempString.append("<<\(startPage)>>") tempString.append(" of \(pageCount)") } else if (string == "<<1/n>>") { tempString.append("<<\(startPage)>>") tempString.append("/\(pageCount)") } else if (string == "<>") { tempString.append("Page \(startPage)") } else if (string == "<>") { tempString.append("Page \(startPage)") tempString.append("of \(pageCount)") } result = result.replacingOccurrences(of: string, with: tempString) } } return result } class func parseDateFormat(formatString: String) -> String { var result: String = formatString for dateFormat in self.getDateFormats() { if (result.contains(dateFormat)) { let formatString: String = dateFormat.replacingOccurrences(of: "m", with: "M") let replace = "<<\(dateFormat)>>" let date = Date() let dateFormatter = DateFormatter() dateFormatter.dateFormat = formatString let dateString = dateFormatter.string(from: date) result = result.replacingOccurrences(of: replace, with: dateString) } } return result } class func getPageFormats() -> [String] { return ["1", "1 of n", "1/n", "Page 1", "Page 1 of n"] } @objc class func getDateFormats() -> [String] { return ["m/d", "m/d/yy", "m/d/yyyy", "mm/dd/yy", "mm/dd/yyyy", "d/m/yy", "d/m/yyyy", "dd/mm/yy", "dd/mm/yyyy", "mm/yy", "mm/yyyy", "m.d.yy", "m.d.yyyy", "mm.dd.yy", "mm.dd.yyyy", "mm.yy", "mm.yyyy", "d.m.yy", "d.m.yyyy", "dd.mm.yy", "dd.mm.yyyy", "yy-mm-dd", "yyyy-mm-dd"] } }