TestDegreeManager.swift 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //
  2. // TestDegreeManager.swift
  3. // KdanAuto
  4. //
  5. // Created by 朱东勇 on 2023/2/9.
  6. //
  7. import Foundation
  8. class TestDegreeManager : NSObject {
  9. var degreeInfo = NSMutableDictionary()
  10. static var tdManager = TestDegreeManager()
  11. class func shared() -> TestDegreeManager {
  12. return tdManager
  13. }
  14. override init() {
  15. var path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
  16. path = NSString(string: path).appendingPathComponent("TestDegreeData.plist");
  17. if let info = try? NSDictionary.init(contentsOfFile: path) {
  18. degreeInfo = NSMutableDictionary.init(dictionary: info);
  19. }
  20. }
  21. func saveInfo() {
  22. var path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
  23. path = NSString(string: path).appendingPathComponent("TestDegreeData.plist");
  24. objc_sync_enter(self)
  25. try? degreeInfo.write(toFile: path, atomically: true)
  26. objc_sync_exit(self)
  27. }
  28. // Clear
  29. func clearAllHistory() {
  30. degreeInfo.removeAllObjects()
  31. }
  32. // Set
  33. func set(_ degree:Double, fileType:String?) {
  34. set(degree, fileType: fileType, type: nil)
  35. saveInfo();
  36. }
  37. func set(_ degree:Double, fileType:String?, type:String?) {
  38. set(degree, fileType: fileType, type: type, fileName: nil)
  39. }
  40. func set(_ degree:Double, fileType:String?, type:String?, fileName:String?) {
  41. set(degree, fileType: fileType, type: type, fileName: fileName, refFilePath: nil)
  42. }
  43. func set(_ degree:Double, fileType:String?, type:String?, fileName:String?, refFilePath:String?) {
  44. let key = String((fileType ?? "") + (type ?? "") + (fileName ?? "") + (refFilePath ?? ""))
  45. let number = NSNumber(floatLiteral: degree)
  46. objc_sync_enter(self)
  47. degreeInfo.setValue(number, forKey: key)
  48. objc_sync_exit(self)
  49. }
  50. //Get
  51. func degreeFor(_ fileType:String?) -> Double {
  52. return degreeFor(fileType, type: nil)
  53. }
  54. func degreeFor(_ fileType:String?, type:String?) -> Double {
  55. return degreeFor(fileType, type: type, fileName: nil)
  56. }
  57. func degreeFor(_ fileType:String?, type:String?, fileName:String?) -> Double {
  58. return degreeFor(fileType, type: type, fileName: fileName, refFilePath: nil)
  59. }
  60. func degreeFor(_ fileType:String?, type:String?, fileName:String?, refFilePath:String?) -> Double {
  61. let key = String((fileType ?? "") + (type ?? "") + (fileName ?? "") + (refFilePath ?? ""))
  62. if nil != degreeInfo.value(forKey: key) {
  63. objc_sync_enter(self)
  64. let number = degreeInfo.value(forKey: key) as! NSNumber
  65. objc_sync_exit(self)
  66. return number.doubleValue
  67. }
  68. return 0.0
  69. }
  70. }
  71. // File Info
  72. extension NSMutableDictionary {
  73. func degree() -> Double {
  74. return TestDegreeManager.shared().degreeFor(self.objc().fileType(),
  75. type: self.objc().type(),
  76. fileName: self.fileName(),
  77. refFilePath: self.refFilePath())
  78. }
  79. }