TSGeneratePhotoOperation.swift 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. //
  2. // TSGeneratePhotoOperation.swift
  3. // AIRingtone
  4. //
  5. // Created by 100Years on 2025/3/24.
  6. //
  7. import Combine
  8. import Alamofire
  9. class TSGeneratePhotoOperationQueue: TSGenerateBaseOperationQueue {
  10. static let shared:TSGeneratePhotoOperationQueue = TSGeneratePhotoOperationQueue()
  11. func creatOperation(uuid: String) -> TSGeneratePhotoOperation {
  12. let operation = super.creatOperation(uuid: uuid, type: TSGeneratePhotoOperation.self)
  13. handleStateDatauPblished(uuid: uuid, generateOperation: operation as! TSGenerateBaseOperation, notificationName: .kGeneratePhotoOperationChanged)
  14. return operation as! TSGeneratePhotoOperation
  15. }
  16. override func getUUIDData(uuid:String)->(TSProgressState,TSActionInfoModel?){
  17. if let photoOperation = TSGeneratePhotoOperationQueue.shared.findOperation(uuid: uuid) as? TSGeneratePhotoOperation {
  18. dePrint("TSBaseOperation stateDatauPblished 发送 = \(photoOperation.stateDatauPblished)")
  19. return (photoOperation.stateDatauPblished.0,photoOperation.currentActionInfoModel)
  20. }
  21. return (.none,TSActionInfoModel())
  22. }
  23. }
  24. class TSGeneratePhotoOperation: TSGenerateBaseOperation , @unchecked Sendable{
  25. override var actionInfoDict:[String:Any]{
  26. return [
  27. "actionType":"image_create",
  28. "comments": "Success",
  29. "costTime":7,
  30. "createdTimestamp":1742183588,
  31. "id":2450,
  32. "percent":1,
  33. "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\"}",
  34. "response":"{\"resultUrl\": \"https://be-aigc.s3-accelerate.amazonaws.com/8b7fcac9-c691-4c3a-b497-401204fad3e9.png\"}",
  35. "status":"success"
  36. ]
  37. }
  38. var gennerateType:TSGennerateType = .photo
  39. override func replaceSaveInfoModel(model:TSActionInfoModel){
  40. model.uuid = uuid
  41. TSPhotoHistory.replaceModel(oldID: currentActionInfoModel.id, newModel: model)
  42. currentActionInfoModel = model
  43. dePrint("TSPhotoHistory.listModelArray.count=\(TSPhotoHistory.listModelArray.count)")
  44. dePrint("model actionStatus 发出=\(model.actionStatus)")
  45. currentActionInfoModelChanged?(currentActionInfoModel)
  46. }
  47. // //模拟数据
  48. // func creatPhoto(oldModel:TSActionInfoModel? = nil,prompt:String,promptSort:String) {
  49. // if let model = oldModel {
  50. // currentActionInfoModel = model
  51. // }else {
  52. // currentActionInfoModel.id = Int.timestampInt()
  53. // currentActionInfoModel.request.prompt = prompt
  54. // currentActionInfoModel.request.promptSort = promptSort
  55. // currentActionInfoModel.actionStatus = .pending
  56. // currentActionInfoModel.status = "pending"
  57. // }
  58. //
  59. // replaceSaveInfoModel(model: currentActionInfoModel)
  60. // stateDatauPblished = (.start,currentActionInfoModel)
  61. // let time = 5.0
  62. //
  63. // for i in 0..<Int(time){
  64. // kDelayOnMainThread(Double(i)) {
  65. // let progress = Float(i)*10/100.0
  66. // self.currentActionInfoModel.percent = progress
  67. // self.currentActionInfoModel.actionStatus = .running
  68. // self.currentActionInfoModel.status = "running"
  69. // self.replaceSaveInfoModel(model: self.currentActionInfoModel)
  70. // self.stateDatauPblished = (.progressString(self.generating(progress: progress)),nil)
  71. // }
  72. // }
  73. //
  74. // kDelayOnMainThread(time+1.0) {
  75. // if kRandomBool(), let infoModel = TSActionInfoModel(JSON: self.actionInfoDict){
  76. // infoModel.id = Int.uuid
  77. // self.replaceSaveInfoModel(model: infoModel)
  78. // self.stateDatauPblished = (.success(nil),self.currentActionInfoModel)
  79. // }else{
  80. // self.currentActionInfoModel.actionStatus = .failed
  81. // self.currentActionInfoModel.status = "failed"
  82. // self.replaceSaveInfoModel(model: self.currentActionInfoModel)
  83. // self.stateDatauPblished = (.failed("error?.localizedDescription"),nil)
  84. // }
  85. //// TSPhotoHistory.dePrintAllModel()
  86. // }
  87. // }
  88. //width 和 height 必须是 32 的倍数
  89. func creatPhoto(oldModel:TSActionInfoModel? = nil,prompt:String,promptSort:String) {
  90. initializeFirstCurrentActionInfoModel(oldModel: oldModel, prompt: prompt, promptSort: promptSort)
  91. generatingProgress = 0
  92. let postDict:[String : Any] = [
  93. "prompt":prompt,
  94. "width":textPicW,
  95. "height":textPicH
  96. ]
  97. creatRequest = TSNetworkShared.post(urlType: .textPicCreate,parameters: postDict) { [weak self] data,error in
  98. guard let self = self else { return }
  99. if let dataDict = data as? [String:Any] ,
  100. dataDict.safeInt(forKey: "code") == 200,
  101. let actionId = dataDict["actionId"] as? Int{
  102. if stopNetwork == false {
  103. self.getActionInfo(action_id:actionId)
  104. return
  105. }
  106. }
  107. handleFailInfoModel(errorString: error?.localizedDescription)
  108. }
  109. }
  110. override func getGeneratingProgressText()->String{
  111. return "Working on your contact Photo \(generatingProgress)% ..."
  112. }
  113. var textPicW:Int{
  114. return kTextPicW
  115. }
  116. var textPicH:Int{
  117. return kTextPicW
  118. }
  119. }