Skip to content

Commit

Permalink
Merge pull request #276 from windrunner21/202-fix-prevent-users-from-…
Browse files Browse the repository at this point in the history
…deleting-all-vehicles

fix: Prevent Users From Deleting All Vehicles
  • Loading branch information
mikaelacaron committed Dec 11, 2023
2 parents 25acad7 + 2623bff commit 4a116b6
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
28 changes: 27 additions & 1 deletion Basic-Car-Maintenance/Shared/Localizable.xcstrings
Expand Up @@ -731,6 +731,22 @@
}
}
},
"Can't Delete Last Vehicle" : {
"localizations" : {
"ru" : {
"stringUnit" : {
"state" : "translated",
"value" : "Невозможно удалить последний автомобиль"
}
},
"tr" : {
"stringUnit" : {
"state" : "translated",
"value" : "Son Araç Silinemiyor"
}
}
}
},
"Cancel" : {
"localizations" : {
"be" : {
Expand Down Expand Up @@ -3934,6 +3950,9 @@
}
}
},
"The last vehicle can't be deleted. Please add a new vehicle before removing this one." : {
"comment" : "Alert message preventing users from deleting their last vehicle"
},
"The title of the event" : {
"comment" : "Maintenance event title text field label placeholder",
"localizations" : {
Expand Down Expand Up @@ -4413,7 +4432,14 @@
}
},
"Update Vehicle Info" : {

"localizations" : {
"tr" : {
"stringUnit" : {
"state" : "translated",
"value" : "Araç Bilgilerini Güncelle"
}
}
}
},
"Vehicle" : {
"comment" : "Maintenance event vehicle picker header",
Expand Down
14 changes: 13 additions & 1 deletion Basic-Car-Maintenance/Shared/Settings/Views/SettingsView.swift
Expand Up @@ -19,6 +19,7 @@ struct SettingsView: View {
@State private var viewModel: SettingsViewModel
@State private var isShowingAddVehicle = false
@State private var showDeleteVehicleError = false
@State private var showDeleteVehicleAlert = false
@State private var showAddVehicleError = false
@State private var errorDetails: Error?
@State private var copiedAppVersion: Bool = false
Expand Down Expand Up @@ -117,7 +118,11 @@ struct SettingsView: View {
Button(role: .destructive) {
Task {
do {
try await viewModel.deleteVehicle(vehicle)
if viewModel.vehicles.count > 1 {
try await viewModel.deleteVehicle(vehicle)
} else {
showDeleteVehicleAlert = true
}
} catch {
errorDetails = error
showDeleteVehicleError = true
Expand Down Expand Up @@ -240,6 +245,13 @@ struct SettingsView: View {
comment: "Label to display error details.")
}
}
.alert("Can't Delete Last Vehicle", isPresented: $showDeleteVehicleAlert) {
Button("OK", role: .cancel) {
showDeleteVehicleAlert = false
}
} message: {
Text("The last vehicle can't be deleted. Please add a new vehicle before removing this one.", comment: "Alert message preventing users from deleting their last vehicle")

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

View workflow job for this annotation

GitHub Actions / SwiftLint

Line should be 110 characters or less; currently it has 185 characters (line_length)
}
.navigationTitle(Text("Settings", comment: "Label to display settings."))
.task {
await viewModel.getVehicles()
Expand Down

0 comments on commit 4a116b6

Please sign in to comment.