KMHeaderFooterManager.swift 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. import Foundation
  2. let kHeaderFooterFolderPath = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.applicationSupportDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).last?.stringByAppendingPathComponent(Bundle.main.bundleIdentifier!).stringByAppendingPathComponent("headerFooter")
  3. let kHeaderFooterPlistPath = kHeaderFooterFolderPath?.stringByAppendingPathComponent("headerFooter.plist") ?? ""
  4. let kHeaderFooterRemovedKey = "kHeaderFooterRemovedKey"
  5. private let kHeaderFooterInfoSaveKey = "kHeaderFooterInfoSaveKey"
  6. let kHeaderFooterManagerInit = "kHeaderFooterManagerInit"
  7. class KMHeaderFooterManager: NSObject, NSCoding {
  8. var headFooterObjects: [KMHeaderFooterObject] = []
  9. static let defaultManager = KMHeaderFooterManager()
  10. override init() {
  11. super.init()
  12. self.reloadData()
  13. }
  14. required init?(coder: NSCoder) {
  15. if let data = coder.decodeObject(forKey: "headFooterObjects") {
  16. headFooterObjects = data as? [KMHeaderFooterObject] ?? []
  17. }
  18. }
  19. func encode(with coder: NSCoder) {
  20. coder.encode(headFooterObjects, forKey: "headFooterObjects")
  21. }
  22. func reloadData() {
  23. do {
  24. let pHeadFooterObjects = self.fetchPlistData()
  25. self.headFooterObjects = pHeadFooterObjects
  26. if let storedData = UserDefaults.standard.value(forKey: kHeaderFooterInfoSaveKey) as? Data {
  27. NSKeyedUnarchiver.setClass(KMHeaderFooterObject.self, forClassName: "KMHeaderFooterObject")
  28. NSKeyedUnarchiver.setClass(KMHeaderFooterManager.self, forClassName: "KMHeaderFooterManager")
  29. if UserDefaults.standard.object(forKey: kHeaderFooterManagerInit) == nil {
  30. if let man = NSKeyedUnarchiver.unarchiveObject(with: storedData) as? KMHeaderFooterManager {
  31. UserDefaults.standard.set("1", forKey: kHeaderFooterManagerInit)
  32. for object in man.headFooterObjects {
  33. object.isMigrate = true
  34. self.headFooterObjects.append(object)
  35. }
  36. }
  37. self.savePlistData()
  38. }
  39. }
  40. } catch {
  41. // 解档失败,处理错误情况
  42. print("Error unarchiving data: \(error)")
  43. }
  44. }
  45. var onlyHeaderFooterObjects: [KMHeaderFooterObject] {
  46. return headFooterObjects.filter { !$0.isBates }
  47. }
  48. var onlyBatesObjects: [KMHeaderFooterObject] {
  49. var arr: [KMHeaderFooterObject] = []
  50. for obj in headFooterObjects {
  51. if KMDataVersionManager.updateBatesData() {
  52. KMDataVersionManager.refrshBatesData(bates: obj)
  53. store()
  54. }
  55. if obj.isBates {
  56. arr.append(obj)
  57. }
  58. }
  59. return arr
  60. }
  61. var dateFormatArray: [String] {
  62. return [
  63. "m/d",
  64. "m/d/yy",
  65. "m/d/yyyy",
  66. "mm/dd/yy",
  67. "mm/dd/yyyy",
  68. "d/m/yy",
  69. "d/m/yyyy",
  70. "dd/mm/yy",
  71. "dd/mm/yyyy",
  72. "mm/yy",
  73. "mm/yyyy",
  74. "m.d.yy",
  75. "m.d.yyyy",
  76. "mm.dd.yy",
  77. "mm.dd.yyyy",
  78. "mm.yy",
  79. "mm.yyyy",
  80. "d.m.yy",
  81. "d.m.yyyy",
  82. "dd.mm.yy",
  83. "dd.mm.yyyy",
  84. "yy-mm-dd",
  85. "yyyy-mm-dd"
  86. ]
  87. }
  88. func removeHeaderFooter(_ obj: KMHeaderFooterObject) -> Bool {
  89. headFooterObjects.removeAll { $0 === obj }
  90. store()
  91. return true
  92. }
  93. func removeAllHeaderFooter() -> Bool {
  94. headFooterObjects.removeAll()
  95. store()
  96. return true
  97. }
  98. func addHeaderFooter(_ obj: KMHeaderFooterObject) -> Bool {
  99. headFooterObjects.insert(obj, at: 0)
  100. store()
  101. return true
  102. }
  103. func store() {
  104. // do {
  105. // let encodedObject = try NSKeyedArchiver.archivedData(withRootObject: self, requiringSecureCoding: false)
  106. // UserDefaults.standard.set(encodedObject, forKey: kHeaderFooterInfoSaveKey)
  107. // UserDefaults.standard.synchronize()
  108. // } catch {
  109. // print("Error while encoding object: \(error)")
  110. // }
  111. self.savePlistData()
  112. }
  113. func clearStored() {
  114. UserDefaults.standard.removeObject(forKey: kHeaderFooterInfoSaveKey)
  115. UserDefaults.standard.synchronize()
  116. try?FileManager.default.removeItem(atPath: kHeaderFooterPlistPath)
  117. }
  118. func fetchHeaderFooterAvailableName() -> String {
  119. var availableIndex = 0
  120. let nameArray = onlyHeaderFooterObjects.map { $0.id }
  121. for string in nameArray {
  122. if string.hasPrefix("HeaderFooter") {
  123. let index = Int(string.suffix(from: "HeaderFooter".endIndex)) ?? 0
  124. if index >= availableIndex {
  125. availableIndex = index + 1
  126. }
  127. }
  128. }
  129. return "HeaderFooter\(availableIndex)"
  130. }
  131. func fetchBatesAvailableName() -> String {
  132. var availableIndex = 0
  133. let nameArray = onlyBatesObjects.map { $0.id }
  134. for string in nameArray {
  135. if string.hasPrefix("Bates") {
  136. let index = Int(string.suffix(from: "Bates".endIndex)) ?? 0
  137. if index >= availableIndex {
  138. availableIndex = index + 1
  139. }
  140. }
  141. }
  142. return "Bates\(availableIndex)"
  143. }
  144. }
  145. extension KMHeaderFooterManager {
  146. func fetchPlistData() -> [KMHeaderFooterObject] {
  147. if (!FileManager.default.fileExists(atPath: kHeaderFooterFolderPath!)) {
  148. try?FileManager.default.createDirectory(atPath: kHeaderFooterFolderPath!, withIntermediateDirectories: false)
  149. }
  150. if (!FileManager.default.fileExists(atPath: kHeaderFooterPlistPath)) {
  151. FileManager.default.createFile(atPath: kHeaderFooterPlistPath, contents: nil)
  152. }
  153. print("页眉页脚文件地址 =" + kHeaderFooterPlistPath)
  154. let plistData = try NSData(contentsOfFile: kHeaderFooterPlistPath)
  155. // 解档 plist 数据
  156. if let headerFooterObjects = try NSKeyedUnarchiver.unarchiveObject(with: plistData as! Data) as? [KMHeaderFooterObject] {
  157. // 如果解档成功,可以使用 headerFooterObject
  158. print("Header footer object: \(headerFooterObjects)")
  159. return headerFooterObjects
  160. } else {
  161. print("Failed to unarchive header footer object")
  162. return []
  163. }
  164. }
  165. func savePlistData() {
  166. // 获取 KMHeaderFooterObject 对象的归档数据
  167. if let archivedData = try? NSKeyedArchiver.archivedData(withRootObject: headFooterObjects, requiringSecureCoding: false) {
  168. // 将数据写入 plist 文件
  169. let plistURL = URL(fileURLWithPath: kHeaderFooterPlistPath)
  170. do {
  171. try archivedData.write(to: plistURL)
  172. print("Header footer object saved to plist file")
  173. } catch {
  174. print("Error writing plist file: \(error)")
  175. }
  176. } else {
  177. print("Failed to archive header footer object")
  178. }
  179. }
  180. }