KMHeaderFooterManager.swift 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  1. //
  2. // KMHeaderFooterManager.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2022/12/27.
  6. //
  7. //import Cocoa
  8. //
  9. //let kHeaderFooterInfoSaveKey = "kHeaderFooterInfoSaveKey"
  10. //class KMHeaderFooterManager: NSObject, NSCoding{
  11. //
  12. //
  13. // let kFolderPath = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.applicationSupportDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).last?.stringByAppendingPathComponent(Bundle.main.bundleIdentifier!).stringByAppendingPathComponent("headerfooter")
  14. // let kPlistPath = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.applicationSupportDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).last?.stringByAppendingPathComponent(Bundle.main.bundleIdentifier!).stringByAppendingPathComponent("headerfooter").stringByAppendingPathComponent("headerfooter.plist")
  15. //
  16. // static let defaultManager = KMHeaderFooterManager()
  17. //// = {
  18. //// var manager = KMHeaderFooterManager()
  19. //// if let storedData = UserDefaults.standard.value(forKey: kHeaderFooterInfoSaveKey) as? Data {
  20. //// manager = NSKeyedUnarchiver.unarchiveObject(with: storedData) as! KMHeaderFooterManager
  21. //// } else {
  22. //// manager = KMHeaderFooterManager()
  23. ////// if manager.headFooterObjects == nil {
  24. ////// manager.headFooterObjects = []
  25. ////// }
  26. //// }
  27. //// return manager
  28. //// }()
  29. //
  30. // func encode(with coder: NSCoder) {
  31. //// coder.encode(self.headFooterObjects, forKey: "headFooterObjects")
  32. // }
  33. //
  34. // required init?(coder: NSCoder) {
  35. //// self.headFooterObjects = coder.decodeObject(forKey: "headFooterObjects") as? [KMHeaderFooterObject]
  36. // }
  37. //
  38. // var datas: Array<KMHeaderFooterObject> = []
  39. //
  40. //// var headFooterObjects: [KMHeaderFooterObject]?
  41. //
  42. // lazy var dateFormatArray: Array = {
  43. // let arr = [
  44. // "m/d",
  45. // "m/d/yy",
  46. // "m/d/yyyy",
  47. // "mm/dd/yy",
  48. // "mm/dd/yyyy",
  49. // "d/m/yy",
  50. // "d/m/yyyy",
  51. // "dd/mm/yy",
  52. // "dd/mm/yyyy",
  53. // "mm/yy",
  54. // "mm/yyyy",
  55. // "m.d.yy",
  56. // "m.d.yyyy",
  57. // "mm.dd.yy",
  58. // "mm.dd.yyyy",
  59. // "mm.yy",
  60. // "mm.yyyy",
  61. // "d.m.yy",
  62. // "d.m.yyyy",
  63. // "dd.mm.yy",
  64. // "dd.mm.yyyy",
  65. // "yy-mm-dd",
  66. // "yyyy-mm-dd"
  67. // ]
  68. // return arr
  69. // }()
  70. //
  71. // func onlyBatesObjects() -> [KMHeaderFooterObject] {
  72. // var arr: [KMHeaderFooterObject] = Array<KMHeaderFooterObject>()
  73. // for i in 0..<datas.count {
  74. // let obj = self.datas[i]
  75. // if KMDataVersionManager.updateBatesData() {
  76. // KMDataVersionManager.refrshBatesData(bates: obj)
  77. // store()
  78. // }
  79. //
  80. // if obj.isBates {
  81. // arr.append(obj)
  82. // }
  83. // }
  84. // return arr
  85. // }
  86. // func onlyHeaderFooterObjects() -> [KMHeaderFooterObject]{
  87. // var arr: [KMHeaderFooterObject] = Array<KMHeaderFooterObject>()
  88. // for i in 0..<self.datas.count {
  89. // let obj = self.datas[i]
  90. //
  91. // if !obj.isBates {
  92. // arr.append(obj)
  93. // }
  94. // }
  95. // return arr
  96. // }
  97. // override init() {
  98. // super.init()
  99. // if (FileManager.default.fileExists(atPath: kPlistPath!)) {
  100. // let dataDict = NSDictionary(contentsOfFile: kPlistPath!)
  101. // if (dataDict == nil) {
  102. // return
  103. // }
  104. //
  105. // for keyIndex in 0 ..< (dataDict?.allKeys.count ?? 0) {
  106. // let key: String = dataDict?.allKeys[keyIndex] as! String
  107. // let backgroundDict: NSDictionary = dataDict?.object(forKey: key) as! NSDictionary
  108. //
  109. // let model = parseDictionary(dict: backgroundDict)
  110. // /// 赋值id
  111. // model.id = key
  112. // self.datas.append(model)
  113. // }
  114. //
  115. // /// 根据id进行排序(升序)
  116. // self.datas.sort(){$0.id > $1.id}
  117. // }
  118. // }
  119. //
  120. // func store() {
  121. // let encodedObject = NSKeyedArchiver.archivedData(withRootObject: self)
  122. // let defaults = UserDefaults.standard
  123. // defaults.set(encodedObject, forKey: kHeaderFooterInfoSaveKey)
  124. // defaults.synchronize()
  125. // }
  126. //
  127. // func fetchBatesAvailableName() -> String {
  128. // var availableIndex = 0
  129. // let nameArray = converArrType(arr: onlyBatesObjects(), keyString: "id")
  130. // for i in 0..<nameArray.count {
  131. // let string = nameArray[i]
  132. // if string.hasPrefix("Bates") {
  133. // let index = Int(string.substring(from: "Bates".endIndex))!
  134. // if index >= availableIndex {
  135. // availableIndex = index + 1
  136. // }
  137. // }
  138. // }
  139. // return String(format: "Bates%ld", availableIndex)
  140. // }
  141. //
  142. // func fetchHeaderFooterAvailableName() -> String {
  143. // var availableIndex = 0
  144. // let nameArray = converArrType(arr: onlyHeaderFooterObjects(), keyString: "id")
  145. // for i in 0..<nameArray.count {
  146. // let string = nameArray[i]
  147. // if string.hasPrefix("HeaderFooter") {
  148. // let index = Int(string.substring(from: "HeaderFooter".endIndex))!
  149. // if index >= availableIndex {
  150. // availableIndex = index + 1
  151. // }
  152. // }
  153. // }
  154. // return String(format: "HeaderFooter%ld", availableIndex)
  155. // }
  156. //
  157. // func converArrType(arr: Array<KMHeaderFooterObject>, keyString: String) -> [String] {
  158. // let newArr = NSMutableArray()
  159. // for item in arr {
  160. // newArr.add(item.id)
  161. // }
  162. // return newArr as! [String]
  163. // }
  164. //
  165. // func addTemplate(_ model: KMHeaderFooterObject) -> Bool {
  166. // if (!FileManager.default.fileExists(atPath: kFolderPath!)) {
  167. // let create: ()? = try?FileManager.default.createDirectory(atPath: kFolderPath!, withIntermediateDirectories: false)
  168. // if (create == nil) {
  169. // return false
  170. // }
  171. // }
  172. //
  173. // if (!FileManager.default.fileExists(atPath: kPlistPath!)) {
  174. // let create = try?FileManager.default.createFile(atPath: kPlistPath!, contents: nil)
  175. // if (create == nil) {
  176. // return false
  177. // }
  178. // }
  179. //
  180. // let dict = NSDictionary(contentsOfFile: kPlistPath!)
  181. // var newDict:NSMutableDictionary!
  182. // if (dict != nil) {
  183. // newDict = NSMutableDictionary(dictionary: dict!)
  184. // } else {
  185. // newDict = NSMutableDictionary()
  186. // }
  187. //
  188. // let modelDict = self.parseModel(model: model)
  189. //
  190. // let tag = model.id
  191. // newDict.addEntries(from: [tag : modelDict])
  192. // model.id = tag
  193. // let result = newDict.write(toFile: kPlistPath!, atomically: true)
  194. // if (result) {
  195. // if (self.datas.count < 1) {
  196. // self.datas.append(model)
  197. // } else {
  198. // self.datas.insert(model, at: 0)
  199. // }
  200. // }
  201. //
  202. // return result
  203. // }
  204. //
  205. // func deleteTemplate(_ model: KMHeaderFooterObject) -> Bool {
  206. // if (model.id.isEmpty) {
  207. // return false
  208. // }
  209. //
  210. // if (!FileManager.default.fileExists(atPath: kPlistPath!)) {
  211. // return false
  212. // }
  213. //
  214. // let key: String = model.id
  215. //
  216. // let dictionary = NSDictionary(contentsOfFile: kPlistPath!)
  217. // var newDictionary: NSMutableDictionary!
  218. // if (dictionary != nil) {
  219. // newDictionary = NSMutableDictionary(dictionary: dictionary!)
  220. // } else {
  221. // newDictionary = NSMutableDictionary()
  222. // }
  223. // newDictionary.removeObject(forKey: key)
  224. //
  225. // let result = newDictionary.write(toFile: kPlistPath!, atomically: true)
  226. // if (result) {
  227. // if (self.datas.contains(model)) {
  228. // self.datas.removeObject(model)
  229. // }
  230. // }
  231. // return result
  232. // }
  233. //
  234. // func deleteAllTemplate() -> Bool {
  235. // if (!FileManager.default.fileExists(atPath: kPlistPath!)) {
  236. // return false
  237. // }
  238. //
  239. // let dictionary = NSDictionary(contentsOfFile: kPlistPath!)
  240. // var newDictionary: NSMutableDictionary!
  241. // if (dictionary != nil) {
  242. // newDictionary = NSMutableDictionary(dictionary: dictionary!)
  243. // } else {
  244. // newDictionary = NSMutableDictionary()
  245. // }
  246. //
  247. // newDictionary.removeAllObjects()
  248. //
  249. // let result = newDictionary.write(toFile: kPlistPath!, atomically: true)
  250. // if (result) {
  251. // self.datas.removeAll()
  252. // }
  253. //
  254. // return result
  255. // }
  256. //
  257. // func updateTemplate(_ model: KMHeaderFooterObject) -> Bool {
  258. // if (!FileManager.default.fileExists(atPath: kFolderPath!)) {
  259. // let create = try?FileManager.default.createDirectory(atPath: kFolderPath!, withIntermediateDirectories: false)
  260. // if (create == nil) {
  261. // return false
  262. // }
  263. // }
  264. //
  265. // if (!FileManager.default.fileExists(atPath: kPlistPath!)) {
  266. // let create = try?FileManager.default.createFile(atPath: kPlistPath!, contents: nil)
  267. // if (create == nil) {
  268. // return false
  269. // }
  270. // }
  271. //
  272. // var flagModel: KMHeaderFooterObject!
  273. // for model_ in self.datas {
  274. // if (model_.id == model.id) {
  275. // flagModel = model_
  276. // break
  277. // }
  278. // }
  279. //
  280. // if (flagModel == nil) {
  281. // return false
  282. // }
  283. //
  284. // let dict = NSDictionary(contentsOfFile: kPlistPath!)
  285. // var newDict:NSMutableDictionary!
  286. // if (dict != nil) {
  287. // newDict = NSMutableDictionary(dictionary: dict!)
  288. // } else {
  289. // newDict = NSMutableDictionary()
  290. // }
  291. //
  292. // let modelDict = self.parseModel(model: model)
  293. // newDict.setObject(modelDict, forKey: flagModel.id as NSCopying)
  294. // let result = newDict.write(toFile: kPlistPath!, atomically: true)
  295. // if (result) {
  296. // let index = self.datas.index(of: flagModel)
  297. // self.datas[index!] = model
  298. // }
  299. //
  300. // return result
  301. // }
  302. // func removeHeaderFooter(_ obj: KMHeaderFooterObject) {
  303. // if (obj.id.count < 1) {
  304. // return
  305. // }
  306. //
  307. // if (!FileManager.default.fileExists(atPath: kPlistPath!)) {
  308. // return
  309. // }
  310. //
  311. // let key: String = obj.id
  312. //
  313. // let dictionary = NSDictionary(contentsOfFile: kPlistPath!)
  314. // var newDictionary: NSMutableDictionary!
  315. // if (dictionary != nil) {
  316. // newDictionary = NSMutableDictionary(dictionary: dictionary!)
  317. // } else {
  318. // newDictionary = NSMutableDictionary()
  319. // }
  320. // newDictionary.removeObject(forKey: key)
  321. //
  322. // let result = newDictionary.write(toFile: kPlistPath!, atomically: true)
  323. // if (result) {
  324. // self.datas.removeObject(obj)
  325. // }
  326. // }
  327. //
  328. // /**
  329. // `Private Methods`
  330. // */
  331. // private func parseModel(model: KMHeaderFooterObject) -> Dictionary<String, Any> {
  332. // var dict: [String : Any] = [:]
  333. // /// 字体相关
  334. // switch model.textFont {
  335. // case .font(name: var name, size: var size):
  336. // dict["fontName"] = name
  337. // dict["fontSize"] = size
  338. // default: break
  339. // }
  340. //
  341. // switch model.textColor {
  342. // case .color(red: var red, green: var green, blue: var blue, alpha: var alpha):
  343. // dict["red"] = red
  344. // dict["green"] = green
  345. // dict["blue"] = blue
  346. // dict["alpha"] = alpha
  347. // default: break
  348. // }
  349. //
  350. // /// 页边距
  351. // dict["leftMargin"] = model.leftMargin
  352. // dict["rightMargin"] = model.rightMargin
  353. // dict["bottomMargin"] = model.bottomMargin
  354. // dict["topMargin"] = model.topMargin
  355. // dict["isBates"] = model.isBates ? "1" : "0"
  356. //
  357. // /// 内容
  358. // dict["topLeftString"] = model.topLeftString
  359. // dict["topCenterString"] = model.topCenterString
  360. // dict["topRightString"] = model.topRightString
  361. // dict["bottomLeftString"] = model.bottomLeftString
  362. // dict["bottomCenterString"] = model.bottomCenterString
  363. // dict["bottomRightString"] = model.bottomRightString
  364. //
  365. // /// 日期
  366. // dict["dateFormatString"] = model.dateFormatString
  367. //
  368. // /// 页面
  369. // dict["pageRangeString"] = model.pageRangeString
  370. // dict["startString"] = model.startString
  371. //
  372. // /// 页面范围
  373. // dict["pageRangeType"] = model.pageRangeType.rawValue
  374. // dict["pageRangeString"] = model.pageRangeString
  375. //
  376. // return dict
  377. // }
  378. //
  379. // private func parseDictionary(dict: NSDictionary) -> KMHeaderFooterObject {
  380. // let model = KMHeaderFooterObject()
  381. //
  382. // /// 字体相关
  383. // model.textFont = .font(name: dict["fontName"] as! String, size: dict["fontSize"] as! CGFloat)
  384. // model.textColor = .color(red: dict["red"] as! CGFloat, green: dict["green"] as! CGFloat, blue: dict["blue"] as! CGFloat, alpha: dict["alpha"] as! CGFloat)
  385. //
  386. // /// 页边距
  387. // model.leftMargin = dict["leftMargin"] as! CGFloat
  388. // model.rightMargin = dict["rightMargin"] as! CGFloat
  389. // model.bottomMargin = dict["bottomMargin"] as! CGFloat
  390. // model.topMargin = dict["topMargin"] as! CGFloat
  391. //
  392. // /// 内容
  393. // model.topLeftString = dict["topLeftString"] as! String
  394. // model.topCenterString = dict["topCenterString"] as! String
  395. // model.topRightString = dict["topRightString"] as! String
  396. // model.bottomLeftString = dict["bottomLeftString"] as! String
  397. // model.bottomCenterString = dict["bottomCenterString"] as! String
  398. // model.bottomRightString = dict["bottomRightString"] as! String
  399. // model.isBates = false
  400. // if (dict["isBates"] != nil) {
  401. // model.isBates = (dict["isBates"] as! String == "1")
  402. // }
  403. //
  404. // /// 日期
  405. // model.dateFormatString = dict["dateFormatString"] as! String
  406. //
  407. // /// 页面
  408. // model.pageRangeString = dict["pageRangeString"] as! String
  409. // model.startString = dict["startString"] as! String
  410. //
  411. // /// 页面范围
  412. // model.pageRangeType = KMWatermarkeModelPageRangeType.init(rawValue: dict["pageRangeType"] as! Int)!
  413. // model.pageRangeString = dict["pageRangeString"] as! String
  414. //
  415. // return model
  416. // }
  417. //
  418. // private func tagString() -> String {
  419. // var result: String = ""
  420. //
  421. // let dateFormatter = DateFormatter()
  422. // dateFormatter.dateFormat = "yyMMddHHmmss"
  423. // result.append(dateFormatter.string(from: Date()))
  424. // result = result.appendingFormat("%04d", arc4random()%10000)
  425. //
  426. // return result
  427. // }
  428. //}
  429. import Foundation
  430. private let kHeaderFooterInfoSaveKey = "kHeaderFooterInfoSaveKey"
  431. //class KMHeaderFooterObject: NSObject, NSCoding, NSCopying {
  432. // var type: Int = 0
  433. // var leftMargin: Int = 0
  434. // var rightMargin: Int = 0
  435. // var bottomMargin: Int = 0
  436. // var topMargin: Int = 0
  437. // var topLeftString: String = ""
  438. // var topCenterString: String = ""
  439. // var topRightString: String = ""
  440. // var bottomLeftString: String = ""
  441. // var bottomCenterString: String = ""
  442. // var bottomRightString: String = ""
  443. // var startString: String = ""
  444. // var fontSize: Float = 16
  445. // var textColor: NSColor = NSColor.black
  446. // var pagesString: String = ""
  447. // var isBates: Bool = false
  448. // var batesPrefixString: String = ""
  449. // var batesSuffixString: String = ""
  450. // var batesDigits: Int = 0
  451. // var hasHeader: Bool = false
  452. // var hasFooter: Bool = false
  453. //// var cellHeight: Float = 0
  454. // var headerFooterID: String = ""
  455. // var pageChoice: Int = 0
  456. // var dateFormatString: String = "m/d"
  457. // var pageFormatString: String = "1"
  458. //
  459. // override init() {
  460. // super.init()
  461. // }
  462. //
  463. // required init?(coder: NSCoder) {
  464. // type = coder.decodeInteger(forKey: "type")
  465. // leftMargin = coder.decodeInteger(forKey: "leftMargin")
  466. // rightMargin = coder.decodeInteger(forKey: "rightMargin")
  467. // bottomMargin = coder.decodeInteger(forKey: "bottomMargin")
  468. // topMargin = coder.decodeInteger(forKey: "topMargin")
  469. // topLeftString = coder.decodeObject(forKey: "topLeftString") as? String ?? ""
  470. // topCenterString = coder.decodeObject(forKey: "topCenterString") as? String ?? ""
  471. // topRightString = coder.decodeObject(forKey: "topRightString") as? String ?? ""
  472. // bottomLeftString = coder.decodeObject(forKey: "bottomLeftString") as? String ?? ""
  473. // bottomCenterString = coder.decodeObject(forKey: "bottomCenterString") as? String ?? ""
  474. // bottomRightString = coder.decodeObject(forKey: "bottomRightString") as? String ?? ""
  475. // startString = coder.decodeObject(forKey: "startString") as? String ?? ""
  476. // fontSize = coder.decodeFloat(forKey: "fontSize")
  477. // textColor = coder.decodeObject(forKey: "textColor") as? NSColor ?? NSColor.black
  478. // pagesString = coder.decodeObject(forKey: "pagesString") as? String ?? ""
  479. // isBates = coder.decodeBool(forKey: "isBates")
  480. // batesPrefixString = coder.decodeObject(forKey: "batesPrefixString") as? String ?? ""
  481. // batesSuffixString = coder.decodeObject(forKey: "batesSuffixString") as? String ?? ""
  482. // batesDigits = coder.decodeInteger(forKey: "batesDigits")
  483. // hasHeader = coder.decodeBool(forKey: "hasHeader")
  484. // hasFooter = coder.decodeBool(forKey: "hasFooter")
  485. // cellHeight = coder.decodeFloat(forKey: "cellHeight")
  486. // headerFooterID = coder.decodeObject(forKey: "headerFooterID") as? String ?? ""
  487. // pageChoice = coder.decodeInteger(forKey: "pageChoice")
  488. // dateFormatString = coder.decodeObject(forKey: "dateFormatString") as? String ?? "m/d"
  489. // pageFormatString = coder.decodeObject(forKey: "pageFormatString") as? String ?? "1"
  490. // }
  491. //
  492. // func encode(with coder: NSCoder) {
  493. // coder.encode(type, forKey: "type")
  494. // coder.encode(leftMargin, forKey: "leftMargin")
  495. // coder.encode(rightMargin, forKey: "rightMargin")
  496. // coder.encode(bottomMargin, forKey: "bottomMargin")
  497. // coder.encode(topMargin, forKey: "topMargin")
  498. // coder.encode(topLeftString, forKey: "topLeftString")
  499. // coder.encode(topCenterString, forKey: "topCenterString")
  500. // coder.encode(topRightString, forKey: "topRightString")
  501. // coder.encode(bottomLeftString, forKey: "bottomLeftString")
  502. // coder.encode(bottomCenterString, forKey: "bottomCenterString")
  503. // coder.encode(bottomRightString, forKey: "bottomRightString")
  504. // coder.encode(startString, forKey: "startString")
  505. // coder.encode(fontSize, forKey: "fontSize")
  506. // coder.encode(textColor, forKey: "textColor")
  507. // coder.encode(pagesString, forKey: "pagesString")
  508. // coder.encode(isBates, forKey: "isBates")
  509. // coder.encode(batesPrefixString, forKey: "batesPrefixString")
  510. // coder.encode(batesSuffixString, forKey: "batesSuffixString")
  511. // coder.encode(batesDigits, forKey: "batesDigits")
  512. // coder.encode(hasHeader, forKey: "hasHeader")
  513. // coder.encode(hasFooter, forKey: "hasFooter")
  514. // coder.encode(cellHeight, forKey: "cellHeight")
  515. // coder.encode(headerFooterID, forKey: "headerFooterID")
  516. // coder.encode(pageChoice, forKey: "pageChoice")
  517. // coder.encode(dateFormatString, forKey: "dateFormatString")
  518. // coder.encode(pageFormatString, forKey: "pageFormatString")
  519. // }
  520. //
  521. // func copy(with zone: NSZone? = nil) -> Any {
  522. // let obj = KMHeaderFooterObject()
  523. // obj.type = type
  524. // obj.leftMargin = leftMargin
  525. // obj.rightMargin = rightMargin
  526. // obj.bottomMargin = bottomMargin
  527. // obj.topMargin = topMargin
  528. // obj.topLeftString = topLeftString
  529. // obj.topCenterString = topCenterString
  530. // obj.topRightString = topRightString
  531. // obj.bottomLeftString = bottomLeftString
  532. // obj.bottomCenterString = bottomCenterString
  533. // obj.bottomRightString = bottomRightString
  534. // obj.startString = startString
  535. // obj.fontSize = fontSize
  536. // obj.textColor = textColor
  537. // obj.pagesString = pagesString
  538. // obj.isBates = isBates
  539. // obj.batesPrefixString = batesPrefixString
  540. // obj.batesSuffixString = batesSuffixString
  541. // obj.batesDigits = batesDigits
  542. // obj.hasHeader = hasHeader
  543. // obj.hasFooter = hasFooter
  544. // obj.cellHeight = cellHeight
  545. // obj.headerFooterID = headerFooterID
  546. // obj.pageChoice = pageChoice
  547. // obj.dateFormatString = dateFormatString
  548. // obj.pageFormatString = pageFormatString
  549. // return obj
  550. // }
  551. //
  552. // var cellHeight: Float {
  553. // get {
  554. // var baseHeight: Float = 33 + 24
  555. // if !topLeftString.isEmpty { baseHeight += 20 }
  556. // if !topCenterString.isEmpty { baseHeight += 20 }
  557. // if !topRightString.isEmpty { baseHeight += 20 }
  558. // if !bottomLeftString.isEmpty { baseHeight += 20 }
  559. // if !bottomCenterString.isEmpty { baseHeight += 20 }
  560. // if !bottomRightString.isEmpty { baseHeight += 20 }
  561. // baseHeight -= 4
  562. // return baseHeight
  563. // }
  564. // set {
  565. //
  566. // }
  567. // }
  568. //}
  569. class KMHeaderFooterManager: NSObject, NSCoding {
  570. var headFooterObjects: [KMHeaderFooterObject] = []
  571. override init() {
  572. super.init()
  573. }
  574. required init?(coder: NSCoder) {
  575. headFooterObjects = coder.decodeObject(forKey: "headFooterObjects") as? [KMHeaderFooterObject] ?? []
  576. }
  577. func encode(with coder: NSCoder) {
  578. coder.encode(headFooterObjects, forKey: "headFooterObjects")
  579. }
  580. static func defaultManager() -> KMHeaderFooterManager {
  581. let storedData = UserDefaults.standard.value(forKey: kHeaderFooterInfoSaveKey) as? Data
  582. if let storedData = storedData {
  583. if let manager = try? NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(storedData) as? KMHeaderFooterManager {
  584. return manager
  585. }
  586. }
  587. let manager = KMHeaderFooterManager()
  588. if manager.headFooterObjects.isEmpty {
  589. manager.headFooterObjects = []
  590. }
  591. return manager
  592. }
  593. var onlyHeaderFooterObjects: [KMHeaderFooterObject] {
  594. return headFooterObjects.filter { !$0.isBates }
  595. }
  596. var onlyBatesObjects: [KMHeaderFooterObject] {
  597. var arr: [KMHeaderFooterObject] = []
  598. for obj in headFooterObjects {
  599. if KMDataVersionManager.updateBatesData() {
  600. KMDataVersionManager.refrshBatesData(bates: obj)
  601. store()
  602. }
  603. if obj.isBates {
  604. arr.append(obj)
  605. }
  606. }
  607. return arr
  608. }
  609. var dateFormatArray: [String] {
  610. return [
  611. "m/d",
  612. "m/d/yy",
  613. "m/d/yyyy",
  614. "mm/dd/yy",
  615. "mm/dd/yyyy",
  616. "d/m/yy",
  617. "d/m/yyyy",
  618. "dd/mm/yy",
  619. "dd/mm/yyyy",
  620. "mm/yy",
  621. "mm/yyyy",
  622. "m.d.yy",
  623. "m.d.yyyy",
  624. "mm.dd.yy",
  625. "mm.dd.yyyy",
  626. "mm.yy",
  627. "mm.yyyy",
  628. "d.m.yy",
  629. "d.m.yyyy",
  630. "dd.mm.yy",
  631. "dd.mm.yyyy",
  632. "yy-mm-dd",
  633. "yyyy-mm-dd"
  634. ]
  635. }
  636. func removeHeaderFooter(_ obj: KMHeaderFooterObject) -> Bool {
  637. headFooterObjects.removeAll { $0 === obj }
  638. store()
  639. return true
  640. }
  641. func removeAllHeaderFooter() {
  642. headFooterObjects.removeAll()
  643. store()
  644. }
  645. func addHeaderFooter(_ obj: KMHeaderFooterObject) {
  646. headFooterObjects.insert(obj, at: 0)
  647. store()
  648. }
  649. func store() {
  650. do {
  651. let encodedObject = try NSKeyedArchiver.archivedData(withRootObject: self, requiringSecureCoding: false)
  652. UserDefaults.standard.set(encodedObject, forKey: kHeaderFooterInfoSaveKey)
  653. UserDefaults.standard.synchronize()
  654. } catch {
  655. print("Error while encoding object: \(error)")
  656. }
  657. }
  658. func clearStored() {
  659. UserDefaults.standard.removeObject(forKey: kHeaderFooterInfoSaveKey)
  660. UserDefaults.standard.synchronize()
  661. }
  662. func fetchHeaderFooterAvailableName() -> String {
  663. var availableIndex = 0
  664. let nameArray = onlyHeaderFooterObjects.map { $0.id }
  665. for string in nameArray {
  666. if string.hasPrefix("HeaderFooter") {
  667. let index = Int(string.suffix(from: "HeaderFooter".endIndex)) ?? 0
  668. if index >= availableIndex {
  669. availableIndex = index + 1
  670. }
  671. }
  672. }
  673. return "HeaderFooter\(availableIndex)"
  674. }
  675. func fetchBatesAvailableName() -> String {
  676. var availableIndex = 0
  677. let nameArray = onlyBatesObjects.map { $0.id }
  678. for string in nameArray {
  679. if string.hasPrefix("Bates") {
  680. let index = Int(string.suffix(from: "Bates".endIndex)) ?? 0
  681. if index >= availableIndex {
  682. availableIndex = index + 1
  683. }
  684. }
  685. }
  686. return "Bates\(availableIndex)"
  687. }
  688. // func deleteTemplate(_ model: KMHeaderFooterObject) -> Bool {
  689. // if (model.id.isEmpty) {
  690. // return false
  691. // }
  692. //
  693. // if (!FileManager.default.fileExists(atPath: kPlistPath!)) {
  694. // return false
  695. // }
  696. //
  697. // let key: String = model.id
  698. //
  699. // let dictionary = NSDictionary(contentsOfFile: kPlistPath!)
  700. // var newDictionary: NSMutableDictionary!
  701. // if (dictionary != nil) {
  702. // newDictionary = NSMutableDictionary(dictionary: dictionary!)
  703. // } else {
  704. // newDictionary = NSMutableDictionary()
  705. // }
  706. // newDictionary.removeObject(forKey: key)
  707. //
  708. // let result = newDictionary.write(toFile: kPlistPath!, atomically: true)
  709. // if (result) {
  710. // if (self.headFooterObjects.contains(model)) {
  711. // self.headFooterObjects.removeObject(model)
  712. // }
  713. // }
  714. // return result
  715. // }
  716. // func deleteAllTemplate() -> Bool {
  717. // if (!FileManager.default.fileExists(atPath: kPlistPath!)) {
  718. // return false
  719. // }
  720. //
  721. // let dictionary = NSDictionary(contentsOfFile: kPlistPath!)
  722. // var newDictionary: NSMutableDictionary!
  723. // if (dictionary != nil) {
  724. // newDictionary = NSMutableDictionary(dictionary: dictionary!)
  725. // } else {
  726. // newDictionary = NSMutableDictionary()
  727. // }
  728. //
  729. // newDictionary.removeAllObjects()
  730. //
  731. // let result = newDictionary.write(toFile: kPlistPath!, atomically: true)
  732. // if (result) {
  733. // self.headFooterObjects.removeAll()
  734. // }
  735. //
  736. // return result
  737. // }
  738. // func updateTemplate(_ model: KMHeaderFooterObject) -> Bool {
  739. // if (!FileManager.default.fileExists(atPath: kFolderPath!)) {
  740. // let create = try?FileManager.default.createDirectory(atPath: kFolderPath!, withIntermediateDirectories: false)
  741. // if (create == nil) {
  742. // return false
  743. // }
  744. // }
  745. //
  746. // if (!FileManager.default.fileExists(atPath: kPlistPath!)) {
  747. // let create = try?FileManager.default.createFile(atPath: kPlistPath!, contents: nil)
  748. // if (create == nil) {
  749. // return false
  750. // }
  751. // }
  752. //
  753. // var flagModel: KMHeaderFooterObject!
  754. // for model_ in self.headFooterObjects {
  755. // if (model_.id == model.id) {
  756. // flagModel = model_
  757. // break
  758. // }
  759. // }
  760. //
  761. // if (flagModel == nil) {
  762. // return false
  763. // }
  764. //
  765. // let dict = NSDictionary(contentsOfFile: kPlistPath!)
  766. // var newDict:NSMutableDictionary!
  767. // if (dict != nil) {
  768. // newDict = NSMutableDictionary(dictionary: dict!)
  769. // } else {
  770. // newDict = NSMutableDictionary()
  771. // }
  772. //
  773. // let modelDict = self.parseModel(model: model)
  774. // newDict.setObject(modelDict, forKey: flagModel.id as NSCopying)
  775. // let result = newDict.write(toFile: kPlistPath!, atomically: true)
  776. // if (result) {
  777. // let index = self.headFooterObjects.index(of: flagModel)
  778. // self.headFooterObjects[index!] = model
  779. // }
  780. //
  781. // return result
  782. // }
  783. }