-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCreateCapsuleViewController.m
228 lines (177 loc) · 7.49 KB
/
CreateCapsuleViewController.m
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
//
// CreateCapsuleViewController.m
// MemoryCapsule
//
// Created by Chris Gonzalez on 3/26/13.
// Copyright (c) 2013 teambrown. All rights reserved.
//
#import "CreateCapsuleViewController.h"
@interface CreateCapsuleViewController ()
// Text field tag identifiers
enum{
capsuleNameTag = 0,
capsuleLocationTag = 1
};
@end
@implementation CreateCapsuleViewController
@synthesize capsuleName;
@synthesize createButton;
@synthesize lockDate;
@synthesize openDate;
@synthesize datePickerLock;
@synthesize dateToolbarLock;
@synthesize datePickerOpen;
@synthesize dateToolbarOpen;
@synthesize capsuleLocation;
NSString *newCapsuleName;
CLLocationCoordinate2D newCapsuleLocation;
NSString *newDateLock;
NSString *newDateOpen;
NSDate *dateSelectedLock;
NSDate *dateSelectedOpen;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
[createButton addTarget:self action:@selector(createButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
// Create view for the lock date picker
self.datePickerLock = [[UIDatePicker alloc]init];
//self.datePickerLock.datePickerMode = UIDatePickerModeDate;
[self.datePickerLock addTarget:self action:@selector(dateChangedLock) forControlEvents:UIControlEventValueChanged];
UIToolbar *toolbarLock = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
UIBarButtonItem *doneButtonLock = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(donePressedLock:)];
[toolbarLock setItems:[NSArray arrayWithObject:doneButtonLock]];
self.dateToolbarLock = toolbarLock;
self.lockDate.inputView = self.datePickerLock;
self.lockDate.inputAccessoryView = self.dateToolbarLock;
// Create view for the open date picker
self.datePickerOpen = [[UIDatePicker alloc]init];
//self.datePickerOpen.datePickerMode = UIDatePickerModeDate;
[self.datePickerOpen addTarget:self action:@selector(dateChangedOpen) forControlEvents:UIControlEventValueChanged];
UIToolbar *toolbarOpen = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
UIBarButtonItem *doneButtonOpen = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(donePressedOpen:)];
[toolbarOpen setItems:[NSArray arrayWithObject:doneButtonOpen]];
self.dateToolbarOpen = toolbarOpen;
self.openDate.inputView = self.datePickerOpen;
self.openDate.inputAccessoryView = self.dateToolbarOpen;
}
- (void)dateChangedLock
{
NSLog(@"dateChanged called");
dateSelectedLock = [self.datePickerLock date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
//[dateFormatter setDateStyle:NSDateFormatterLongStyle];
newDateLock = [dateFormatter stringFromDate:dateSelectedLock];
}
- (void)donePressedLock:(id)sender
{
NSLog(@"donePressed called by %@", sender);
lockDate.text = newDateLock;
[self.lockDate resignFirstResponder];
}
- (void)dateChangedOpen
{
NSLog(@"dateChanged called");
dateSelectedOpen = [self.datePickerOpen date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
newDateOpen = [dateFormatter stringFromDate:dateSelectedOpen];
}
- (void)donePressedOpen:(id)sender
{
NSLog(@"donePressed called by %@", sender);
openDate.text = newDateOpen;
[self.openDate resignFirstResponder];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
NSLog(@"Sender %i entered %@", textField.tag, textField.text);
switch(textField.tag){
case 0:
newCapsuleName = textField.text;
break;
case 1:
[self computeLatLongForGivenLocation:textField.text];
break;
default:
return NO;
}
[textField resignFirstResponder];
return YES;
}
-(void)textFieldDidEndEditing:(UITextField *)textField
{
NSLog(@"Sender %i entered %@", textField.tag, textField.text);
switch(textField.tag){
case 0:
newCapsuleName = textField.text;
break;
case 1:
[self computeLatLongForGivenLocation:textField.text];
break;
default:
break;
}
[textField resignFirstResponder];
}
- (IBAction)createButtonPressed:(id)sender
{
// bug note: MAKE SURE YOU CLICK RETURN ON UITEXTFIELD OR ELSE QUERY WILL CRASH APP
NSLog(@"CreateButtonPressed");
// Link new capsule into the CapsulesList
PFUser *user = [PFUser currentUser];
PFQuery *query = [PFQuery queryWithClassName:@"CapsulesList"];
[query whereKey:@"userName" equalTo:[user username]];
PFObject *newCapsule = [query getFirstObject];
[newCapsule addUniqueObjectsFromArray:[NSArray arrayWithObjects:newCapsuleName, nil] forKey:@"capsules"];
[newCapsule setObject:[user username] forKey:@"userName"];
[newCapsule saveInBackground];
//Convert our latlong into a pfpoint
PFGeoPoint *pfCapsuleLocation = [PFGeoPoint geoPointWithLatitude:newCapsuleLocation.latitude
longitude:newCapsuleLocation.longitude];
//Save new capsule into the Capsule table
PFObject *newCapsuleObject = [PFObject objectWithClassName:@"Capsule"];
[newCapsuleObject setObject:[user username] forKey:@"createdBy"];
[newCapsuleObject setObject:newCapsuleName forKey:@"capsuleName"];
[newCapsuleObject setObject:pfCapsuleLocation forKey:@"location"];
[newCapsuleObject setObject:dateSelectedLock forKey:@"lockDate"];
[newCapsuleObject setObject:dateSelectedOpen forKey:@"openDate"];
[newCapsuleObject setObject:[NSNumber numberWithBool:YES] forKey:@"open"];
[newCapsuleObject saveInBackground];
[self.navigationController popViewControllerAnimated:YES];
}
# pragma mark - Helper functions
-(void) computeLatLongForGivenLocation: (NSString *)address{
double latitude = 0, longitude = 0;
NSString *esc_addr = [address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *req = [NSString stringWithFormat:@"http://maps.google.com/maps/api/geocode/json?sensor=false&address=%@", esc_addr];
NSString *result = [NSString stringWithContentsOfURL:[NSURL URLWithString:req] encoding:NSUTF8StringEncoding error:NULL];
if (result) {
NSScanner *scanner = [NSScanner scannerWithString:result];
if ([scanner scanUpToString:@"\"lat\" :" intoString:nil] && [scanner scanString:@"\"lat\" :" intoString:nil]) {
[scanner scanDouble:&latitude];
if ([scanner scanUpToString:@"\"lng\" :" intoString:nil] && [scanner scanString:@"\"lng\" :" intoString:nil]) {
[scanner scanDouble:&longitude];
}
}
}
CLLocationCoordinate2D center;
center.latitude = latitude;
center.longitude = longitude;
NSLog(@"Lat: %f, Long: %f", latitude, longitude);
newCapsuleLocation = center;
}
@end