KMBatesManager.swift 18 KB

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