TSGeneralRintoneVM.swift 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. //
  2. // TSGeneralRintoneVM.swift
  3. // AIRingtone
  4. //
  5. // Created by 100Years on 2025/3/6.
  6. //
  7. import Combine
  8. import Alamofire
  9. let actionInfoDict:[String:Any] = [
  10. "actionType":"music_create",
  11. "comments": "Success",
  12. "costTime":15,
  13. "createdTimestamp":1741338454,
  14. "id":1536,
  15. "percent":1,
  16. "request":"{\"prompt\": \"Create a Techno ringtone with a repetitive bassline, crisp hi-hats, and subtle synth textures. Use a BPM of 125-130 for a sleek, modern sound., Create a uplifting and modern music track blending Pop, Electronic, and Ambient elements. Use a BPM of 100-120, a catchy melody with synth or piano, warm harmonies, and a mix of electronic and organic sounds. Ensure a clear structure (Intro, Verse, Chorus, Outro) and a light, positive vibe suitable for background or casual listening\", \"duration\": 5}",
  17. "response":"{\"coverUrl\": \"https://be-aigc.s3-accelerate.amazonaws.com/f0fb7739-a5cc-4805-9b68-b4a5890eb285.png\", \"title\": \"Neon Pulse\\\" \\n\\\"Horizon Glow\", \"musicUrl\": \"https://be-aigc.s3-accelerate.amazonaws.com/c47d40dd-d07c-4edc-a6d9-8382438149d1.wav\"}",
  18. "status":"success"
  19. ]
  20. class TSGeneralRintoneVM {
  21. var creatRequest:Request?
  22. var queryRequest:Request?
  23. var stopNetwork = false
  24. @Published var stateDatauPblished:(TSProgressState,TSActionInfoModel?) = (TSProgressState.none,nil)
  25. var aiText:String = ""
  26. var generatingProgress = 0
  27. // //模拟数据
  28. // func creatRintone(text:String) {
  29. //
  30. // stateDatauPblished = (.start,nil)
  31. // stateDatauPblished = (.progressString(generating(progress: 0.0)),nil)
  32. //
  33. // kDelayOnMainThread(0.2) {
  34. // self.stateDatauPblished = (.progressString(self.generating(progress: 0.5)),nil)
  35. // }
  36. //
  37. // kDelayOnMainThread(1.0) {
  38. // if Bool.random() {
  39. // let infoModel = TSActionInfoModel(JSON: actionInfoDict)
  40. // self.stateDatauPblished = (.success(nil),infoModel)
  41. // }else{
  42. // self.stateDatauPblished = (.failed("error?.localizedDescription"),nil)
  43. // }
  44. // }
  45. // }
  46. func creatRintone(text:String) {
  47. generatingProgress = 0
  48. aiText = text
  49. let postDict:[String : Any] = [
  50. "prompt":text,
  51. "duration":20
  52. ]
  53. stateDatauPblished = (.start,nil)
  54. stateDatauPblished = (.progressString(generating(progress: 0.0)),nil)
  55. creatRequest = TSNetworkShared.post(urlType: .musicCreate,parameters: postDict) { [weak self] data,error in
  56. guard let self = self else { return }
  57. if let dataDict = data as? [String:Any] ,
  58. dataDict.safeInt(forKey: "code") == 200,
  59. let actionId = dataDict["actionId"] as? Int{
  60. if stopNetwork == false {
  61. self.getActionInfo(action_id:actionId)
  62. }
  63. }else{
  64. self.stateDatauPblished = (.failed(error?.localizedDescription ?? ""),nil)
  65. }
  66. }
  67. }
  68. func getActionInfo(action_id:Int){
  69. queryRequest = TSNetworkShared.get(urlType: .actionInfo,parameters: ["action_id":action_id]) { [weak self] data,error in
  70. guard let self = self else { return }
  71. if let result = kNetWorkResultSuccess(data: data) {
  72. if let genmojiModel = TSActionInfoModel(JSON: result) {
  73. switch genmojiModel.actionStatus {
  74. case .success:
  75. TSToastShared.hideLoading()
  76. self.stateDatauPblished = (.success(nil),genmojiModel)
  77. generatingProgress = 0
  78. case .failed:
  79. self.stateDatauPblished = (.failed(kNetWorkMessage(data: data) ?? ""),nil)
  80. generatingProgress = 0
  81. default:
  82. stateDatauPblished = (.progressString(generating(progress: genmojiModel.percent)),nil)
  83. if stopNetwork == false {
  84. kDelayOnMainThread(1.0) {
  85. self.getActionInfo(action_id: action_id)
  86. }
  87. }
  88. }
  89. }
  90. }else{
  91. self.stateDatauPblished = (.failed(error?.localizedDescription ?? ""),nil)
  92. }
  93. }
  94. }
  95. func cancelAllRequest(){
  96. creatRequest?.cancel()
  97. queryRequest?.cancel()
  98. stopNetwork = true
  99. }
  100. func generating(progress:Float) -> String {
  101. //Generating 0%-100%
  102. var progressInt = Int(progress*100)
  103. if generatingProgress >= progressInt{
  104. return "Generating \(generatingProgress)%"
  105. }
  106. if progressInt > 99 {
  107. progressInt = 99
  108. }
  109. generatingProgress = progressInt
  110. return "Generating \(progressInt)%"
  111. }
  112. }