Skip to content

Commit

Permalink
Refactor preview for OdometerView
Browse files Browse the repository at this point in the history
And fix SwiftLint warnings
  • Loading branch information
mikaelacaron committed Oct 28, 2024
1 parent bdead2b commit c560f22
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ struct EditMaintenanceEventView: View {
notes: "Changed engine oil"
)

var viewModel = DashboardViewModel(userUID: "user123")
let viewModel = DashboardViewModel(userUID: "user123")

EditMaintenanceEventView(
selectedEvent: $selectedEvent,
Expand Down
58 changes: 27 additions & 31 deletions Basic-Car-Maintenance/Shared/Odometer/Views/OdometerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit c560f22

Please sign in to comment.