DataModel.swift 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. //
  2. // DataModel.swift
  3. // KdanAuto
  4. //
  5. // Created by 朱东勇 on 2022/11/21.
  6. //
  7. import Foundation
  8. let kLoadDirectoryPathKey : String = "kLoadDirectoryPathKey";
  9. let kOriginPathComponent : String = "Origin Files"
  10. let kResultPathComponent : String = "Result Files"
  11. let kCheckPathComponent : String = "Check Files"
  12. let kLatestReportIDKey : String = "LatestReportID"
  13. let kComparativeDifferenceKey : String = "ComparativeDifference"
  14. class DataModel : NSObject {
  15. static var shared : DataModel = DataModel()
  16. var _directoryPath : String = ""
  17. /// Setter & Getter
  18. public func setDirectoryPath(_ path:String) {
  19. let userDefaults = UserDefaults.standard
  20. userDefaults.setValue(path, forKey: kLoadDirectoryPathKey)
  21. userDefaults.synchronize()
  22. }
  23. public func directoryPath() -> String {
  24. var path = UserDefaults.standard.value(forKey: kLoadDirectoryPathKey)
  25. if path == nil {
  26. path = ""
  27. }
  28. return path as! String
  29. }
  30. // Expand
  31. public func setIsExpand(_ fileType:String, expand:Bool) {
  32. let userDefaults = UserDefaults.standard
  33. if expand {
  34. userDefaults.setValue("true", forKey: String("\(fileType)-expand"))
  35. }else {
  36. userDefaults.setValue(nil, forKey: String("\(fileType)-expand"))
  37. }
  38. userDefaults.synchronize()
  39. }
  40. public func isExpand(_ fileType:String) -> Bool {
  41. let userDefaults = UserDefaults.standard
  42. return userDefaults.value(forKey: String("\(fileType)-expand")) != nil
  43. }
  44. public func originFilesFor(_ fileType:String, type:String) -> [String] {
  45. var path = self.directoryPath() as NSString
  46. path = path.appendingPathComponent(fileType) as NSString
  47. path = path.appendingPathComponent(type) as NSString
  48. path = path.appendingPathComponent(kOriginPathComponent) as NSString
  49. if !FileManager.default.fileExists(atPath: path as String) {
  50. return []
  51. }
  52. let items = try? FileManager.default.subpathsOfDirectory(atPath: path as String)
  53. let subPaths = NSMutableArray(array: items ?? [])
  54. subPaths.remove(".DS_Store")
  55. var index = Int(0)
  56. while (index < subPaths.count) {
  57. let item = subPaths.object(at: index)
  58. index += 1
  59. let filePath = path.appendingPathComponent(item as! String);
  60. var isDirectory = ObjCBool(false)
  61. if (!FileManager.default.fileExists(atPath: filePath, isDirectory: &isDirectory) || isDirectory.boolValue ||
  62. NSString(string: filePath).contains(".DS_Store")) {
  63. subPaths.remove(item);
  64. index -= 1
  65. }
  66. }
  67. return subPaths as! [String]
  68. }
  69. public func generaNewReportID() -> String {
  70. let string = NSString.init(format: "%.0f", NSDate.now.timeIntervalSince1970) as String
  71. let userDefaults = UserDefaults.standard
  72. userDefaults.set(string, forKey: kLatestReportIDKey)
  73. userDefaults.synchronize()
  74. return string
  75. }
  76. public func latestReportID() -> String? {
  77. let userDefaults = UserDefaults.standard
  78. return userDefaults.value(forKey: kLatestReportIDKey) as? String
  79. }
  80. public func comparativeDifference() -> Int {
  81. let userDefaults = UserDefaults.standard
  82. if (userDefaults.value(forKey: kComparativeDifferenceKey) != nil) {
  83. return userDefaults.value(forKey: kComparativeDifferenceKey) as? Int ?? Int(255 * 0.03)
  84. }
  85. return Int(255 * 0.03)
  86. }
  87. public func setComparativeDifference(_ comparativeDifference:Int) {
  88. let userDefaults = UserDefaults.standard
  89. userDefaults.set(comparativeDifference, forKey: kComparativeDifferenceKey)
  90. userDefaults.synchronize()
  91. }
  92. /// Save Data
  93. }