TSGenerateBaseOperation.swift 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. }
  43. @Published var stateDatauPblished:(TSProgressState,TSActionInfoModel?) = (TSProgressState.none,nil){
  44. didSet{
  45. dePrint("TSBaseOperation stateDatauPblished didSet = \(stateDatauPblished)")
  46. if case .start = stateDatauPblished.0 {
  47. start()
  48. }else if stateDatauPblished.0.isResult {
  49. finished()
  50. }
  51. }
  52. }
  53. var creatRequest:Request?
  54. var queryRequest:Request?
  55. var stopNetwork = false
  56. var generatingProgress = 0
  57. var action_id:Int = 0
  58. var currentActionInfoModelChanged:((TSActionInfoModel)->Void)?
  59. @Published var currentActionInfoModel: TSActionInfoModel = TSActionInfoModel()
  60. func initializeFirstCurrentActionInfoModel(oldModel:TSActionInfoModel? = nil,prompt:String,promptSort:String) {
  61. if let model = oldModel {
  62. currentActionInfoModel = model
  63. }else {
  64. currentActionInfoModel.id = Int.timestampInt()
  65. currentActionInfoModel.request.prompt = prompt
  66. currentActionInfoModel.request.promptSort = promptSort
  67. currentActionInfoModel.actionStatus = .pending
  68. currentActionInfoModel.status = "pending"
  69. }
  70. replaceSaveInfoModel(model: currentActionInfoModel)
  71. stateDatauPblished = (.start,currentActionInfoModel)
  72. // stateDatauPblished = (.progressString(generating(progress: 0.0)),nil)
  73. }
  74. func replaceSaveInfoModel(model:TSActionInfoModel){ }
  75. func handleFailInfoModel(errorString:String?){
  76. self.currentActionInfoModel.actionStatus = .failed
  77. self.currentActionInfoModel.status = "failed"
  78. self.replaceSaveInfoModel(model: self.currentActionInfoModel)
  79. self.stateDatauPblished = (.failed(errorString ?? ""),nil)
  80. }
  81. func getActionInfo(oldModel:TSActionInfoModel) {
  82. currentActionInfoModel = oldModel
  83. self.getActionInfo(action_id:oldModel.id)
  84. }
  85. func getActionInfo(action_id:Int){
  86. self.action_id = action_id
  87. queryRequest = TSNetworkShared.get(urlType: .actionInfo,parameters: ["action_id":action_id]) { [weak self] data,error in
  88. guard let self = self else { return }
  89. if let result = kNetWorkResultSuccess(data: data) {
  90. if let genmojiModel = TSActionInfoModel(JSON: result) {
  91. self.replaceSaveInfoModel(model: genmojiModel)
  92. switch genmojiModel.actionStatus {
  93. case .success:
  94. TSToastShared.hideLoading()
  95. self.stateDatauPblished = (.success(nil),genmojiModel)
  96. generatingProgress = 0
  97. case .failed:
  98. self.stateDatauPblished = (.failed(kNetWorkMessage(data: data) ?? ""),nil)
  99. generatingProgress = 0
  100. default:
  101. stateDatauPblished = (.progressString(generating(progress: genmojiModel.percent)),nil)
  102. if stopNetwork == false {
  103. kDelayOnMainThread(1.0) {
  104. self.getActionInfo(action_id: action_id)
  105. }
  106. }
  107. }
  108. return
  109. }
  110. }
  111. handleFailInfoModel(errorString: error?.localizedDescription)
  112. }
  113. }
  114. func generating(progress:Float) -> String {
  115. //Generating 0%-100%
  116. var progressInt = Int(progress*100)
  117. if generatingProgress >= progressInt{
  118. return getGeneratingProgressText()
  119. }
  120. if progressInt > 99 {
  121. progressInt = 99
  122. }
  123. generatingProgress = progressInt
  124. return getGeneratingProgressText()
  125. }
  126. func getGeneratingProgressText()->String{
  127. return "Working on your ringtone \(generatingProgress)%..."
  128. }
  129. func cancelAllRequest(){
  130. creatRequest?.cancel()
  131. queryRequest?.cancel()
  132. stopNetwork = true
  133. cancel()
  134. }
  135. }
  136. var kRandomBoolLastResult:Bool = true
  137. func kRandomBool() -> Bool {
  138. if !kRandomBoolLastResult {
  139. // 如果上一次是 false,这次必须返回 true
  140. kRandomBoolLastResult = true
  141. return true
  142. } else {
  143. // 如果上一次是 true,随机返回 true 或 false
  144. let randomResult = Bool.random()
  145. kRandomBoolLastResult = randomResult
  146. return randomResult
  147. }
  148. }