Skip to content

Conversation

@CoraJacobson
Copy link

Copy link

@swift-student swift-student left a comment

Choose a reason for hiding this comment

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

Great start on this project, Cora.


import Foundation

class GigController {

Choose a reason for hiding this comment

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

This class is looking nice and clean so far

Comment on lines +46 to +89
switch loginType {
case .signUp:
gigController?.signUp(with: user, completion: { (result) in
do {
let success = try result.get()
if success {
DispatchQueue.main.async {
let alertController = UIAlertController(title: "Sign up successful!", message: "Please log in.", preferredStyle: .alert)
let alertAction = UIAlertAction(title: "OK", style: .default, handler: nil)
alertController.addAction(alertAction)
self.present(alertController, animated: true) {
self.loginType = .signIn
self.signInSegmentedControl.selectedSegmentIndex = 1
self.signInButton.setTitle("Sign In", for: .normal)
}
}
}
} catch {
print("Error signing up: \(error)")
}
})
case .signIn:
gigController?.signIn(with: user, completion: { (result) in
do {
let success = try result.get()
if success {
DispatchQueue.main.async {
self.dismiss(animated: true, completion: nil)

}
}
} catch {
if let error = error as? GigController.NetworkError {
switch error {
case .failedSignIn:
print("Sign in failed")
case .noData, .noToken:
print("No data received")
default:
print("Other error occurred")
}
}
}
})

Choose a reason for hiding this comment

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

This is a rather large switch statement, maybe a good place to refactor out into a couple smaller functions.

Copy link

@swift-student swift-student left a comment

Choose a reason for hiding this comment

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

Great job on this project, Cora. The UI looks nice with the custom color, and you did the stretch goal. Your network calls all look good. Like I noted, don't be afraid to add some whitespace to separate logical chunks of code, and also consider refactoring tasks out to smaller functions to make things more easily readable.

Comment on lines +38 to +60
do {
let jsonData = try JSONEncoder().encode(user)
print(String(data: jsonData, encoding: .utf8)!)
request.httpBody = jsonData
let task = URLSession.shared.dataTask(with: request) { (_, response, error) in
if let error = error {
print("SignUp failed with error: \(error)")
completion(.failure(.failedSignUp))
return
}
guard let response = response as? HTTPURLResponse,
response.statusCode == 200 else {
print("Sign up was unsuccesful")
completion(.failure(.failedSignUp))
return
}
completion(.success(true))
}
task.resume()
} catch {
print("Error encoding user: \(error)")
completion(.failure(.failedSignUp))
}

Choose a reason for hiding this comment

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

I would prefer to only wrap the do catch block around the try statement so it is a little more clear what is going to throw an error. Nothing inherently wrong with this code, just a matter of style.

Comment on lines +37 to +63
if let title = titleTextField.text,
!title.isEmpty,
let description = descriptionTextView.text,
!description.isEmpty {
let gig = Gig(title: title, dueDate: datePicker.date, description: description)
if let gigController = gigController {
if gigController.gigs.contains(gig) {
let alert = UIAlertController(title: "This gig is already saved.", message: nil, preferredStyle: .alert)
let okButton = UIAlertAction(title: "OK", style: .cancel, handler: nil)
alert.addAction(okButton)
present(alert, animated: true, completion: nil)
} else {
gigController.createGig(with: gig, completion: { (result) in
switch result {
case .success(true):
DispatchQueue.main.async {
self.navigationController?.popViewController(animated: true)
}
case .failure(let error):
print("Error creating gig: \(error)")
default:
return
}
})
}
}
}

Choose a reason for hiding this comment

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

Don't be afraid to add some whitespace to group things into logical chunks. As with the switch statement I pointed out, this chunk of code might be a good place to refactor into a couple smaller functions for clarity.

Comment on lines +43 to +48
if gigController.gigs.contains(gig) {
let alert = UIAlertController(title: "This gig is already saved.", message: nil, preferredStyle: .alert)
let okButton = UIAlertAction(title: "OK", style: .cancel, handler: nil)
alert.addAction(okButton)
present(alert, animated: true, completion: nil)
} else {

Choose a reason for hiding this comment

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

Nice job doing the stretch goal!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants