TSGenerateHistoryVC.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. //
  2. // TSGenerateHistoryVC.swift
  3. // AIEmoji
  4. //
  5. // Created by 100Years on 2025/6/22.
  6. //
  7. import RealmSwift
  8. class TSGenerateHistoryVC: TSBaseVC {
  9. var listModelArray = List<TSDBActionInfoModel>()//惰性加载特性
  10. //###################################### 导航栏 view ######################################
  11. lazy var vipBtn: UIButton = {
  12. let vipBtn = UIButton.createButton(image: UIImage(named: "nav_vip")) { [weak self] in
  13. guard let self = self else { return }
  14. if kPurchaseCountDownTime.isCountDown {
  15. kPresentModalVC(target: self, modelVC: TSPurchasePromotionalVC(isAnimation: false))
  16. }else{
  17. TSPurchaseVC.show(target: self)
  18. }
  19. }
  20. vipBtn.isHidden = true
  21. return vipBtn
  22. }()
  23. lazy var purchaseCountdownView : TSPurchaseCountdownView = {
  24. let purchaseCountdownView = TSPurchaseCountdownView()
  25. purchaseCountdownView.clickBlock = { [weak self] in
  26. guard let self = self else { return }
  27. kPresentModalVC(target: self, modelVC: TSPurchasePromotionalVC(isAnimation: false))
  28. }
  29. purchaseCountdownView.isHidden = true
  30. return purchaseCountdownView
  31. }()
  32. lazy var navBarView: TSBaseNavContentBarView = {
  33. let navBarView = TSBaseNavContentBarView()
  34. let label = UILabel.createLabel(text: "Creations",font: .font(name: .KelsiFill,size: 28))
  35. navBarView.barView.addSubview(label)
  36. label.snp.makeConstraints { make in
  37. make.centerY.equalToSuperview()
  38. make.leading.equalTo(17.0)
  39. }
  40. let deleteBtn = UIButton.createButton(image: .delete) { [weak self] in
  41. guard let self = self else { return }
  42. clickNavRight()
  43. }
  44. let clearView = UIView()
  45. let stackView:UIStackView = UIStackView()
  46. stackView.spacing = 8
  47. navBarView.barView.addSubview(stackView)
  48. stackView.addArrangedSubview(vipBtn)
  49. stackView.addArrangedSubview(purchaseCountdownView)
  50. stackView.addArrangedSubview(clearView)
  51. stackView.addArrangedSubview(deleteBtn)
  52. stackView.snp.makeConstraints { make in
  53. make.centerY.equalToSuperview()
  54. make.trailing.equalTo(-16)
  55. }
  56. vipBtn.snp.makeConstraints { make in
  57. make.width.height.equalTo(24)
  58. }
  59. purchaseCountdownView.snp.makeConstraints { make in
  60. make.height.equalTo(24)
  61. }
  62. clearView.snp.makeConstraints { make in
  63. make.width.equalTo(0.1)
  64. }
  65. deleteBtn.snp.makeConstraints { make in
  66. make.width.height.equalTo(24)
  67. }
  68. kMainAsync {
  69. label.applyGradient(colors: ["#FA794F".uiColor,"#F8C32A".uiColor,"#FEFBF4".uiColor])
  70. }
  71. return navBarView
  72. }()
  73. //###################################### 集合视图 ######################################
  74. let collectionViewBtootm:CGFloat = 80
  75. let identifier = "TSAIListHistoryCell"
  76. lazy var collectionView: UICollectionView = {
  77. let layout = UICollectionViewFlowLayout()
  78. layout.scrollDirection = .vertical
  79. let itemW = (k_ScreenWidth-32.0-13.0-2.0)/2.0
  80. let itemH = kGetScaleHeight(originalSize: CGSize(width: 165.0, height: 220.0), width: itemW)
  81. layout.itemSize = CGSize(width: itemW, height: itemH)
  82. layout.minimumInteritemSpacing = 13
  83. layout.minimumLineSpacing = 16
  84. layout.sectionInset = UIEdgeInsets(top: 10, left: 16, bottom: k_Height_TabBar+20, right: 16)
  85. let collectionView = TSBaseCollectionView(frame: .zero, collectionViewLayout: layout)
  86. collectionView.delegate = self
  87. collectionView.dataSource = self
  88. collectionView.register(TSGenerateHistoryCell.self, forCellWithReuseIdentifier: identifier)
  89. return collectionView
  90. }()
  91. lazy var pageNullView: TSPageNullView = {
  92. let pageNullView = TSPageNullView()
  93. pageNullView.isHidden = true
  94. return pageNullView
  95. }()
  96. override func createData() {
  97. }
  98. var navRightBtn = UIButton()
  99. override func createView() {
  100. addNormalNavBarView()
  101. navBarContentView.addSubview(navBarView)
  102. navBarView.snp.makeConstraints { make in
  103. make.edges.equalToSuperview()
  104. }
  105. contentView.addSubview(pageNullView)
  106. contentView.addSubview(collectionView)
  107. collectionView.snp.makeConstraints { make in
  108. make.edges.equalToSuperview()
  109. }
  110. }
  111. override func dealThings() {
  112. updateDataView()
  113. NotificationCenter.default.addObserver(self, selector: #selector(operationChanged(_:)), name: .kGenerateBasePhotoOperation, object: nil)
  114. NotificationCenter.default.addObserver(self, selector: #selector(updateDataView), name: .kAIPhotoDataChanged, object: nil)
  115. NotificationCenter.default.addObserver(self, selector: #selector(updateVipView), name: .kPurchaseDidChanged, object: nil)
  116. updateVipView()
  117. NotificationCenter.default.addObserver(self, selector: #selector(checkkPurchaseCountDownTime), name: .kCloseTSPurchasePromotionalVC, object: nil)
  118. checkkPurchaseCountDownTime()
  119. }
  120. @objc func checkkPurchaseCountDownTime(){
  121. kMainAsync {
  122. if kPurchaseCountDownTime.isCountDown {
  123. self.purchaseCountdownView.isHidden = false
  124. kPurchaseCountDownTime.complete = { [weak self] minutes, seconds, end in
  125. guard let self = self else { return }
  126. kMainAsync {
  127. if end {
  128. self.purchaseCountdownView.isHidden = true
  129. }else{
  130. self.purchaseCountdownView.minLabel.text = minutes
  131. self.purchaseCountdownView.secLabel.text = seconds
  132. }
  133. }
  134. }
  135. }else{
  136. self.purchaseCountdownView.isHidden = true
  137. }
  138. }
  139. }
  140. @objc func operationChanged(_ notification: Notification) {
  141. if let userInfo = notification.userInfo as? [String: Any],let state = userInfo["state"] as? TSProgressState {
  142. if state.reloadNewData {
  143. self.updateDataView()
  144. }
  145. }
  146. }
  147. @objc func updateVipView() {
  148. kMainAsync{
  149. self.vipBtn.isHidden = PurchaseManager.default.isVip
  150. }
  151. }
  152. @objc func updateDataView(){
  153. listModelArray = dbHistory.listModels//惰性加载特性
  154. updateView()
  155. }
  156. func updateView() {
  157. collectionView.reloadData()
  158. navRightBtn.isHidden = listModelArray.count <= 0
  159. pageNullView.isHidden = listModelArray.count > 0
  160. }
  161. override func viewWillAppear(_ animated: Bool) {
  162. super.viewWillAppear(animated)
  163. checkkPurchaseCountDownTime()
  164. }
  165. @objc func clickNavRight() {
  166. TSCustomAlertController.show(in: self, config: TSCustomAlertController.AlertConfig(
  167. message: "Are you sure to delete all histories?".localized,
  168. cancelTitle: "Delete All".localized,
  169. cancelColor: .red,
  170. confirmTitle: "Retain".localized,
  171. confirmColor: .white,
  172. cancelAction: { [weak self] in
  173. guard let self = self else { return }
  174. self.removeAllHistoryList()
  175. self.updateDataView()
  176. }
  177. ))
  178. }
  179. }
  180. extension TSGenerateHistoryVC: UICollectionViewDataSource ,UICollectionViewDelegate {
  181. public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  182. return listModelArray.count
  183. }
  184. public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  185. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: identifier, for: indexPath)
  186. if let cell = cell as? TSGenerateHistoryCell ,let dbModel = listModelArray[safe:indexPath.row]{
  187. cell.dataModel = dbModel.getModel()
  188. cell.buttonTapped = { [weak self] cmd in
  189. self?.handelCellCmd(cmd: cmd,indexPath: indexPath)
  190. }
  191. }
  192. return cell
  193. }
  194. public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  195. guard let selectedModel = listModelArray[safe:indexPath.row] else { return }
  196. let filteredResults = listModelArray.filter("status == %@", "success")//直接过滤List(返回Results类型)
  197. let filteredList = List<TSDBActionInfoModel>()
  198. filteredList.append(objectsIn: filteredResults)
  199. let browseVC = TSAIPhotoBrowseVC()
  200. browseVC.dataModelArray = filteredList
  201. browseVC.currentIndex = filteredList.firstIndex(of: selectedModel) ?? 0
  202. browseVC.deleteComplete = { [weak self] deleteModel in
  203. guard let self = self else { return }
  204. dbHistory.deleteListModel(uuid: deleteModel.uuid)
  205. updateDataView()
  206. }
  207. kPresentModalVC(target: self, modelVC: browseVC,transitionStyle: .crossDissolve)
  208. }
  209. }
  210. extension TSGenerateHistoryVC{
  211. func handelCellCmd(cmd:String,indexPath: IndexPath){
  212. if let currentActionInfoModel = listModelArray[safe:indexPath.row]{
  213. if cmd == "delete_task_expired" {
  214. dbHistory.deleteListModel(id: currentActionInfoModel.id)
  215. updateDataView()
  216. }else if cmd == "delete_task_sensitive" {
  217. showDeleteErrorAlert(message: "Delete this error history?".localized, deleteHandler: { [weak self] in
  218. guard let self = self else { return }
  219. dbHistory.deleteListModel(id: currentActionInfoModel.id)
  220. updateDataView()
  221. })
  222. }else if cmd == "delete" {
  223. TSCustomAlertController.show(in: self, config: TSCustomAlertController.AlertConfig(
  224. message: "Are you sure to delete?".localized,
  225. cancelTitle: "Delete".localized,
  226. cancelColor: .red,
  227. confirmTitle: "Retain".localized,
  228. confirmColor: .white,
  229. cancelAction: { [weak self] in
  230. guard let self = self else { return }
  231. removeDelete(index: indexPath.row)
  232. }
  233. ))
  234. }
  235. }
  236. }
  237. func removeDelete(index:Int){
  238. dbHistory.deleteListModel(index: index)
  239. updateDataView()
  240. }
  241. func removeAllHistoryList(){
  242. dbHistory.deleteResultModels()
  243. // dbHistory.deleteAll()
  244. updateDataView()
  245. }
  246. var dbHistory:TSDBHistory{
  247. TSRMShared.aiGenerateDB
  248. }
  249. }
  250. extension TSGenerateHistoryVC{
  251. static func showPosition(){
  252. AppDelegate.tabbar?.changeSelectedIndex(index: 1)
  253. }
  254. }