-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCCPostView.m
309 lines (238 loc) · 8.86 KB
/
CCPostView.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
//
// CCPostView.m
// CleanCity
//
// Created by codecamp on 12/18/10.
// Copyright 2010 CodeCamp Klagenfurt. All rights reserved.
//
#import "CCPostView.h"
#import "NSData+Base64.h"
#import "UIImage+Extras.h"
@implementation CCPostView
@synthesize commenttext;
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
[comment becomeFirstResponder];
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
UIImageView *navbarimg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"navbar"]];
[navbar addSubview:navbarimg];
[navbar sendSubviewToBack:navbarimg];
map = [[CCNearIncidentsMapView alloc] init];
map.postView = self;
if (commenttext) comment.text = self.commenttext;
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
static BOOL firstLaunch = YES;
if (![[NSUserDefaults standardUserDefaults] objectForKey:@"userid"] && firstLaunch) {
CCFacebookLoginView *fblogin = [[CCFacebookLoginView alloc] init];
[self presentModalViewController:fblogin animated:NO];
firstLaunch = NO;
}
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)){
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];
map.displayedForRotation = YES;
[self showMapView];
} else {
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationSlide];
if (map.displayedForRotation) {
[self closeMapView];
map.displayedForRotation = NO;
}
}
}
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)dealloc {
[comment release];
[imageSourceChooser release];
[imagePicker release];
[pickedImage release];
[locationManager stopUpdatingLocation];
[locationManager release];
[location release];
[super dealloc];
}
#pragma mark Interface Builder
- (IBAction) cancel {
[comment setText:@""];
[pickedImage release];
pickedImage = nil;
[pickedImagePreview removeFromSuperview];
[pickedImagePreview release], pickedImagePreview=nil;
}
- (IBAction) post {
if (pickedImage) {
CCIncident *incident = [[CCIncident alloc] initWithDescription:comment.text andImage:pickedImage andLat:location.coordinate.latitude andLon:location.coordinate.longitude];
CCLOG(@"incident: %@",incident);
if (!alert) {
alert = [[CCAlertView alloc] initWithFrame:CGRectMake(70.0f, 50.0f, 180.0f, 180.0f)];
}
[self.view addSubview:alert];
[incident send:self];
[incident release];
} else {
//TODO: Present Error
}
}
- (IBAction) showImageSourceChooser {
if (!imageSourceChooser) {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
imageSourceChooser = [[UIActionSheet alloc] initWithTitle:NSLocalizedString(@"IMAGE_SOURCE_TITLE", @"")
delegate:self
cancelButtonTitle:NSLocalizedString(@"IMAGE_SOURCE_CANCEL", @"")
destructiveButtonTitle:nil
otherButtonTitles:NSLocalizedString(@"IMAGE_SOURCE_LIBRARY", @""), NSLocalizedString(@"IMAGE_SOURCE_CAMERA", @""), nil];
} else {
imageSourceChooser = [[UIActionSheet alloc] initWithTitle:NSLocalizedString(@"IMAGE_SOURCE_TITLE", @"")
delegate:self
cancelButtonTitle:NSLocalizedString(@"IMAGE_SOURCE_CANCEL", @"")
destructiveButtonTitle:nil
otherButtonTitles:NSLocalizedString(@"IMAGE_SOURCE_LIBRARY", @""), nil];
}
}
[imageSourceChooser showInView:self.view];
}
- (IBAction) showMapView {
if (!map) {
map = [[CCNearIncidentsMapView alloc] init];
}
map.view.frame = CGRectMake(0, 0, 320, 480);
map.view.alpha = 0;
[UIView animateWithDuration:0.5 animations:^(void){
map.view.alpha = 1;
[map viewDidAppear:YES];
}];
[self.view addSubview:map.view];
[comment resignFirstResponder];
[map viewWillAppear:YES];
}
- (void) closeMapView {
self.view.frame = CGRectMake(0, 20, 320, 460);
[comment becomeFirstResponder];
[UIView animateWithDuration:0.5 animations:^(void){
map.view.alpha = 0;
} completion:^(BOOL x){
[map.view removeFromSuperview];
}];
}
#pragma mark UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
CCLOG(@"Button %d tapped", buttonIndex);
if (!imagePicker) {
imagePicker = [[UIImagePickerController alloc] init];
imagePicker.allowsEditing = NO;
}
imagePicker.delegate = self;
if (buttonIndex == 0) imagePicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
else if (buttonIndex == 1 && [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
else return;
self.commenttext = comment.text;
[self presentModalViewController:imagePicker animated:YES];
}
#pragma mark UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[pickedImage release];
pickedImage = [[info objectForKey:UIImagePickerControllerOriginalImage] retain];
if (!pickedImagePreview) {
pickedImagePreview = [[UIImageView alloc] initWithFrame:CGRectMake(20, 200, 30, 30)];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(zoomPreview)];
[pickedImagePreview addGestureRecognizer:tap];
[tap release];
pickedImagePreview.userInteractionEnabled = YES;
}
[self.view addSubview:pickedImagePreview];
[self.view bringSubviewToFront:pickedImagePreview];
pickedImagePreview.image = [pickedImage imageByScalingProportionallyToSize:CGSizeMake(200, 200)];
[self dismissModalViewControllerAnimated:YES];
CCLOG(@"Selected Image");
}
#pragma mark CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
[location release];
location = [newLocation retain];
if (map) {
map.location = location;
}
CCLOG(@"Got location %@", location);
}
#pragma mark CCProgressCallbackProtocol
-(void) startProgress:(NSNumber*)count {
[alert startProgress:count];
}
-(void) doProgress:(NSNumber*)progressIndex {
[alert doProgress:progressIndex];
}
-(void) endProgress {
[alert endProgress];
[alert removeFromSuperview];
[self cancel];
}
-(void) subProgress:(NSString*)message {
[alert subProgress:message];
}
#pragma mark Animations
- (void) zoomPreview {
static BOOL zoomed = NO;
if (zoomed) {
[self greyOutBackground:NO];
[UIView animateWithDuration:0.5 animations:^(void){
pickedImagePreview.frame = CGRectMake(20, 200, 30, 30);
[[self.view viewWithTag:33] setAlpha:0];
[[self.view viewWithTag:33] setFrame:CGRectMake(4, 184, 31, 31)];
} completion:^(BOOL finished) {
[[self.view viewWithTag:33] removeFromSuperview];
}];
} else {
[self greyOutBackground:YES];
UIImageView *closeButton = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"close_button"]];
closeButton.alpha = 0;
closeButton.frame = CGRectMake(4, 184, 31, 31);
closeButton.tag = 33;
closeButton.userInteractionEnabled = YES;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(zoomPreview)];
[closeButton addGestureRecognizer:tap];
[self.view addSubview:closeButton];
[UIView animateWithDuration:0.5 animations:^(void){
pickedImagePreview.frame = CGRectMake(70, 60, 170, 170);
closeButton.alpha = 1;
closeButton.frame = CGRectMake(54, 44, 31, 31);
}];
}
zoomed = !zoomed;
}
- (void) greyOutBackground:(BOOL)yesOrNo {
if (yesOrNo) {
UIView *black = [[UIView alloc] initWithFrame:CGRectMake(-20, 0, 480, 320)];
black.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:1];
black.alpha = 0;
black.tag = 42;
[self.view addSubview:black];
[self.view bringSubviewToFront:pickedImagePreview];
[self.view bringSubviewToFront:[self.view viewWithTag:33]];
[UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^(void){
black.alpha = 0.5;
} completion:nil];
[black release];
} else {
[UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^(void){
[[self.view viewWithTag:42] setAlpha:0];
} completion:^(BOOL x){
[[self.view viewWithTag:42] removeFromSuperview];
}];
}
}
@end