TSDBManager.swift 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. for index in listModels.indices {
  54. deleteResources(index: index)
  55. }
  56. TSRMShared.writeThread {
  57. listModels.removeAll()
  58. TSRMShared.realm.delete(listModels)
  59. }
  60. }
  61. func deleteListModel(id:Int) {
  62. if let index = listModels.firstIndex(where: { $0.id == id }) {
  63. deleteListModel(index: index)
  64. }
  65. }
  66. func deleteListModel(uuid:String) {
  67. if let index = listModels.firstIndex(where: { $0.uuid == uuid }) {
  68. deleteListModel(index: index)
  69. }
  70. }
  71. func deleteListModel(index:Int) {
  72. TSRMShared.writeThread {
  73. let model = listModels[safe: index]
  74. deleteResources(index:index)
  75. listModels.remove(at: index)
  76. if let model = model {
  77. TSRMShared.realm.delete(model)
  78. }
  79. debugPrint("listModels.remove(at: \(index))")
  80. }
  81. }
  82. func deleteResources(index:Int) {
  83. if let model = listModels[safe: index] {
  84. model.deleteResources()
  85. }
  86. }
  87. func updateData(_ actionInfoModel:TSActionInfoModel,uuid:String? = nil){
  88. let dbModel = TSDBActionInfoModel.createDBModel(actionInfoModel: actionInfoModel)
  89. let replaceUUID = uuid ?? dbModel.uuid
  90. let frozenList = self.listModels.freeze()
  91. if let index = frozenList.firstIndex(where: { $0.uuid == replaceUUID }) {
  92. dePrint("updateData 替换 \(dbModel)")
  93. TSRMShared.writeThread {
  94. listModels[index] = dbModel// 如果找到,替换该元素
  95. }
  96. } else {
  97. TSRMShared.writeThread {
  98. dePrint("updateData 插入 \(dbModel)")
  99. listModels.insert(dbModel, at: 0)// 如果没有找到,添加到末尾
  100. }
  101. }
  102. }
  103. func updateDatas(_ actionInfoModels:[TSActionInfoModel]){
  104. for actionInfoModel in actionInfoModels {
  105. updateData(actionInfoModel)
  106. }
  107. }
  108. func addDatas(_ actionInfoModels:[TSActionInfoModel]){
  109. for actionInfoModel in actionInfoModels {
  110. let dbModel = TSDBActionInfoModel.createDBModel(actionInfoModel: actionInfoModel)
  111. TSRMShared.writeThread {
  112. listModels.append(dbModel)
  113. }
  114. }
  115. }
  116. }
  117. extension TSRealmManager {
  118. func getDBHistory(type:TSDBHistoryType) -> TSDBHistory {
  119. let predicate = NSPredicate(format: "primaryKey == %@",type.rawValue)
  120. let historys = TSRMShared.realm.objects(TSDBHistory.self).filter(predicate)
  121. if let history = historys.first {
  122. return history
  123. }else {
  124. let dbHistory = TSDBHistory(type: type)
  125. TSRMShared.update(dbHistory)
  126. return dbHistory
  127. }
  128. }
  129. func createExampleModel(id:Int,oldImageName:String,newImageName:String,generateStyle:TSGeneratorImageStyle)->TSActionInfoModel{
  130. let model = TSActionInfoModel()
  131. model.id = id
  132. model.modelType = .example
  133. model.request.prompt = "Example"
  134. model.request.inputText = "Example"
  135. model.request.width = 330
  136. model.request.height = 440
  137. model.request.imageUrl = oldImageName
  138. model.response.resultUrl = newImageName
  139. model.status = "success"
  140. model.request.generatorStyle = generateStyle
  141. return model
  142. }
  143. //老照片修复
  144. var aiListDB:TSDBHistory {
  145. let history = getDBHistory(type: TSDBHistoryType.aiList)
  146. // if history.listModels.count == 0,UserDefaults.standard.string(forKey: "insertAIListExampleData") == nil {
  147. // let id = Date.timestampInt
  148. // history.updateDatas([
  149. // createExampleModel(id:id+1, oldImageName: "ailist_example_image_old_1", newImageName: "ailist_example_image_new_1",generateStyle:.enhance),
  150. // createExampleModel(id:id+2, oldImageName: "ailist_example_image_old_0", newImageName: "ailist_example_image_new_0",generateStyle:.colorize)
  151. // ])
  152. // UserDefaults.standard.set("1", forKey: "insertAIListExampleData")
  153. // UserDefaults.standard.synchronize()
  154. // }
  155. if history.listModels.count > 0,UserDefaults.standard.string(forKey: "aiListDBDeleteIncorrectData") == nil {
  156. var primaryKeysToKeep:[String] = []
  157. for model in history.listModels {
  158. primaryKeysToKeep.append(model.primaryKey)
  159. }
  160. writeThread {
  161. let objectsToDelete = realm.objects(TSDBActionInfoModel.self)
  162. .filter("NOT (primaryKey IN %@)", primaryKeysToKeep)
  163. realm.delete(objectsToDelete)
  164. }
  165. UserDefaults.standard.set("1", forKey: "aiListDBDeleteIncorrectData")
  166. UserDefaults.standard.synchronize()
  167. }
  168. return history
  169. }
  170. }