TSUserDefaultData.swift 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. //
  2. // TSUserDefaultData.swift
  3. // AIRingtone
  4. //
  5. // Created by 100Years on 2025/3/5.
  6. //
  7. import ObjectMapper
  8. func getUserInfoJsonString()->String {
  9. let uuid: String
  10. let uuidUdKey = "my_UUID"
  11. if let saved = UserDefaults.standard.string(forKey: uuidUdKey),
  12. !saved.isEmpty {
  13. uuid = saved
  14. } else {
  15. let newUuid = UUID().uuidString
  16. UserDefaults.standard.set(newUuid, forKey: uuidUdKey)
  17. UserDefaults.standard.synchronize()
  18. uuid = newUuid
  19. }
  20. let dic:[String:Any] = [
  21. "device":UIDevice.current.modelName,
  22. "deviceId":uuid,
  23. "iosVersion":UIDevice.current.systemVersion,
  24. "appVersion":appShortVersion(),
  25. "subscriptionStatus":kPurchaseDefault.isVip ? "active" : "fallow",
  26. ]
  27. if let jSONString = dic.toJSONString() {
  28. return jSONString
  29. }
  30. return ""
  31. }
  32. func kHandleTSHistory(){
  33. }
  34. // MARK: - 基础历史记录类
  35. class TSBaseHistoryManager<ModelType: TSBaseModel> {
  36. // 子类必须重写的属性
  37. var historyKey: String { fatalError("必须重写 historyKey") }
  38. var exampleDataKey: String { fatalError("必须重写 exampleDataKey") }
  39. var exampleModels: [ModelType] { fatalError("必须重写 exampleModels") }
  40. func findModelID(modelID: Int)->Int?{
  41. fatalError("必须重写 findModelID")
  42. }
  43. func saveModelAfterProcess(){
  44. }
  45. // 存储属性
  46. private var _historyString: String {
  47. get { UserDefaults.standard.string(forKey: historyKey) ?? "" }
  48. set { UserDefaults.standard.set(newValue, forKey: historyKey) }
  49. }
  50. private var _listModels: [ModelType]?
  51. var listModels: [ModelType] {
  52. get {
  53. if _listModels == nil { loadModels() }
  54. return _listModels ?? []
  55. }
  56. set {
  57. _listModels = newValue
  58. }
  59. }
  60. // MARK: - 公共方法
  61. func saveModel(model: ModelType, at index: Int = 0) {
  62. listModels.insert(model, at: index)
  63. saveHistory()
  64. saveModelAfterProcess()
  65. }
  66. func removeModel(model:ModelType) {
  67. self.listModels.removeAll { $0 === model }
  68. saveHistory()
  69. }
  70. func removeModel(index: Int) {
  71. guard index >= 0 && index < listModels.count else { return }
  72. listModels.remove(at: index)
  73. saveHistory()
  74. }
  75. func replaceModel(oldID: Int, newModel: ModelType){
  76. if let index = findModelID(modelID: oldID) {
  77. listModels[index] = newModel
  78. dePrint("\(Self.self).listModels Model replaced at index \(index)")
  79. } else {
  80. listModels.insert(newModel, at: 0)
  81. dePrint("\(Self.self).listModels Model not found")
  82. }
  83. dePrint("\(Self.self).listModels.count=\(listModels.count)")
  84. saveHistory()
  85. }
  86. func replaceAndSaveModel(saveModel:ModelType,compareBlock:(ModelType,ModelType)->Bool){
  87. if let index = listModels.firstIndex(where: { model in
  88. compareBlock(saveModel,model)
  89. }){
  90. dePrint("\(Self.self).listModels Model replaced at index \(index)")
  91. listModels[index] = saveModel
  92. saveHistory()
  93. }else{
  94. self.saveModel(model: saveModel)
  95. }
  96. }
  97. func dePrintAllModel() {
  98. dePrint("=======================结果查询开始======================")
  99. dePrint("\(Self.self).listModels.count=\(listModels.count)")
  100. for model in listModels {
  101. dePrint(model.toJSON())
  102. }
  103. dePrint("=======================结果查询结束======================")
  104. }
  105. // MARK: - 私有方法
  106. private func saveHistory() {
  107. if let jsonString = listModels.toJSONString() {
  108. _historyString = jsonString
  109. }
  110. }
  111. private func loadModels() {
  112. if exampleModels.count > 0 {
  113. // 第一次运行时插入示例数据
  114. if UserDefaults.standard.string(forKey: exampleDataKey) == nil {
  115. insertExampleData()
  116. UserDefaults.standard.set("1", forKey: exampleDataKey)
  117. }
  118. }
  119. // 从历史记录加载模型
  120. if let models = Mapper<ModelType>().mapArray(JSONString: _historyString) {
  121. _listModels = models
  122. } else {
  123. _listModels = []
  124. }
  125. }
  126. private func insertExampleData() {
  127. if let jsonString = exampleModels.toJSONString() {
  128. _historyString = jsonString
  129. }
  130. }
  131. }
  132. // MARK: - 海报历史记录
  133. final class TSPosterHistory: TSBaseHistoryManager<TSGenmojiModel> {
  134. static let shared = TSPosterHistory()
  135. override var historyKey: String { "kPosterTextPicHistoryListString" }
  136. override var exampleDataKey: String { "insertPosterExampleData" }
  137. override func findModelID(modelID: Int) -> Int? {
  138. return listModels.firstIndex(where: {$0.id == modelID})
  139. }
  140. override var exampleModels: [TSGenmojiModel] {
  141. [
  142. createExampleModel(imageName: "poster_example_0"),
  143. createExampleModel(imageName: "poster_example_1"),
  144. createExampleModel(imageName: "poster_example_2")
  145. ]
  146. }
  147. private func createExampleModel(imageName: String) -> TSGenmojiModel {
  148. let model = TSGenmojiModel()
  149. model.modelType = .example
  150. model.request.prompt = "Example"
  151. model.request.promptSort = "Example"
  152. model.response.resultUrl = imageName
  153. return model
  154. }
  155. }
  156. //海报历史记录
  157. class TSTextToPicHistory{
  158. @UserDefault(key: "textPicHistoryListString", defaultValue: "")
  159. static private var historyString: String
  160. static var listModelArray: [TSGenmojiModel] = {
  161. // if UserDefaults.standard.string(forKey: "insertPosterExampleData") == nil {
  162. // insertExampleData()
  163. // UserDefaults.standard.set("1", forKey: "insertPosterExampleData")
  164. // UserDefaults.standard.synchronize()
  165. // }
  166. if let listModelArray = Mapper<TSGenmojiModel>().mapArray(JSONString: historyString){
  167. return listModelArray
  168. }
  169. return []
  170. }()
  171. static func saveModel(model:TSGenmojiModel){
  172. listModelArray.insert(model, at: 0)
  173. saveHistoryString()
  174. }
  175. static func removeModel(model:TSGenmojiModel){
  176. listModelArray.removeAll { $0 === model }
  177. saveHistoryString()
  178. }
  179. static func removeIndex(index:Int){
  180. listModelArray.remove(at: index)
  181. saveHistoryString()
  182. }
  183. static func removeAll(){
  184. listModelArray.removeAll()
  185. saveHistoryString()
  186. }
  187. static func saveHistoryString(){
  188. if let jsonString = listModelArray.toJSONString() {
  189. historyString = jsonString
  190. }
  191. }
  192. // private static func insertExampleData(){
  193. // let array = [
  194. // createExampleModel(imageName: "poster_example_0"),
  195. // createExampleModel(imageName: "poster_example_1"),
  196. // createExampleModel(imageName: "poster_example_2")
  197. // ]
  198. // if let jsonString = array.toJSONString() {
  199. // historyString = jsonString
  200. // }
  201. // }
  202. // private static func createExampleModel(imageName:String)->TSActionInfoModel{
  203. // let model = TSActionInfoModel()
  204. // model.modelType = .example
  205. // model.request.prompt = "Example"
  206. // model.request.promptSort = "Example"
  207. // model.request.width = kTextPicW
  208. // model.request.height = kTextPicH
  209. // model.response.resultUrl = imageName
  210. // return model
  211. // }
  212. }