// // TestDegreeManager.swift // KdanAuto // // Created by 朱东勇 on 2023/2/9. // import Foundation class TestDegreeManager : NSObject { var degreeInfo = NSMutableDictionary() static var tdManager = TestDegreeManager() class func shared() -> TestDegreeManager { return tdManager } override init() { var path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first! path = NSString(string: path).appendingPathComponent("TestDegreeData.plist"); if let info = try? NSDictionary.init(contentsOfFile: path) { degreeInfo = NSMutableDictionary.init(dictionary: info); } } func saveInfo() { var path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first! path = NSString(string: path).appendingPathComponent("TestDegreeData.plist"); objc_sync_enter(self) try? degreeInfo.write(toFile: path, atomically: true) objc_sync_exit(self) } // Clear func clearAllHistory() { degreeInfo.removeAllObjects() } // Set func set(_ degree:Double, fileType:String?) { set(degree, fileType: fileType, type: nil) saveInfo(); } func set(_ degree:Double, fileType:String?, type:String?) { set(degree, fileType: fileType, type: type, fileName: nil) } func set(_ degree:Double, fileType:String?, type:String?, fileName:String?) { set(degree, fileType: fileType, type: type, fileName: fileName, refFilePath: nil) } func set(_ degree:Double, fileType:String?, type:String?, fileName:String?, refFilePath:String?) { let key = String((fileType ?? "") + (type ?? "") + (fileName ?? "") + (refFilePath ?? "")) let number = NSNumber(floatLiteral: degree) objc_sync_enter(self) degreeInfo.setValue(number, forKey: key) objc_sync_exit(self) } //Get func degreeFor(_ fileType:String?) -> Double { return degreeFor(fileType, type: nil) } func degreeFor(_ fileType:String?, type:String?) -> Double { return degreeFor(fileType, type: type, fileName: nil) } func degreeFor(_ fileType:String?, type:String?, fileName:String?) -> Double { return degreeFor(fileType, type: type, fileName: fileName, refFilePath: nil) } func degreeFor(_ fileType:String?, type:String?, fileName:String?, refFilePath:String?) -> Double { let key = String((fileType ?? "") + (type ?? "") + (fileName ?? "") + (refFilePath ?? "")) if nil != degreeInfo.value(forKey: key) { objc_sync_enter(self) let number = degreeInfo.value(forKey: key) as! NSNumber objc_sync_exit(self) return number.doubleValue } return 0.0 } } // File Info extension NSMutableDictionary { func degree() -> Double { return TestDegreeManager.shared().degreeFor(self.objc().fileType(), type: self.objc().type(), fileName: self.fileName(), refFilePath: self.refFilePath()) } }