-
Notifications
You must be signed in to change notification settings - Fork 15
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
MFMailComposeViewController #91
base: develop
Are you sure you want to change the base?
Changes from 3 commits
d16171d
e34fe87
74e9ed4
ab46cfa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,8 @@ | |
|
||
import UIKit | ||
import PetAdoptionTransportKit | ||
import MessageUI | ||
import Foundation | ||
|
||
enum Sections: Int | ||
{ | ||
|
@@ -27,7 +29,7 @@ struct SectionItem | |
|
||
//////////////////////////////////////////////////////////// | ||
|
||
class PetListingDetailVC: UITableViewController | ||
class PetListingDetailVC: UITableViewController, MFMailComposeViewControllerDelegate | ||
{ | ||
//////////////////////////////////////////////////////////// | ||
// MARK: - IBOutlets | ||
|
@@ -174,6 +176,32 @@ class PetListingDetailVC: UITableViewController | |
|
||
self.imageContainerScrollView.contentSize = CGSize(width: fullWidth, height: self.imageContainerScrollView.frame.height) | ||
} | ||
|
||
//////////////////////////////////////////////////////////// | ||
// MARK: - Email Logic | ||
//////////////////////////////////////////////////////////// | ||
|
||
func configureMailController() -> MFMailComposeViewController { | ||
let mailComposerVC = MFMailComposeViewController() | ||
let emailString = pet?.contact.email | ||
mailComposerVC.mailComposeDelegate = self | ||
mailComposerVC.setToRecipients([emailString!]) | ||
mailComposerVC.setSubject("Adoption!") | ||
mailComposerVC.setMessageBody("Hello! I'm interested in giving this pet a new home!", isHTML: false) | ||
|
||
return mailComposerVC | ||
} | ||
|
||
func displayEmailError() { | ||
let sendEmailErrorAlert = UIAlertController(title: "Could not send email", message: nil, preferredStyle: .alert) | ||
let dismiss = UIAlertAction(title: "OK", style: .default, handler: nil) | ||
sendEmailErrorAlert.addAction(dismiss) | ||
self.present(sendEmailErrorAlert, animated: true, completion: nil) | ||
} | ||
|
||
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) { | ||
controller.dismiss(animated: true, completion: nil) | ||
} | ||
|
||
//////////////////////////////////////////////////////////// | ||
// MARK: - UIScrollViewDelegate | ||
|
@@ -268,6 +296,31 @@ class PetListingDetailVC: UITableViewController | |
} | ||
|
||
//////////////////////////////////////////////////////////// | ||
|
||
|
||
//////////////////////////////////////////////////////////// | ||
// MARK: - UITableViewDataSource | ||
//////////////////////////////////////////////////////////// | ||
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 MARK section isn't needed, because you're already under the existing MARK section for the UITableViewDataSource. Just push the beginning of the function underneath that comment line just above where you added this. 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. Got it, just removed it. |
||
|
||
|
||
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { | ||
|
||
let sectionNumber = indexPath.section | ||
let rowNumber = indexPath.row | ||
if sectionNumber == 3 && rowNumber == 1 { | ||
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. I get what you're doing here, and this works as the table view is currently set up, but this is probably not the best approach. You probably want to avoid using "magic numbers" like this. At the VERY least, I would define constants near the top of the file to make it easier to change these values if the need ever arose, but I actually have a more robust solution for this if you're interested in taking a look. 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. I'd be down to look at the solution you have currently since I'm having issues figuring out how to go about selecting the email row specifically. I only did it this way for the time being since all the pets have the same number of cells but of course that's subject to change in the future. 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. OK, I'll push up a separate pull request with what I came up with and link it to you on Slack. I'll be happy to go over with you also if you want. 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. For now, let's just leave this in here like this, only because I don't want you to have to wait on me. If you could, though, could you add a TODO comment here? Something like: // TODO: Refactor this approach to avoid using magic numbers or something to that effect. |
||
let mailComposeViewController = configureMailController() | ||
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. If the return type for |
||
if MFMailComposeViewController.canSendMail() { | ||
self.present(mailComposeViewController, animated: true, completion: nil) | ||
} else { | ||
displayEmailError() | ||
} | ||
} | ||
|
||
} | ||
|
||
|
||
//////////////////////////////////////////////////////////// | ||
|
||
|
||
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? | ||
{ | ||
|
This file was deleted.
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.
You might want to do an if check on the email to make sure it isn't an empty string before trying to compose an email. I've noticed from the API responses that the email field is not always filled out.
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.
In fact, that being the case, you might also want to make the return type optional, so that you can check for it later on when you are about to try presenting the view controller.