-
-
Notifications
You must be signed in to change notification settings - Fork 149
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
5 Edit Vehicle #269
5 Edit Vehicle #269
Changes from all commits
6b91caf
8942178
5594ab5
97dfae3
885c0c7
6636651
74d19bc
76a8479
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// | ||
// EditVehicleEvent.swift | ||
// Basic-Car-Maintenance | ||
// | ||
// Created by Traton Gossink on 11/18/23. | ||
// | ||
|
||
import FirebaseFirestoreSwift | ||
import Foundation | ||
|
||
struct EditVehicleEvent: Codable, Identifiable, Hashable { | ||
@DocumentID var id: String? | ||
var userID: String? | ||
let name: String | ||
let make: String | ||
let model: String | ||
let year: String | ||
let color: String | ||
let VIN: String | ||
let licenseplatenumber: String | ||
var vehicle: Vehicle? | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,8 @@ | |
var contributors: [Contributor]? | ||
|
||
var vehicles = [Vehicle]() | ||
var errorMessage: String = "" | ||
var showErrorAlert = false | ||
|
||
var sortedContributors: [Contributor] { | ||
guard let contributors = contributors, !contributors.isEmpty else { | ||
|
@@ -88,6 +90,30 @@ | |
} | ||
} | ||
|
||
///Updates users vehicle that is being edited. Fetches selected vehicle from Firestore and saves to that selected vehicle. | ||
Check warning on line 93 in Basic-Car-Maintenance/Shared/Settings/ViewModels/SettingsViewModel.swift GitHub Actions / SwiftLint
|
||
func updateEvent(_ editVehicleEvent: EditVehicleEvent) async { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You shouldn't be making a new model |
||
|
||
if let uid = authenticationViewModel.user?.uid { | ||
guard let id = editVehicleEvent.id else { return } | ||
var eventToUpdate = editVehicleEvent | ||
eventToUpdate.userID = uid | ||
do { | ||
try Firestore | ||
.firestore() | ||
.collection(FirestoreCollection.vehicles) | ||
.document(id) | ||
.setData(from: eventToUpdate) | ||
} catch { | ||
showErrorAlert.toggle() | ||
errorMessage = error.localizedDescription | ||
} | ||
} | ||
|
||
AnalyticsService.shared.logEvent(.maintenanceUpdate) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not a |
||
|
||
await self.getVehicles() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You don't need the |
||
} | ||
|
||
/// Fetches the user's vehicles from Firestore based on their unique user ID. | ||
func getVehicles() async { | ||
if let uid = authenticationViewModel.user?.uid { | ||
|
@@ -110,7 +136,7 @@ | |
} | ||
} | ||
} | ||
|
||
/// Deletes a vehicle from both Firestore and the local ``SettingsViewModel/vehicles`` array. | ||
/// | ||
/// - Parameter vehicle: The vehicle to be deleted. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
// | ||
// EditVehicleView.swift | ||
// Basic-Car-Maintenance | ||
// | ||
// Created by Traton Gossink on 11/6/23. | ||
// | ||
|
||
import SwiftUI | ||
|
||
struct EditVehicleView: View, Observable { | ||
@Binding var selectedEvent: EditVehicleEvent? | ||
var viewModel: SettingsViewModel | ||
@State private var selectedVehicle: Vehicle? | ||
@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 = "" | ||
@Environment(\.dismiss) var dismiss | ||
|
||
var body: some View { | ||
NavigationStack { | ||
Form { | ||
Section { | ||
TextField("Name", text: $name) | ||
} header: { | ||
Text("Name") | ||
} | ||
Section { | ||
TextField("Make", text: $make) | ||
} header: { | ||
Text("Make") | ||
} | ||
|
||
Section { | ||
TextField("Model", text: $model) | ||
} header: { | ||
Text("Model") | ||
} | ||
|
||
Section { | ||
TextField("Year", text: $year) | ||
} header: { | ||
Text("Year") | ||
} | ||
|
||
Section { | ||
TextField("Color", text: $color) | ||
} header: { | ||
Text("Color") | ||
} | ||
Section { | ||
TextField("VIN", text: $VIN) | ||
} header: { | ||
Text("VIN") | ||
} | ||
Section { | ||
TextField("License Plate Number", text: $licensePlateNumber) | ||
} header: { | ||
Text("License Plate Number") | ||
} | ||
} | ||
.analyticsView("\(Self.self)") | ||
.onAppear { | ||
guard let selectedEvent = selectedEvent else { return } | ||
setEditVehicleEventValues(event: selectedEvent) | ||
} | ||
.navigationTitle(Text("Update Vehicle Info")) | ||
.toolbar { | ||
ToolbarItem(placement: .topBarLeading) { | ||
Button { | ||
dismiss() | ||
} label: { | ||
Text("Cancel") | ||
} | ||
} | ||
|
||
ToolbarItem(placement: .topBarTrailing) { | ||
Button { | ||
if let selectedVehicle, let selectedEvent { | ||
var event = EditVehicleEvent(name: name, | ||
make: make, | ||
model: model, | ||
year: year, | ||
color: color, | ||
VIN: VIN, | ||
licenseplatenumber: licensePlateNumber, | ||
vehicle: selectedVehicle) | ||
event.id = selectedEvent.id | ||
Task { | ||
await viewModel.updateEvent(event) | ||
dismiss() | ||
} | ||
} | ||
} label: { | ||
Text("Update") | ||
} | ||
.disabled(name.isEmpty) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Follow the same rules as |
||
} | ||
} | ||
} | ||
} | ||
|
||
func setEditVehicleEventValues(event: EditVehicleEvent) { | ||
self.name = event.name | ||
self.make = event.make | ||
self.model = event.model | ||
self.year = event.year | ||
self.color = event.color | ||
self.VIN = event.VIN | ||
self.licensePlateNumber = event.licenseplatenumber | ||
self.selectedVehicle = event.vehicle | ||
} | ||
} | ||
|
||
#Preview { | ||
EditVehicleView(selectedEvent: | ||
.constant(EditVehicleEvent(name: "", make: "", model: "", year: "", color: "", VIN: "", licenseplatenumber: "")), | ||
viewModel: | ||
SettingsViewModel(authenticationViewModel: AuthenticationViewModel()) | ||
) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,11 @@ | |
@State private var errorDetails: Error? | ||
@State private var copiedAppVersion: Bool = false | ||
|
||
@State private var selectedVehicleEvent: EditVehicleEvent? | ||
@State private var isEditingVehicle = false | ||
@State var editViewModel: EditVehicleView? | ||
@State private var vehicleToEdit: Vehicle? | ||
Comment on lines
+26
to
+29
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You only need the Also don't call this |
||
|
||
private let appVersion = "Version \(Bundle.main.versionNumber) (\(Bundle.main.buildNumber))" | ||
|
||
init(authenticationViewModel: AuthenticationViewModel) { | ||
|
@@ -93,7 +98,7 @@ | |
Text(vehicle.make) | ||
|
||
Text(vehicle.model) | ||
|
||
if let year = vehicle.year, !year.isEmpty { | ||
Text(year) | ||
} | ||
|
@@ -124,11 +129,23 @@ | |
} label: { | ||
Text("Delete", comment: "Label to delete a vehicle") | ||
} | ||
Button { | ||
isEditingVehicle = true | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here is where you would put |
||
} label: { | ||
VStack { | ||
Text("Edit", comment: "Button label to edit this vehicle") | ||
Image(systemName: SFSymbol.pencil) | ||
} | ||
} | ||
} | ||
.sheet(isPresented: $isEditingVehicle) { | ||
EditVehicleView( | ||
selectedEvent: $selectedVehicleEvent, viewModel: viewModel) | ||
Comment on lines
+141
to
+143
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here we'd pass the |
||
} | ||
} | ||
|
||
Button { | ||
// TODO: Show Paywall | ||
// Show paywall if adding more than 1 vehicle, or show the `isShowingAddVehicle` view | ||
isShowingAddVehicle = true | ||
} label: { | ||
|
@@ -234,7 +251,7 @@ | |
.onChange(of: scenePhase) { _, newScenePhase in | ||
guard case .active = newScenePhase else { return } | ||
|
||
// TODO: Show Paywall | ||
|
||
guard let action = actionService.action, | ||
action == .addVehicle | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a thing I recently learned, you can't have two comments for a single key
So for this you'd need to make a new key like
EditVehicle
and then you'd write the commend, and in the actual Localizable.xcstrings file you'd write justEdit