将会话、权限、位置、收款与排队存储拆分为独立模块,同步更新各 ViewModel 与单元测试,并补充素材管理 UI 与个人空间设置交互。 Co-authored-by: Cursor <cursoragent@cursor.com>
62 lines
1.6 KiB
Swift
62 lines
1.6 KiB
Swift
//
|
|
// PaymentCollectionRecordViewModel.swift
|
|
// suixinkan
|
|
//
|
|
|
|
import Foundation
|
|
|
|
/// 收款记录 ViewModel,拉取近 7 天记录并按日分组展示。
|
|
final class PaymentCollectionRecordViewModel {
|
|
|
|
private(set) var loading = false
|
|
private(set) var groups: [RepaymentCollectionRecordGroup] = []
|
|
|
|
var onStateChange: (() -> Void)?
|
|
|
|
private let appStore: AppStore
|
|
|
|
init(appStore: AppStore = .shared) {
|
|
self.appStore = appStore
|
|
}
|
|
|
|
func loadRecord(api: PaymentAPI) async {
|
|
guard !loading else { return }
|
|
loading = true
|
|
notifyStateChange()
|
|
defer {
|
|
loading = false
|
|
notifyStateChange()
|
|
}
|
|
|
|
let scenicId = appStore.session.currentScenicId
|
|
guard scenicId > 0 else {
|
|
groups = []
|
|
return
|
|
}
|
|
|
|
do {
|
|
let response = try await api.collectionRecord(scenicId: scenicId)
|
|
groups = PaymentRecordGrouper.group(response: response)
|
|
} catch {
|
|
groups = []
|
|
}
|
|
}
|
|
|
|
private func notifyStateChange() {
|
|
onStateChange?()
|
|
}
|
|
}
|
|
|
|
/// 收款记录分组逻辑,对齐 Android `PaymentCollectionRecordViewModel.getRecord`。
|
|
enum PaymentRecordGrouper {
|
|
static func group(response: RepaymentCollectionRecordResponse) -> [RepaymentCollectionRecordGroup] {
|
|
let mapByDate = Dictionary(grouping: response.list, by: \.createDate)
|
|
return response.analyse.map { analyseItem in
|
|
RepaymentCollectionRecordGroup(
|
|
analyse: analyseItem,
|
|
items: mapByDate[analyseItem.date] ?? []
|
|
)
|
|
}
|
|
}
|
|
}
|