TestDegreeManager.swift 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. try! degreeInfo.write(toFile: path, atomically: true)
  25. }
  26. // Clear
  27. func clearAllHistory() {
  28. degreeInfo.removeAllObjects()
  29. }
  30. // Set
  31. func set(_ degree:Double, fileType:String?) {
  32. set(degree, fileType: fileType, type: nil)
  33. }
  34. func set(_ degree:Double, fileType:String?, type:String?) {
  35. set(degree, fileType: fileType, type: type, fileName: nil)
  36. }
  37. func set(_ degree:Double, fileType:String?, type:String?, fileName:String?) {
  38. set(degree, fileType: fileType, type: type, fileName: fileName, refFilePath: nil)
  39. }
  40. func set(_ degree:Double, fileType:String?, type:String?, fileName:String?, refFilePath:String?) {
  41. let key = String((fileType ?? "") + (type ?? "") + (fileName ?? "") + (refFilePath ?? ""))
  42. let number = NSNumber(floatLiteral: degree)
  43. objc_sync_enter(self)
  44. degreeInfo.setValue(number, forKey: key)
  45. objc_sync_exit(self)
  46. }
  47. //Get
  48. func degreeFor(_ fileType:String?) -> Double {
  49. return degreeFor(fileType, type: nil)
  50. }
  51. func degreeFor(_ fileType:String?, type:String?) -> Double {
  52. return degreeFor(fileType, type: type, fileName: nil)
  53. }
  54. func degreeFor(_ fileType:String?, type:String?, fileName:String?) -> Double {
  55. return degreeFor(fileType, type: type, fileName: fileName, refFilePath: nil)
  56. }
  57. func degreeFor(_ fileType:String?, type:String?, fileName:String?, refFilePath:String?) -> Double {
  58. let key = String((fileType ?? "") + (type ?? "") + (fileName ?? "") + (refFilePath ?? ""))
  59. if nil != degreeInfo.value(forKey: key) {
  60. let number = degreeInfo.value(forKey: key) as! NSNumber
  61. return number.doubleValue
  62. }
  63. return 0.0
  64. }
  65. }
  66. // File Info
  67. extension NSMutableDictionary {
  68. func degree() -> Double {
  69. return TestDegreeManager.shared().degreeFor(self.objc().fileType(),
  70. type: self.objc().type(),
  71. fileName: self.fileName(),
  72. refFilePath: self.refFilePath())
  73. }
  74. }