KMSignatureManager.swift 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. //
  2. // KMSignatureManager.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by lizhe on 2023/10/9.
  6. //
  7. import Foundation
  8. @objcMembers class KMSignatureManager: NSObject {
  9. var signatureList: [KMSignature] = []
  10. override init() {
  11. signatureList = []
  12. }
  13. @objc func loadAllSignatureList() {
  14. signatureList.removeAll()
  15. if let fileDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {
  16. let newFilePath = fileDirectory.appendingPathComponent("NewSignatureList")
  17. if FileManager.default.fileExists(atPath: newFilePath.path) {
  18. // 使用 New 的数据
  19. } else { // 第一次加载 或 未存 New 的数据
  20. let filePath = fileDirectory.appendingPathComponent("SignatureList")
  21. if FileManager.default.fileExists(atPath: filePath.path) {
  22. NSKeyedUnarchiver.setClass(KMSignature.self, forClassName: "KMSignature")
  23. if let array = NSKeyedUnarchiver.unarchiveObject(withFile: filePath.path) as? [KMSignature] {
  24. // signatureList = array
  25. for model in array {
  26. signatureList.append(model)
  27. }
  28. }
  29. }
  30. }
  31. }
  32. if let fileDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {
  33. let filePath = fileDirectory.appendingPathComponent("NewSignatureList")
  34. if FileManager.default.fileExists(atPath: filePath.path) {
  35. // NSKeyedUnarchiver.setClass(KMSignature.self, forClassName: "KMSignature")
  36. if let array = NSKeyedUnarchiver.unarchiveObject(withFile: filePath.path) as? [KMSignature] {
  37. // signatureList = array
  38. for model in array {
  39. signatureList.append(model)
  40. }
  41. }
  42. }
  43. }
  44. }
  45. func removeAllObject() {
  46. signatureList.removeAll()
  47. }
  48. func removeObject(index: Int) {
  49. signatureList.remove(at: index)
  50. }
  51. @objc func addSignature(_ signature: KMSignature) {
  52. signatureList.append(signature)
  53. }
  54. @objc func saveSingaturesToFile() {
  55. if let fileDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {
  56. let filePath = fileDirectory.appendingPathComponent("NewSignatureList")
  57. NSKeyedArchiver.archiveRootObject(signatureList, toFile: filePath.path)
  58. }
  59. }
  60. }