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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
|
//
// InputTableViewController.m
// FixMyStreet
//
// Created by Matthew on 26/09/2008.
// Copyright 2008 UK Citizens Online Democracy. All rights reserved.
//
#import "InputTableViewController.h"
#import "SettingsViewController.h"
#import "FixMyStreetAppDelegate.h"
#import "EditSubjectViewController.h"
#import "AboutViewController.h"
#import "Reachability.h"
@implementation InputTableViewController
//@synthesize image;
//@synthesize imagCell;
//@synthesize reportSubject;
- (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)nibBundle {
if (self = [super initWithNibName:nibName bundle:nibBundle]) {
self.title = @"FixMyStreet";
}
return self;
}
/*
- (id)initWithStyle:(UITableViewStyle)style {
// Override initWithStyle: if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
if (self = [super initWithStyle:style]) {
}
return self;
}
*/
- (void)viewWillAppear:(BOOL)animated {
[self enableSubmissionButton];
}
// Implement viewDidLoad to do additional setup after loading the view.
- (void)viewDidLoad {
[super viewDidLoad];
backButton = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonSystemItemCancel target:nil action:nil];
self.navigationItem.backBarButtonItem = backButton;
UIBarButtonItem* rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Report" style:UIBarButtonItemStyleDone target:self action:@selector(uploadReport) ];
rightBarButtonItem.enabled = NO;
self.navigationItem.rightBarButtonItem = rightBarButtonItem;
[rightBarButtonItem release];
// Let's start trying to find our location...
[MyCLController sharedInstance].delegate = self;
[self startLocation];
FixMyStreetAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
if (delegate.image) {
UIImage *newImage = [[UIImage alloc] initWithData:delegate.image];
imageView.image = newImage;
[newImage release];
}
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
FixMyStreetAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
if (delegate.name || delegate.email || delegate.phone)
return 4;
return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 1;
}
-(void)enableSubmissionButton {
[actionsToDoView reloadData];
FixMyStreetAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
if (delegate.image && delegate.latitude && delegate.subject && delegate.subject.length) {
self.navigationItem.rightBarButtonItem.enabled = YES;
} else {
self.navigationItem.rightBarButtonItem.enabled = NO;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier;
UITableViewCell *cell;
FixMyStreetAppDelegate* delegate = [[UIApplication sharedApplication] delegate];
// Possible editing of subject within main view (I think I prefer it as is)
// And possible display of selected image within cell somewhere/somehow (I like how Contacts does it, but haven't
// managed that so far
if (indexPath.section == 1) {
CellIdentifier = @"CellText";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
//CGRect frame = CGRectMake(0, 0, 250, 44);
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
//cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
//UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(1.0, 1.0, 250, 44)];
//textField.placeholder = @"Subject";
// [textField addTarget:self action:nil forControlEvents:UIControlEventValueChanged];
//cell.accessoryView = textField;
//[textField release];
}
} else {
/* if (indexPath.section == 0) {
CellIdentifier = @"CellImage";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[imageCell alloc] initWithFrame:CGRectMake(0, 0, 400, 44) reuseIdentifier:CellIdentifier] autorelease];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
} else { */
CellIdentifier = @"Cell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
}
if (indexPath.section == 0) {
if (delegate.image) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
cell.text = @"Take photo";
actionTakePhotoCell = cell;
} else if (indexPath.section == 2) {
if (delegate.latitude) {
cell.accessoryView = nil;
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else if ([MyCLController sharedInstance].updating) {
UIActivityIndicatorView* activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[activityView startAnimating];
cell.accessoryView = activityView;
[activityView release];
} else {
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
cell.text = @"Fetch location";
actionFetchLocationCell = cell;
} else if (indexPath.section == 1) {
if (delegate.subject && delegate.subject.length) {
if (!subjectLabel) {
subjectLabel = [[UILabel alloc] initWithFrame:CGRectMake(10,0,70,40)];
subjectLabel.font = [UIFont boldSystemFontOfSize:17];
subjectLabel.text = @"Subject:";
[cell.contentView addSubview:subjectLabel];
}
subjectLabel.hidden = NO;
if (!subjectContent) {
subjectContent = [[UILabel alloc] initWithFrame:CGRectMake(80,0,190,40)];
subjectContent.font = [UIFont systemFontOfSize:17];
[cell.contentView addSubview:subjectContent];
}
cell.text = nil;
subjectContent.text = delegate.subject;
subjectContent.hidden = NO;
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
subjectContent.hidden = YES;
subjectLabel.hidden = YES;
cell.text = @"Short summary of problem";
cell.textColor = [UIColor grayColor];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
actionSubjectCell = cell;
} else if (indexPath.section == 3) {
if (delegate.name && delegate.name.length && delegate.email && delegate.email.length) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
cell.text = @"Your details";
} else {
cell.text = @"Eh?";
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if (indexPath.section == 0) {
[self addPhoto:nil];
} else if (indexPath.section == 2) {
if ([self startLocation]) {
UIActivityIndicatorView* activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[activityView startAnimating];
actionFetchLocationCell.accessoryView = activityView;
[activityView release];
}
} else if (indexPath.section == 1) {
FixMyStreetAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
EditSubjectViewController* editSubjectViewController = [[EditSubjectViewController alloc] initWithNibName:@"EditSubjectView" bundle:nil];
[editSubjectViewController setAll:delegate.subject viewTitle:@"Edit subject" placeholder:@"Subject" keyboardType:UIKeyboardTypeDefault capitalisation:UITextAutocapitalizationTypeSentences];
[self.navigationController pushViewController:editSubjectViewController animated:YES];
[editSubjectViewController release];
} else if (indexPath.section == 3) {
[self gotoSettings:nil firstTime:NO];
}
}
-(IBAction)addPhoto:(id) sender {
BOOL cameraAvailable = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
BOOL photosAvailable = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary];
if (!cameraAvailable && !photosAvailable) {
UITableViewCell *cell = [actionsToDoView cellForRowAtIndexPath:0]; // XXX
cell.text = @"No photo mechanism available";
return;
}
UIImagePickerController* picker = [[UIImagePickerController alloc] init];
if (cameraAvailable) {
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
} else {
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
}
picker.delegate = self;
picker.allowsImageEditing = NO;
[self presentModalViewController:picker animated:YES];
}
- (void)viewDidAppear:(BOOL)animated {
backButton.title = @"Cancel";
}
/*
- (void)viewWillDisappear:(BOOL)animated {
}
*/
/*
- (void)viewDidDisappear:(BOOL)animated {
}
*/
/*
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
*/
// Check this, I can't remember if you need to release nib things.
- (void)dealloc {
[imageView release];
[actionTakePhotoCell release];
[actionFetchLocationCell release];
[actionSubjectCell release];
[actionsToDoView release];
[settingsButton release];
[backButton release];
[subjectLabel release];
[subjectContent release];
[super dealloc];
}
// UIImagePickerControllerDelegate prototype
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)newImage editingInfo:(NSDictionary *)editingInfo {
FixMyStreetAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
NSData *imageData = UIImageJPEGRepresentation(newImage, 0.8);
delegate.image = imageData;
imageView.image = newImage;
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
[picker release];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
[picker release];
}
// MyCLControllerDelegate and related
-(BOOL)startLocation {
NetworkStatus internetConnectionStatus = [[Reachability sharedReachability] internetConnectionStatus];
if (internetConnectionStatus == NotReachable) {
UIAlertView *v = [[UIAlertView alloc] initWithTitle:@"Location required" message:@"FixMyStreet needs some sort of connection in order to find your location." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[v show];
[v release];
return FALSE;
} else {
[[MyCLController sharedInstance] startUpdatingLocation];
return TRUE;
}
}
-(void)newLocationUpdate:(CLLocation *)location {
//UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Hey" message:text delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
//[alert show];
//[alert release];
FixMyStreetAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
// delegate.location = location;
delegate.latitude = [NSString stringWithFormat:@"%f", location.coordinate.latitude];
delegate.longitude = [NSString stringWithFormat:@"%f", location.coordinate.longitude];
[self enableSubmissionButton];
}
-(void)newLocationError:(NSString *)text {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Location required" message:text delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
[self enableSubmissionButton];
}
// Buttons
// I realise this flips the navbar too, but can't seem to do it nicely with a container parent, and not really that important!
-(IBAction)gotoAbout:(id)sender {
backButton.title = @"Back";
AboutViewController* aboutViewController = [[AboutViewController alloc] initWithNibName:@"AboutView" bundle:nil];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration: 0.75];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.navigationController.view cache:YES];
[self.navigationController pushViewController:aboutViewController animated:NO];
[UIView commitAnimations];
[aboutViewController release];
}
-(IBAction)gotoSettings:(id)sender firstTime:(BOOL)firstTime {
backButton.title = @"Done";
SettingsViewController* settingsViewController = [[SettingsViewController alloc] initWithStyle:UITableViewStyleGrouped];
if (firstTime)
settingsViewController.firstTime = firstTime;
[self.navigationController pushViewController:settingsViewController animated:YES];
[settingsViewController release];
}
-(void)uploadReport {
FixMyStreetAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
if (!delegate.name || !delegate.email) {
[self gotoSettings:nil firstTime:YES];
} else {
[delegate uploadReport];
}
}
-(void)reportUploaded:(BOOL)success {
if (success)
imageView.image = nil;
[self enableSubmissionButton];
}
@end
|