123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289 |
- //
- // TSAIChangeEmoteVM.swift
- // AIEmoji
- //
- // Created by 100Years on 2025/4/10.
- //
- import ObjectMapper
- import Alamofire
- class TSAIChangeEmoteVM {
-
- //###################################### 样式选择 ######################################
- var selectedStyleIndex:Int = 1
- lazy var selectStyleModels: [TSGenerateStyleModel] = {
- var selectStyleModels = [TSGenerateStyleModel]()
- if let dataArray = Mapper<TSGenerateStyleModel>().mapArray(JSONfile: jsonName){
- selectStyleModels = dataArray
-
- if let model = dataArray.safeObj(At: selectedStyleIndex) {
- selectStyleModel = model //加上默认的选择
- }
- }
-
- return selectStyleModels
- }()
-
- var selectStyleModel:TSGenerateStyleModel?
-
- var isSavePhotoMark:Bool = false
- var isNeedSavePhoto:Bool {
-
- if let selectStyleModel = selectStyleModel,
- selectStyleModel.clickType == 1{
- return false
- }
-
- if isSavePhotoMark == false{
- for model in selectStyleModels {
- if let _ = model.actionInfoModel {
- return true
- }
- }
- }
- return false
- }
- //###################################### 生成图片 ######################################
- var uploadRequest:Request?
- var creatRequest:Request?
- var queryRequest:Request?
- var stopNetwork = false
-
- @Published var stateDatauPblished:(TSProgressState,TSActionInfoModel?) = (TSProgressState.none,nil)
- var aiText:String = ""
- var generatingProgress = 0
-
- var upLoadImage:UIImage?{
- didSet{
- imageUrl = nil
- isSavePhotoMark = false
- for model in selectStyleModels {
- model.actionInfoModel = nil
- }
- }
- }
- var imageUrl:String?
-
- var style:TSAIChangeEmoteVC.Style
- init(style: TSAIChangeEmoteVC.Style) {
- self.style = style
- }
- }
- extension TSAIChangeEmoteVM {
-
- func creatImage() {
- guard let imageUrl = imageUrl else { return }
- guard let selectStyleModel = selectStyleModel else { return }
-
- generatingProgress = 0
- stopNetwork = false
- stateDatauPblished = (.start,nil)
- stateDatauPblished = (.progressString(generating(progress: 0.0)),nil)
-
- var urlType:TSNeURLType = .changeEmotion
- var postDict:[String:Any] = [:]
- switch style {
- case .changeEmote:
- urlType = .changeEmotion
- postDict = ["emotionType":selectStyleModel.style,
- "imageUrl":imageUrl,
- "device":getUserInfoJsonString()]
- case .changehairColor:
- urlType = .imageRewrite
- postDict = ["prompt":selectStyleModel.prompt,
- "imageUrl":imageUrl,
- "device":getUserInfoJsonString()]
- case .changehair:
- urlType = .changeHair
- postDict = ["hairType":Int(selectStyleModel.style) ?? 101,
- "imageUrl":imageUrl,
- "device":getUserInfoJsonString()]
- }
-
- creatRequest = TSNetworkShared.post(urlType: urlType,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:
- if let url = URL(string:genmojiModel.response.resultUrl) {
- UIImageView.downloadImageWithProgress(urlString: genmojiModel.response.resultUrl) { [weak self] progress in
- guard let self = self else { return }
-
- let progressInt = Int(progress*10.0)
- let progressString = "Changing".localized + " \(90 + progressInt)%"
- stateDatauPblished = (.progressString(progressString),nil)
- dePrint("生成后图片下载进度 \(progress)")
- } completion: {[weak self] image in
- guard let self = self else { return }
- self.stateDatauPblished = (.success(nil),genmojiModel)
- self.selectStyleModel?.actionInfoModel = genmojiModel
- generatingProgress = 0
- }
- }else{
- self.stateDatauPblished = (.success(nil),genmojiModel)
- self.selectStyleModel?.actionInfoModel = 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 {
- let progress = progress*(0.9) // 预留 10% 进度给图片下载
- //Generating 0%-100%
- var progressInt = Int(progress*100)
- if progressInt > 99 {
- progressInt = 99
- }
-
- generatingProgress = progressInt
- return "Changing".localized + " \(progressInt)%"
- }
-
- // //模拟数据
- // func uploadAndCreatImage() {
- //
- // stateDatauPblished = (.start,nil)
- // stateDatauPblished = (.progressString(generating(progress: 0.0)),nil)
- //
- // kDelayOnMainThread(0.2) {
- // self.stateDatauPblished = (.progressString(self.generating(progress: 0.2)),nil)
- // }
- //
- // kDelayOnMainThread(0.5) {
- // self.stateDatauPblished = (.progressString(self.generating(progress: 0.5)),nil)
- // }
- //
- // kDelayOnMainThread(0.8) {
- // self.stateDatauPblished = (.progressString(self.generating(progress: 0.8)),nil)
- // }
- //
- //
- // kDelayOnMainThread(2.0) {
- // if kRandomBool() {
- // let infoModel = TSActionInfoModel(JSON:actionInfoDictPoster )
- // self.stateDatauPblished = (.success(nil),infoModel)
- // }else{
- // self.stateDatauPblished = (.failed("error?.localizedDescription"),nil)
- // }
- // }
- //
- // }
-
- func uploadAndCreatImage() {
-
- if let model = selectStyleModel?.actionInfoModel {
- self.stateDatauPblished = (.success(nil),model)
- return
- }
-
- if let imageUrl = imageUrl,imageUrl.contains("http") {
- creatImage()
- return
- }
-
- guard let upLoadImage = upLoadImage else { return }
- stopNetwork = false
- stateDatauPblished = (.start,nil)
-
- stateDatauPblished = (.progressString(uploadingPhoto(progress: 0.0)),nil)
- uploadRequest = TSNetworkShared.uploadImage(upLoadImage: upLoadImage, maxKb: imageMaxKb) { [weak self] progress in
- guard let self = self else { return }
- if generatingProgress == 0 {
- stateDatauPblished = (.progressString(uploadingPhoto(progress: progress)),nil)
- }
- } completion: { [weak self] data, error in
- guard let self = self else { return }
- if let error = error {
- imageUrl = nil
- self.stateDatauPblished = (.failed(error.localizedDescription),nil)
- }else{
- if let string = data as? String {
- imageUrl = string
- creatImage()
- }
- }
- }
- }
-
- var imageMaxKb:Int{
- switch style {
- case .changeEmote:
- return 10*1024
- case .changehairColor:
- return 10*1024
- case .changehair:
- return 10*1024
- }
- }
-
- func uploadingPhoto(progress:Float) -> String {
- //Uploading Photo 0%-100%
- var progressInt = Int(progress*100)
- if progressInt > 99 {
- progressInt = 99
- }
- return "Uploading Photo".localized + " \(progressInt)%"
- }
- }
- extension TSAIChangeEmoteVM {
- var jsonName:String{
- switch style {
- case .changeEmote:
- return "ai_change_emote_style.json"
- case .changehairColor:
- return "ai_change_hair_color_style.json"
- case .changehair:
- return "ai_change_hair_style.json"
- }
- }
- }
|