123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- //
- // TSGenerateBaseOperation.swift
- // AIRingtone
- //
- // Created by 100Years on 2025/3/24.
- //
- import Combine
- import Alamofire
- class TSGenerateBaseOperationQueue: TSBaseOperationQueue {
- // 存储每个操作的 AnyCancellable
- var stateables: [String: AnyCancellable] = [:]
-
- var generateOperationStateChanged:((String)->Void)?
-
-
- override func cancelOperations(uuid: String) {
- super.cancelOperations(uuid: uuid)
- stateables.removeValue(forKey: uuid)
- }
- func handleStateDatauPblished(uuid:String,generateOperation: TSGenerateBaseOperation,notificationName:Notification.Name) {
- stateables[uuid] = generateOperation.$stateDatauPblished.sink { [weak self] state in
- guard let self = self else { return }
- DispatchQueue.main.async {
- self.generateOperationStateChanged?(uuid)
-
- let uuidData = self.getUUIDData(uuid: uuid)
- NotificationCenter.default.post(
- name: notificationName,
- object: nil,
- userInfo: [
- "uuid": uuid,
- "count":self.queue.maxConcurrentOperationCount,
- "state":uuidData.0,
- "actionInfo":uuidData.1,
- ])
- }
- }
- }
-
- func getUUIDData(uuid:String)->(TSProgressState,TSActionInfoModel?){
- return (.none,TSActionInfoModel())
- }
-
- }
- class TSGenerateBaseOperation: TSBaseOperation , @unchecked Sendable{
-
- var actionInfoDict:[String:Any]{
- return [:]
- }
-
- @Published var stateDatauPblished:(TSProgressState,TSActionInfoModel?) = (TSProgressState.none,nil){
- didSet{
- dePrint("TSBaseOperation stateDatauPblished didSet = \(stateDatauPblished)")
- if case .start = stateDatauPblished.0 {
- start()
- }else if stateDatauPblished.0.isResult {
- finished()
- }
- }
- }
-
- var creatRequest:Request?
- var queryRequest:Request?
- var stopNetwork = false
- var generatingProgress = 0
- var action_id:Int = 0
-
- var currentActionInfoModelChanged:((TSActionInfoModel)->Void)?
- @Published var currentActionInfoModel: TSActionInfoModel = TSActionInfoModel()
-
- func initializeFirstCurrentActionInfoModel(oldModel:TSActionInfoModel? = nil,prompt:String,promptSort:String) {
- if let model = oldModel {
- currentActionInfoModel = model
- }else {
- currentActionInfoModel.id = Int.timestampInt()
- currentActionInfoModel.request.prompt = prompt
- currentActionInfoModel.request.promptSort = promptSort
- currentActionInfoModel.actionStatus = .pending
- currentActionInfoModel.status = "pending"
- }
- replaceSaveInfoModel(model: currentActionInfoModel)
- stateDatauPblished = (.start,currentActionInfoModel)
- // stateDatauPblished = (.progressString(generating(progress: 0.0)),nil)
- }
-
- func replaceSaveInfoModel(model:TSActionInfoModel){ }
- func handleFailInfoModel(errorString:String?){
- self.currentActionInfoModel.actionStatus = .failed
- self.currentActionInfoModel.status = "failed"
- self.replaceSaveInfoModel(model: self.currentActionInfoModel)
- self.stateDatauPblished = (.failed(errorString ?? ""),nil)
- }
-
- func getActionInfo(oldModel:TSActionInfoModel) {
- currentActionInfoModel = oldModel
- self.getActionInfo(action_id:oldModel.id)
- }
- func getActionInfo(action_id:Int){
- self.action_id = action_id
- queryRequest = TSNetworkShared.get(urlType: .actionInfo,parameters: ["action_id":action_id]) { [weak self] data,error in
- guard let self = self else { return }
- if let result = kNetWorkResultSuccess(data: data) {
- if let genmojiModel = TSActionInfoModel(JSON: result) {
- self.replaceSaveInfoModel(model: genmojiModel)
- switch genmojiModel.actionStatus {
- case .success:
- TSToastShared.hideLoading()
- self.stateDatauPblished = (.success(nil),genmojiModel)
- generatingProgress = 0
- case .failed:
- self.stateDatauPblished = (.failed(kNetWorkMessage(data: data) ?? ""),nil)
- generatingProgress = 0
- default:
- stateDatauPblished = (.progressString(generating(progress: genmojiModel.percent)),nil)
- if stopNetwork == false {
- kDelayOnMainThread(1.0) {
- self.getActionInfo(action_id: action_id)
- }
- }
- }
-
- return
- }
- }
-
- handleFailInfoModel(errorString: error?.localizedDescription)
-
- }
- }
- func generating(progress:Float) -> String {
- //Generating 0%-100%
- var progressInt = Int(progress*100)
- if generatingProgress >= progressInt{
- return getGeneratingProgressText()
- }
- if progressInt > 99 {
- progressInt = 99
- }
-
- generatingProgress = progressInt
- return getGeneratingProgressText()
- }
-
-
- func getGeneratingProgressText()->String{
- return "Working on your ringtone \(generatingProgress)%..."
- }
-
- func cancelAllRequest(){
- creatRequest?.cancel()
- queryRequest?.cancel()
- stopNetwork = true
-
- cancel()
- }
-
- }
- var kRandomBoolLastResult:Bool = true
- func kRandomBool() -> Bool {
- if !kRandomBoolLastResult {
- // 如果上一次是 false,这次必须返回 true
- kRandomBoolLastResult = true
- return true
- } else {
- // 如果上一次是 true,随机返回 true 或 false
- let randomResult = Bool.random()
- kRandomBoolLastResult = randomResult
- return randomResult
- }
- }
|