// // CreateGroupVC.swift // QuickLocation // // Created by 八条 on 2026/6/2. // import UIKit import RxSwift import RxCocoa import RxDataSources class CreateGroupVC: BaseViewController { fileprivate var rootView: CreateGroupView! override func loadView() { rootView = CreateGroupView(frame: UIScreen.main.bounds) view = rootView } private var viewModel = CreateGroupViewModel() override func viewDidLoad() { super.viewDidLoad() view.backgroundColor = UIColor(hexStr: "#FAFAFA") bindViewModel() reactiveAction() viewModel.loadData() viewModel.requestGroupTemplates() } private func reactiveAction() { rootView.iconCarousel.onSelectTemplate = { [weak self] template in self?.viewModel.groupType = template.type self?.rootView.typeNameLab.text = template.name } rootView.submitBtn.rx.tap.subscribe(onNext: { [weak self] _ in self?.viewModel.requestCreateGroup() }).disposed(by: disposeBag) rootView.onConfirmAddTag = { [weak self] text in guard let self = self else { return } switch self.viewModel.addCustomTag(text) { case .success: self.rootView.dismissAddTagSheet() case .empty: DLToast.show(text: "请输入标签") case .duplicate: DLToast.show(text: "标签已存在") } } } private func bindViewModel() { rootView.groupNameTF.rx.text.orEmpty .bind(to: viewModel.groupName) .disposed(by: disposeBag) rootView.groupContentTV.rx.text.orEmpty .bind(to: viewModel.groupDesc) .disposed(by: disposeBag) viewModel.output.sectionedItems .bind(to: rootView.tagView.rx.items(dataSource: dataSource)) .disposed(by: disposeBag) viewModel.output.groupTemplates .observe(on: MainScheduler.instance) .subscribe(onNext: { [weak self] templates in self?.rootView.configureGroupTemplates(templates) }) .disposed(by: disposeBag) viewModel.output.sectionedItems .observe(on: MainScheduler.instance) .subscribe(onNext: { [weak self] _ in DispatchQueue.main.async { self?.rootView.updateTagViewHeight() } }) .disposed(by: disposeBag) rootView.tagView.rx.itemSelected .subscribe(onNext: { [weak self] indexPath in guard let self = self else { return } self.rootView.tagView.deselectItem(at: indexPath, animated: false) let tag = self.dataSource[indexPath] if tag == CreateGroupViewModel.addTagToken { self.rootView.showAddTagSheet() } else { self.viewModel.cellAction.execute(tag) } }) .disposed(by: disposeBag) rootView.tagView.rx.setDelegate(self) .disposed(by: disposeBag) } // MARK: - dataSource private lazy var dataSource: RxCollectionViewSectionedReloadDataSource = { RxCollectionViewSectionedReloadDataSource { datasource, collectionView, indexPath, item in let cell: TagCell = collectionView.dequeueReusableCell(for: indexPath) if item == CreateGroupViewModel.addTagToken { cell.configureAsAdd() } else { cell.configure(item, isSelected: self.viewModel.isSelected(tag: item)) } return cell } }() } extension CreateGroupVC: UICollectionViewDelegateFlowLayout { func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { guard dataSource.sectionModels.indices.contains(indexPath.section), dataSource.sectionModels[indexPath.section].items.indices.contains(indexPath.item) else { return CGSize(width: 52, height: 28) } return TagCell.preferredSize(for: dataSource[indexPath]) } }