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 3 commits
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
6 changes: 3 additions & 3 deletions PetAdoption-iOS/PetAdoption-iOS.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -722,7 +722,7 @@
TargetAttributes = {
D0950AC31C618C2F002E198C = {
CreatedOnToolsVersion = 7.2;
DevelopmentTeam = 4X359D924E;
DevelopmentTeam = N3E7249ZAC;
LastSwiftMigration = 0910;
};
D0950AD71C618C2F002E198C = {
Expand Down Expand Up @@ -1181,7 +1181,7 @@
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = "$(inherited)";
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CODE_SIGN_IDENTITY = "iPhone Developer";
DEVELOPMENT_TEAM = 4X359D924E;
DEVELOPMENT_TEAM = N3E7249ZAC;
FRAMEWORK_SEARCH_PATHS = (
"$(inherited)",
"$(PROJECT_DIR)",
Expand All @@ -1203,7 +1203,7 @@
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = "$(inherited)";
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CODE_SIGN_IDENTITY = "iPhone Developer";
DEVELOPMENT_TEAM = 4X359D924E;
DEVELOPMENT_TEAM = N3E7249ZAC;
FRAMEWORK_SEARCH_PATHS = (
"$(inherited)",
"$(PROJECT_DIR)",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

import UIKit
import PetAdoptionTransportKit
import MessageUI
import Foundation

enum Sections: Int
{
Expand All @@ -27,7 +29,7 @@ struct SectionItem

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

class PetListingDetailVC: UITableViewController
class PetListingDetailVC: UITableViewController, MFMailComposeViewControllerDelegate
{
////////////////////////////////////////////////////////////
// MARK: - IBOutlets
Expand Down Expand Up @@ -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
Copy link
Member

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.

Copy link
Member

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.

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
Expand Down Expand Up @@ -268,6 +296,31 @@ class PetListingDetailVC: UITableViewController
}

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


////////////////////////////////////////////////////////////
// MARK: - UITableViewDataSource
////////////////////////////////////////////////////////////
Copy link
Member

Choose a reason for hiding this comment

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

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, just removed it.



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)
} else {
displayEmailError()
}
}

}


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


override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String?
{
Expand Down
30 changes: 15 additions & 15 deletions PetAdoption-iOS/PetAdoption-iOS/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,21 @@
<string>????</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>Fabric</key>
<dict>
<key>APIKey</key>
<string>ec01996362ce555e215240573ed73fade4f9a26d</string>
<key>Kits</key>
<array>
<dict>
<key>KitInfo</key>
<dict/>
<key>KitName</key>
<string>Crashlytics</string>
</dict>
</array>
</dict>
<key>LSRequiresIPhoneOS</key>
<key>Fabric</key>
<dict>
<key>APIKey</key>
<string>ec01996362ce555e215240573ed73fade4f9a26d</string>
<key>Kits</key>
<array>
<dict>
<key>KitInfo</key>
<dict/>
<key>KitName</key>
<string>Crashlytics</string>
</dict>
</array>
</dict>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>NSAppTransportSecurity</key>
<dict>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@
<scene sceneID="ovg-Kd-rXg">
<objects>
<tableViewController id="d7T-Wp-bkt" customClass="PetListingDetailVC" customModule="PetAdoption_iOS" customModuleProvider="target" sceneMemberID="viewController">
<tableView key="view" clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="prototypes" style="plain" separatorStyle="default" allowsSelection="NO" rowHeight="44" sectionHeaderHeight="28" sectionFooterHeight="28" id="Byl-ep-Vay">
<tableView key="view" clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="prototypes" style="plain" separatorStyle="default" rowHeight="44" sectionHeaderHeight="28" sectionFooterHeight="28" id="Byl-ep-Vay">
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
Expand Down Expand Up @@ -204,7 +204,7 @@
<outlet property="descriptionLabel" destination="lWj-Dv-7lT" id="D2l-lV-7Iu"/>
</connections>
</tableViewCell>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" selectionStyle="default" indentationWidth="10" reuseIdentifier="NormalCell" textLabel="mm4-b4-PZX" detailTextLabel="fE6-AQ-m4g" rowHeight="37" style="IBUITableViewCellStyleValue1" id="zgm-QT-Qja">
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" selectionStyle="blue" indentationWidth="10" reuseIdentifier="NormalCell" textLabel="mm4-b4-PZX" detailTextLabel="fE6-AQ-m4g" rowHeight="37" style="IBUITableViewCellStyleValue1" id="zgm-QT-Qja">
<rect key="frame" x="0.0" y="315" width="375" height="37"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="zgm-QT-Qja" id="CWP-3Z-ZnD">
Expand Down
8 changes: 0 additions & 8 deletions PetAdoption-iOS/PetAdoption-iOS/apikey.plist.example

This file was deleted.