TSAIListHistoryVC.swift 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. //
  2. // TSAIListHistoryVC.swift
  3. // TSLiveWallpaper
  4. //
  5. // Created by 100Years on 2025/6/10.
  6. //
  7. import RealmSwift
  8. class TSAIListHistoryVC: TSBaseVC {
  9. var listModelArray:[TSActionInfoModel] = []
  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. TSPurchaseVC.show(target: self) {}
  15. }
  16. return vipBtn
  17. }()
  18. lazy var navBarView: TSBaseNavContentBarView = {
  19. let navBarView = TSBaseNavContentBarView()
  20. let label = UILabel.createLabel(text: "History".localized,font: .font(name: .Pacifico, size: 24))
  21. navBarView.barView.addSubview(label)
  22. label.snp.makeConstraints { make in
  23. make.center.equalToSuperview()
  24. }
  25. navBarView.barView.addSubview(vipBtn)
  26. vipBtn.snp.makeConstraints { make in
  27. make.centerY.equalToSuperview()
  28. make.trailing.equalTo(-16)
  29. make.width.height.equalTo(32)
  30. }
  31. kMainAsync {
  32. label.applyGradient(colors: ["#F1D3AB".uiColor,"#E4A858".uiColor])
  33. }
  34. return navBarView
  35. }()
  36. //###################################### 集合视图 ######################################
  37. let collectionViewBtootm:CGFloat = 80
  38. let identifier = "TSAIListHistoryCell"
  39. lazy var collectionView: UICollectionView = {
  40. let layout = UICollectionViewFlowLayout()
  41. layout.scrollDirection = .vertical
  42. let itemW = (k_ScreenWidth-32.0-13.0-2.0)/2.0
  43. let itemH = kGetScaleHeight(originalSize: CGSize(width: 165.0, height: 220.0), width: itemW)
  44. layout.itemSize = CGSize(width: itemW, height: itemH)
  45. layout.minimumInteritemSpacing = 13
  46. layout.minimumLineSpacing = 16
  47. layout.sectionInset = UIEdgeInsets(top: 10, left: 16, bottom: k_Height_TabBar+20, right: 16)
  48. let collectionView = TSBaseCollectionView(frame: .zero, collectionViewLayout: layout)
  49. collectionView.delegate = self
  50. collectionView.dataSource = self
  51. collectionView.register(TSAIListHistoryCell.self, forCellWithReuseIdentifier: identifier)
  52. return collectionView
  53. }()
  54. lazy var pageNullView: TSPageNullView = {
  55. let pageNullView = TSPageNullView()
  56. pageNullView.isHidden = true
  57. return pageNullView
  58. }()
  59. override func createData() {
  60. }
  61. var navRightBtn = UIButton()
  62. override func createView() {
  63. addNormalNavBarView()
  64. // setPageTitle("History".localized)
  65. // navRightBtn = setNavigationItem("", imageName: "ai_delete", direction: .right, action: #selector(clickNavRight))
  66. navBarContentView.addSubview(navBarView)
  67. navBarView.snp.makeConstraints { make in
  68. make.edges.equalToSuperview()
  69. }
  70. contentView.addSubview(pageNullView)
  71. contentView.addSubview(collectionView)
  72. collectionView.snp.makeConstraints { make in
  73. make.edges.equalToSuperview()
  74. }
  75. }
  76. override func dealThings() {
  77. updateDataView()
  78. NotificationCenter.default.addObserver(self, selector: #selector(operationChanged(_:)), name: .kGenerateBasePhotoOperation, object: nil)
  79. NotificationCenter.default.addObserver(self, selector: #selector(updateDataView), name: .kAIPhotoDataChanged, object: nil)
  80. NotificationCenter.default.addObserver(self, selector: #selector(updateVipView), name: .kPurchaseDidChanged, object: nil)
  81. updateVipView()
  82. }
  83. @objc func operationChanged(_ notification: Notification) {
  84. if let userInfo = notification.userInfo as? [String: Any],let state = userInfo["state"] as? TSProgressState {
  85. if state.reloadNewData {
  86. self.updateDataView()
  87. }
  88. }
  89. }
  90. @objc func updateVipView() {
  91. kMainAsync{
  92. self.vipBtn.isHidden = PurchaseManager.default.isVip
  93. }
  94. }
  95. @objc func updateDataView(){
  96. dbHistory.getModelList { [weak self] array in
  97. guard let self = self else { return }
  98. listModelArray = array
  99. updateView()
  100. }
  101. // listModelArray = dbHistory.getModelList()
  102. // updateView()
  103. }
  104. func updateView() {
  105. collectionView.reloadData()
  106. navRightBtn.isHidden = listModelArray.count <= 0
  107. pageNullView.isHidden = listModelArray.count > 0
  108. }
  109. override func viewWillAppear(_ animated: Bool) {
  110. print("viewWillAppear")
  111. }
  112. @objc func clickNavRight() {
  113. TSCustomAlertController.show(in: self, config: TSCustomAlertController.AlertConfig(
  114. message: "Are you sure to delete all histories?".localized,
  115. cancelTitle: "Delete All".localized,
  116. cancelColor: .red,
  117. confirmTitle: "Retain".localized,
  118. confirmColor: .white,
  119. cancelAction: { [weak self] in
  120. guard let self = self else { return }
  121. self.removeAllHistoryList()
  122. self.updateDataView()
  123. }
  124. ))
  125. }
  126. }
  127. extension TSAIListHistoryVC: UICollectionViewDataSource ,UICollectionViewDelegate {
  128. public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  129. return listModelArray.count
  130. }
  131. public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  132. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: identifier, for: indexPath)
  133. if let cell = cell as? TSAIListHistoryCell ,let dbModel = listModelArray.safeObj(At:indexPath.row){
  134. cell.dataModel = dbModel
  135. cell.buttonTapped = { [weak self] cmd in
  136. self?.handelCellCmd(cmd: cmd,indexPath: indexPath)
  137. }
  138. }
  139. return cell
  140. }
  141. public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  142. if let dbModel = listModelArray.safeObj(At:indexPath.row){
  143. // //单个浏览
  144. // let detailsVC = TSAIPhotoDetailsVC()
  145. // detailsVC.infoModel = dbModel
  146. // detailsVC.deleteBlock = { [weak self] in
  147. // guard let self = self else { return }
  148. // removeDelete(index: indexPath.row)
  149. // }
  150. // kPresentModalVC(target: self, modelVC: detailsVC)
  151. // return
  152. //多个浏览
  153. var dataModelArray: [TSActionInfoModel] = []
  154. for itemModel in listModelArray {
  155. if itemModel.status == "success" || itemModel.modelType == .example {
  156. dataModelArray.append(itemModel)
  157. }
  158. }
  159. let vc = TSAIPhotoDetailsBrowserVC()
  160. vc.currentIndex = dataModelArray.firstIndex(of: dbModel) ?? 0
  161. vc.dataModelArray = dataModelArray
  162. vc.deleteBlock = { [weak self] model in
  163. guard let self = self else { return }
  164. TSRMShared.aiListDB.deleteListModel(id: model.id)
  165. updateDataView()
  166. }
  167. kPresentModalVC(target: self, modelVC: vc)
  168. }
  169. }
  170. }
  171. extension TSAIListHistoryVC{
  172. func handelCellCmd(cmd:String,indexPath: IndexPath){
  173. if let currentActionInfoModel = listModelArray.safeObj(At: indexPath.item){
  174. if cmd == "delete_task_expired" {
  175. TSRMShared.aiListDB.deleteListModel(id: currentActionInfoModel.id)
  176. updateDataView()
  177. }else if cmd == "delete_task_sensitive" {
  178. showDeleteErrorAlert(message: "Delete this error history?".localized, deleteHandler: { [weak self] in
  179. guard let self = self else { return }
  180. TSRMShared.aiListDB.deleteListModel(id: currentActionInfoModel.id)
  181. updateDataView()
  182. })
  183. }else if cmd == "delete" {
  184. TSCustomAlertController.show(in: self, config: TSCustomAlertController.AlertConfig(
  185. message: "Are you sure to delete?".localized,
  186. cancelTitle: "Delete".localized,
  187. cancelColor: .red,
  188. confirmTitle: "Retain".localized,
  189. confirmColor: .white,
  190. cancelAction: { [weak self] in
  191. guard let self = self else { return }
  192. removeDelete(index: indexPath.row)
  193. }
  194. ))
  195. }
  196. }
  197. }
  198. func removeDelete(index:Int){
  199. dbHistory.deleteListModel(index: index)
  200. updateDataView()
  201. }
  202. func removeAllHistoryList(){
  203. dbHistory.deleteAll()
  204. updateDataView()
  205. }
  206. var dbHistory:TSDBHistory{
  207. TSRMShared.aiListDB
  208. }
  209. }
  210. extension TSAIListHistoryVC{
  211. static func showPosition(){
  212. AppDelegate.tabbar?.changeSelectedIndex(index: 1)
  213. }
  214. }