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

5 Edit Vehicle #269

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
8 changes: 8 additions & 0 deletions Basic-Car-Maintenance.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@
8A3D748C2AD9C41D0000FEEB /* AlertItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8A3D748B2AD9C41D0000FEEB /* AlertItem.swift */; };
8AEE816F2ACF37F800FC0C2A /* Action.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8AEE816E2ACF37F800FC0C2A /* Action.swift */; };
8AEE81722ACF384D00FC0C2A /* MainTabView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8AEE81712ACF384D00FC0C2A /* MainTabView.swift */; };
E55B630D2B079E5A006BDDDF /* EditVehicleView.swift in Sources */ = {isa = PBXBuildFile; fileRef = E55B630C2B079E5A006BDDDF /* EditVehicleView.swift */; };
E58499662ACDDA8B00634660 /* ContributorsListView.swift in Sources */ = {isa = PBXBuildFile; fileRef = E58499652ACDDA8B00634660 /* ContributorsListView.swift */; };
E58499682ACDDA9A00634660 /* ContributorsProfileView.swift in Sources */ = {isa = PBXBuildFile; fileRef = E58499672ACDDA9A00634660 /* ContributorsProfileView.swift */; };
E584996A2ACDDAFF00634660 /* Contributor.swift in Sources */ = {isa = PBXBuildFile; fileRef = E58499692ACDDAFF00634660 /* Contributor.swift */; };
E5EDD11D2B0933700059C52E /* EditVehicleEvent.swift in Sources */ = {isa = PBXBuildFile; fileRef = E5EDD11C2B0933700059C52E /* EditVehicleEvent.swift */; };
FF09FC912AB6FF44006BE61A /* AuthenticationView.swift in Sources */ = {isa = PBXBuildFile; fileRef = FF09FC902AB6FF44006BE61A /* AuthenticationView.swift */; };
FF153AFF2B07C3E000D0BA30 /* FirebaseCrashlytics in Frameworks */ = {isa = PBXBuildFile; productRef = FF153AFE2B07C3E000D0BA30 /* FirebaseCrashlytics */; };
FF218EF62B00865F0025A533 /* AnalyticsService.swift in Sources */ = {isa = PBXBuildFile; fileRef = FF218EF52B00865F0025A533 /* AnalyticsService.swift */; };
Expand Down Expand Up @@ -121,9 +123,11 @@
8AEE816E2ACF37F800FC0C2A /* Action.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Action.swift; sourceTree = "<group>"; };
8AEE81712ACF384D00FC0C2A /* MainTabView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MainTabView.swift; sourceTree = "<group>"; };
8AEE81732ACF394E00FC0C2A /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
E55B630C2B079E5A006BDDDF /* EditVehicleView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EditVehicleView.swift; sourceTree = "<group>"; };
E58499652ACDDA8B00634660 /* ContributorsListView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContributorsListView.swift; sourceTree = "<group>"; };
E58499672ACDDA9A00634660 /* ContributorsProfileView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContributorsProfileView.swift; sourceTree = "<group>"; };
E58499692ACDDAFF00634660 /* Contributor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Contributor.swift; sourceTree = "<group>"; };
E5EDD11C2B0933700059C52E /* EditVehicleEvent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EditVehicleEvent.swift; sourceTree = "<group>"; };
FF0813562AD0A83000910EFA /* UITests.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = UITests.xcconfig; sourceTree = "<group>"; };
FF0813572AD0A92700910EFA /* Widget.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = Widget.xcconfig; sourceTree = "<group>"; };
FF098EFA2AB3424E003EC0FE /* Basic-Car-Maintenance.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = "Basic-Car-Maintenance.xcconfig"; sourceTree = SOURCE_ROOT; };
Expand Down Expand Up @@ -422,6 +426,7 @@
8014A4CE2AD75928005B51F6 /* AppIcon.swift */,
57CDD9A32ADC320F002EFED0 /* OdometerReading.swift */,
8A3D748B2AD9C41D0000FEEB /* AlertItem.swift */,
E5EDD11C2B0933700059C52E /* EditVehicleEvent.swift */,
);
path = Models;
sourceTree = "<group>";
Expand All @@ -435,6 +440,7 @@
E58499652ACDDA8B00634660 /* ContributorsListView.swift */,
E58499672ACDDA9A00634660 /* ContributorsProfileView.swift */,
FF09FC902AB6FF44006BE61A /* AuthenticationView.swift */,
E55B630C2B079E5A006BDDDF /* EditVehicleView.swift */,
);
path = Views;
sourceTree = "<group>";
Expand Down Expand Up @@ -725,13 +731,15 @@
8A3D74862AD6D9A10000FEEB /* AlertView.swift in Sources */,
57CDD9A02ADC31A8002EFED0 /* AddOdometerReadingView.swift in Sources */,
57CDD99E2ADC3173002EFED0 /* OdometerViewModel.swift in Sources */,
E55B630D2B079E5A006BDDDF /* EditVehicleView.swift in Sources */,
FF755B3E2A908E7A00F49A13 /* SettingsView.swift in Sources */,
FF3DDF522AA4D28F009D91C4 /* DashboardViewModel.swift in Sources */,
FFE0AF562AD66C3500AB46F8 /* OdometerView.swift in Sources */,
8AEE816F2ACF37F800FC0C2A /* Action.swift in Sources */,
0CA7ED092AE82BF100609019 /* ContributionTip.swift in Sources */,
FFBFE0972A98F7CB000A9BEB /* AddVehicleView.swift in Sources */,
8A3D748C2AD9C41D0000FEEB /* AlertItem.swift in Sources */,
E5EDD11D2B0933700059C52E /* EditVehicleEvent.swift in Sources */,
8A3D748A2AD9C3E00000FEEB /* MainTabViewModel.swift in Sources */,
E584996A2ACDDAFF00634660 /* Contributor.swift in Sources */,
8014A4D32AD77C92005B51F6 /* ChooseAppIconViewModel.swift in Sources */,
Expand Down
6 changes: 5 additions & 1 deletion Basic-Car-Maintenance/Shared/Localizable.xcstrings
Original file line number Diff line number Diff line change
Expand Up @@ -1518,7 +1518,7 @@
}
},
"Edit" : {
"comment" : "Button label to edit this maintenance",
"comment" : "Button label to edit this vehicle\nButton label to edit this maintenance",
Copy link
Owner

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 just Edit

"localizations" : {
"be" : {
"stringUnit" : {
Expand Down Expand Up @@ -4280,6 +4280,7 @@
}
},
"Update" : {
"comment" : "Label to update vehicle information.",
"localizations" : {
"be" : {
"stringUnit" : {
Expand Down Expand Up @@ -4407,6 +4408,9 @@
}
}
}
},
"Update Vehicle Info" : {

},
"Vehicle" : {
"comment" : "Maintenance event vehicle picker header",
Expand Down
22 changes: 22 additions & 0 deletions Basic-Car-Maintenance/Shared/Models/EditVehicleEvent.swift
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
Expand Up @@ -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 {
Expand Down Expand Up @@ -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

View workflow job for this annotation

GitHub Actions / SwiftLint

Prefer at least one space after slashes for comments (comment_spacing)

Check warning on line 93 in Basic-Car-Maintenance/Shared/Settings/ViewModels/SettingsViewModel.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Line should be 110 characters or less; currently it has 126 characters (line_length)
func updateEvent(_ editVehicleEvent: EditVehicleEvent) async {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You shouldn't be making a new model EditVehicleEvent, you should just be using Vehicle


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)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a maintenanceUpdate this is a vehicleUpdate


await self.getVehicles()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need the self it's implied

}

/// Fetches the user's vehicles from Firestore based on their unique user ID.
func getVehicles() async {
if let uid = authenticationViewModel.user?.uid {
Expand All @@ -110,7 +136,7 @@
}
}
}

/// Deletes a vehicle from both Firestore and the local ``SettingsViewModel/vehicles`` array.
///
/// - Parameter vehicle: The vehicle to be deleted.
Expand Down
124 changes: 124 additions & 0 deletions Basic-Car-Maintenance/Shared/Settings/Views/EditVehicleView.swift
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)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Follow the same rules as isVehicleValid in the AddVehicleView

}
}
}
}

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: "")),

