KMBatesManager.swift 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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 kBatesFolderPath = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.applicationSupportDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).last?.stringByAppendingPathComponent(Bundle.main.bundleIdentifier!).stringByAppendingPathComponent("bates")
  10. let kBatesPlistPath = 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<KMBatesModel> = []
  13. var defaultModel: KMBatesModel = KMBatesModel()
  14. override init() {
  15. super.init()
  16. #if DEBUG
  17. print("kBatesPlistPath = \(kBatesPlistPath ?? "")")
  18. #endif
  19. if (FileManager.default.fileExists(atPath: kBatesPlistPath!)) {
  20. let dataDict = NSDictionary(contentsOfFile: kBatesPlistPath!)
  21. if (dataDict == nil) {
  22. return
  23. }
  24. for keyIndex in 0 ..< (dataDict?.allKeys.count)! {
  25. let key: String = dataDict?.allKeys[keyIndex] as! String
  26. let modelDict: NSDictionary = dataDict?.object(forKey: key) as! NSDictionary
  27. let model = parseDictionary(dict: modelDict)
  28. model.tag = key
  29. self.datas.append(model)
  30. }
  31. /// 根据id进行排序(升序)
  32. self.datas.sort(){$0.tag > $1.tag}
  33. }
  34. defaultModel.name = self.fetchBatesAvailableName()
  35. }
  36. //MARK: - Get
  37. func fetchBatesAvailableName() -> String {
  38. var availableIndex = 0
  39. for item in datas {
  40. if item.name.hasPrefix("Bates") {
  41. if let index = Int(item.name.dropFirst("Bates".count)), index >= availableIndex {
  42. availableIndex = index + 1
  43. }
  44. }
  45. }
  46. return "Bates\(availableIndex)"
  47. }
  48. //MARK: - 增删改查
  49. func addTemplate(_ model: KMBatesModel) -> Bool {
  50. if (!FileManager.default.fileExists(atPath: kBatesFolderPath!)) {
  51. let create: ()? = try?FileManager.default.createDirectory(atPath: kBatesFolderPath!, withIntermediateDirectories: false)
  52. if (create == nil) {
  53. return false
  54. }
  55. }
  56. if (!FileManager.default.fileExists(atPath: kBatesPlistPath!)) {
  57. let create = try?FileManager.default.createFile(atPath: kBatesPlistPath!, contents: nil)
  58. if (create == nil) {
  59. return false
  60. }
  61. }
  62. let dict = NSDictionary(contentsOfFile: kBatesPlistPath!)
  63. var newDict:NSMutableDictionary!
  64. if (dict != nil) {
  65. newDict = NSMutableDictionary(dictionary: dict!)
  66. } else {
  67. newDict = NSMutableDictionary()
  68. }
  69. let modelDict = self.parseModel(model: model)
  70. let tag = model.tag
  71. newDict.addEntries(from: [tag : modelDict])
  72. let result = newDict.write(toFile: kBatesPlistPath!, atomically: true)
  73. if (result) {
  74. if (self.datas.count < 1) {
  75. self.datas.append(model)
  76. } else {
  77. self.datas.insert(model, at: 0)
  78. }
  79. }
  80. return result
  81. }
  82. func deleteTemplate(_ model: KMBatesModel) -> Bool {
  83. if (model.tag.isEmpty) {
  84. return false
  85. }
  86. if (!FileManager.default.fileExists(atPath: kBatesPlistPath!)) {
  87. return false
  88. }
  89. let key: String = model.tag
  90. let dictionary = NSDictionary(contentsOfFile: kBatesPlistPath!)
  91. var newDictionary: NSMutableDictionary!
  92. if (dictionary != nil) {
  93. newDictionary = NSMutableDictionary(dictionary: dictionary!)
  94. } else {
  95. newDictionary = NSMutableDictionary()
  96. }
  97. newDictionary.removeObject(forKey: key)
  98. let result = newDictionary.write(toFile: kBatesPlistPath!, atomically: true)
  99. if (result) {
  100. if (self.datas.contains(model)) {
  101. self.datas.removeObject(model)
  102. }
  103. }
  104. return result
  105. }
  106. func deleteAllTemplate() -> Bool {
  107. if (!FileManager.default.fileExists(atPath: kBatesPlistPath!)) {
  108. return false
  109. }
  110. let dictionary = NSDictionary(contentsOfFile: kBatesPlistPath!)
  111. var newDictionary: NSMutableDictionary!
  112. if (dictionary != nil) {
  113. newDictionary = NSMutableDictionary(dictionary: dictionary!)
  114. } else {
  115. newDictionary = NSMutableDictionary()
  116. }
  117. newDictionary.removeAllObjects()
  118. let result = newDictionary.write(toFile: kBatesPlistPath!, atomically: true)
  119. if (result) {
  120. self.datas.removeAll()
  121. }
  122. return result
  123. }
  124. func updateTemplate(_ model: KMBatesModel) -> Bool {
  125. if (!FileManager.default.fileExists(atPath: kBatesFolderPath!)) {
  126. let create = try?FileManager.default.createDirectory(atPath: kBatesFolderPath!, withIntermediateDirectories: false)
  127. if (create == nil) {
  128. return false
  129. }
  130. }
  131. if (!FileManager.default.fileExists(atPath: kBatesPlistPath!)) {
  132. let create = try?FileManager.default.createFile(atPath: kBatesPlistPath!, contents: nil)
  133. if (create == nil) {
  134. return false
  135. }
  136. }
  137. var flagModel: KMBatesModel!
  138. for model_ in self.datas {
  139. if (model_.tag == model.tag) {
  140. flagModel = model_
  141. break
  142. }
  143. }
  144. if (flagModel == nil) {
  145. return false
  146. }
  147. let dict = NSDictionary(contentsOfFile: kBatesPlistPath!)
  148. var newDict:NSMutableDictionary!
  149. if (dict != nil) {
  150. newDict = NSMutableDictionary(dictionary: dict!)
  151. } else {
  152. newDict = NSMutableDictionary()
  153. }
  154. let modelDict = self.parseModel(model: model)
  155. newDict.setObject(modelDict, forKey: flagModel.tag as NSCopying)
  156. let result = newDict.write(toFile: kBatesPlistPath!, atomically: true)
  157. if (result) {
  158. if let index = self.datas.firstIndex(of: flagModel) {
  159. self.datas[index] = model
  160. }
  161. }
  162. return result
  163. }
  164. func updateModel(_ model: KMBatesModel, with dict: NSDictionary) {
  165. /// 字体相关
  166. model.fontName = dict["fontName"] as! String
  167. model.fontsize = dict["fontsize"] as! CGFloat
  168. if let value = dict.object(forKey: "color") {
  169. model.color = NSColor.km_init(hex: value as! String)
  170. }
  171. /// 页边距
  172. model.leftMargin = dict["leftMargin"] as! Int
  173. model.rightMargin = dict["rightMargin"] as! Int
  174. model.bottomMargin = dict["bottomMargin"] as! Int
  175. model.topMargin = dict["topMargin"] as! Int
  176. /// 内容
  177. model.topLeftString = dict["topLeftString"] as! String
  178. model.topCenterString = dict["topCenterString"] as! String
  179. model.topRightString = dict["topRightString"] as! String
  180. model.bottomLeftString = dict["bottomLeftString"] as! String
  181. model.bottomCenterString = dict["bottomCenterString"] as! String
  182. model.bottomRightString = dict["bottomRightString"] as! String
  183. model.prefixString = dict["prefixString"] as! String
  184. model.suffixString = dict["suffixString"] as! String
  185. model.digits = dict["digits"] as! Int
  186. model.startString = dict["startString"] as! String
  187. model.tag = dict["tag"] as! String
  188. if let value = dict.object(forKey: "name") {
  189. model.name = value as! String
  190. }
  191. }
  192. //MARK: - Parse
  193. func parseModel(model: KMBatesModel) -> Dictionary<String, Any> {
  194. var dict: [String : Any] = [:]
  195. /// 字体相关
  196. dict["fontName"] = model.fontName
  197. dict["fontsize"] = model.fontsize
  198. dict["color"] = model.color.toHex()
  199. /// 页边距
  200. dict["leftMargin"] = model.leftMargin
  201. dict["rightMargin"] = model.rightMargin
  202. dict["bottomMargin"] = model.bottomMargin
  203. dict["topMargin"] = model.topMargin
  204. /// 内容
  205. dict["topLeftString"] = model.topLeftString
  206. dict["topCenterString"] = model.topCenterString
  207. dict["topRightString"] = model.topRightString
  208. dict["bottomLeftString"] = model.bottomLeftString
  209. dict["bottomCenterString"] = model.bottomCenterString
  210. dict["bottomRightString"] = model.bottomRightString
  211. dict["prefixString"] = model.prefixString
  212. dict["suffixString"] = model.suffixString
  213. dict["digits"] = model.digits
  214. dict["startString"] = model.startString
  215. dict["tag"] = model.tag
  216. dict["name"] = model.name
  217. return dict
  218. }
  219. private func parseDictionary(dict: NSDictionary) -> KMBatesModel {
  220. let model = KMBatesModel()
  221. /// 字体相关
  222. model.fontName = dict["fontName"] as! String
  223. model.fontsize = dict["fontsize"] as! CGFloat
  224. if let value = dict.object(forKey: "color") {
  225. model.color = NSColor.km_init(hex: value as! String)
  226. }
  227. /// 页边距
  228. model.leftMargin = dict["leftMargin"] as! Int
  229. model.rightMargin = dict["rightMargin"] as! Int
  230. model.bottomMargin = dict["bottomMargin"] as! Int
  231. model.topMargin = dict["topMargin"] as! Int
  232. /// 内容
  233. model.topLeftString = dict["topLeftString"] as! String
  234. model.topCenterString = dict["topCenterString"] as! String
  235. model.topRightString = dict["topRightString"] as! String
  236. model.bottomLeftString = dict["bottomLeftString"] as! String
  237. model.bottomCenterString = dict["bottomCenterString"] as! String
  238. model.bottomRightString = dict["bottomRightString"] as! String
  239. model.prefixString = dict["prefixString"] as! String
  240. model.suffixString = dict["suffixString"] as! String
  241. model.digits = dict["digits"] as! Int
  242. model.startString = dict["startString"] as! String
  243. if let value = dict["tag"] {
  244. model.tag = value as! String
  245. }
  246. if let value = dict["name"] {
  247. model.name = value as! String
  248. }
  249. return model
  250. }
  251. //MARK: - Compare
  252. class func compareIsChangedModel(_ model: KMBatesModel, withDict dict: NSDictionary) -> Bool {
  253. if let value = dict["fontName"] {
  254. if model.fontName != (value as! String) {
  255. return true
  256. }
  257. }
  258. if let value = dict["fontsize"] {
  259. if model.fontsize != (value as! CGFloat) {
  260. return true
  261. }
  262. }
  263. if let value = dict["color"] {
  264. if model.color.toHex() != (value as! String) {
  265. return true
  266. }
  267. }
  268. if let value = dict["leftMargin"] {
  269. if model.leftMargin != (value as! Int) {
  270. return true
  271. }
  272. }
  273. if let value = dict["rightMargin"] {
  274. if model.rightMargin != (value as! Int) {
  275. return true
  276. }
  277. }
  278. if let value = dict["bottomMargin"] {
  279. if model.bottomMargin != (value as! Int) {
  280. return true
  281. }
  282. }
  283. if let value = dict["topMargin"] {
  284. if model.topMargin != (value as! Int) {
  285. return true
  286. }
  287. }
  288. if let value = dict["topLeftString"] {
  289. if model.topLeftString != (value as! String) {
  290. return true
  291. }
  292. }
  293. if let value = dict["topCenterString"] {
  294. if model.topCenterString != (value as! String) {
  295. return true
  296. }
  297. }
  298. if let value = dict["topRightString"] {
  299. if model.topRightString != (value as! String) {
  300. return true
  301. }
  302. }
  303. if let value = dict["bottomLeftString"] {
  304. if model.bottomLeftString != (value as! String) {
  305. return true
  306. }
  307. }
  308. if let value = dict["bottomCenterString"] {
  309. if model.bottomCenterString != (value as! String) {
  310. return true
  311. }
  312. }
  313. if let value = dict["bottomRightString"] {
  314. if model.bottomRightString != (value as! String) {
  315. return true
  316. }
  317. }
  318. if let value = dict["prefixString"] {
  319. if model.prefixString != (value as! String) {
  320. return true
  321. }
  322. }
  323. if let value = dict["suffixString"] {
  324. if model.suffixString != (value as! String) {
  325. return true
  326. }
  327. }
  328. if let value = dict["digits"] {
  329. if model.digits != (value as! Int) {
  330. return true
  331. }
  332. }
  333. if let value = dict["startString"] {
  334. if model.startString != (value as! String) {
  335. return true
  336. }
  337. }
  338. return false
  339. }
  340. }
  341. //Class
  342. extension KMBatesManager {
  343. class func parseModel(model: KMBatesModel, _ pageCount: UInt) -> [String] {
  344. var topLeftString: String = ""
  345. if (!model.topLeftString.isEmpty) {
  346. var string = KMBatesManager.parsePageFormat(formatString: model.topLeftString, startPage: model.startString, pageCount: "\(pageCount)")
  347. string = KMBatesManager.parseDateFormat(formatString: string)
  348. topLeftString = string
  349. }
  350. var topCenterString: String = ""
  351. if (!model.topCenterString.isEmpty) {
  352. var string = KMBatesManager.parsePageFormat(formatString: model.topCenterString, startPage: model.startString, pageCount: "\(pageCount)")
  353. string = KMBatesManager.parseDateFormat(formatString: string)
  354. topCenterString = string
  355. }
  356. var topRightString: String = ""
  357. if (!model.topRightString.isEmpty) {
  358. var string = KMBatesManager.parsePageFormat(formatString: model.topRightString, startPage: model.startString, pageCount: "\(pageCount)")
  359. string = KMBatesManager.parseDateFormat(formatString: string)
  360. topRightString = string
  361. }
  362. var bottomLeftString: String = ""
  363. if (!model.bottomLeftString.isEmpty) {
  364. var string = KMBatesManager.parsePageFormat(formatString: model.bottomLeftString, startPage: model.startString, pageCount: "\(pageCount)")
  365. string = KMBatesManager.parseDateFormat(formatString: string)
  366. bottomLeftString = string
  367. }
  368. var bottomCenterString: String = ""
  369. if (!model.bottomCenterString.isEmpty) {
  370. var string = KMBatesManager.parsePageFormat(formatString: model.bottomCenterString, startPage: model.startString, pageCount: "\(pageCount)")
  371. string = KMBatesManager.parseDateFormat(formatString: string)
  372. bottomCenterString = string
  373. }
  374. var bottomRightString: String = ""
  375. if (!model.bottomRightString.isEmpty) {
  376. var string = KMBatesManager.parsePageFormat(formatString: model.bottomRightString, startPage: model.startString, pageCount: "\(pageCount)")
  377. string = KMBatesManager.parseDateFormat(formatString: string)
  378. bottomRightString = string
  379. }
  380. return [topLeftString, topCenterString, topRightString, bottomLeftString, bottomCenterString, bottomRightString]
  381. }
  382. class func parsePageFormat(formatString: String, startPage: String, pageCount: String) -> String {
  383. var result = formatString
  384. for pageFormat in self.getPageFormats() {
  385. let string = "<<\(pageFormat)>>"
  386. if (result.contains(string)) {
  387. var tempString = ""
  388. if (string == "<<1>>") {
  389. tempString.append("<<\(startPage)>>")
  390. } else if (string == "<<1 of n>>") {
  391. tempString.append("<<\(startPage)>>")
  392. tempString.append(" of \(pageCount)")
  393. } else if (string == "<<1/n>>") {
  394. tempString.append("<<\(startPage)>>")
  395. tempString.append("/\(pageCount)")
  396. } else if (string == "<<Page 1>>") {
  397. tempString.append("Page \(startPage)")
  398. } else if (string == "<<Page 1 of n>>") {
  399. tempString.append("Page \(startPage)")
  400. tempString.append("of \(pageCount)")
  401. }
  402. result = result.replacingOccurrences(of: string, with: tempString)
  403. }
  404. }
  405. return result
  406. }
  407. class func parseDateFormat(formatString: String) -> String {
  408. var result: String = formatString
  409. for dateFormat in self.getDateFormats() {
  410. if (result.contains(dateFormat)) {
  411. let formatString: String = dateFormat.replacingOccurrences(of: "m", with: "M")
  412. let replace = "<<\(dateFormat)>>"
  413. let date = Date()
  414. let dateFormatter = DateFormatter()
  415. dateFormatter.dateFormat = formatString
  416. let dateString = dateFormatter.string(from: date)
  417. result = result.replacingOccurrences(of: replace, with: dateString)
  418. }
  419. }
  420. return result
  421. }
  422. class func getPageFormats() -> [String] {
  423. return ["1",
  424. "1 of n",
  425. "1/n",
  426. "Page 1",
  427. "Page 1 of n"]
  428. }
  429. @objc class func getDateFormats() -> [String] {
  430. return ["m/d", "m/d/yy", "m/d/yyyy",
  431. "mm/dd/yy", "mm/dd/yyyy",
  432. "d/m/yy", "d/m/yyyy",
  433. "dd/mm/yy", "dd/mm/yyyy",
  434. "mm/yy", "mm/yyyy",
  435. "m.d.yy", "m.d.yyyy",
  436. "mm.dd.yy", "mm.dd.yyyy", "mm.yy", "mm.yyyy",
  437. "d.m.yy", "d.m.yyyy",
  438. "dd.mm.yy", "dd.mm.yyyy",
  439. "yy-mm-dd",
  440. "yyyy-mm-dd"]
  441. }
  442. }