Files
suixinkan_uikit/suixinkan/Features/Payment/ViewModels/PaymentCollectionRecordViewModel.swift
汉秋 005349f8e6 模块化 AppStore 并完善素材管理与个人空间设置。
将会话、权限、位置、收款与排队存储拆分为独立模块,同步更新各 ViewModel 与单元测试,并补充素材管理 UI 与个人空间设置交互。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-13 11:01:08 +08:00

62 lines
1.6 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.

//
// 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] ?? []
)
}
}
}