TSGenerateBaseOperation.swift 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. //
  2. // TSGenerateBaseOperation.swift
  3. // AIRingtone
  4. //
  5. // Created by 100Years on 2025/3/24.
  6. //
  7. import Combine
  8. import Alamofire
  9. class TSGenerateBaseOperationQueue: TSBaseOperationQueue {
  10. // 存储每个操作的 AnyCancellable
  11. var stateables: [String: AnyCancellable] = [:]
  12. var generateOperationStateChanged:((String)->Void)?
  13. override func cancelOperations(uuid: String) {
  14. super.cancelOperations(uuid: uuid)
  15. stateables.removeValue(forKey: uuid)
  16. }
  17. func handleStateDatauPblished(uuid:String,generateOperation: TSGenerateBaseOperation,notificationName:Notification.Name) {
  18. stateables[uuid] = generateOperation.$stateDatauPblished.sink { [weak self] state in
  19. guard let self = self else { return }
  20. DispatchQueue.main.async {
  21. self.generateOperationStateChanged?(uuid)
  22. let uuidData = self.getUUIDData(uuid: uuid)
  23. NotificationCenter.default.post(
  24. name: notificationName,
  25. object: nil,
  26. userInfo: [
  27. "uuid": uuid,
  28. "count":self.queue.maxConcurrentOperationCount,
  29. "state":uuidData.0,
  30. "actionInfo":uuidData.1,
  31. ])
  32. }
  33. }
  34. }
  35. func getUUIDData(uuid:String)->(TSProgressState,TSActionInfoModel?){
  36. return (.none,TSActionInfoModel())
  37. }
  38. }
  39. class TSGenerateBaseOperation: TSBaseOperation , @unchecked Sendable{
  40. var actionInfoDict:[String:Any]{
  41. return [
  42. "actionType":"music_create",
  43. "comments": "Success",
  44. "costTime":15,
  45. "createdTimestamp":1741338454,
  46. "id":1536,
  47. "percent":1,
  48. "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}",
  49. "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\"}",
  50. "status":"success"
  51. ]
  52. }
  53. @Published var stateDatauPblished:(TSProgressState,TSActionInfoModel?) = (TSProgressState.none,nil){
  54. didSet{
  55. dePrint("TSBaseOperation stateDatauPblished didSet = \(stateDatauPblished)")
  56. if case .start = stateDatauPblished.0 {
  57. start()
  58. }else if stateDatauPblished.0.isResult {
  59. finished()
  60. }
  61. }
  62. }
  63. var creatRequest:Request?
  64. var queryRequest:Request?
  65. var stopNetwork = false
  66. var generatingProgress = 0
  67. var action_id:Int = 0
  68. var currentActionInfoModelChanged:((TSActionInfoModel)->Void)?
  69. @Published var currentActionInfoModel: TSActionInfoModel = TSActionInfoModel()
  70. func getActionInfo(action_id:Int){
  71. self.action_id = action_id
  72. queryRequest = TSNetworkShared.get(urlType: .actionInfo,parameters: ["action_id":action_id]) { [weak self] data,error in
  73. guard let self = self else { return }
  74. if let result = kNetWorkResultSuccess(data: data) {
  75. if let genmojiModel = TSActionInfoModel(JSON: result) {
  76. switch genmojiModel.actionStatus {
  77. case .success:
  78. TSToastShared.hideLoading()
  79. self.stateDatauPblished = (.success(nil),genmojiModel)
  80. generatingProgress = 0
  81. case .failed:
  82. self.stateDatauPblished = (.failed(kNetWorkMessage(data: data) ?? ""),nil)
  83. generatingProgress = 0
  84. default:
  85. stateDatauPblished = (.progressString(generating(progress: genmojiModel.percent)),nil)
  86. if stopNetwork == false {
  87. kDelayOnMainThread(1.0) {
  88. self.getActionInfo(action_id: action_id)
  89. }
  90. }
  91. }
  92. }
  93. }else{
  94. self.stateDatauPblished = (.failed(error?.localizedDescription ?? ""),nil)
  95. }
  96. }
  97. }
  98. func generating(progress:Float) -> String {
  99. //Generating 0%-100%
  100. var progressInt = Int(progress*100)
  101. if generatingProgress >= progressInt{
  102. return getGeneratingProgressText()
  103. }
  104. if progressInt > 99 {
  105. progressInt = 99
  106. }
  107. generatingProgress = progressInt
  108. return getGeneratingProgressText()
  109. }
  110. func getGeneratingProgressText()->String{
  111. return "Generating \(generatingProgress)%"
  112. }
  113. func cancelAllRequest(){
  114. creatRequest?.cancel()
  115. queryRequest?.cancel()
  116. stopNetwork = true
  117. cancel()
  118. }
  119. }