123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- //
- // ASGenerateBaseOperation.swift
- // AIPlayRingtones
- //
- // Created by 100Years on 2025/5/16.
- //
- import Combine
- import Alamofire
- class ASGenerateBaseOperationQueue: ASBaseOperationQueue {
- // 存储每个操作的 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: ASGenerateBaseOperation,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)->(ASProgressState,ASActionInfoModel?){
- return (.none,ASActionInfoModel())
- }
-
- }
- class ASGenerateBaseOperation: ASBaseOperation , @unchecked Sendable{
-
- var actionInfoDict:[String:Any]{
- return [:]
- }
-
- @Published var stateDatauPblished:(ASProgressState,ASActionInfoModel?) = (ASProgressState.none,nil){
- didSet{
- // logPrint("ASBaseOperation stateDatauPblished didSet = \(stateDatauPblished)")
- // if let block = self.stateDataPblishedChanged{
- // logPrint("刷新进度block 真=\(self.stateDatauPblished)")
- // self.stateDataPblishedChanged?(self.stateDatauPblished.0,self.stateDatauPblished.1)
- // }else{
- // logPrint("刷新进度block 空=\(self.stateDatauPblished)")
- // }
-
- DispatchQueue.main.async {
- self.stateDataPblishedChanged?(self.stateDatauPblished.0,self.stateDatauPblished.1)
- }
-
- if case .start = stateDatauPblished.0 {
- start()
- }else if stateDatauPblished.0.isResult {
- DispatchQueue.main.asyncAfter(deadline: .now()+0.3){//稍微延迟,让通知报成功状态发送出去
- self.finished()
- }
- }
- }
- }
-
- var queryRequest:Request?
- var stopNetwork = false
- var generatingProgress = 0
- var action_id:Int = 0
- var isSaveProcessToDB:Bool = false //是否保存过程到数据库
- {
- didSet{
- if isSaveProcessToDB == true {
- saveDataDB()
- }
- }
- }
-
- var currentActionInfoModelChanged:((ASActionInfoModel)->Void)?
- var stateDataPblishedChanged:((ASProgressState,ASActionInfoModel?)->Void)?
- @Published var currentActionInfoModel: ASActionInfoModel = ASActionInfoModel()
-
- func initializeActionInfoModel(oldModel:ASActionInfoModel) {
- currentActionInfoModel = oldModel
- replaceSaveInfoModel(model: currentActionInfoModel)
- stateDatauPblished = (.start,currentActionInfoModel)
- }
-
- func replaceSaveInfoModel(model:ASActionInfoModel){ }
-
- func handleGenerateSuccess(){}
- func saveDataDB(){}
- func backstageGeneration(){}
-
- func handleFailInfoModel(errorString:String?,code:Int = 0){
- self.currentActionInfoModel.actionStatus = .failed
- self.currentActionInfoModel.status = "failed"
- generatingProgress = 0
- self.replaceSaveInfoModel(model: self.currentActionInfoModel)
- self.stateDatauPblished = (ASProgressState.getFailed(errorString ?? "",code),self.currentActionInfoModel)
- }
-
- func getActionInfo(oldModel:ASActionInfoModel) {
- 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 stopNetwork == true {
- return
- }
-
- if let error = error {
- logPrint("getActionInfo error error = \(error)")
- handleFailInfoModel(errorString: error.tsDesc,code: error.tsCode)
- return
- }
-
- if let result = kNetWorkResultSuccess(data: data) {
- if let genmojiModel = ASActionInfoModel(JSON: result) {
-
- if genmojiModel.actionStatus != .success {
- self.replaceSaveInfoModel(model: genmojiModel)
- }
-
- switch genmojiModel.actionStatus {
- case .success:
-
- let successBlock = { [weak self] in
- guard let self = self else { return }
- self.replaceSaveInfoModel(model: genmojiModel)
- self.stateDatauPblished = (.success(nil),genmojiModel)
- generatingProgress = 0
- self.handleGenerateSuccess()
- }
-
- if URL(string:genmojiModel.response.musicUrl) != nil {
- ASDownloadManager.getDownLoadRing(urlString: genmojiModel.response.musicUrl) { [weak self] progress in
- guard let self = self else { return }
- let progressInt = Int(progress*10.0)
- let progressString = "Generating".localized + " \(90 + progressInt)%"
- stateDatauPblished = (.progress(kPercentScale+Float(progress)*0.1,progressString),currentActionInfoModel)
- logPrint("生成后下载进度 \(progress)")
- } complete: { [weak self] url, success in
- guard let self = self else { return }
- if let url = url {
- genmojiModel.ringSavePath = url.path.documentLastURL
-
- if let info = TSBusinessAudioPlayer.getAudioFileInfo(path: url.path) {
- if let duration = info.durationInSeconds {
- genmojiModel.response.duration = Int(duration)
- }
-
- if let size = info.sizeInBytes {
- genmojiModel.response.size = Int(size)
- }
- }
- successBlock()
- }else{
- self.handleFailInfoModel(errorString: nil)
- }
- }
-
- }else{
- successBlock()
- }
- case .failed:
- logPrint("getActionInfo error failed")
- handleFailInfoModel(errorString:genmojiModel.response.codeErrorMsg,code: genmojiModel.response.code)
- default:
- let progressText = generating(progress: genmojiModel.percent)
- stateDatauPblished = (.progress(genmojiModel.percent*kPercentScale,progressText),currentActionInfoModel)
- if stopNetwork == false {
- kDelayOnMainThread(2.0) {
- self.getActionInfo(action_id: action_id)
- }
- }
- }
-
- return
- }
- }
- logPrint("getActionInfo error nil")
- handleFailInfoModel(errorString: nil)
-
- }
- }
-
- 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)%..."
- }
-
- override func cancelCleanContent() {
- logPrint("cancelCleanContent")
- stopNetwork = true
- queryRequest?.cancel()
- }
- }
- let kPercentScale:Float = 0.9 //下载进度的缩减比例
- 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
- }
- }
|