import SwiftUI /// 我的比价记录(每次比价的概要,含成功/失败)。数据由安卓端比价后上报,iOS 只读。 struct CompareRecordsView: View { @State private var vm = CompareRecordsViewModel() var body: some View { Group { if vm.isLoading && vm.records.isEmpty { ProgressView().frame(maxWidth: .infinity, maxHeight: .infinity) } else if vm.loaded && vm.records.isEmpty { ContentUnavailableView { Label("还没有比价记录", systemImage: "list.bullet.rectangle") } description: { Text(vm.errorMessage ?? "在安卓 App 上完成比价后,记录会自动同步到这里") } } else { List { ForEach(vm.records) { record in CompareRecordRow(record: record) .listRowSeparator(.hidden) } if vm.hasMore { HStack { Spacer(); ProgressView(); Spacer() } .listRowSeparator(.hidden) .onAppear { Task { await vm.loadMore() } } } } .listStyle(.plain) .refreshable { await vm.loadFirst() } } } .navigationTitle("我的比价记录") .navigationBarTitleDisplayMode(.inline) .task { if !vm.loaded { await vm.loadFirst() } } } } private struct CompareRecordRow: View { let record: ComparisonRecordDTO var body: some View { VStack(alignment: .leading, spacing: 8) { HStack { Text(record.storeName ?? "比价记录") .font(.headline) .lineLimit(1) Spacer() Text(AppDate.friendly(record.createdAt)) .font(.caption) .foregroundStyle(.secondary) } if record.status == "success" { HStack(spacing: 6) { if let sp = record.sourcePlatformName, let spc = record.sourcePriceCents { Text("\(sp) \(spc.centsToYuanText)") .strikethrough(record.isSourceBest != true) .foregroundStyle(.secondary) } Image(systemName: "arrow.right").font(.caption2).foregroundStyle(.secondary) if let bp = record.bestPlatformName, let bpc = record.bestPriceCents { Text("\(bp) \(bpc.centsToYuanText)").bold() } } .font(.subheadline) if let saved = record.savedAmountCents, saved > 0 { Text("省 \(saved.centsToYuanText)") .font(.subheadline.bold()) .foregroundStyle(AppColors.success) } else if record.isSourceBest == true { Text("源平台已是最低价") .font(.caption) .foregroundStyle(.secondary) } } else { Text(record.information ?? "本次比价未完成") .font(.subheadline) .foregroundStyle(.secondary) } } .padding(.vertical, 6) } }