123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266 |
- //
- // TSAIListHistoryVC.swift
- // TSLiveWallpaper
- //
- // Created by 100Years on 2025/6/10.
- //
- import RealmSwift
- class TSAIListHistoryVC: TSBaseVC {
- var listModelArray:[TSActionInfoModel] = []
-
- //###################################### 导航栏 view ######################################
- lazy var vipBtn: UIButton = {
- let vipBtn = UIButton.createButton(image: UIImage(named: "nav_vip")) { [weak self] in
- guard let self = self else { return }
- TSPurchaseVC.show(target: self) {}
- }
- return vipBtn
- }()
-
- lazy var navBarView: TSBaseNavContentBarView = {
- let navBarView = TSBaseNavContentBarView()
- let label = UILabel.createLabel(text: "History".localized,font: .font(name: .Pacifico, size: 24))
- navBarView.barView.addSubview(label)
- label.snp.makeConstraints { make in
- make.center.equalToSuperview()
- }
-
- navBarView.barView.addSubview(vipBtn)
- vipBtn.snp.makeConstraints { make in
- make.centerY.equalToSuperview()
- make.trailing.equalTo(-16)
- make.width.height.equalTo(32)
- }
-
- kMainAsync {
- label.applyGradient(colors: ["#F1D3AB".uiColor,"#E4A858".uiColor])
- }
-
- return navBarView
- }()
-
- //###################################### 集合视图 ######################################
- let collectionViewBtootm:CGFloat = 80
- let identifier = "TSAIListHistoryCell"
- lazy var collectionView: UICollectionView = {
- let layout = UICollectionViewFlowLayout()
- layout.scrollDirection = .vertical
-
- let itemW = (k_ScreenWidth-32.0-13.0-2.0)/2.0
- let itemH = kGetScaleHeight(originalSize: CGSize(width: 165.0, height: 220.0), width: itemW)
- layout.itemSize = CGSize(width: itemW, height: itemH)
- layout.minimumInteritemSpacing = 13
- layout.minimumLineSpacing = 16
- layout.sectionInset = UIEdgeInsets(top: 10, left: 16, bottom: k_Height_TabBar+20, right: 16)
-
- let collectionView = TSBaseCollectionView(frame: .zero, collectionViewLayout: layout)
- collectionView.delegate = self
- collectionView.dataSource = self
- collectionView.register(TSAIListHistoryCell.self, forCellWithReuseIdentifier: identifier)
- return collectionView
- }()
-
- lazy var pageNullView: TSPageNullView = {
- let pageNullView = TSPageNullView()
- pageNullView.isHidden = true
- return pageNullView
- }()
-
- override func createData() {
-
- }
-
- var navRightBtn = UIButton()
- override func createView() {
- addNormalNavBarView()
-
- // setPageTitle("History".localized)
- // navRightBtn = setNavigationItem("", imageName: "ai_delete", direction: .right, action: #selector(clickNavRight))
-
- navBarContentView.addSubview(navBarView)
- navBarView.snp.makeConstraints { make in
- make.edges.equalToSuperview()
- }
-
- contentView.addSubview(pageNullView)
- contentView.addSubview(collectionView)
- collectionView.snp.makeConstraints { make in
- make.edges.equalToSuperview()
- }
- }
-
- override func dealThings() {
- updateDataView()
- NotificationCenter.default.addObserver(self, selector: #selector(operationChanged(_:)), name: .kGenerateBasePhotoOperation, object: nil)
- NotificationCenter.default.addObserver(self, selector: #selector(updateDataView), name: .kAIPhotoDataChanged, object: nil)
-
- NotificationCenter.default.addObserver(self, selector: #selector(updateVipView), name: .kPurchaseDidChanged, object: nil)
- updateVipView()
- }
-
- @objc func operationChanged(_ notification: Notification) {
- if let userInfo = notification.userInfo as? [String: Any],let state = userInfo["state"] as? TSProgressState {
- if state.reloadNewData {
- self.updateDataView()
- }
- }
- }
-
- @objc func updateVipView() {
- kMainAsync{
- self.vipBtn.isHidden = PurchaseManager.default.isVip
- }
- }
-
- @objc func updateDataView(){
- dbHistory.getModelList { [weak self] array in
- guard let self = self else { return }
- listModelArray = array
- updateView()
- }
- // listModelArray = dbHistory.getModelList()
- // updateView()
- }
-
- func updateView() {
- collectionView.reloadData()
-
- navRightBtn.isHidden = listModelArray.count <= 0
- pageNullView.isHidden = listModelArray.count > 0
- }
-
- override func viewWillAppear(_ animated: Bool) {
- print("viewWillAppear")
- }
-
- @objc func clickNavRight() {
- TSCustomAlertController.show(in: self, config: TSCustomAlertController.AlertConfig(
- message: "Are you sure to delete all histories?".localized,
- cancelTitle: "Delete All".localized,
- cancelColor: .red,
- confirmTitle: "Retain".localized,
- confirmColor: .white,
- cancelAction: { [weak self] in
- guard let self = self else { return }
- self.removeAllHistoryList()
- self.updateDataView()
- }
- ))
- }
-
- }
- extension TSAIListHistoryVC: UICollectionViewDataSource ,UICollectionViewDelegate {
-
- public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
- return listModelArray.count
- }
-
- public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
-
- let cell = collectionView.dequeueReusableCell(withReuseIdentifier: identifier, for: indexPath)
- if let cell = cell as? TSAIListHistoryCell ,let dbModel = listModelArray.safeObj(At:indexPath.row){
- cell.dataModel = dbModel
- cell.buttonTapped = { [weak self] cmd in
- self?.handelCellCmd(cmd: cmd,indexPath: indexPath)
- }
- }
-
- return cell
- }
- public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
- if let dbModel = listModelArray.safeObj(At:indexPath.row){
-
- // //单个浏览
- // let detailsVC = TSAIPhotoDetailsVC()
- // detailsVC.infoModel = dbModel
- // detailsVC.deleteBlock = { [weak self] in
- // guard let self = self else { return }
- // removeDelete(index: indexPath.row)
- // }
- // kPresentModalVC(target: self, modelVC: detailsVC)
- // return
-
- //多个浏览
- var dataModelArray: [TSActionInfoModel] = []
- for itemModel in listModelArray {
- if itemModel.status == "success" || itemModel.modelType == .example {
- dataModelArray.append(itemModel)
- }
- }
-
- let vc = TSAIPhotoDetailsBrowserVC()
- vc.currentIndex = dataModelArray.firstIndex(of: dbModel) ?? 0
- vc.dataModelArray = dataModelArray
- vc.deleteBlock = { [weak self] model in
- guard let self = self else { return }
- TSRMShared.aiListDB.deleteListModel(id: model.id)
- updateDataView()
- }
- kPresentModalVC(target: self, modelVC: vc)
-
- }
- }
- }
- extension TSAIListHistoryVC{
- func handelCellCmd(cmd:String,indexPath: IndexPath){
- if let currentActionInfoModel = listModelArray.safeObj(At: indexPath.item){
- if cmd == "delete_task_expired" {
- TSRMShared.aiListDB.deleteListModel(id: currentActionInfoModel.id)
- updateDataView()
- }else if cmd == "delete_task_sensitive" {
- showDeleteErrorAlert(message: "Delete this error history?".localized, deleteHandler: { [weak self] in
- guard let self = self else { return }
- TSRMShared.aiListDB.deleteListModel(id: currentActionInfoModel.id)
- updateDataView()
- })
- }else if cmd == "delete" {
- TSCustomAlertController.show(in: self, config: TSCustomAlertController.AlertConfig(
- message: "Are you sure to delete?".localized,
- cancelTitle: "Delete".localized,
- cancelColor: .red,
- confirmTitle: "Retain".localized,
- confirmColor: .white,
- cancelAction: { [weak self] in
- guard let self = self else { return }
- removeDelete(index: indexPath.row)
- }
- ))
- }
- }
- }
-
- func removeDelete(index:Int){
- dbHistory.deleteListModel(index: index)
- updateDataView()
- }
-
- func removeAllHistoryList(){
- dbHistory.deleteAll()
- updateDataView()
- }
-
- var dbHistory:TSDBHistory{
- TSRMShared.aiListDB
- }
- }
- extension TSAIListHistoryVC{
- static func showPosition(){
- AppDelegate.tabbar?.changeSelectedIndex(index: 1)
- }
-
- }
|