123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- //
- // DataModel.swift
- // KdanAuto
- //
- // Created by 朱东勇 on 2022/11/21.
- //
- import Foundation
- let kLoadDirectoryPathKey : String = "kLoadDirectoryPathKey";
- let kOriginPathComponent : String = "Origin Files"
- let kResultPathComponent : String = "Result Files"
- let kCheckPathComponent : String = "Check Files"
- let kLatestReportIDKey : String = "LatestReportID"
- let kComparativeDifferenceKey : String = "ComparativeDifference"
-
- class DataModel : NSObject {
- static var shared : DataModel = DataModel()
-
- var _directoryPath : String = ""
-
- /// Setter & Getter
-
- public func setDirectoryPath(_ path:String) {
- let userDefaults = UserDefaults.standard
- userDefaults.setValue(path, forKey: kLoadDirectoryPathKey)
- userDefaults.synchronize()
- }
-
- public func directoryPath() -> String {
- var path = UserDefaults.standard.value(forKey: kLoadDirectoryPathKey)
-
- if path == nil {
- path = ""
- }
-
- return path as! String
- }
-
-
-
- // Expand
- public func setIsExpand(_ fileType:String, expand:Bool) {
- let userDefaults = UserDefaults.standard
- if expand {
- userDefaults.setValue("true", forKey: String("\(fileType)-expand"))
- }else {
- userDefaults.setValue(nil, forKey: String("\(fileType)-expand"))
- }
- userDefaults.synchronize()
- }
-
- public func isExpand(_ fileType:String) -> Bool {
- let userDefaults = UserDefaults.standard
-
- return userDefaults.value(forKey: String("\(fileType)-expand")) != nil
- }
-
-
- public func originFilesFor(_ fileType:String, type:String) -> [String] {
- var path = self.directoryPath() as NSString
- path = path.appendingPathComponent(fileType) as NSString
- path = path.appendingPathComponent(type) as NSString
- path = path.appendingPathComponent(kOriginPathComponent) as NSString
-
- if !FileManager.default.fileExists(atPath: path as String) {
- return []
- }
-
- let items = try? FileManager.default.subpathsOfDirectory(atPath: path as String)
- let subPaths = NSMutableArray(array: items ?? [])
- subPaths.remove(".DS_Store")
-
- var index = Int(0)
- while (index < subPaths.count) {
- let item = subPaths.object(at: index)
- index += 1
-
- let filePath = path.appendingPathComponent(item as! String);
-
- var isDirectory = ObjCBool(false)
- if (!FileManager.default.fileExists(atPath: filePath, isDirectory: &isDirectory) || isDirectory.boolValue ||
- NSString(string: filePath).contains(".DS_Store")) {
- subPaths.remove(item);
- index -= 1
- }
- }
-
- return subPaths as! [String]
- }
-
- public func generaNewReportID() -> String {
- let string = NSString.init(format: "%.0f", NSDate.now.timeIntervalSince1970) as String
-
- let userDefaults = UserDefaults.standard
- userDefaults.set(string, forKey: kLatestReportIDKey)
- userDefaults.synchronize()
-
- return string
- }
-
- public func latestReportID() -> String? {
- let userDefaults = UserDefaults.standard
-
- return userDefaults.value(forKey: kLatestReportIDKey) as? String
- }
-
- public func comparativeDifference() -> Int {
- let userDefaults = UserDefaults.standard
-
- if (userDefaults.value(forKey: kComparativeDifferenceKey) != nil) {
- return userDefaults.value(forKey: kComparativeDifferenceKey) as? Int ?? Int(255 * 0.03)
- }
-
- return Int(255 * 0.03)
- }
-
- public func setComparativeDifference(_ comparativeDifference:Int) {
- let userDefaults = UserDefaults.standard
- userDefaults.set(comparativeDifference, forKey: kComparativeDifferenceKey)
- userDefaults.synchronize()
- }
-
- /// Save Data
-
- }
|