123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808 |
- //
- // 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<KMHeaderFooterObject> = []
- //
- //// 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<KMHeaderFooterObject>()
- // for i in 0..<datas.count {
- // let obj = self.datas[i]
- // if KMDataVersionManager.updateBatesData() {
- // KMDataVersionManager.refrshBatesData(bates: obj)
- // store()
- // }
- //
- // if obj.isBates {
- // arr.append(obj)
- // }
- // }
- // return arr
- // }
- // func onlyHeaderFooterObjects() -> [KMHeaderFooterObject]{
- // var arr: [KMHeaderFooterObject] = Array<KMHeaderFooterObject>()
- // for i in 0..<self.datas.count {
- // let obj = self.datas[i]
- //
- // if !obj.isBates {
- // arr.append(obj)
- // }
- // }
- // return arr
- // }
- // override init() {
- // super.init()
- // if (FileManager.default.fileExists(atPath: kPlistPath!)) {
- // let dataDict = NSDictionary(contentsOfFile: kPlistPath!)
- // if (dataDict == nil) {
- // return
- // }
- //
- // for keyIndex in 0 ..< (dataDict?.allKeys.count ?? 0) {
- // let key: String = dataDict?.allKeys[keyIndex] as! String
- // let backgroundDict: NSDictionary = dataDict?.object(forKey: key) as! NSDictionary
- //
- // let model = parseDictionary(dict: backgroundDict)
- // /// 赋值id
- // model.id = key
- // self.datas.append(model)
- // }
- //
- // /// 根据id进行排序(升序)
- // self.datas.sort(){$0.id > $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..<nameArray.count {
- // let string = nameArray[i]
- // if string.hasPrefix("Bates") {
- // let index = Int(string.substring(from: "Bates".endIndex))!
- // if index >= 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..<nameArray.count {
- // let string = nameArray[i]
- // if string.hasPrefix("HeaderFooter") {
- // let index = Int(string.substring(from: "HeaderFooter".endIndex))!
- // if index >= availableIndex {
- // availableIndex = index + 1
- // }
- // }
- // }
- // return String(format: "HeaderFooter%ld", availableIndex)
- // }
- //
- // func converArrType(arr: Array<KMHeaderFooterObject>, 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<String, Any> {
- // 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
- // }
- }
|