jsdw_ios/QuickLocation/Section/Group/CreateGroup/CreateGroupViewModel.swift

106 lines
3.0 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// 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()
)
}
}