123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- //
- // TSDBManager.swift
- // TSLiveWallpaper
- //
- // Created by 100Years on 2025/6/10.
- //
- import RealmSwift
- import ObjectMapper
- // 1. 定义历史记录类型枚举
- enum TSDBHistoryType: String,CaseIterable {
- case aiList = "kDBAIList" //旧照片修复
- }
- //MARK: TSDBAIChatList - 用于存储会话及消息列表
- class TSDBHistory: Object {
- @Persisted(primaryKey: true) var primaryKey: String = ""
- @Persisted var listModels = List<TSDBActionInfoModel>()
-
- var type: TSDBHistoryType {
- get { TSDBHistoryType(rawValue: primaryKey) ?? .aiList }
- set { primaryKey = newValue.rawValue }
- }
-
- convenience init(type: TSDBHistoryType) {
- self.init()
- self.primaryKey = type.rawValue
- }
-
- func getModelList() -> [TSActionInfoModel] {
- var msgModel:[TSActionInfoModel] = []
- for msgDBModel in listModels {
- msgModel.append(msgDBModel.getModel())
- }
- return msgModel
- }
-
-
- func getModelList(completion:@escaping ([TSActionInfoModel])->Void){
- let frozenList = self.listModels.freeze()
- DispatchQueue.global(qos: .userInitiated).async {
- var msgModel:[TSActionInfoModel] = []
- for msgDBModel in frozenList {
- msgModel.append(msgDBModel.getModel())
- }
- DispatchQueue.main.async {
- completion(msgModel)
- }
- }
- }
-
- func getModelList(count:Int) -> [TSActionInfoModel] {
- let listModels = Array(listModels.prefix(count))
- var msgModel:[TSActionInfoModel] = []
- for msgDBModel in listModels {
- msgModel.append(msgDBModel.getModel())
- }
- return msgModel
- }
-
- func deleteAll() {
- for index in listModels.indices {
- deleteResources(index: index)
- }
- TSRMShared.writeThread {
- listModels.removeAll()
- TSRMShared.realm.delete(listModels)
- }
- }
-
- func deleteListModel(id:Int) {
- if let index = listModels.firstIndex(where: { $0.id == id }) {
- deleteListModel(index: index)
- }
- }
-
- func deleteListModel(uuid:String) {
- if let index = listModels.firstIndex(where: { $0.uuid == uuid }) {
- deleteListModel(index: index)
- }
- }
-
- func deleteListModel(index:Int) {
- TSRMShared.writeThread {
- let model = listModels[safe: index]
- deleteResources(index:index)
- listModels.remove(at: index)
- if let model = model {
- TSRMShared.realm.delete(model)
- }
-
- debugPrint("listModels.remove(at: \(index))")
- }
- }
- func deleteResources(index:Int) {
- if let model = listModels[safe: index] {
- model.deleteResources()
- }
- }
-
- func updateData(_ actionInfoModel:TSActionInfoModel,uuid:String? = nil){
- let dbModel = TSDBActionInfoModel.createDBModel(actionInfoModel: actionInfoModel)
- let replaceUUID = uuid ?? dbModel.uuid
- let frozenList = self.listModels.freeze()
- if let index = frozenList.firstIndex(where: { $0.uuid == replaceUUID }) {
- dePrint("updateData 替换 \(dbModel)")
- TSRMShared.writeThread {
- listModels[index] = dbModel// 如果找到,替换该元素
- }
- } else {
- TSRMShared.writeThread {
- dePrint("updateData 插入 \(dbModel)")
- listModels.insert(dbModel, at: 0)// 如果没有找到,添加到末尾
- }
- }
- }
-
- func updateDatas(_ actionInfoModels:[TSActionInfoModel]){
- for actionInfoModel in actionInfoModels {
- updateData(actionInfoModel)
- }
- }
-
- func addDatas(_ actionInfoModels:[TSActionInfoModel]){
- for actionInfoModel in actionInfoModels {
- let dbModel = TSDBActionInfoModel.createDBModel(actionInfoModel: actionInfoModel)
- TSRMShared.writeThread {
- listModels.append(dbModel)
- }
- }
- }
- }
- extension TSRealmManager {
- func getDBHistory(type:TSDBHistoryType) -> TSDBHistory {
- let predicate = NSPredicate(format: "primaryKey == %@",type.rawValue)
- let historys = TSRMShared.realm.objects(TSDBHistory.self).filter(predicate)
- if let history = historys.first {
- return history
- }else {
- let dbHistory = TSDBHistory(type: type)
- TSRMShared.update(dbHistory)
- return dbHistory
- }
- }
-
- func createExampleModel(id:Int,oldImageName:String,newImageName:String,generateStyle:TSGeneratorImageStyle)->TSActionInfoModel{
- let model = TSActionInfoModel()
- model.id = id
- model.modelType = .example
- model.request.prompt = "Example"
- model.request.inputText = "Example"
- model.request.width = 330
- model.request.height = 440
- model.request.imageUrl = oldImageName
- model.response.resultUrl = newImageName
- model.status = "success"
- model.request.generatorStyle = generateStyle
- return model
- }
-
- //老照片修复
- var aiListDB:TSDBHistory {
- let history = getDBHistory(type: TSDBHistoryType.aiList)
- // if history.listModels.count == 0,UserDefaults.standard.string(forKey: "insertAIListExampleData") == nil {
- // let id = Date.timestampInt
- // history.updateDatas([
- // createExampleModel(id:id+1, oldImageName: "ailist_example_image_old_1", newImageName: "ailist_example_image_new_1",generateStyle:.enhance),
- // createExampleModel(id:id+2, oldImageName: "ailist_example_image_old_0", newImageName: "ailist_example_image_new_0",generateStyle:.colorize)
- // ])
- // UserDefaults.standard.set("1", forKey: "insertAIListExampleData")
- // UserDefaults.standard.synchronize()
- // }
-
- if history.listModels.count > 0,UserDefaults.standard.string(forKey: "aiListDBDeleteIncorrectData") == nil {
- var primaryKeysToKeep:[String] = []
- for model in history.listModels {
- primaryKeysToKeep.append(model.primaryKey)
- }
- writeThread {
- let objectsToDelete = realm.objects(TSDBActionInfoModel.self)
- .filter("NOT (primaryKey IN %@)", primaryKeysToKeep)
- realm.delete(objectsToDelete)
- }
- UserDefaults.standard.set("1", forKey: "aiListDBDeleteIncorrectData")
- UserDefaults.standard.synchronize()
- }
-
- return history
- }
-
- }
|