-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathFavoriteListDetails.swift
84 lines (64 loc) · 2.53 KB
/
FavoriteListDetails.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
81
82
83
//
// FavoriteListDetails.swift
//
//
// Created by Atousa Duprat on 4/29/16.
//
//
import UIKit
class FavoriteListDetails: UIViewController, UITextViewDelegate, UITextFieldDelegate {
var restaurant : Restaurant!
@IBOutlet var Name: UILabel!
@IBOutlet var Previous: UILabel!
@IBOutlet var Date: UILabel!
@IBOutlet var Note: UITextView!
@IBOutlet var Rating: UITextField!
override func viewDidLoad() {
// TODO: if visit today, retrieve data
self.Note.text = ""
self.Rating.text = ""
Name.text = restaurant.name
Previous.text = "" // String(format:"%d Previous Visits", restaurant.visits!.count)
let now = NSDate()
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "MM-dd-yyyy" // "yyyy-MM-dd HH:mm:ss ZZZ"
self.Date?.text = dateFormatter.stringFromDate(now)
self.Note.delegate = self
self.Rating.delegate = self
}
func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
if(text == "\n") {
textView.resignFirstResponder()
return false
}
return true
}
@IBAction func onDonePressed(sender: AnyObject) {
if(Note.text == "") {
let alert = UIAlertController(title: "Alert", message: "You must write notes before saving", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "ok", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
return
}
let r = Int(Rating.text!)
if(r == nil || r!<0 || r!>5) {
let alert = UIAlertController(title: "Alert", message: "Rating must be an integer [0-5]", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "ok", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
return
}
self.addNotes()
self.performSegueWithIdentifier("unwindToFavoriteList", sender: self)
}
func addNotes() {
let v = visitDescriptor()
v.date = NSDate()
v.rating = Int(Rating.text!)!
v.notes = Note.text
CDM.addVisit(restaurant, descr: v)
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
Rating.resignFirstResponder()
return true
}
}