-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDogResultsTableViewController.swift
More file actions
128 lines (98 loc) · 4.69 KB
/
DogResultsTableViewController.swift
File metadata and controls
128 lines (98 loc) · 4.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
//
// DogResultsTableViewController.swift
// Fuzz Therapy
//
// Created by Jade Vance on 8/20/16.
// Copyright © 2016 Jade Vance. All rights reserved.
//
import UIKit
import RealmSwift
import MessageUI
import Haneke
class DogResultsTableViewController: UITableViewController, MFMailComposeViewControllerDelegate {
let realm = try! Realm()
var results: Results<SearchResults>!
func loadResults() {
results = try! Realm().objects(SearchResults)
}
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
// Clears out the Realm Seeds, for dev only
// try! realm.write {
// realm.deleteAll()
// }
loadResults()
}
override func viewWillAppear(animated: Bool) {
self.tableView.reloadData()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return results.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// Table view cells are reused and should be dequeued using a cell identifier.
let cellIdentifier = "DogResultsTableViewCell"
// Fetches the appropriate result for the data source layout.
let result = results[indexPath.row]
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! DogResultsTableViewCell
cell.dogName.text = result.dogName
cell.availability.text = result.availability
cell.name.text = result.name
if let dogImage = cell.dogImage as? UIImageView {
if let url = NSURL(string: result.dogPicture!) {
dogImage.contentMode = UIViewContentMode.ScaleAspectFill
dogImage.hnk_setImageFromURL(url)
}
}
cell.messageButton.tag = indexPath.row
cell.messageButton.addTarget(self, action: #selector(DogResultsTableViewController.sendEmailButtonTapped(_:)), forControlEvents: UIControlEvents.TouchUpInside)
return cell
}
func sendEmailButtonTapped(sender: UIButton!) {
// inidcates which realm instance was tapped, integer value from array
let buttonTag = sender.tag
let mailComposeViewController = configuredMailComposeViewController()
if MFMailComposeViewController.canSendMail() {
self.presentViewController(mailComposeViewController, animated: true, completion: nil)
} else {
self.showSendMailErrorAlert()
}
}
func configuredMailComposeViewController() -> MFMailComposeViewController {
let mailComposerVC = MFMailComposeViewController()
mailComposerVC.mailComposeDelegate = self
mailComposerVC.setToRecipients(["fuzztherapyapp@gmail.com"])
mailComposerVC.setSubject("Hello from Fuzz Therapy!")
mailComposerVC.setMessageBody("Hi, I would love to meet your dog! Are you available on...", isHTML: false)
return mailComposerVC
}
func showSendMailErrorAlert() {
let sendMailErrorAlert = UIAlertController(title: "Could Not Send Email", message: "Your device could not send e-mail. Please check e-mail configuration and try again.", preferredStyle: .Alert)
let OKAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: {
(_)in
})
sendMailErrorAlert.addAction(OKAction)
self.presentViewController(sendMailErrorAlert, animated: true, completion: nil)
}
// MARK: MFMailComposeViewControllerDelegate Method
func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) {
controller.dismissViewControllerAnimated(true, completion: nil)
}
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
@IBAction func onHomeButtonPressed(sender: AnyObject) {
self.performSegueWithIdentifier("unwindToMenu", sender: self)
}
}