TSDBManager.swift 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. //
  2. // TSDBManager.swift
  3. // TSLiveWallpaper
  4. //
  5. // Created by 100Years on 2025/6/10.
  6. //
  7. import RealmSwift
  8. import ObjectMapper
  9. // 1. 定义历史记录类型枚举
  10. enum TSDBHistoryType: String,CaseIterable {
  11. case aiList = "kDBAIList" //旧照片修复
  12. }
  13. //MARK: TSDBAIChatList - 用于存储会话及消息列表
  14. class TSDBHistory: Object {
  15. @Persisted(primaryKey: true) var primaryKey: String = ""
  16. @Persisted var listModels = List<TSDBActionInfoModel>()
  17. var type: TSDBHistoryType {
  18. get { TSDBHistoryType(rawValue: primaryKey) ?? .aiList }
  19. set { primaryKey = newValue.rawValue }
  20. }
  21. convenience init(type: TSDBHistoryType) {
  22. self.init()
  23. self.primaryKey = type.rawValue
  24. }
  25. func getModelList() -> [TSActionInfoModel] {
  26. var msgModel:[TSActionInfoModel] = []
  27. for msgDBModel in listModels {
  28. msgModel.append(msgDBModel.getModel())
  29. }
  30. return msgModel
  31. }
  32. func getModelList(completion:@escaping ([TSActionInfoModel])->Void){
  33. let frozenList = self.listModels.freeze()
  34. DispatchQueue.global(qos: .userInitiated).async {
  35. var msgModel:[TSActionInfoModel] = []
  36. for msgDBModel in frozenList {
  37. msgModel.append(msgDBModel.getModel())
  38. }
  39. DispatchQueue.main.async {
  40. completion(msgModel)
  41. }
  42. }
  43. }
  44. func getModelList(count:Int) -> [TSActionInfoModel] {
  45. let listModels = Array(listModels.prefix(count))
  46. var msgModel:[TSActionInfoModel] = []
  47. for msgDBModel in listModels {
  48. msgModel.append(msgDBModel.getModel())
  49. }
  50. return msgModel
  51. }
  52. func deleteAll() {
  53. TSRMShared.writeThread {
  54. listModels.removeAll()
  55. }
  56. }
  57. func deleteListModel(id:Int) {
  58. TSRMShared.writeThread {
  59. if let index = listModels.firstIndex(where: { $0.id == id }) {
  60. listModels.remove(at: index)
  61. debugPrint("listModels.remove(at: \(index))")
  62. }
  63. }
  64. }
  65. func deleteListModel(index:Int) {
  66. TSRMShared.writeThread {
  67. listModels.remove(at: index)
  68. debugPrint("listModels.remove(at: \(index))")
  69. }
  70. }
  71. func updateData(_ actionInfoModel:TSActionInfoModel,id:Int? = nil){
  72. let dbModel = TSDBActionInfoModel.createDBModel(actionInfoModel: actionInfoModel)
  73. var replaceID = dbModel.id
  74. if let id = id {
  75. replaceID = id
  76. }
  77. let frozenList = self.listModels.freeze()
  78. if let index = frozenList.firstIndex(where: { $0.id == replaceID }) {
  79. TSRMShared.writeThread {
  80. listModels[index] = dbModel// 如果找到,替换该元素
  81. }
  82. } else {
  83. print("Thread.current insert1=\(Thread.current)")
  84. TSRMShared.writeThread {
  85. listModels.insert(dbModel, at: 0)// 如果没有找到,添加到末尾
  86. print("Thread.current insert2=\(Thread.current)")
  87. }
  88. }
  89. }
  90. func updateDatas(_ actionInfoModels:[TSActionInfoModel]){
  91. for actionInfoModel in actionInfoModels {
  92. updateData(actionInfoModel)
  93. }
  94. }
  95. func addDatas(_ actionInfoModels:[TSActionInfoModel]){
  96. for actionInfoModel in actionInfoModels {
  97. let dbModel = TSDBActionInfoModel.createDBModel(actionInfoModel: actionInfoModel)
  98. TSRMShared.writeThread {
  99. listModels.append(dbModel)
  100. }
  101. }
  102. }
  103. }
  104. extension TSRealmManager {
  105. func getDBHistory(type:TSDBHistoryType) -> TSDBHistory {
  106. let predicate = NSPredicate(format: "primaryKey == %@",type.rawValue)
  107. let historys = TSRMShared.realm.objects(TSDBHistory.self).filter(predicate)
  108. if let history = historys.first {
  109. return history
  110. }else {
  111. let dbHistory = TSDBHistory(type: type)
  112. TSRMShared.update(dbHistory)
  113. return dbHistory
  114. }
  115. }
  116. func createExampleModel(id:Int,oldImageName:String,newImageName:String)->TSActionInfoModel{
  117. let model = TSActionInfoModel()
  118. model.id = id
  119. model.modelType = .example
  120. model.request.prompt = "Example"
  121. model.request.inputText = "Example"
  122. model.request.width = 330
  123. model.request.height = 440
  124. model.request.imageUrl = oldImageName
  125. model.response.resultUrl = newImageName
  126. model.status = "success"
  127. return model
  128. }
  129. //老照片修复
  130. var aiListDB:TSDBHistory {
  131. let history = getDBHistory(type: TSDBHistoryType.aiList)
  132. if history.listModels.count == 0,UserDefaults.standard.string(forKey: "insertAIListExampleData") == nil {
  133. let id = Date.timestampInt
  134. history.updateDatas([
  135. createExampleModel(id:id+1, oldImageName: "ailist_example_image_old_1", newImageName: "ailist_example_image_new_1"),
  136. createExampleModel(id:id+2, oldImageName: "ailist_example_image_old_0", newImageName: "ailist_example_image_new_0")
  137. ])
  138. UserDefaults.standard.set("1", forKey: "insertAIListExampleData")
  139. UserDefaults.standard.synchronize()
  140. }
  141. return history
  142. }
  143. }