TSGenerateHistoryVC.swift 12 KB

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