Skip to content
This repository has been archived by the owner on Dec 26, 2022. It is now read-only.

Commit

Permalink
Resolve #54. We may want specific error messages for when things fail…
Browse files Browse the repository at this point in the history
… the save button test.
  • Loading branch information
GiselleSerate committed Jul 10, 2017
1 parent f238874 commit 77d2299
Showing 1 changed file with 19 additions and 14 deletions.
33 changes: 19 additions & 14 deletions SuiteLife/SuiteLife/EditSinglePropertyTableViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,21 +96,26 @@ class EditSinglePropertyViewController: UIViewController, UITextFieldDelegate {
private func determineSaveButtonState() {
// Check if the property must be unique and if the textfield has text in it.
if (uniqueRequired!) && (textField.text != nil) {
// Disable saving, which means users must wait for the callback (and it must be successful) to save.
self.saveButton?.isEnabled = false
let usersRef = databaseRef.child("users")
// Query to see if any user has this property already.
usersRef.queryOrdered(byChild: propertyKey!).queryEqual(toValue: textField.text).observeSingleEvent(of: .value, with: {(snapshot) in
// If the property is taken, disable the save button.
self.saveButton.isEnabled = !snapshot.exists()
// Enable the save button if the property is taken by the current user.
for child in snapshot.children {
if let childRef = child as? DataSnapshot {
// Check if the current user ID is equal to the ID of the user trying to save the current property.
self.saveButton?.isEnabled = (self.currentUserID == childRef.key)
if (searchable)! && ((textField.text?.characters.count)! < 3) { // Not enough characters here. We need at least 3 characters in a searchable field to be searchable.
saveButton?.isEnabled = false
}
else {
// Disable saving, which means users must wait for the callback (and it must be successful) to save.
self.saveButton?.isEnabled = false
let usersRef = databaseRef.child("users")
// Query to see if any user has this property already.
usersRef.queryOrdered(byChild: propertyKey!).queryEqual(toValue: textField.text).observeSingleEvent(of: .value, with: {(snapshot) in
// If the property is taken, disable the save button.
self.saveButton.isEnabled = !snapshot.exists()
// Enable the save button if the property is taken by the current user.
for child in snapshot.children {
if let childRef = child as? DataSnapshot {
// Check if the current user ID is equal to the ID of the user trying to save the current property.
self.saveButton?.isEnabled = (self.currentUserID == childRef.key)
}
}
}
})
})
}
// If we get here, it must not be required that the property be unique, so if there is text in the box, we can allow saving.
} else if textField.text != nil {
saveButton?.isEnabled = true
Expand Down

0 comments on commit 77d2299

Please sign in to comment.