106 lines
3.0 KiB
Swift
106 lines
3.0 KiB
Swift
//
|
||
// CreateGroupViewModel.swift
|
||
// QuickLocation
|
||
//
|
||
// Created by 八条 on 2026/6/3.
|
||
//
|
||
|
||
import RxSwift
|
||
import RxCocoa
|
||
import RxDataSources
|
||
import SwiftyUserDefaults
|
||
|
||
typealias GroupTagListSectionModel = SectionModel<String, String>
|
||
|
||
class CreateGroupViewModel {
|
||
struct Input {
|
||
|
||
}
|
||
struct Output {
|
||
var sectionedItems: Observable<[GroupTagListSectionModel]>
|
||
}
|
||
|
||
let input: Input
|
||
let output: Output
|
||
var disposeBag = DisposeBag()
|
||
|
||
private let sectionedItems = PublishSubject<[GroupTagListSectionModel]>()
|
||
|
||
private let tagList = ["私密", "游戏", "运动", "美食",
|
||
"自驾", "聚会", "旅行", "学习"]
|
||
|
||
var selectedTagList: [String] = [] {
|
||
didSet {
|
||
loadData()
|
||
}
|
||
}
|
||
|
||
var iconIndex = 1
|
||
let groupName = BehaviorRelay<String>(value: "")
|
||
let groupDesc = BehaviorRelay<String>(value: "")
|
||
|
||
// MARK: - Cell点击
|
||
lazy var cellAction: Action<String, Void> = { this in
|
||
return Action { tag in
|
||
if let idx = this.selectedTagList.firstIndex(of: tag) {
|
||
this.selectedTagList.remove(at: idx)
|
||
} else {
|
||
this.selectedTagList.append(tag)
|
||
}
|
||
return .empty()
|
||
}
|
||
}(self)
|
||
|
||
/// 当前tag是否选中
|
||
func isSelected(tag: String) -> Bool {
|
||
return selectedTagList.first(where: { tag == $0 }) != nil
|
||
}
|
||
|
||
// MARK: - 加载数据
|
||
func loadData() {
|
||
sectionedItems.onNext(tagList.mapSection())
|
||
}
|
||
|
||
// MARK: - Request
|
||
func requestCreateGroup() {
|
||
let name = groupName.value.trimmingCharacters(in: .whitespacesAndNewlines)
|
||
guard !name.isEmpty else {
|
||
DLToast.show(text: "请输入圈子名称")
|
||
return
|
||
}
|
||
|
||
let desc = groupDesc.value.trimmingCharacters(in: .whitespacesAndNewlines)
|
||
guard !desc.isEmpty else {
|
||
DLToast.show(text: "请输入圈子描述")
|
||
return
|
||
}
|
||
|
||
DLToast.showLoading()
|
||
GroupService.operate(opType: "create",
|
||
requestData: ["group_name": name,
|
||
"icon_index": iconIndex,
|
||
"description": desc,
|
||
"labels": selectedTagList]).subscribe(onNext: { response in
|
||
DLToast.showSuccess(text: "创建成功") {
|
||
AppRouter.shared.popOrDismiss()
|
||
}
|
||
}, onError: { (error) in
|
||
guard let code = error.underlyingError?.code else { return }
|
||
if code == 20009 { // "您创建的圈子个数已达上限,请升级会员等级"
|
||
CreateGroupVipPopView.show()
|
||
}
|
||
else {
|
||
DLToast.show(text: error.localizedDescription)
|
||
}
|
||
}).disposed(by: disposeBag)
|
||
}
|
||
|
||
// MARK: - init
|
||
init() {
|
||
input = Input()
|
||
output = Output(
|
||
sectionedItems: sectionedItems.asObservable()
|
||
)
|
||
}
|
||
}
|