From c560f22fe032c453c3f62426d972ed1f3dbda4df Mon Sep 17 00:00:00 2001 From: Mikaela Caron Date: Mon, 28 Oct 2024 12:35:25 -0400 Subject: [PATCH] Refactor preview for OdometerView And fix SwiftLint warnings --- .../Dashboard/Views/EditEventDetailView.swift | 2 +- .../Shared/Odometer/Views/OdometerView.swift | 58 +++++++++---------- .../Settings/Views/EditVehicleView.swift | 2 +- 3 files changed, 29 insertions(+), 33 deletions(-) diff --git a/Basic-Car-Maintenance/Shared/Dashboard/Views/EditEventDetailView.swift b/Basic-Car-Maintenance/Shared/Dashboard/Views/EditEventDetailView.swift index 0451cac1..5e33923d 100644 --- a/Basic-Car-Maintenance/Shared/Dashboard/Views/EditEventDetailView.swift +++ b/Basic-Car-Maintenance/Shared/Dashboard/Views/EditEventDetailView.swift @@ -100,7 +100,7 @@ struct EditMaintenanceEventView: View { notes: "Changed engine oil" ) - var viewModel = DashboardViewModel(userUID: "user123") + let viewModel = DashboardViewModel(userUID: "user123") EditMaintenanceEventView( selectedEvent: $selectedEvent, diff --git a/Basic-Car-Maintenance/Shared/Odometer/Views/OdometerView.swift b/Basic-Car-Maintenance/Shared/Odometer/Views/OdometerView.swift index b0d14aa3..68096fde 100644 --- a/Basic-Car-Maintenance/Shared/Odometer/Views/OdometerView.swift +++ b/Basic-Car-Maintenance/Shared/Odometer/Views/OdometerView.swift @@ -113,52 +113,48 @@ struct OdometerView: View { } #Preview { - var viewModel = OdometerViewModel(userUID: nil) + let viewModel = OdometerViewModel(userUID: nil) let firstCar = createVehicle(id: "id1", name: "My 1st car") - viewModel.vehicles.append(firstCar) - let secondCar = createVehicle(id: "id2", name: "2nd Car") - viewModel.vehicles.append(secondCar) + + viewModel.vehicles.append(contentsOf: [firstCar, secondCar]) let firstReading = createReading(vehicleID: secondCar.id!, date: "2024/10/18", distance: 20) - viewModel.readings.append(firstReading) - let secondReading = createReading(vehicleID: firstCar.id!, date: "2024/10/15", distance: 1000) - viewModel.readings.append(secondReading) let thirdReading = createReading(vehicleID: firstCar.id!, date: "2024/10/13", distance: 10) - viewModel.readings.append(thirdReading) + viewModel.readings.append(contentsOf: [firstReading, secondReading, thirdReading]) return OdometerView(viewModel: viewModel) .environment(ActionService.shared) -} - -private func createVehicle(id: String, name: String) -> Vehicle { - Vehicle(id: id, - userID: nil, - name: name, - make: "", - model: "", - year: nil, - color: nil, - vin: nil, - licensePlateNumber: nil) -} + + func createVehicle(id: String, name: String) -> Vehicle { + Vehicle(id: id, + userID: nil, + name: name, + make: "", + model: "", + year: nil, + color: nil, + vin: nil, + licensePlateNumber: nil) + } -private func createReading(vehicleID: String, date: String, distance: Int) -> OdometerReading { - let formatter = DateFormatter() - formatter.dateFormat = "yyyy/MM/dd" - let firstDate = formatter.date(from: date)! - return OdometerReading(id: UUID().uuidString, - userID: "", - date: firstDate, - distance: distance, - isMetric: false, - vehicleID: vehicleID) + func createReading(vehicleID: String, date: String, distance: Int) -> OdometerReading { + let formatter = DateFormatter() + formatter.dateFormat = "yyyy/MM/dd" + let firstDate = formatter.date(from: date)! + return OdometerReading(id: UUID().uuidString, + userID: "", + date: firstDate, + distance: distance, + isMetric: false, + vehicleID: vehicleID) + } } diff --git a/Basic-Car-Maintenance/Shared/Settings/Views/EditVehicleView.swift b/Basic-Car-Maintenance/Shared/Settings/Views/EditVehicleView.swift index 77b9d77b..4fcb9e45 100644 --- a/Basic-Car-Maintenance/Shared/Settings/Views/EditVehicleView.swift +++ b/Basic-Car-Maintenance/Shared/Settings/Views/EditVehicleView.swift @@ -139,7 +139,7 @@ struct EditVehicleView: View, Observable { vin: "5YJSA1E26JF123456", licensePlateNumber: "ABC123" ) - var viewModel = SettingsViewModel(authenticationViewModel: AuthenticationViewModel()) + let viewModel = SettingsViewModel(authenticationViewModel: AuthenticationViewModel()) EditVehicleView(selectedVehicle: $selectedVehicle, viewModel: viewModel)