123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328 |
- //
- // TSUserDefaultData.swift
- // AIRingtone
- //
- // Created by 100Years on 2025/3/5.
- //
- import ObjectMapper
- func getUserInfoJsonString()->String {
- let uuid: String
- let uuidUdKey = "my_UUID"
- if let saved = UserDefaults.standard.string(forKey: uuidUdKey),
- !saved.isEmpty {
- uuid = saved
- } else {
- let newUuid = UUID().uuidString
- UserDefaults.standard.set(newUuid, forKey: uuidUdKey)
- UserDefaults.standard.synchronize()
- uuid = newUuid
- }
-
- let dic:[String:Any] = [
- "device":UIDevice.current.modelName,
- "deviceId":uuid,
- "iosVersion":UIDevice.current.systemVersion,
- "appVersion":appShortVersion(),
- "subscriptionStatus":kPurchaseDefault.isVip ? "active" : "fallow",
- ]
-
- if let jSONString = dic.toJSONString() {
- return jSONString
- }
-
- return ""
- }
- func kHandleTSHistory(){
-
- }
- // 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: - 私有方法
- private 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)
- }
- }
- // 从历史记录加载模型
- if let models = Mapper<ModelType>().mapArray(JSONString: _historyString) {
- _listModels = models
- } else {
- _listModels = []
- }
- }
-
- private func insertExampleData() {
- if let jsonString = exampleModels.toJSONString() {
- _historyString = jsonString
- }
- }
- }
- // MARK: - 新的储存方法
- // MARK: - 变老
- final class TSChangeOldAgeHistory: TSBaseHistoryManager<TSGenmojiModel> {
- static let shared = TSChangeOldAgeHistory()
- override var historyKey: String { "kTSChangeOldAgeHistoryListString" }
- // override var exampleDataKey: String { "insertPosterExampleData" }
-
- override func findModelID(modelID: Int) -> Int? {
- return listModels.firstIndex(where: {$0.id == modelID})
- }
-
- override var exampleModels: [TSGenmojiModel] {
- []
- }
- //
- // private func createExampleModel(imageName: String) -> TSGenmojiModel {
- // let model = TSGenmojiModel()
- // model.modelType = .example
- // model.request.prompt = "Example"
- // model.request.promptSort = "Example"
- // model.response.resultUrl = imageName
- // return model
- // }
- }
- // MARK: - 变年轻
- final class TSChangeBabyAgeHistory: TSBaseHistoryManager<TSGenmojiModel> {
- static let shared = TSChangeBabyAgeHistory()
- override var historyKey: String { "kTSChangeBabyAgeHistoryListString" }
-
- override var exampleModels: [TSGenmojiModel] {
- []
- }
- override func findModelID(modelID: Int) -> Int? {
- return listModels.firstIndex(where: {$0.id == modelID})
- }
- }
- // MARK: - 旧的储存方法
- //海报历史记录
- class TSTextToPicHistory{
- @UserDefault(key: "textPicHistoryListString", defaultValue: "")
- static private var historyString: String
- static var listModelArray: [TSGenmojiModel] = {
-
- // if UserDefaults.standard.string(forKey: "insertPosterExampleData") == nil {
- // insertExampleData()
- // UserDefaults.standard.set("1", forKey: "insertPosterExampleData")
- // UserDefaults.standard.synchronize()
- // }
-
- if let listModelArray = Mapper<TSGenmojiModel>().mapArray(JSONString: historyString){
- return listModelArray
- }
- return []
- }()
-
- static func saveModel(model:TSGenmojiModel){
- listModelArray.insert(model, at: 0)
- saveHistoryString()
- }
-
- static func removeModel(model:TSGenmojiModel){
- listModelArray.removeAll { $0 === model }
- saveHistoryString()
- }
-
- static func removeIndex(index:Int){
- listModelArray.remove(at: index)
- saveHistoryString()
- }
-
- static func removeAll(){
- listModelArray.removeAll()
- saveHistoryString()
- }
-
- static func saveHistoryString(){
- if let jsonString = listModelArray.toJSONString() {
- historyString = jsonString
- }
- }
-
- // private static func insertExampleData(){
- // let array = [
- // createExampleModel(imageName: "poster_example_0"),
- // createExampleModel(imageName: "poster_example_1"),
- // createExampleModel(imageName: "poster_example_2")
- // ]
- // if let jsonString = array.toJSONString() {
- // historyString = jsonString
- // }
- // }
-
- // private static func createExampleModel(imageName:String)->TSActionInfoModel{
- // let model = TSActionInfoModel()
- // model.modelType = .example
- // model.request.prompt = "Example"
- // model.request.promptSort = "Example"
- // model.request.width = kTextPicW
- // model.request.height = kTextPicH
- // model.response.resultUrl = imageName
- // return model
- // }
- }
|