Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add VehicleDetailView #362

Merged
merged 4 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Basic-Car-Maintenance/Shared/Localizable.xcstrings
Original file line number Diff line number Diff line change
Expand Up @@ -5528,6 +5528,9 @@
}
}
},
"Vehicle Details" : {
"comment" : "Label about vehicle details."
},
"Vehicle License Plate Number" : {
"localizations" : {
"be" : {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ struct EditVehicleView: View, Observable {
@Binding var selectedVehicle: Vehicle?
var viewModel: SettingsViewModel

/// closure to update the values passed in, and set them to all the state properties
var onVehicleUpdated: ((Vehicle) -> Void)?

@Environment(\.dismiss) var dismiss

@State private var name = ""
Expand Down Expand Up @@ -98,6 +101,9 @@ struct EditVehicleView: View, Observable {
vehicle.id = selectedVehicle.id
Task {
await viewModel.updateVehicle(vehicle)
if let onVehicleUpdated {
onVehicleUpdated(vehicle)
}
dismiss()
}
}
Expand Down
65 changes: 38 additions & 27 deletions Basic-Car-Maintenance/Shared/Settings/Views/SettingsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ struct SettingsView: View {
@State private var selectedVehicle: Vehicle?
@State private var isShowingEditVehicleView = false

@State private var isShowingVehicleDetailView = false

private let appVersion = "Version \(Bundle.main.versionNumber) (\(Bundle.main.buildNumber))"

init(authenticationViewModel: AuthenticationViewModel) {
Expand Down Expand Up @@ -90,39 +92,45 @@ struct SettingsView: View {

Section {
ForEach(viewModel.vehicles) { vehicle in
VStack(alignment: .leading, spacing: 2) {
Text("\(vehicle.name)")
.fontWeight(.bold)
.font(.headline)

Group {
HStack {
if let year = vehicle.year, !year.isEmpty {
Text(year)
Button {
selectedVehicle = vehicle
isShowingVehicleDetailView = true
} label: {
VStack(alignment: .leading, spacing: 2) {
Text("\(vehicle.name)")
.fontWeight(.bold)
.font(.headline)

Group {
HStack {
if let year = vehicle.year, !year.isEmpty {
Text(year)
}

Text(vehicle.make)

Text(vehicle.model)
}

Text(vehicle.make)
if let licensePlateNumber =
vehicle.licensePlateNumber,
!licensePlateNumber.isEmpty {
Text("Plate: \(licensePlateNumber)")
}

Text(vehicle.model)
}

if let licensePlateNumber =
vehicle.licensePlateNumber,
!licensePlateNumber.isEmpty {
Text("Plate: \(licensePlateNumber)")
}

if let vin = vehicle.vin, !vin.isEmpty {
Text("VIN: \(vin)")
if let vin = vehicle.vin, !vin.isEmpty {
Text("VIN: \(vin)")
}

if let color = vehicle.color, !color.isEmpty {
Text("Color: \(color)")
}
}

if let color = vehicle.color, !color.isEmpty {
Text("Color: \(color)")
}
.font(.callout)
.foregroundStyle(.secondary)
}
.font(.callout)
.foregroundStyle(.secondary)
}
.buttonStyle(.plain)
.swipeActions {
Button(role: .destructive) {
Task {
Expand Down Expand Up @@ -232,6 +240,9 @@ struct SettingsView: View {
}
}
}
.navigationDestination(isPresented: $isShowingVehicleDetailView) {
VehicleDetailView(selectedVehicle: $selectedVehicle, viewModel: viewModel)
}
.sheet(isPresented: $isShowingEditVehicleView) {
EditVehicleView(selectedVehicle: $selectedVehicle, viewModel: viewModel)
}
Expand Down
121 changes: 121 additions & 0 deletions Basic-Car-Maintenance/Shared/Settings/Views/VehicleDetailView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
//
// VehicleDetailView.swift
// Basic-Car-Maintenance
//
// https://github.com/mikaelacaron/Basic-Car-Maintenance
// See LICENSE for license information.
//

import SwiftUI

struct VehicleDetailView: View {

@Binding var selectedVehicle: Vehicle?
var viewModel: SettingsViewModel

@State private var name = ""
@State private var make = ""
@State private var model = ""
@State private var year = ""
@State private var color = ""
@State private var VIN = ""
@State private var licensePlateNumber = ""

@State private var isShowingEditVehicleView = false

var body: some View {
NavigationStack {
Form {
Section {
Text(name)
} header: {
Text("Name")
}

Section {
Text(make)
} header: {
Text("Make")
}

Section {
Text(model)
} header: {
Text("Model")
}

Section {
Text(year)
} header: {
Text("Year")
}

Section {
Text(color)
} header: {
Text("Color")
}

Section {
Text(VIN)
} header: {
Text("VIN")
}

Section {
Text(licensePlateNumber)
} header: {
Text("License Plate Number")
}
}
.analyticsView("\(Self.self)")
.onAppear {
guard let selectedVehicle else { return }
setVehicleValues(selectedVehicle)
}
.toolbar {
ToolbarItem {
Button {
isShowingEditVehicleView.toggle()
} label: {
Text("Edit")
}
}
}
.navigationTitle(Text("Vehicle Details", comment: "Label about vehicle details."))
.sheet(isPresented: $isShowingEditVehicleView) {
EditVehicleView(
selectedVehicle: $selectedVehicle,
viewModel: viewModel,
onVehicleUpdated: setVehicleValues
)
}
}
}

private func setVehicleValues(_ vehicle: Vehicle) {
self.name = vehicle.name
self.make = vehicle.make
self.model = vehicle.model
self.year = vehicle.year ?? ""
self.color = vehicle.color ?? ""
self.VIN = vehicle.vin ?? ""
self.licensePlateNumber = vehicle.licensePlateNumber ?? ""
}
}

#Preview {
@Previewable @State var selectedVehicle: Vehicle? = Vehicle(
id: UUID().uuidString,
name: "My Car",
make: "Ford",
model: "F-150",
year: "2020",
color: "Red",
vin: "5YJSA1E26JF123456",
licensePlateNumber: "ABC123"
)
var viewModel = SettingsViewModel(authenticationViewModel: AuthenticationViewModel())

VehicleDetailView(selectedVehicle: $selectedVehicle, viewModel: viewModel)
}
Loading