TSBaseHistoryManager.swift 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. //
  2. // TSBaseHistoryManager.swift
  3. // AIEmoji
  4. //
  5. // Created by 100Years on 2025/4/23.
  6. //
  7. import ObjectMapper
  8. // MARK: - 基础历史记录类
  9. class TSBaseHistoryManager<ModelType: TSBaseModel> {
  10. // 子类必须重写的属性
  11. var historyKey: String { fatalError("必须重写 historyKey") }
  12. var exampleDataKey: String { fatalError("必须重写 exampleDataKey") }
  13. var exampleModels: [ModelType] { fatalError("必须重写 exampleModels") }
  14. func findModelID(modelID: Int)->Int?{
  15. fatalError("必须重写 findModelID")
  16. }
  17. func saveModelAfterProcess(){
  18. }
  19. // 存储属性
  20. private var _historyString: String {
  21. get { UserDefaults.standard.string(forKey: historyKey) ?? "" }
  22. set { UserDefaults.standard.set(newValue, forKey: historyKey) }
  23. }
  24. private var _listModels: [ModelType]?
  25. var listModels: [ModelType] {
  26. get {
  27. if _listModels == nil { loadModels() }
  28. return _listModels ?? []
  29. }
  30. set {
  31. _listModels = newValue
  32. }
  33. }
  34. // MARK: - 公共方法
  35. func saveModel(model: ModelType, at index: Int = 0) {
  36. listModels.insert(model, at: index)
  37. saveHistory()
  38. saveModelAfterProcess()
  39. }
  40. func removeModel(model:ModelType) {
  41. self.listModels.removeAll { $0 === model }
  42. saveHistory()
  43. }
  44. func removeModel(index: Int) {
  45. guard index >= 0 && index < listModels.count else { return }
  46. listModels.remove(at: index)
  47. saveHistory()
  48. }
  49. func removeALLModel() {
  50. listModels.removeAll()
  51. saveHistory()
  52. }
  53. func replaceModel(oldID: Int, newModel: ModelType){
  54. if let index = findModelID(modelID: oldID) {
  55. listModels[index] = newModel
  56. dePrint("\(Self.self).listModels Model replaced at index \(index)")
  57. } else {
  58. listModels.insert(newModel, at: 0)
  59. dePrint("\(Self.self).listModels Model not found")
  60. }
  61. dePrint("\(Self.self).listModels.count=\(listModels.count)")
  62. saveHistory()
  63. }
  64. func replaceAndSaveModel(saveModel:ModelType,compareBlock:(ModelType,ModelType)->Bool){
  65. if let index = listModels.firstIndex(where: { model in
  66. compareBlock(saveModel,model)
  67. }){
  68. dePrint("\(Self.self).listModels Model replaced at index \(index)")
  69. listModels[index] = saveModel
  70. saveHistory()
  71. }else{
  72. self.saveModel(model: saveModel)
  73. }
  74. }
  75. func dePrintAllModel() {
  76. dePrint("=======================结果查询开始======================")
  77. dePrint("\(Self.self).listModels.count=\(listModels.count)")
  78. for model in listModels {
  79. dePrint(model.toJSON())
  80. }
  81. dePrint("=======================结果查询结束======================")
  82. }
  83. // MARK: - 私有方法
  84. func saveHistory() {
  85. if let jsonString = listModels.toJSONString() {
  86. _historyString = jsonString
  87. }
  88. }
  89. private func loadModels() {
  90. if exampleModels.count > 0 {
  91. // 第一次运行时插入示例数据
  92. if UserDefaults.standard.string(forKey: exampleDataKey) == nil {
  93. insertExampleData()
  94. UserDefaults.standard.set("1", forKey: exampleDataKey)
  95. }
  96. }
  97. debugPrint("_listModels 读取前")
  98. // 从历史记录加载模型
  99. if let models = Mapper<ModelType>().mapArray(JSONString: _historyString) {
  100. _listModels = models
  101. } else {
  102. _listModels = []
  103. }
  104. debugPrint("_listModels 读取后")
  105. }
  106. private func insertExampleData() {
  107. if let jsonString = exampleModels.toJSONString() {
  108. _historyString = jsonString
  109. }
  110. }
  111. }