-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathViewController.swift
80 lines (67 loc) · 3.36 KB
/
ViewController.swift
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
import UIKit
import Parse
import ParseUI
class ViewController: UIViewController, PFLogInViewControllerDelegate, PFSignUpViewControllerDelegate {
var user_name: String = ""
override func viewDidLoad() {
print("didLoad\n")
super.viewDidLoad()
PFUser.logOut()
}
override func viewDidAppear(animated: Bool) {
print("didAppear\n")
super.viewDidAppear(animated)
if (PFUser.currentUser() == nil) {
let loginViewController = LoginViewController()
loginViewController.delegate = self
loginViewController.fields = [.UsernameAndPassword , .LogInButton , .PasswordForgotten , .SignUpButton]
loginViewController.emailAsUsername = true
loginViewController.signUpController?.emailAsUsername = true
loginViewController.signUpController?.delegate = self
self.presentViewController(loginViewController, animated: false, completion: nil)
}
}
func logInViewController(logInController: PFLogInViewController, didLogInUser user: PFUser) {
self.dismissViewControllerAnimated(true, completion: nil)
print("Logged in")
getCustomerInfo()
}
func signUpViewController(signUpController: PFSignUpViewController, didSignUpUser user: PFUser) {
self.dismissViewControllerAnimated(true, completion: nil)
self.performSegueWithIdentifier("showSetup", sender: self)
//presentLoggedInAlert()
}
func presentLoggedInAlert() {
let alertController = UIAlertController(title: "You're logged in", message: "Welcome to Carrot", preferredStyle: .Alert)
let OKAction = UIAlertAction(title: "OK", style: .Default) { (action) in
self.dismissViewControllerAnimated(true, completion: nil)
}
alertController.addAction(OKAction)
self.presentViewController(alertController, animated: true, completion: nil)
}
func getCustomerInfo() {
PFCloud.callFunctionInBackground("getCustomerFromObjectId", withParameters: ["object_id" : PFUser.currentUser()!.objectId!]) { (returnData: AnyObject?, error: NSError?) -> Void in
if (error == nil) {
if let data = returnData {
let results = Utilities.convertStringToDictionary(data as! String)
let first_name = results!["first_name"] as! String
let last_name = results!["last_name"] as! String
self.user_name = first_name + " " + last_name
self.performSegueWithIdentifier("showMain2", sender: self)
}
} else {
print("Customer Error = \(error)")
}
}
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "showMain2") {
let destVC = segue.destinationViewController as! MainViewController
destVC.goalName = PFUser.currentUser()!.objectForKey("goal_item") as! String
destVC.goalPrice = PFUser.currentUser()!.objectForKey("goal_price") as! Double
destVC.carrotName = PFUser.currentUser()!.objectForKey("carrot_item") as! String
destVC.carrotPrice = PFUser.currentUser()!.objectForKey("carrot_price") as! Double
destVC.username = user_name
}
}
}