// // KMHeaderFooterManager.swift // PDF Reader Pro // // Created by tangchao on 2022/12/27. // //import Cocoa // //let kHeaderFooterInfoSaveKey = "kHeaderFooterInfoSaveKey" //class KMHeaderFooterManager: NSObject, NSCoding{ // // // let kFolderPath = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.applicationSupportDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).last?.stringByAppendingPathComponent(Bundle.main.bundleIdentifier!).stringByAppendingPathComponent("headerfooter") // let kPlistPath = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.applicationSupportDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).last?.stringByAppendingPathComponent(Bundle.main.bundleIdentifier!).stringByAppendingPathComponent("headerfooter").stringByAppendingPathComponent("headerfooter.plist") // // static let defaultManager = KMHeaderFooterManager() //// = { //// var manager = KMHeaderFooterManager() //// if let storedData = UserDefaults.standard.value(forKey: kHeaderFooterInfoSaveKey) as? Data { //// manager = NSKeyedUnarchiver.unarchiveObject(with: storedData) as! KMHeaderFooterManager //// } else { //// manager = KMHeaderFooterManager() ////// if manager.headFooterObjects == nil { ////// manager.headFooterObjects = [] ////// } //// } //// return manager //// }() // // func encode(with coder: NSCoder) { //// coder.encode(self.headFooterObjects, forKey: "headFooterObjects") // } // // required init?(coder: NSCoder) { //// self.headFooterObjects = coder.decodeObject(forKey: "headFooterObjects") as? [KMHeaderFooterObject] // } // // var datas: Array = [] // //// var headFooterObjects: [KMHeaderFooterObject]? // // lazy var dateFormatArray: Array = { // let arr = [ // "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" // ] // return arr // }() // // func onlyBatesObjects() -> [KMHeaderFooterObject] { // var arr: [KMHeaderFooterObject] = Array() // for i in 0.. [KMHeaderFooterObject]{ // var arr: [KMHeaderFooterObject] = Array() // for i in 0.. $1.id} // } // } // // func store() { // let encodedObject = NSKeyedArchiver.archivedData(withRootObject: self) // let defaults = UserDefaults.standard // defaults.set(encodedObject, forKey: kHeaderFooterInfoSaveKey) // defaults.synchronize() // } // // func fetchBatesAvailableName() -> String { // var availableIndex = 0 // let nameArray = converArrType(arr: onlyBatesObjects(), keyString: "id") // for i in 0..= availableIndex { // availableIndex = index + 1 // } // } // } // return String(format: "Bates%ld", availableIndex) // } // // func fetchHeaderFooterAvailableName() -> String { // var availableIndex = 0 // let nameArray = converArrType(arr: onlyHeaderFooterObjects(), keyString: "id") // for i in 0..= availableIndex { // availableIndex = index + 1 // } // } // } // return String(format: "HeaderFooter%ld", availableIndex) // } // // func converArrType(arr: Array, keyString: String) -> [String] { // let newArr = NSMutableArray() // for item in arr { // newArr.add(item.id) // } // return newArr as! [String] // } // // func addTemplate(_ model: KMHeaderFooterObject) -> Bool { // if (!FileManager.default.fileExists(atPath: kFolderPath!)) { // let create: ()? = try?FileManager.default.createDirectory(atPath: kFolderPath!, withIntermediateDirectories: false) // if (create == nil) { // return false // } // } // // if (!FileManager.default.fileExists(atPath: kPlistPath!)) { // let create = try?FileManager.default.createFile(atPath: kPlistPath!, contents: nil) // if (create == nil) { // return false // } // } // // let dict = NSDictionary(contentsOfFile: kPlistPath!) // var newDict:NSMutableDictionary! // if (dict != nil) { // newDict = NSMutableDictionary(dictionary: dict!) // } else { // newDict = NSMutableDictionary() // } // // let modelDict = self.parseModel(model: model) // // let tag = model.id // newDict.addEntries(from: [tag : modelDict]) // model.id = tag // let result = newDict.write(toFile: kPlistPath!, 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: KMHeaderFooterObject) -> Bool { // if (model.id.isEmpty) { // return false // } // // if (!FileManager.default.fileExists(atPath: kPlistPath!)) { // return false // } // // let key: String = model.id // // let dictionary = NSDictionary(contentsOfFile: kPlistPath!) // var newDictionary: NSMutableDictionary! // if (dictionary != nil) { // newDictionary = NSMutableDictionary(dictionary: dictionary!) // } else { // newDictionary = NSMutableDictionary() // } // newDictionary.removeObject(forKey: key) // // let result = newDictionary.write(toFile: kPlistPath!, atomically: true) // if (result) { // if (self.datas.contains(model)) { // self.datas.removeObject(model) // } // } // return result // } // // func deleteAllTemplate() -> Bool { // if (!FileManager.default.fileExists(atPath: kPlistPath!)) { // return false // } // // let dictionary = NSDictionary(contentsOfFile: kPlistPath!) // var newDictionary: NSMutableDictionary! // if (dictionary != nil) { // newDictionary = NSMutableDictionary(dictionary: dictionary!) // } else { // newDictionary = NSMutableDictionary() // } // // newDictionary.removeAllObjects() // // let result = newDictionary.write(toFile: kPlistPath!, atomically: true) // if (result) { // self.datas.removeAll() // } // // return result // } // // func updateTemplate(_ model: KMHeaderFooterObject) -> Bool { // if (!FileManager.default.fileExists(atPath: kFolderPath!)) { // let create = try?FileManager.default.createDirectory(atPath: kFolderPath!, withIntermediateDirectories: false) // if (create == nil) { // return false // } // } // // if (!FileManager.default.fileExists(atPath: kPlistPath!)) { // let create = try?FileManager.default.createFile(atPath: kPlistPath!, contents: nil) // if (create == nil) { // return false // } // } // // var flagModel: KMHeaderFooterObject! // for model_ in self.datas { // if (model_.id == model.id) { // flagModel = model_ // break // } // } // // if (flagModel == nil) { // return false // } // // let dict = NSDictionary(contentsOfFile: kPlistPath!) // var newDict:NSMutableDictionary! // if (dict != nil) { // newDict = NSMutableDictionary(dictionary: dict!) // } else { // newDict = NSMutableDictionary() // } // // let modelDict = self.parseModel(model: model) // newDict.setObject(modelDict, forKey: flagModel.id as NSCopying) // let result = newDict.write(toFile: kPlistPath!, atomically: true) // if (result) { // let index = self.datas.index(of: flagModel) // self.datas[index!] = model // } // // return result // } // func removeHeaderFooter(_ obj: KMHeaderFooterObject) { // if (obj.id.count < 1) { // return // } // // if (!FileManager.default.fileExists(atPath: kPlistPath!)) { // return // } // // let key: String = obj.id // // let dictionary = NSDictionary(contentsOfFile: kPlistPath!) // var newDictionary: NSMutableDictionary! // if (dictionary != nil) { // newDictionary = NSMutableDictionary(dictionary: dictionary!) // } else { // newDictionary = NSMutableDictionary() // } // newDictionary.removeObject(forKey: key) // // let result = newDictionary.write(toFile: kPlistPath!, atomically: true) // if (result) { // self.datas.removeObject(obj) // } // } // // /** // `Private Methods` // */ // private func parseModel(model: KMHeaderFooterObject) -> Dictionary { // var dict: [String : Any] = [:] // /// 字体相关 // switch model.textFont { // case .font(name: var name, size: var size): // dict["fontName"] = name // dict["fontSize"] = size // default: break // } // // switch model.textColor { // case .color(red: var red, green: var green, blue: var blue, alpha: var alpha): // dict["red"] = red // dict["green"] = green // dict["blue"] = blue // dict["alpha"] = alpha // default: break // } // // /// 页边距 // dict["leftMargin"] = model.leftMargin // dict["rightMargin"] = model.rightMargin // dict["bottomMargin"] = model.bottomMargin // dict["topMargin"] = model.topMargin // dict["isBates"] = model.isBates ? "1" : "0" // // /// 内容 // 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["dateFormatString"] = model.dateFormatString // // /// 页面 // dict["pageRangeString"] = model.pageRangeString // dict["startString"] = model.startString // // /// 页面范围 // dict["pageRangeType"] = model.pageRangeType.rawValue // dict["pageRangeString"] = model.pageRangeString // // return dict // } // // private func parseDictionary(dict: NSDictionary) -> KMHeaderFooterObject { // let model = KMHeaderFooterObject() // // /// 字体相关 // model.textFont = .font(name: dict["fontName"] as! String, size: dict["fontSize"] as! CGFloat) // model.textColor = .color(red: dict["red"] as! CGFloat, green: dict["green"] as! CGFloat, blue: dict["blue"] as! CGFloat, alpha: dict["alpha"] as! CGFloat) // // /// 页边距 // model.leftMargin = dict["leftMargin"] as! CGFloat // model.rightMargin = dict["rightMargin"] as! CGFloat // model.bottomMargin = dict["bottomMargin"] as! CGFloat // model.topMargin = dict["topMargin"] as! CGFloat // // /// 内容 // 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.isBates = false // if (dict["isBates"] != nil) { // model.isBates = (dict["isBates"] as! String == "1") // } // // /// 日期 // model.dateFormatString = dict["dateFormatString"] as! String // // /// 页面 // model.pageRangeString = dict["pageRangeString"] as! String // model.startString = dict["startString"] as! String // // /// 页面范围 // model.pageRangeType = KMWatermarkeModelPageRangeType.init(rawValue: dict["pageRangeType"] as! Int)! // model.pageRangeString = dict["pageRangeString"] as! String // // return model // } // // private func tagString() -> String { // var result: String = "" // // let dateFormatter = DateFormatter() // dateFormatter.dateFormat = "yyMMddHHmmss" // result.append(dateFormatter.string(from: Date())) // result = result.appendingFormat("%04d", arc4random()%10000) // // return result // } //} import Foundation private let kHeaderFooterInfoSaveKey = "kHeaderFooterInfoSaveKey" //class KMHeaderFooterObject: NSObject, NSCoding, NSCopying { // var type: Int = 0 // var leftMargin: Int = 0 // var rightMargin: Int = 0 // var bottomMargin: Int = 0 // var topMargin: Int = 0 // var topLeftString: String = "" // var topCenterString: String = "" // var topRightString: String = "" // var bottomLeftString: String = "" // var bottomCenterString: String = "" // var bottomRightString: String = "" // var startString: String = "" // var fontSize: Float = 16 // var textColor: NSColor = NSColor.black // var pagesString: String = "" // var isBates: Bool = false // var batesPrefixString: String = "" // var batesSuffixString: String = "" // var batesDigits: Int = 0 // var hasHeader: Bool = false // var hasFooter: Bool = false //// var cellHeight: Float = 0 // var headerFooterID: String = "" // var pageChoice: Int = 0 // var dateFormatString: String = "m/d" // var pageFormatString: String = "1" // // override init() { // super.init() // } // // required init?(coder: NSCoder) { // type = coder.decodeInteger(forKey: "type") // leftMargin = coder.decodeInteger(forKey: "leftMargin") // rightMargin = coder.decodeInteger(forKey: "rightMargin") // bottomMargin = coder.decodeInteger(forKey: "bottomMargin") // topMargin = coder.decodeInteger(forKey: "topMargin") // topLeftString = coder.decodeObject(forKey: "topLeftString") as? String ?? "" // topCenterString = coder.decodeObject(forKey: "topCenterString") as? String ?? "" // topRightString = coder.decodeObject(forKey: "topRightString") as? String ?? "" // bottomLeftString = coder.decodeObject(forKey: "bottomLeftString") as? String ?? "" // bottomCenterString = coder.decodeObject(forKey: "bottomCenterString") as? String ?? "" // bottomRightString = coder.decodeObject(forKey: "bottomRightString") as? String ?? "" // startString = coder.decodeObject(forKey: "startString") as? String ?? "" // fontSize = coder.decodeFloat(forKey: "fontSize") // textColor = coder.decodeObject(forKey: "textColor") as? NSColor ?? NSColor.black // pagesString = coder.decodeObject(forKey: "pagesString") as? String ?? "" // isBates = coder.decodeBool(forKey: "isBates") // batesPrefixString = coder.decodeObject(forKey: "batesPrefixString") as? String ?? "" // batesSuffixString = coder.decodeObject(forKey: "batesSuffixString") as? String ?? "" // batesDigits = coder.decodeInteger(forKey: "batesDigits") // hasHeader = coder.decodeBool(forKey: "hasHeader") // hasFooter = coder.decodeBool(forKey: "hasFooter") // cellHeight = coder.decodeFloat(forKey: "cellHeight") // headerFooterID = coder.decodeObject(forKey: "headerFooterID") as? String ?? "" // pageChoice = coder.decodeInteger(forKey: "pageChoice") // dateFormatString = coder.decodeObject(forKey: "dateFormatString") as? String ?? "m/d" // pageFormatString = coder.decodeObject(forKey: "pageFormatString") as? String ?? "1" // } // // func encode(with coder: NSCoder) { // coder.encode(type, forKey: "type") // coder.encode(leftMargin, forKey: "leftMargin") // coder.encode(rightMargin, forKey: "rightMargin") // coder.encode(bottomMargin, forKey: "bottomMargin") // coder.encode(topMargin, forKey: "topMargin") // coder.encode(topLeftString, forKey: "topLeftString") // coder.encode(topCenterString, forKey: "topCenterString") // coder.encode(topRightString, forKey: "topRightString") // coder.encode(bottomLeftString, forKey: "bottomLeftString") // coder.encode(bottomCenterString, forKey: "bottomCenterString") // coder.encode(bottomRightString, forKey: "bottomRightString") // coder.encode(startString, forKey: "startString") // coder.encode(fontSize, forKey: "fontSize") // coder.encode(textColor, forKey: "textColor") // coder.encode(pagesString, forKey: "pagesString") // coder.encode(isBates, forKey: "isBates") // coder.encode(batesPrefixString, forKey: "batesPrefixString") // coder.encode(batesSuffixString, forKey: "batesSuffixString") // coder.encode(batesDigits, forKey: "batesDigits") // coder.encode(hasHeader, forKey: "hasHeader") // coder.encode(hasFooter, forKey: "hasFooter") // coder.encode(cellHeight, forKey: "cellHeight") // coder.encode(headerFooterID, forKey: "headerFooterID") // coder.encode(pageChoice, forKey: "pageChoice") // coder.encode(dateFormatString, forKey: "dateFormatString") // coder.encode(pageFormatString, forKey: "pageFormatString") // } // // func copy(with zone: NSZone? = nil) -> Any { // let obj = KMHeaderFooterObject() // obj.type = type // obj.leftMargin = leftMargin // obj.rightMargin = rightMargin // obj.bottomMargin = bottomMargin // obj.topMargin = topMargin // obj.topLeftString = topLeftString // obj.topCenterString = topCenterString // obj.topRightString = topRightString // obj.bottomLeftString = bottomLeftString // obj.bottomCenterString = bottomCenterString // obj.bottomRightString = bottomRightString // obj.startString = startString // obj.fontSize = fontSize // obj.textColor = textColor // obj.pagesString = pagesString // obj.isBates = isBates // obj.batesPrefixString = batesPrefixString // obj.batesSuffixString = batesSuffixString // obj.batesDigits = batesDigits // obj.hasHeader = hasHeader // obj.hasFooter = hasFooter // obj.cellHeight = cellHeight // obj.headerFooterID = headerFooterID // obj.pageChoice = pageChoice // obj.dateFormatString = dateFormatString // obj.pageFormatString = pageFormatString // return obj // } // // var cellHeight: Float { // get { // var baseHeight: Float = 33 + 24 // if !topLeftString.isEmpty { baseHeight += 20 } // if !topCenterString.isEmpty { baseHeight += 20 } // if !topRightString.isEmpty { baseHeight += 20 } // if !bottomLeftString.isEmpty { baseHeight += 20 } // if !bottomCenterString.isEmpty { baseHeight += 20 } // if !bottomRightString.isEmpty { baseHeight += 20 } // baseHeight -= 4 // return baseHeight // } // set { // // } // } //} class KMHeaderFooterManager: NSObject, NSCoding { var headFooterObjects: [KMHeaderFooterObject] = [] override init() { super.init() } required init?(coder: NSCoder) { headFooterObjects = coder.decodeObject(forKey: "headFooterObjects") as? [KMHeaderFooterObject] ?? [] } func encode(with coder: NSCoder) { coder.encode(headFooterObjects, forKey: "headFooterObjects") } static func defaultManager() -> KMHeaderFooterManager { let storedData = UserDefaults.standard.value(forKey: kHeaderFooterInfoSaveKey) as? Data if let storedData = storedData { if let manager = try? NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(storedData) as? KMHeaderFooterManager { return manager } } let manager = KMHeaderFooterManager() if manager.headFooterObjects.isEmpty { manager.headFooterObjects = [] } return manager } var onlyHeaderFooterObjects: [KMHeaderFooterObject] { return headFooterObjects.filter { !$0.isBates } } var onlyBatesObjects: [KMHeaderFooterObject] { var arr: [KMHeaderFooterObject] = [] for obj in headFooterObjects { if KMDataVersionManager.updateBatesData() { KMDataVersionManager.refrshBatesData(bates: obj) store() } if obj.isBates { arr.append(obj) } } return arr } var dateFormatArray: [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" ] } func removeHeaderFooter(_ obj: KMHeaderFooterObject) -> Bool { headFooterObjects.removeAll { $0 === obj } store() return true } func removeAllHeaderFooter() { headFooterObjects.removeAll() store() } func addHeaderFooter(_ obj: KMHeaderFooterObject) { headFooterObjects.insert(obj, at: 0) store() } func store() { do { let encodedObject = try NSKeyedArchiver.archivedData(withRootObject: self, requiringSecureCoding: false) UserDefaults.standard.set(encodedObject, forKey: kHeaderFooterInfoSaveKey) UserDefaults.standard.synchronize() } catch { print("Error while encoding object: \(error)") } } func clearStored() { UserDefaults.standard.removeObject(forKey: kHeaderFooterInfoSaveKey) UserDefaults.standard.synchronize() } func fetchHeaderFooterAvailableName() -> String { var availableIndex = 0 let nameArray = onlyHeaderFooterObjects.map { $0.id } for string in nameArray { if string.hasPrefix("HeaderFooter") { let index = Int(string.suffix(from: "HeaderFooter".endIndex)) ?? 0 if index >= availableIndex { availableIndex = index + 1 } } } return "HeaderFooter\(availableIndex)" } func fetchBatesAvailableName() -> String { var availableIndex = 0 let nameArray = onlyBatesObjects.map { $0.id } for string in nameArray { if string.hasPrefix("Bates") { let index = Int(string.suffix(from: "Bates".endIndex)) ?? 0 if index >= availableIndex { availableIndex = index + 1 } } } return "Bates\(availableIndex)" } // func deleteTemplate(_ model: KMHeaderFooterObject) -> Bool { // if (model.id.isEmpty) { // return false // } // // if (!FileManager.default.fileExists(atPath: kPlistPath!)) { // return false // } // // let key: String = model.id // // let dictionary = NSDictionary(contentsOfFile: kPlistPath!) // var newDictionary: NSMutableDictionary! // if (dictionary != nil) { // newDictionary = NSMutableDictionary(dictionary: dictionary!) // } else { // newDictionary = NSMutableDictionary() // } // newDictionary.removeObject(forKey: key) // // let result = newDictionary.write(toFile: kPlistPath!, atomically: true) // if (result) { // if (self.headFooterObjects.contains(model)) { // self.headFooterObjects.removeObject(model) // } // } // return result // } // func deleteAllTemplate() -> Bool { // if (!FileManager.default.fileExists(atPath: kPlistPath!)) { // return false // } // // let dictionary = NSDictionary(contentsOfFile: kPlistPath!) // var newDictionary: NSMutableDictionary! // if (dictionary != nil) { // newDictionary = NSMutableDictionary(dictionary: dictionary!) // } else { // newDictionary = NSMutableDictionary() // } // // newDictionary.removeAllObjects() // // let result = newDictionary.write(toFile: kPlistPath!, atomically: true) // if (result) { // self.headFooterObjects.removeAll() // } // // return result // } // func updateTemplate(_ model: KMHeaderFooterObject) -> Bool { // if (!FileManager.default.fileExists(atPath: kFolderPath!)) { // let create = try?FileManager.default.createDirectory(atPath: kFolderPath!, withIntermediateDirectories: false) // if (create == nil) { // return false // } // } // // if (!FileManager.default.fileExists(atPath: kPlistPath!)) { // let create = try?FileManager.default.createFile(atPath: kPlistPath!, contents: nil) // if (create == nil) { // return false // } // } // // var flagModel: KMHeaderFooterObject! // for model_ in self.headFooterObjects { // if (model_.id == model.id) { // flagModel = model_ // break // } // } // // if (flagModel == nil) { // return false // } // // let dict = NSDictionary(contentsOfFile: kPlistPath!) // var newDict:NSMutableDictionary! // if (dict != nil) { // newDict = NSMutableDictionary(dictionary: dict!) // } else { // newDict = NSMutableDictionary() // } // // let modelDict = self.parseModel(model: model) // newDict.setObject(modelDict, forKey: flagModel.id as NSCopying) // let result = newDict.write(toFile: kPlistPath!, atomically: true) // if (result) { // let index = self.headFooterObjects.index(of: flagModel) // self.headFooterObjects[index!] = model // } // // return result // } }