Check warning on line 120 in Basic-Car-Maintenance/Shared/Settings/Views/EditVehicleView.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Line should be 110 characters or less; currently it has 125 characters (line_length)
viewModel:
SettingsViewModel(authenticationViewModel: AuthenticationViewModel())
)
}
21 changes: 19 additions & 2 deletions Basic-Car-Maintenance/Shared/Settings/Views/SettingsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You only need the vehicleToEdit, you shouldn't be using a separate model for editing, so EditVehicleEvent is not needed

Also don't call this editViewModel is not needed


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

init(authenticationViewModel: AuthenticationViewModel) {
Expand Down Expand Up @@ -93,7 +98,7 @@
Text(vehicle.make)

Text(vehicle.model)

if let year = vehicle.year, !year.isEmpty {
Text(year)
}
Expand Down Expand Up @@ -124,11 +129,23 @@
} label: {
Text("Delete", comment: "Label to delete a vehicle")
}
Button {
isEditingVehicle = true
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is where you would put vehicleToEdit = vehicle, because this is in the ForEach loop, and it's specifying which vehicle is going to show in the EditVehicleView

} 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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we'd pass the vehicleToEdit because we are using the Vehicle model, not the new one you created

}
}

Button {
// TODO: Show Paywall

Check warning on line 148 in Basic-Car-Maintenance/Shared/Settings/Views/SettingsView.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

TODOs should be resolved (Show Paywall) (todo)
// Show paywall if adding more than 1 vehicle, or show the `isShowingAddVehicle` view
isShowingAddVehicle = true
} label: {
Expand Down Expand Up @@ -234,7 +251,7 @@
.onChange(of: scenePhase) { _, newScenePhase in
guard case .active = newScenePhase else { return }

// TODO: Show Paywall

Check warning on line 254 in Basic-Car-Maintenance/Shared/Settings/Views/SettingsView.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

TODOs should be resolved (Show Paywall) (todo)

guard let action = actionService.action,
action == .addVehicle
Expand Down
Loading