TSUserDefaultData.swift 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. //
  2. // TSUserDefaultData.swift
  3. // AIRingtone
  4. //
  5. // Created by 100Years on 2025/3/5.
  6. //
  7. import ObjectMapper
  8. //海报历史记录
  9. class TSPosterHistory{
  10. @UserDefault(key: "kPosterTextPicHistoryListString", defaultValue: "")
  11. static private var historyString: String
  12. static var listModelArray: [TSActionInfoModel] = {
  13. if UserDefaults.standard.string(forKey: "insertPosterExampleData") == nil {
  14. insertExampleData()
  15. UserDefaults.standard.set("1", forKey: "insertPosterExampleData")
  16. UserDefaults.standard.synchronize()
  17. }
  18. if let listModelArray = Mapper<TSActionInfoModel>().mapArray(JSONString: historyString){
  19. return listModelArray
  20. }
  21. return []
  22. }()
  23. static func saveModel(model:TSActionInfoModel){
  24. listModelArray.insert(model, at: 0)
  25. saveHistoryString()
  26. }
  27. static func removeModel(model:TSActionInfoModel){
  28. listModelArray.removeAll { $0 === model }
  29. saveHistoryString()
  30. }
  31. static func removeIndex(index:Int){
  32. listModelArray.remove(at: index)
  33. saveHistoryString()
  34. }
  35. static func saveHistoryString(){
  36. if let jsonString = listModelArray.toJSONString() {
  37. historyString = jsonString
  38. }
  39. }
  40. private static func insertExampleData(){
  41. let array = [
  42. createExampleModel(imageName: "poster_example_0"),
  43. createExampleModel(imageName: "poster_example_1"),
  44. createExampleModel(imageName: "poster_example_2")
  45. ]
  46. if let jsonString = array.toJSONString() {
  47. historyString = jsonString
  48. }
  49. }
  50. private static func createExampleModel(imageName:String)->TSActionInfoModel{
  51. let model = TSActionInfoModel()
  52. model.modelType = .example
  53. model.request.prompt = "Example"
  54. model.request.promptSort = "Example"
  55. model.request.width = kTextPicW
  56. model.request.height = kTextPicH
  57. model.response.resultUrl = imageName
  58. return model
  59. }
  60. }
  61. //头像历史记录
  62. class TSPhotoHistory{
  63. @UserDefault(key: "kPhotoTextPicHistoryListString", defaultValue: "")
  64. static private var historyString: String
  65. static var listModelArray: [TSActionInfoModel] = {
  66. if UserDefaults.standard.string(forKey: "insertPhotoExampleData") == nil {
  67. insertExampleData()
  68. UserDefaults.standard.set("1", forKey: "insertPhotoExampleData")
  69. UserDefaults.standard.synchronize()
  70. }
  71. if let listModelArray = Mapper<TSActionInfoModel>().mapArray(JSONString: historyString){
  72. return listModelArray
  73. }
  74. return []
  75. }()
  76. static func saveModel(model:TSActionInfoModel){
  77. listModelArray.insert(model, at: 0)
  78. saveHistoryString()
  79. }
  80. static func removeModel(model:TSActionInfoModel){
  81. listModelArray.removeAll { $0 === model }
  82. saveHistoryString()
  83. }
  84. static func removeIndex(index:Int){
  85. listModelArray.remove(at: index)
  86. saveHistoryString()
  87. }
  88. static func saveHistoryString(){
  89. if let jsonString = listModelArray.toJSONString() {
  90. historyString = jsonString
  91. }
  92. }
  93. private static func insertExampleData(){
  94. let array = [
  95. createExampleModel(imageName: "photo_example_0"),
  96. createExampleModel(imageName: "photo_example_1"),
  97. createExampleModel(imageName: "photo_example_2")
  98. ]
  99. if let jsonString = array.toJSONString() {
  100. historyString = jsonString
  101. }
  102. }
  103. private static func createExampleModel(imageName:String)->TSActionInfoModel{
  104. let model = TSActionInfoModel()
  105. model.modelType = .example
  106. model.request.prompt = "Example"
  107. model.request.promptSort = "Example"
  108. model.request.width = kTextPicW
  109. model.request.height = kTextPicW
  110. model.response.resultUrl = imageName
  111. return model
  112. }
  113. }
  114. //AI铃声历史记录
  115. class TSAIRintoneHistory{
  116. @UserDefault(key: "kRintoneTextMusicHistoryListString", defaultValue: "")
  117. static private var historyString: String
  118. static var listModelArray: [TSActionInfoModel] = {
  119. if UserDefaults.standard.string(forKey: "insertRintoneExampleData") == nil {
  120. insertExampleData()
  121. UserDefaults.standard.set("1", forKey: "insertRintoneExampleData")
  122. UserDefaults.standard.synchronize()
  123. }
  124. if let listModelArray = Mapper<TSActionInfoModel>().mapArray(JSONString: historyString){
  125. return listModelArray
  126. }
  127. return []
  128. }()
  129. static func saveModel(model:TSActionInfoModel){
  130. listModelArray.insert(model, at: 0)
  131. saveHistoryString()
  132. }
  133. static func removeModel(model:TSActionInfoModel){
  134. listModelArray.removeAll { $0 === model }
  135. saveHistoryString()
  136. }
  137. static func removeIndex(index:Int){
  138. listModelArray.remove(at: index)
  139. saveHistoryString()
  140. }
  141. static func saveHistoryString(){
  142. if let jsonString = listModelArray.toJSONString() {
  143. historyString = jsonString
  144. }
  145. }
  146. private static func insertExampleData(){
  147. let array = [
  148. createExampleModel(),
  149. ]
  150. if let jsonString = array.toJSONString() {
  151. historyString = jsonString
  152. }
  153. }
  154. private static func createExampleModel()->TSActionInfoModel{
  155. let model = TSActionInfoModel()
  156. model.modelType = .example
  157. model.request.duration = 30
  158. model.request.prompt = "Example"
  159. model.request.promptSort = "Example"
  160. model.response.coverUrl = "http://d3a93z8fj970a4.cloudfront.net/53337c41-9610-4b92-a8fc-0871a0895880"
  161. model.response.title = "Glass"
  162. model.response.musicUrl = "http://d3a93z8fj970a4.cloudfront.net/328324a2-2576-4f55-8c77-2fa16b88e043"
  163. return model
  164. }
  165. }