jsdw_ios/QuickLocation/Section/Schedule/CreateSchedule/CreateScheduleVM.swift

115 lines
3.3 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.

//
// CreateScheduleVM.swift
// QuickLocation
//
// Created by on 2026/6/24.
//
import RxSwift
import RxCocoa
import RxDataSources
// MARK: -
struct SchedulePointItem: IdentifiableType, Equatable {
let identity: String = UUID().uuidString
var locationName: String = ""
var address: String = ""
var latitude: Double = 0
var longitude: Double = 0
var province: String = ""
var city: String = ""
var district: String = ""
var street: String = ""
var country: String = ""
var formatted_address: String = ""
var expectedTime: Date?
var remark: String = ""
}
typealias SchedulePointSection = SectionModel<String, SchedulePointItem>
class CreateScheduleVM {
let disposeBag = DisposeBag()
// MARK: - Input
let addPointTapped = PublishSubject<Void>()
let deletePointAt = PublishSubject<Int>()
let updatePoint = PublishSubject<(index: Int, item: SchedulePointItem)>()
let selectedDate = BehaviorRelay<Date>(value: Date())
var selectedGroupKeys: [String] = []
///
func toggleGroupKey(_ key: String) {
if let idx = selectedGroupKeys.firstIndex(of: key) {
selectedGroupKeys.remove(at: idx)
} else {
selectedGroupKeys.append(key)
}
}
/// key
func removeGroupKey(_ key: String) {
selectedGroupKeys.removeAll { $0 == key }
}
// MARK: - Output
let pointsRelay: BehaviorRelay<[SchedulePointItem]>
let dateString: Observable<String>
init() {
pointsRelay = BehaviorRelay<[SchedulePointItem]>(value: [
SchedulePointItem()
])
dateString = selectedDate
.map {
let fmt = DateFormatter()
fmt.dateFormat = "yyyy年MM月dd日"
return fmt.string(from: $0)
}
//
addPointTapped
.withLatestFrom(pointsRelay)
.map { list in
var new = list
new.append(SchedulePointItem())
return new
}
.bind(to: pointsRelay)
.disposed(by: disposeBag)
//
deletePointAt
.withLatestFrom(pointsRelay) { ($1, $0) }
.subscribe(onNext: { list, idx in
guard list.count > 1, idx < list.count else { return }
var new = list
new.remove(at: idx)
self.pointsRelay.accept(new)
})
.disposed(by: disposeBag)
//
updatePoint
.withLatestFrom(pointsRelay) { ($1, $0) }
.subscribe(onNext: { list, update in
guard update.index < list.count else { return }
var new = list
new[update.index] = update.item
self.pointsRelay.accept(new)
})
.disposed(by: disposeBag)
}
/// LocationPicker
func updatePointLocation(index: Int, name: String, address: String) {
var list = pointsRelay.value
guard index < list.count else { return }
list[index].locationName = name
list[index].address = address
pointsRelay.accept(list)
}
}