123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- //
- // TSGenneralPicVM.swift
- // AIRingtone
- //
- // Created by 100Years on 2025/3/5.
- //
- import Combine
- import Alamofire
- let kTextPicW:Int = 800
- let kTextPicH:Int = 1440
- let actionInfoDictPoster:[String:Any] = [
- "actionType":"image_create",
- "comments": "Success",
- "costTime":9,
- "createdTimestamp":1742183242,
- "id":2449,
- "percent":1,
- "request":"{\"prompt\": \"Traditional Chinese ink painting style phoenix soaring through misty mountain peaks, dynamic black brushstrokes with crimson accents on rice paper texture, Googie architecture space station, 1950s atomic age aesthetic, curved aluminum panels, starburst patterns, glass dome observatory --edge_threshold 0.5 --atomic_age_style 0.8 --chrome_reflection 1.2\", \"width\": 800, \"height\": 1440, \"countryCode\": \"FR\"}",
- "response": "{\"resultUrl\": \"https://be-aigc.s3-accelerate.amazonaws.com/4c946f78-b1e7-4ffe-ba18-fff26b10178c.png\"}",
- "status":"success"
- ]
- let actionInfoDictPhoto:[String:Any] = [
- "actionType":"image_create",
- "comments": "Success",
- "costTime":7,
- "createdTimestamp":1742183588,
- "id":2450,
- "percent":1,
- "request":"{\"prompt\": \"Steampunk floating library with brass gears and clockwork mechanisms, leather-bound books flying through amber-lit fog, Victorian-era architecture blended with retro-futurism, retro anime cel-shading, 1980s Toei Animation style, bold black outlines, limited color palette (--color 1980s_anime), visible film grain --edge_threshold 0.4 --cel_shading 0.8, Retain the original stone size of the photo\", \"width\": 800, \"height\": 800, \"countryCode\": \"US\"}",
- "response":"{\"resultUrl\": \"https://be-aigc.s3-accelerate.amazonaws.com/8b7fcac9-c691-4c3a-b497-401204fad3e9.png\"}",
- "status":"success"
- ]
- class TSGenneralPicVM {
-
- var creatRequest:Request?
- var queryRequest:Request?
- var stopNetwork = false
-
- @Published var stateDatauPblished:(TSProgressState,TSActionInfoModel?) = (TSProgressState.none,nil)
- var aiText:String = ""
- var gennerateType:TSGennerateType = .poster
- var generatingProgress = 0
-
- //模拟数据
- func creatImageEmoji(text:String) {
- stateDatauPblished = (.start,nil)
- stateDatauPblished = (.progressString(generating(progress: 0.0)),nil)
- kDelayOnMainThread(0.2) {
- self.stateDatauPblished = (.progressString(self.generating(progress: 0.5)),nil)
- }
- kDelayOnMainThread(1.0) {
- if Bool.random() {
- let infoModel = TSActionInfoModel(JSON: self.gennerateType == .poster ? actionInfoDictPoster : actionInfoDictPhoto)
- self.stateDatauPblished = (.success(nil),infoModel)
- }else{
- self.stateDatauPblished = (.failed("error?.localizedDescription"),nil)
- }
- }
- }
-
- // //width 和 height 必须是 32 的倍数
- // func creatImageEmoji(text:String) {
- // generatingProgress = 0
- // aiText = text
- // let postDict:[String : Any] = [
- // "prompt":text,
- // "width":textPicW,
- // "height":textPicH
- // ]
- // stateDatauPblished = (.start,nil)
- // stateDatauPblished = (.progressString(generating(progress: 0.0)),nil)
- // creatRequest = TSNetworkShared.post(urlType: .textPicCreate,parameters: postDict) { [weak self] data,error in
- // guard let self = self else { return }
- // if let dataDict = data as? [String:Any] ,
- // dataDict.safeInt(forKey: "code") == 200,
- // let actionId = dataDict["actionId"] as? Int{
- // if stopNetwork == false {
- // self.getActionInfo(action_id:actionId)
- // }
- // }else{
- // self.stateDatauPblished = (.failed(error?.localizedDescription ?? ""),nil)
- // }
- // }
- // }
-
- func getActionInfo(action_id:Int){
- 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) {
- 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)
- }
- }
- }
- }
- }else{
- self.stateDatauPblished = (.failed(error?.localizedDescription ?? ""),nil)
- }
- }
- }
-
- func cancelAllRequest(){
- creatRequest?.cancel()
- queryRequest?.cancel()
- stopNetwork = true
- }
-
- func generating(progress:Float) -> String {
- //Generating 0%-100%
- var progressInt = Int(progress*100)
- if generatingProgress >= progressInt{
- return "Generating \(generatingProgress)%"
- }
- if progressInt > 99 {
- progressInt = 99
- }
-
- generatingProgress = progressInt
- return "Generating \(progressInt)%"
- }
- }
- extension TSGenneralPicVM {
-
- var textPicW:Int{
- return kTextPicW
- }
-
- var textPicH:Int{
- if gennerateType == .photo {
- return kTextPicW
- }
- return kTextPicH
- }
- }
|