123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- //
- // TSBaseHistoryManager.swift
- // AIEmoji
- //
- // Created by 100Years on 2025/4/23.
- //
- import ObjectMapper
- // MARK: - 基础历史记录类
- class TSBaseHistoryManager<ModelType: TSBaseModel> {
- // 子类必须重写的属性
- var historyKey: String { fatalError("必须重写 historyKey") }
- var exampleDataKey: String { fatalError("必须重写 exampleDataKey") }
- var exampleModels: [ModelType] { fatalError("必须重写 exampleModels") }
- func findModelID(modelID: Int)->Int?{
- fatalError("必须重写 findModelID")
- }
- func saveModelAfterProcess(){
-
- }
-
- // 存储属性
- private var _historyString: String {
- get { UserDefaults.standard.string(forKey: historyKey) ?? "" }
- set { UserDefaults.standard.set(newValue, forKey: historyKey) }
- }
-
- private var _listModels: [ModelType]?
- var listModels: [ModelType] {
- get {
- if _listModels == nil { loadModels() }
- return _listModels ?? []
- }
- set {
- _listModels = newValue
- }
- }
-
- // MARK: - 公共方法
- func saveModel(model: ModelType, at index: Int = 0) {
- listModels.insert(model, at: index)
- saveHistory()
- saveModelAfterProcess()
- }
-
- func removeModel(model:ModelType) {
- self.listModels.removeAll { $0 === model }
- saveHistory()
- }
-
- func removeModel(index: Int) {
- guard index >= 0 && index < listModels.count else { return }
- listModels.remove(at: index)
- saveHistory()
- }
-
- func removeALLModel() {
- listModels.removeAll()
- saveHistory()
- }
-
- func replaceModel(oldID: Int, newModel: ModelType){
- if let index = findModelID(modelID: oldID) {
- listModels[index] = newModel
- dePrint("\(Self.self).listModels Model replaced at index \(index)")
- } else {
- listModels.insert(newModel, at: 0)
- dePrint("\(Self.self).listModels Model not found")
- }
- dePrint("\(Self.self).listModels.count=\(listModels.count)")
- saveHistory()
- }
-
- func replaceAndSaveModel(saveModel:ModelType,compareBlock:(ModelType,ModelType)->Bool){
- if let index = listModels.firstIndex(where: { model in
- compareBlock(saveModel,model)
- }){
- dePrint("\(Self.self).listModels Model replaced at index \(index)")
- listModels[index] = saveModel
- saveHistory()
- }else{
- self.saveModel(model: saveModel)
- }
- }
-
- func dePrintAllModel() {
- dePrint("=======================结果查询开始======================")
- dePrint("\(Self.self).listModels.count=\(listModels.count)")
- for model in listModels {
- dePrint(model.toJSON())
- }
- dePrint("=======================结果查询结束======================")
- }
-
- // MARK: - 私有方法
- func saveHistory() {
- if let jsonString = listModels.toJSONString() {
- _historyString = jsonString
- }
- }
-
- private func loadModels() {
- if exampleModels.count > 0 {
- // 第一次运行时插入示例数据
- if UserDefaults.standard.string(forKey: exampleDataKey) == nil {
- insertExampleData()
- UserDefaults.standard.set("1", forKey: exampleDataKey)
- }
- }
- debugPrint("_listModels 读取前")
- // 从历史记录加载模型
- if let models = Mapper<ModelType>().mapArray(JSONString: _historyString) {
- _listModels = models
- } else {
- _listModels = []
- }
- debugPrint("_listModels 读取后")
- }
-
- private func insertExampleData() {
- if let jsonString = exampleModels.toJSONString() {
- _historyString = jsonString
- }
- }
- }
|