TSGenneralPicVM.swift 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. //
  2. // TSGenneralPicVM.swift
  3. // AIRingtone
  4. //
  5. // Created by 100Years on 2025/3/5.
  6. //
  7. import Combine
  8. import Alamofire
  9. let kTextPicW:Int = 800
  10. let kTextPicH:Int = 1440
  11. let actionInfoDictPoster:[String:Any] = [
  12. "actionType":"image_create",
  13. "comments": "Success",
  14. "costTime":9,
  15. "createdTimestamp":1742183242,
  16. "id":2449,
  17. "percent":1,
  18. "request":"{\"prompt\": \"Traditional Chinese ink painting style phoenix soaring through misty mountain peaks, dynamic black brushstrokes with crimson accents on rice paper texture, Googie architecture space station, 1950s atomic age aesthetic, curved aluminum panels, starburst patterns, glass dome observatory --edge_threshold 0.5 --atomic_age_style 0.8 --chrome_reflection 1.2\", \"width\": 800, \"height\": 1440, \"countryCode\": \"FR\"}",
  19. "response": "{\"resultUrl\": \"https://be-aigc.s3-accelerate.amazonaws.com/4c946f78-b1e7-4ffe-ba18-fff26b10178c.png\"}",
  20. "status":"success"
  21. ]
  22. let actionInfoDictPhoto:[String:Any] = [
  23. "actionType":"image_create",
  24. "comments": "Success",
  25. "costTime":7,
  26. "createdTimestamp":1742183588,
  27. "id":2450,
  28. "percent":1,
  29. "request":"{\"prompt\": \"Steampunk floating library with brass gears and clockwork mechanisms, leather-bound books flying through amber-lit fog, Victorian-era architecture blended with retro-futurism, retro anime cel-shading, 1980s Toei Animation style, bold black outlines, limited color palette (--color 1980s_anime), visible film grain --edge_threshold 0.4 --cel_shading 0.8, Retain the original stone size of the photo\", \"width\": 800, \"height\": 800, \"countryCode\": \"US\"}",
  30. "response":"{\"resultUrl\": \"https://be-aigc.s3-accelerate.amazonaws.com/8b7fcac9-c691-4c3a-b497-401204fad3e9.png\"}",
  31. "status":"success"
  32. ]
  33. class TSGenneralPicVM {
  34. var creatRequest:Request?
  35. var queryRequest:Request?
  36. var stopNetwork = false
  37. @Published var stateDatauPblished:(TSProgressState,TSActionInfoModel?) = (TSProgressState.none,nil)
  38. var aiText:String = ""
  39. var gennerateType:TSGennerateType = .poster
  40. var generatingProgress = 0
  41. //模拟数据
  42. func creatImageEmoji(text:String) {
  43. stateDatauPblished = (.start,nil)
  44. stateDatauPblished = (.progressString(generating(progress: 0.0)),nil)
  45. kDelayOnMainThread(0.2) {
  46. self.stateDatauPblished = (.progressString(self.generating(progress: 0.5)),nil)
  47. }
  48. kDelayOnMainThread(1.0) {
  49. if Bool.random() {
  50. let infoModel = TSActionInfoModel(JSON: self.gennerateType == .poster ? actionInfoDictPoster : actionInfoDictPhoto)
  51. self.stateDatauPblished = (.success(nil),infoModel)
  52. }else{
  53. self.stateDatauPblished = (.failed("error?.localizedDescription"),nil)
  54. }
  55. }
  56. }
  57. // //width 和 height 必须是 32 的倍数
  58. // func creatImageEmoji(text:String) {
  59. // generatingProgress = 0
  60. // aiText = text
  61. // let postDict:[String : Any] = [
  62. // "prompt":text,
  63. // "width":textPicW,
  64. // "height":textPicH
  65. // ]
  66. // stateDatauPblished = (.start,nil)
  67. // stateDatauPblished = (.progressString(generating(progress: 0.0)),nil)
  68. // creatRequest = TSNetworkShared.post(urlType: .textPicCreate,parameters: postDict) { [weak self] data,error in
  69. // guard let self = self else { return }
  70. // if let dataDict = data as? [String:Any] ,
  71. // dataDict.safeInt(forKey: "code") == 200,
  72. // let actionId = dataDict["actionId"] as? Int{
  73. // if stopNetwork == false {
  74. // self.getActionInfo(action_id:actionId)
  75. // }
  76. // }else{
  77. // self.stateDatauPblished = (.failed(error?.localizedDescription ?? ""),nil)
  78. // }
  79. // }
  80. // }
  81. func getActionInfo(action_id:Int){
  82. queryRequest = TSNetworkShared.get(urlType: .actionInfo,parameters: ["action_id":action_id]) { [weak self] data,error in
  83. guard let self = self else { return }
  84. if let result = kNetWorkResultSuccess(data: data) {
  85. if let genmojiModel = TSActionInfoModel(JSON: result) {
  86. switch genmojiModel.actionStatus {
  87. case .success:
  88. TSToastShared.hideLoading()
  89. self.stateDatauPblished = (.success(nil),genmojiModel)
  90. generatingProgress = 0
  91. case .failed:
  92. self.stateDatauPblished = (.failed(kNetWorkMessage(data: data) ?? ""),nil)
  93. generatingProgress = 0
  94. default:
  95. stateDatauPblished = (.progressString(generating(progress: genmojiModel.percent)),nil)
  96. if stopNetwork == false {
  97. kDelayOnMainThread(1.0) {
  98. self.getActionInfo(action_id: action_id)
  99. }
  100. }
  101. }
  102. }
  103. }else{
  104. self.stateDatauPblished = (.failed(error?.localizedDescription ?? ""),nil)
  105. }
  106. }
  107. }
  108. func cancelAllRequest(){
  109. creatRequest?.cancel()
  110. queryRequest?.cancel()
  111. stopNetwork = true
  112. }
  113. func generating(progress:Float) -> String {
  114. //Generating 0%-100%
  115. var progressInt = Int(progress*100)
  116. if generatingProgress >= progressInt{
  117. return "Generating \(generatingProgress)%"
  118. }
  119. if progressInt > 99 {
  120. progressInt = 99
  121. }
  122. generatingProgress = progressInt
  123. return "Generating \(progressInt)%"
  124. }
  125. }
  126. extension TSGenneralPicVM {
  127. var textPicW:Int{
  128. return kTextPicW
  129. }
  130. var textPicH:Int{
  131. if gennerateType == .photo {
  132. return kTextPicW
  133. }
  134. return kTextPicH
  135. }
  136. }