-
-
Notifications
You must be signed in to change notification settings - Fork 149
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #271 from mikaelacaron/211-update-odometer-reading
Update Odometer Reading
- Loading branch information
Showing
5 changed files
with
161 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
Basic-Car-Maintenance/Shared/Odometer/Views/EditOdometerReadingView.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
// | ||
// EditOdometerReadingView.swift | ||
// Basic-Car-Maintenance | ||
// | ||
// Created by Mikaela Caron on 11/25/23. | ||
// | ||
|
||
import SwiftUI | ||
|
||
struct EditOdometerReadingView: View { | ||
let selectedReading: OdometerReading | ||
|
||
let vehicles: [Vehicle] | ||
let updateTapped: (OdometerReading) -> Void | ||
|
||
@State private var date = Date() | ||
@State private var isMetric = false | ||
@State private var distance = 0 | ||
|
||
@Environment(\.dismiss) private var dismiss | ||
|
||
var body: some View { | ||
NavigationStack { | ||
Form { | ||
Section { | ||
HStack { | ||
TextField("Distance", value: $distance, format: .number) | ||
|
||
Picker(selection: $isMetric) { | ||
Text("Miles").tag(false) | ||
Text("Kilometers").tag(true) | ||
} label: { | ||
Text("Preferred units", | ||
comment: "Label for units selected when adding an odometer reading") | ||
} | ||
.pickerStyle(.segmented) | ||
} | ||
} | ||
|
||
Section { | ||
if let vehicleName = vehicles | ||
.filter({ $0.id == selectedReading.vehicleID }).first?.name { | ||
Text(vehicleName) | ||
.opacity(0.3) | ||
} | ||
} header: { | ||
Text("Vehicle") | ||
} | ||
|
||
DatePicker(selection: $date, displayedComponents: .date) { | ||
Text("Date", comment: "Date picker label") | ||
} | ||
.dynamicTypeSize(...DynamicTypeSize.accessibility2) | ||
} | ||
.onAppear { | ||
setEditReadingValues(selectedReading) | ||
} | ||
.navigationTitle(Text("Edit Reading", | ||
comment: "Title for form when editing an odometer reading")) | ||
.toolbar { | ||
ToolbarItem(placement: .topBarLeading) { | ||
Button { | ||
dismiss() | ||
} label: { | ||
Text("Cancel") | ||
} | ||
} | ||
|
||
ToolbarItem { | ||
Button { | ||
let reading = OdometerReading(id: selectedReading.id, | ||
date: date, | ||
distance: distance, | ||
isMetric: isMetric, | ||
vehicleID: selectedReading.vehicleID) | ||
updateTapped(reading) | ||
} label: { | ||
Text("Update", | ||
comment: "Label for submit button on form to update an existing entry") | ||
} | ||
.disabled(distance < 0) | ||
} | ||
} | ||
} | ||
.analyticsView("\(Self.self)") | ||
} | ||
|
||
func setEditReadingValues(_ reading: OdometerReading) { | ||
self.date = reading.date | ||
self.isMetric = reading.isMetric | ||
self.distance = reading.distance | ||
} | ||
} | ||
|
||
#Preview { | ||
EditOdometerReadingView( | ||
selectedReading: OdometerReading(date: Date(), | ||
distance: 0, | ||
isMetric: false, | ||
vehicleID: ""), | ||
vehicles: []) { _ in } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters