-
Notifications
You must be signed in to change notification settings - Fork 4
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
[INSPECT-313] FEATURE** Further enhance inspection validation #318
Changes from 6 commits
29346d8
99ccfbe
d70c294
c9c8d3d
d520884
08b180b
7f83c08
305ce17
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -66,16 +66,32 @@ class ShiftViewController: BaseViewController { | |
} | ||
|
||
override func viewWillDisappear(_ animated: Bool) { | ||
print(self) | ||
NotificationCenter.default.removeObserver(self) | ||
} | ||
|
||
override func viewWillAppear(_ animated: Bool) { | ||
self.updateStatuses(); | ||
setupCollectionView() | ||
self.collectionView.reloadData() | ||
addListeners() | ||
} | ||
|
||
/// Iterates through inspections checking formDidValidate. If any form does validate, modifies all appropraite statuses to `.Errors` | ||
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. optimized updateStatuses so that if model is null, we don't have a crash (use of guard as found elsewhere in the file) |
||
private func updateStatuses() { | ||
guard let model = self.model else { return } | ||
if(model.status != "Completed"){ | ||
if (model.inspections.allSatisfy(){$0.formDidValidate}){ | ||
model.set(status: .Draft) | ||
model.inspections.forEach { inspection in | ||
inspection.set(status: .Draft) | ||
} | ||
} else { | ||
model.set(status: .Errors) | ||
model.inspections.forEach { inspection in | ||
inspection.set(status: inspection.formDidValidate ? .Draft : .Errors) | ||
} | ||
} | ||
} | ||
} | ||
private func addListeners() { | ||
NotificationCenter.default.removeObserver(self, name: .TableButtonClicked, object: nil) | ||
NotificationCenter.default.removeObserver(self, name: .InputItemValueChanged, object: nil) | ||
|
@@ -96,7 +112,7 @@ class ShiftViewController: BaseViewController { | |
blowbyModal.initialize(shift: currentShiftModel, delegate: self, onStart: { [weak self] (model) in | ||
guard self != nil else { return } | ||
}) { | ||
// Canceled | ||
// Cancelled | ||
} | ||
} | ||
|
||
|
@@ -107,7 +123,7 @@ class ShiftViewController: BaseViewController { | |
|
||
func setup(model: ShiftModel) { | ||
self.model = model | ||
self.isEditable = model.getStatus() == .Draft || model.getStatus() == .PendingSync | ||
self.isEditable = [.Draft, .PendingSync, .Errors].contains(model.getStatus()) | ||
if model.getStatus() == .PendingSync { | ||
model.set(shouldSync: false) | ||
for inspection in model.inspections { | ||
|
@@ -116,8 +132,8 @@ class ShiftViewController: BaseViewController { | |
Alert.show(title: "Changed to draft", message: "Status changed to draft. tap submit when you've made your changes.") | ||
} | ||
|
||
if model.getStatus() == .Draft { | ||
// make sure inspections are editable. | ||
if [.Draft, .Errors].contains(model.getStatus()) { | ||
// make sure inspections are editable. | ||
for inspection in model.inspections { | ||
inspection.set(shouldSync: false) | ||
} | ||
|
@@ -177,12 +193,14 @@ class ShiftViewController: BaseViewController { | |
@objc func completeAction(sender: UIBarButtonItem) { | ||
guard let model = self.model else { return } | ||
self.dismissKeyboard() | ||
|
||
self.updateStatuses() | ||
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. Added updateStatus before checking canSubmit, so we have the most up to date info on the validation of inspections |
||
// if can submit | ||
var alertMessage = "This shift and the inspections will be uploaded when possible" | ||
if model.shiftStartDate < Calendar.current.startOfDay(for: Date()) { | ||
alertMessage += "\n\n You've entered a date that occurred before today. If this was intentional, no problem! Otherwise, please double-check the entered date: \n\(model.shiftStartDate.stringShort())" | ||
} | ||
if canSubmit() { | ||
if canSubmit() && model.inspections.allSatisfy({ $0.formDidValidate }) { | ||
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 addition of the if statement prevents the user to submit an error containing inspection (avoiding our open loop validation problem) |
||
Alert.show(title: "Are you sure?", message: alertMessage, yes: {[weak self] in | ||
guard let strongSelf = self else { return } | ||
model.set(shouldSync: true) | ||
|
@@ -253,7 +271,7 @@ class ShiftViewController: BaseViewController { | |
navigation.navigationBar.tintColor = .white | ||
navigation.navigationBar.titleTextAttributes = [.foregroundColor: UIColor.white] | ||
setGradiantBackground(navigationBar: navigation.navigationBar) | ||
if let model = self.model, model.getStatus() == .Draft { | ||
if let model = self.model, [.Draft, .Errors].contains(model.getStatus()) { | ||
setRightNavButtons() | ||
} | ||
} | ||
|
@@ -268,19 +286,19 @@ class ShiftViewController: BaseViewController { | |
|
||
// MARK: Validation | ||
func canSubmit() -> Bool { | ||
return validationMessage() == "" | ||
return validationMessage().isEmpty | ||
} | ||
|
||
func validationMessage() -> String { | ||
var message: String = "" | ||
guard let model = self.model else { return message } | ||
var counter = 1 | ||
if model.startTime == "" { | ||
if model.startTime.isEmpty { | ||
message = "\(message)\n\(counter)- Missing Shift Start time." | ||
counter += 1 | ||
} | ||
|
||
if model.endTime == "" { | ||
if model.endTime.isEmpty { | ||
message = "\(message)\n\(counter)- Missing Shift End time." | ||
counter += 1 | ||
} | ||
|
@@ -306,7 +324,7 @@ class ShiftViewController: BaseViewController { | |
} | ||
|
||
for inspection in model.inspections { | ||
if inspection.inspectionTime == "" { | ||
if inspection.inspectionTime.isEmpty { | ||
message = "\(message)\n\(counter)- Missing Time of Inspection." | ||
counter += 1 | ||
} | ||
|
@@ -343,6 +361,17 @@ class ShiftViewController: BaseViewController { | |
} | ||
} | ||
} | ||
|
||
// Check for invalid inspections | ||
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. checks our invalid inspections and let's the user know that the inspections contain validation errors. It is possible to let the users know which validation in particular here too, but that is handled in the inspections page specifically (may be something to review with SO) |
||
let invalidInspections = model.inspections.filter { !$0.formDidValidate } | ||
if !invalidInspections.isEmpty { | ||
message = "\(message)\n\(counter)- One or more inspections contain validation errors. Please review each inspection." | ||
counter += 1 | ||
} | ||
|
||
if !message.isEmpty { | ||
model.set(status: .Errors) | ||
} | ||
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 if !message.isEmpty block may be redundant and removable with our other checks in place, however I will have to confirm with some testing |
||
|
||
return message | ||
} | ||
|
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.
Important
The linter went wild in this file. So there is a lot more diffs than anticipated.
There is information added about working with XCode 15, and deleting all Realm objects added