AutoCat/AutoCat/Screens/ReportScreen/ReportScreen.swift

131 lines
5.4 KiB
Swift

//
// ReportScreen.swift
// AutoCat
//
// Created by Selim Mustafaev on 16.11.2024.
// Copyright © 2024 Selim Mustafaev. All rights reserved.
//
import SwiftUI
import AutoCatCore
struct ReportScreen: View {
@State var viewModel: ReportViewModel
var body: some View {
Form {
Section {
LabeledContent {
Text(viewModel.vehicle.brand?.name?.original ?? "")
} label: {
AsyncImage(url: URL(string: viewModel.vehicle.brand?.logo ?? "")) { phase in
phase.image?
.resizable()
.aspectRatio(contentMode: .fit)
.frame(height: 32)
}
}
}
Section("General") {
LabeledContent("Year", value: String(viewModel.vehicle.year))
LabeledContent("Color", value: viewModel.vehicle.color ?? "")
LabeledContent("Category", value: viewModel.vehicle.category ?? "")
LabeledContent("Steering wheel position", value: viewModel.steerignWheelPosition)
LabeledContent("Japanese", value: viewModel.isJapanese)
}
Section("Identifiers") {
LabeledContent("Plate number", value: viewModel.plateNumber)
LabeledContent("VIN", value: viewModel.vehicle.vin1 ?? "")
LabeledContent("STS", value: viewModel.vehicle.sts ?? "")
LabeledContent("PTS", value: viewModel.vehicle.pts ?? "")
}
Section("Engine") {
LabeledContent("Number", value: viewModel.vehicle.engine?.number ?? "")
LabeledContent("Fuel type", value: viewModel.vehicle.engine?.fuelType ?? "")
LabeledContent("Volume (cm³)", value: String(viewModel.vehicle.engine?.volume ?? 0))
LabeledContent("Power (HP)", value: String(viewModel.vehicle.engine?.powerHp ?? 0))
LabeledContent("Power (kw)", value: String(viewModel.vehicle.engine?.powerKw ?? 0))
}
Section("History") {
LabeledContent("Events", value: String(viewModel.vehicle.events.count))
.navigationLink(isActive: !viewModel.vehicle.events.isEmpty,
onTap: viewModel.openEvents)
LabeledContent("OSAGO", value: String(viewModel.vehicle.osagoContracts.count))
.navigationLink(isActive: !viewModel.vehicle.osagoContracts.isEmpty,
onTap: viewModel.openOsago)
LabeledContent("Owners", value: String(viewModel.vehicle.ownershipPeriods.count))
.navigationLink(isActive: !viewModel.vehicle.ownershipPeriods.isEmpty,
onTap: viewModel.openOwners)
LabeledContent("Photos", value: String(viewModel.vehicle.photos.count))
.navigationLink(isActive: !viewModel.vehicle.photos.isEmpty,
onTap: viewModel.openPhotoGallery)
LabeledContent("Ads", value: String(viewModel.vehicle.ads.count))
.navigationLink(isActive: !viewModel.vehicle.ads.isEmpty,
onTap: viewModel.openAds)
LabeledContent("Notes", value: String(viewModel.vehicle.notes.count))
.navigationLink(isActive: !viewModel.vehicle.notes.isEmpty,
onTap: viewModel.openNotes)
}
if viewModel.showDebugInfo {
Section("Debug info") {
makeDebugInfoCell(title: "Avtocod", model: viewModel.vehicle.debugInfo?.autocod)
makeDebugInfoCell(title: "Vin01 (VIN)", model: viewModel.vehicle.debugInfo?.vin01vin)
makeDebugInfoCell(title: "Vin01 (base)", model: viewModel.vehicle.debugInfo?.vin01base)
makeDebugInfoCell(title: "Vin01 (history)", model: viewModel.vehicle.debugInfo?.vin01history)
makeDebugInfoCell(title: "Nomerogram", model: viewModel.vehicle.debugInfo?.nomerogram)
}
}
Section {
Button("Check GB") {
Task { await viewModel.checkGB() }
}
}
}
.onAppear {
Task { await viewModel.onAppear() }
}
.hud($viewModel.hud)
.toolbar {
if let link = viewModel.shareLink {
ShareLink(item: link)
}
}
}
@ViewBuilder
func makeDebugInfoCell(title: String, model: DebugInfoEntryDto?) -> some View {
let color: Color = switch model?.status {
case .success: .green
case .warning: .orange
case .error: .red
case .none: .gray
}
let cell = LabeledContent(title) {
Circle()
.fill(color)
.frame(width: 16, height: 16)
}
if let model, let error = model.error {
cell.navigationLink {
viewModel.showAlert(text: error, status: model.status)
}
} else {
cell
}
}
}
#Preview {
ReportScreen(viewModel: .init(vehicle: .preview, isPersistent: false))
}