TSUserDefaultData.swift 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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 removeALLModel() {
  76. listModels.removeAll()
  77. saveHistory()
  78. }
  79. func replaceModel(oldID: Int, newModel: ModelType){
  80. if let index = findModelID(modelID: oldID) {
  81. listModels[index] = newModel
  82. dePrint("\(Self.self).listModels Model replaced at index \(index)")
  83. } else {
  84. listModels.insert(newModel, at: 0)
  85. dePrint("\(Self.self).listModels Model not found")
  86. }
  87. dePrint("\(Self.self).listModels.count=\(listModels.count)")
  88. saveHistory()
  89. }
  90. func replaceAndSaveModel(saveModel:ModelType,compareBlock:(ModelType,ModelType)->Bool){
  91. if let index = listModels.firstIndex(where: { model in
  92. compareBlock(saveModel,model)
  93. }){
  94. dePrint("\(Self.self).listModels Model replaced at index \(index)")
  95. listModels[index] = saveModel
  96. saveHistory()
  97. }else{
  98. self.saveModel(model: saveModel)
  99. }
  100. }
  101. func dePrintAllModel() {
  102. dePrint("=======================结果查询开始======================")
  103. dePrint("\(Self.self).listModels.count=\(listModels.count)")
  104. for model in listModels {
  105. dePrint(model.toJSON())
  106. }
  107. dePrint("=======================结果查询结束======================")
  108. }
  109. // MARK: - 私有方法
  110. private func saveHistory() {
  111. if let jsonString = listModels.toJSONString() {
  112. _historyString = jsonString
  113. }
  114. }
  115. private func loadModels() {
  116. if exampleModels.count > 0 {
  117. // 第一次运行时插入示例数据
  118. if UserDefaults.standard.string(forKey: exampleDataKey) == nil {
  119. insertExampleData()
  120. UserDefaults.standard.set("1", forKey: exampleDataKey)
  121. }
  122. }
  123. // 从历史记录加载模型
  124. if let models = Mapper<ModelType>().mapArray(JSONString: _historyString) {
  125. _listModels = models
  126. } else {
  127. _listModels = []
  128. }
  129. }
  130. private func insertExampleData() {
  131. if let jsonString = exampleModels.toJSONString() {
  132. _historyString = jsonString
  133. }
  134. }
  135. }
  136. // MARK: - 新的储存方法
  137. // MARK: - 变老
  138. final class TSChangeOldAgeHistory: TSBaseHistoryManager<TSGenmojiModel> {
  139. static let shared = TSChangeOldAgeHistory()
  140. override var historyKey: String { "kTSChangeOldAgeHistoryListString" }
  141. // override var exampleDataKey: String { "insertPosterExampleData" }
  142. override func findModelID(modelID: Int) -> Int? {
  143. return listModels.firstIndex(where: {$0.id == modelID})
  144. }
  145. override var exampleModels: [TSGenmojiModel] {
  146. []
  147. }
  148. //
  149. // private func createExampleModel(imageName: String) -> TSGenmojiModel {
  150. // let model = TSGenmojiModel()
  151. // model.modelType = .example
  152. // model.request.prompt = "Example"
  153. // model.request.promptSort = "Example"
  154. // model.response.resultUrl = imageName
  155. // return model
  156. // }
  157. }
  158. // MARK: - 变年轻
  159. final class TSChangeBabyAgeHistory: TSBaseHistoryManager<TSGenmojiModel> {
  160. static let shared = TSChangeBabyAgeHistory()
  161. override var historyKey: String { "kTSChangeBabyAgeHistoryListString" }
  162. override var exampleModels: [TSGenmojiModel] {
  163. []
  164. }
  165. override func findModelID(modelID: Int) -> Int? {
  166. return listModels.firstIndex(where: {$0.id == modelID})
  167. }
  168. }
  169. // MARK: - 旧的储存方法
  170. //海报历史记录
  171. class TSTextToPicHistory{
  172. @UserDefault(key: "textPicHistoryListString", defaultValue: "")
  173. static private var historyString: String
  174. static var listModelArray: [TSGenmojiModel] = {
  175. // if UserDefaults.standard.string(forKey: "insertPosterExampleData") == nil {
  176. // insertExampleData()
  177. // UserDefaults.standard.set("1", forKey: "insertPosterExampleData")
  178. // UserDefaults.standard.synchronize()
  179. // }
  180. if let listModelArray = Mapper<TSGenmojiModel>().mapArray(JSONString: historyString){
  181. return listModelArray
  182. }
  183. return []
  184. }()
  185. static func saveModel(model:TSGenmojiModel){
  186. listModelArray.insert(model, at: 0)
  187. saveHistoryString()
  188. }
  189. static func removeModel(model:TSGenmojiModel){
  190. listModelArray.removeAll { $0 === model }
  191. saveHistoryString()
  192. }
  193. static func removeIndex(index:Int){
  194. listModelArray.remove(at: index)
  195. saveHistoryString()
  196. }
  197. static func removeAll(){
  198. listModelArray.removeAll()
  199. saveHistoryString()
  200. }
  201. static func saveHistoryString(){
  202. if let jsonString = listModelArray.toJSONString() {
  203. historyString = jsonString
  204. }
  205. }
  206. // private static func insertExampleData(){
  207. // let array = [
  208. // createExampleModel(imageName: "poster_example_0"),
  209. // createExampleModel(imageName: "poster_example_1"),
  210. // createExampleModel(imageName: "poster_example_2")
  211. // ]
  212. // if let jsonString = array.toJSONString() {
  213. // historyString = jsonString
  214. // }
  215. // }
  216. // private static func createExampleModel(imageName:String)->TSActionInfoModel{
  217. // let model = TSActionInfoModel()
  218. // model.modelType = .example
  219. // model.request.prompt = "Example"
  220. // model.request.promptSort = "Example"
  221. // model.request.width = kTextPicW
  222. // model.request.height = kTextPicH
  223. // model.response.resultUrl = imageName
  224. // return model
  225. // }
  226. }