KMBatesManager.swift 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. //
  2. // KMBatesManager.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2022/12/28.
  6. //
  7. import Cocoa
  8. class KMBatesManager: NSObject {
  9. let kFolderPath = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.applicationSupportDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).last?.stringByAppendingPathComponent(Bundle.main.bundleIdentifier!).stringByAppendingPathComponent("bates")
  10. let kPlistPath = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.applicationSupportDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).last?.stringByAppendingPathComponent(Bundle.main.bundleIdentifier!).stringByAppendingPathComponent("bates").stringByAppendingPathComponent("bates.plist")
  11. static let defaultManager = KMBatesManager()
  12. var datas: Array<KMHeaderFooterObject> = []
  13. override init() {
  14. super.init()
  15. if (FileManager.default.fileExists(atPath: kPlistPath!)) {
  16. let dataDict = NSDictionary(contentsOfFile: kPlistPath!)
  17. if (dataDict == nil) {
  18. return
  19. }
  20. for keyIndex in 0 ..< (dataDict?.allKeys.count)! {
  21. let key: String = dataDict?.allKeys[keyIndex] as! String
  22. let modelDict: NSDictionary = dataDict?.object(forKey: key) as! NSDictionary
  23. let model = parseDictionary(dict: modelDict)
  24. /// 赋值id
  25. model.id = key
  26. self.datas.append(model)
  27. }
  28. /// 根据id进行排序(升序)
  29. self.datas.sort(){$0.id > $1.id}
  30. }
  31. }
  32. func addTemplate(_ model: KMHeaderFooterObject) -> Bool {
  33. if (!FileManager.default.fileExists(atPath: kFolderPath!)) {
  34. let create: ()? = try?FileManager.default.createDirectory(atPath: kFolderPath!, withIntermediateDirectories: false)
  35. if (create == nil) {
  36. return false
  37. }
  38. }
  39. if (!FileManager.default.fileExists(atPath: kPlistPath!)) {
  40. let create = try?FileManager.default.createFile(atPath: kPlistPath!, contents: nil)
  41. if (create == nil) {
  42. return false
  43. }
  44. }
  45. let dict = NSDictionary(contentsOfFile: kPlistPath!)
  46. var newDict:NSMutableDictionary!
  47. if (dict != nil) {
  48. newDict = NSMutableDictionary(dictionary: dict!)
  49. } else {
  50. newDict = NSMutableDictionary()
  51. }
  52. let modelDict = self.parseModel(model: model)
  53. let tag = tagString()
  54. newDict.addEntries(from: [tag : modelDict])
  55. model.id = tag
  56. let result = newDict.write(toFile: kPlistPath!, atomically: true)
  57. if (result) {
  58. if (self.datas.count < 1) {
  59. self.datas.append(model)
  60. } else {
  61. self.datas.insert(model, at: 0)
  62. }
  63. }
  64. return result
  65. }
  66. func deleteTemplate(_ model: KMHeaderFooterObject) -> Bool {
  67. if (model.id.isEmpty) {
  68. return false
  69. }
  70. if (!FileManager.default.fileExists(atPath: kPlistPath!)) {
  71. return false
  72. }
  73. let key: String = model.id
  74. let dictionary = NSDictionary(contentsOfFile: kPlistPath!)
  75. var newDictionary: NSMutableDictionary!
  76. if (dictionary != nil) {
  77. newDictionary = NSMutableDictionary(dictionary: dictionary!)
  78. } else {
  79. newDictionary = NSMutableDictionary()
  80. }
  81. newDictionary.removeObject(forKey: key)
  82. let result = newDictionary.write(toFile: kPlistPath!, atomically: true)
  83. if (result) {
  84. if (self.datas.contains(model)) {
  85. self.datas.removeObject(model)
  86. }
  87. }
  88. return result
  89. }
  90. func deleteAllTemplate() -> Bool {
  91. if (!FileManager.default.fileExists(atPath: kPlistPath!)) {
  92. return false
  93. }
  94. let dictionary = NSDictionary(contentsOfFile: kPlistPath!)
  95. var newDictionary: NSMutableDictionary!
  96. if (dictionary != nil) {
  97. newDictionary = NSMutableDictionary(dictionary: dictionary!)
  98. } else {
  99. newDictionary = NSMutableDictionary()
  100. }
  101. newDictionary.removeAllObjects()
  102. let result = newDictionary.write(toFile: kPlistPath!, atomically: true)
  103. if (result) {
  104. self.datas.removeAll()
  105. }
  106. return result
  107. }
  108. func updateTemplate(_ model: KMHeaderFooterObject) -> Bool {
  109. if (!FileManager.default.fileExists(atPath: kFolderPath!)) {
  110. let create = try?FileManager.default.createDirectory(atPath: kFolderPath!, withIntermediateDirectories: false)
  111. if (create == nil) {
  112. return false
  113. }
  114. }
  115. if (!FileManager.default.fileExists(atPath: kPlistPath!)) {
  116. let create = try?FileManager.default.createFile(atPath: kPlistPath!, contents: nil)
  117. if (create == nil) {
  118. return false
  119. }
  120. }
  121. var flagModel: KMHeaderFooterObject!
  122. for model_ in self.datas {
  123. if (model_.id == model.id) {
  124. flagModel = model_
  125. break
  126. }
  127. }
  128. if (flagModel == nil) {
  129. return false
  130. }
  131. let dict = NSDictionary(contentsOfFile: kPlistPath!)
  132. var newDict:NSMutableDictionary!
  133. if (dict != nil) {
  134. newDict = NSMutableDictionary(dictionary: dict!)
  135. } else {
  136. newDict = NSMutableDictionary()
  137. }
  138. let modelDict = self.parseModel(model: model)
  139. newDict.setObject(modelDict, forKey: flagModel.id as NSCopying)
  140. let result = newDict.write(toFile: kPlistPath!, atomically: true)
  141. if (result) {
  142. let index = self.datas.index(of: flagModel)
  143. self.datas[index!] = model
  144. }
  145. return result
  146. }
  147. /**
  148. `Private Methods`
  149. */
  150. private func parseModel(model: KMHeaderFooterObject) -> Dictionary<String, Any> {
  151. var dict: [String : Any] = [:]
  152. /// 字体相关
  153. switch model.textFont {
  154. case .font(name: var name, size: var size):
  155. dict["fontName"] = name
  156. dict["fontSize"] = size
  157. default: break
  158. }
  159. switch model.textColor {
  160. case .color(red: var red, green: var green, blue: var blue, alpha: var alpha):
  161. dict["red"] = red
  162. dict["green"] = green
  163. dict["blue"] = blue
  164. dict["alpha"] = alpha
  165. default: break
  166. }
  167. /// 页边距
  168. dict["leftMargin"] = model.leftMargin
  169. dict["rightMargin"] = model.rightMargin
  170. dict["bottomMargin"] = model.bottomMargin
  171. dict["topMargin"] = model.topMargin
  172. /// 内容
  173. dict["topLeftString"] = model.topLeftString
  174. dict["topCenterString"] = model.topCenterString
  175. dict["topRightString"] = model.topRightString
  176. dict["bottomLeftString"] = model.bottomLeftString
  177. dict["bottomCenterString"] = model.bottomCenterString
  178. dict["bottomRightString"] = model.bottomRightString
  179. dict["prefixString"] = model.batesPrefixString
  180. dict["suffixString"] = model.batesSuffixString
  181. dict["digits"] = model.batesDigits
  182. dict["startString"] = model.startString
  183. dict["pageRangeType"] = model.pageRangeType
  184. dict["pageRangeString"] = model.pageRangeString
  185. return dict
  186. }
  187. private func parseDictionary(dict: NSDictionary) -> KMHeaderFooterObject {
  188. let model = KMHeaderFooterObject()
  189. /// 字体相关
  190. model.textFont = .font(name: dict["fontName"] as! String, size: dict["fontSize"] as! CGFloat)
  191. model.textColor = .color(red: dict["red"] as! CGFloat, green: dict["green"] as! CGFloat, blue: dict["blue"] as! CGFloat, alpha: dict["alpha"] as! CGFloat)
  192. /// 页边距
  193. model.leftMargin = dict["leftMargin"] as! Int
  194. model.rightMargin = dict["rightMargin"] as! Int
  195. model.bottomMargin = dict["bottomMargin"] as! Int
  196. model.topMargin = dict["topMargin"] as! Int
  197. /// 内容
  198. model.topLeftString = dict["topLeftString"] as! String
  199. model.topCenterString = dict["topCenterString"] as! String
  200. model.topRightString = dict["topRightString"] as! String
  201. model.bottomLeftString = dict["bottomLeftString"] as! String
  202. model.bottomCenterString = dict["bottomCenterString"] as! String
  203. model.bottomRightString = dict["bottomRightString"] as! String
  204. model.batesPrefixString = dict["prefixString"] as! String
  205. model.batesSuffixString = dict["suffixString"] as! String
  206. model.batesDigits = dict["digits"] as! Int
  207. model.startString = dict["startString"] as! String
  208. model.pageRangeType = KMWatermarkeModelPageRangeType(rawValue: dict["pageRangeType"] as! Int)!
  209. model.pageRangeString = dict["pageRangeString"] as! String
  210. return model
  211. }
  212. private func tagString() -> String {
  213. var result: String = ""
  214. let dateFormatter = DateFormatter()
  215. dateFormatter.dateFormat = "yyMMddHHmmss"
  216. result.append(dateFormatter.string(from: Date()))
  217. result = result.appendingFormat("%04d", arc4random()%10000)
  218. return result
  219. }
  220. }