Skip to content
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

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions PetAdoption-iOS/PetAdoption-iOS.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1188,7 +1188,7 @@
);
INFOPLIST_FILE = "PetAdoption-iOS/Info.plist";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.cfo.petadoption;
PRODUCT_BUNDLE_IDENTIFIER = com.lhaquebaruma.petadoption;
Copy link
Member

Choose a reason for hiding this comment

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

Looks like you forgot to set the bundle identifier back again heh

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'll be sure to set it to cfo before I push the updates to git

PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_SWIFT3_OBJC_INFERENCE = On;
SWIFT_VERSION = 4.0;
Expand All @@ -1210,7 +1210,7 @@
);
INFOPLIST_FILE = "PetAdoption-iOS/Info.plist";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.cfo.petadoption;
PRODUCT_BUNDLE_IDENTIFIER = com.lhaquebaruma.petadoption;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_SWIFT3_OBJC_INFERENCE = On;
SWIFT_VERSION = 4.0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,16 +181,17 @@ class PetListingDetailVC: UITableViewController, MFMailComposeViewControllerDele
// MARK: - Email Logic
////////////////////////////////////////////////////////////

func configureMailController() -> MFMailComposeViewController {
func configureMailController() -> MFMailComposeViewController? {
guard let pet = pet, pet.contact.email != "" else { return nil }
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
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)
Expand All @@ -202,6 +203,21 @@ class PetListingDetailVC: UITableViewController, MFMailComposeViewControllerDele
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
controller.dismiss(animated: true, completion: nil)
}

////////////////////////////////////////////////////////////
// MARK: - Phone Logic
////////////////////////////////////////////////////////////

// func callCellPushed(sender: AnyObject) {
// if let url = URL(string: "tel://\(pet?.contact.phone)") {
// if #available(iOS 10.0, *) {
// UIApplication.shared.open(url, options: [:], completionHandler: nil)
// } else {
// print("Unable to send call.")
// }
// }
// }
//
Copy link
Member

Choose a reason for hiding this comment

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

Typically I would say not to leave commented out code like this in. It's fine for now since I know you're working on the phone number issue next.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Got it


////////////////////////////////////////////////////////////
// MARK: - UIScrollViewDelegate
Expand Down Expand Up @@ -295,32 +311,26 @@ class PetListingDetailVC: UITableViewController, MFMailComposeViewControllerDele
return UITableViewCell()
}

////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////
// MARK: - UITableViewDataSource
////////////////////////////////////////////////////////////


override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

let sectionNumber = indexPath.section
let rowNumber = indexPath.row

if sectionNumber == 3 && rowNumber == 1 {
Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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.

Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Member

Choose a reason for hiding this comment

The 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()
Copy link
Member

Choose a reason for hiding this comment

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

If the return type for configureMailController() is optional like I suggested above, you can wrap all of this code in an if let and only try to present the controller if there is actually an email address.

if MFMailComposeViewController.canSendMail() {
self.present(mailComposeViewController, animated: true, completion: nil)
self.present((mailComposeViewController)!, animated: true, completion: nil)
Copy link
Member

Choose a reason for hiding this comment

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

The idea with making the return from configureMailController() be optional was that you could use an if let and avoid force unwrapping it like you're doing. Something more like

if let mailComposeViewController = configureMailController() {
    if MFMailComposeViewController.canSendMail() {
        self.present(mailComposeViewController, animated: true, completion: nil)
    } else {
        displayEmailError()
    }
}

Copy link
Collaborator Author

@baruma baruma Jun 5, 2018

Choose a reason for hiding this comment

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

It's been a while since I looked over at this! I actually meddled with the solution you gave and it works except that all the cells available become active and end up triggering the mailComposeVC. How should I go about preventing that?

Copy link
Member

Choose a reason for hiding this comment

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

Do you mean implementing the above snippet of code? I just made that change locally (it's not that different than what you currently had there) and it seems to only be trying to compose mail on the mail row.

} else {
displayEmailError()
}
}

}


////////////////////////////////////////////////////////////
// If the return type for configureMailController() is optional like I suggested above,
// you can wrap all of this code in an if let and only try to
// present the controller if there is actually an email address.
//
Copy link
Member

Choose a reason for hiding this comment

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

These look like the comments I made in my last review hehe... You can go ahead and remove these.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Removed it!


}

override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String?
{
Expand All @@ -338,4 +348,5 @@ class PetListingDetailVC: UITableViewController, MFMailComposeViewControllerDele
return nil
}
}

}