Subversion Repositories Projects

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
805 - 1
/////////////////////////////////////////////////////////////////////////////////
2
// Copyright (C) 2010, Frank Blumenberg
3
//
4
// See License.txt for complete licensing and attribution information.
5
// Permission is hereby granted, free of charge, to any person obtaining a copy
6
// of this software and associated documentation files (the "Software"), to deal
7
// in the Software without restriction, including without limitation the rights
8
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
// copies of the Software, and to permit persons to whom the Software is
10
// furnished to do so, subject to the following conditions:
11
//
12
// The above copyright notice and this permission notice shall be included in all
13
// copies or substantial portions of the Software.
14
//
15
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
// THE SOFTWARE.
22
//
23
/////////////////////////////////////////////////////////////////////////////////
24
 
25
#import "MainViewController.h"
26
#import "InAppSettings.h"
27
#import "Settings.h"
28
#import "GradientButton.h"
29
#import "MKConnectionController.h"
30
#import "MKDataConstants.h"
31
#import "MKIpConnection.h"
32
#import "MKFakeConnection.h"
33
#import "LcdViewController.h"
34
#import "AnalogViewController.h"
35
#import "SettingsSelectionViewController.h"
36
 
37
#define CONNECT_SECTIONID 0
38
 
39
@interface MainViewController (Private)
40
 
41
-(void)initCellForConnectionButton:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath;
42
-(void)initCellForConnectionButtonDisconnected:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath;
43
-(void)initCellForConnectionButtonConnecting:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath;
44
-(void)initCellForConnectionButtonConnected:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath;
45
-(void)initCellForMenu:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath;
46
 
47
@end
48
 
49
 
50
//////////////////////////////////////////////////////////////////////////////////////////////
51
@implementation MainViewController
52
 
53
@synthesize versionString = _versionString;
54
@synthesize controllers = _controllers;
55
 
56
#pragma mark -
57
#pragma mark View lifecycle
58
 
59
 
60
- (void)viewDidLoad {
61
 
62
 
63
  self.title = @"µKopter";
64
  //self.tableView.backgroundColor=[UIColor darkGrayColor];
65
 
66
  NSMutableArray *array = [[NSMutableArray alloc] init];
67
 
68
//  SettingsMainViewController* settingsController = [[SettingsMainViewController alloc]
69
//                     initWithNibName:@"SettingsMainViewController" bundle:nil];
70
 
71
  SettingsSelectionViewController* settingsController = [[SettingsSelectionViewController alloc]
72
                                            initWithStyle:UITableViewStyleGrouped];
73
 
74
  settingsController.title = NSLocalizedString(@"Settings",@"Settings controller title");
75
  [array addObject:settingsController];
76
  [settingsController release];
77
 
78
  LcdViewController* lcdController = [[LcdViewController alloc]
79
                                              initWithNibName:@"LcdViewController" bundle:nil];
80
 
81
  lcdController.title = NSLocalizedString(@"LCD Menu",@"LCD Menu controller title");
82
  [array addObject:lcdController];
83
  [lcdController release];
84
 
85
 
86
  AnalogViewController* analogController = [[AnalogViewController alloc]
87
                                              initWithStyle:UITableViewStylePlain];
88
 
89
  analogController.title = NSLocalizedString(@"Analog Values",@"Analog values controller title");
90
  [array addObject:analogController];
91
  [analogController release];
92
 
93
  self.controllers = array;
94
  [array release];
95
 
96
  connectionState=MKConnectionStateDisconnected;
97
 
98
  //Disable tableview scrolling.
99
  self.tableView.scrollEnabled = NO;
100
 
101
  NSNotificationCenter* nc=[NSNotificationCenter defaultCenter];
102
 
103
  [nc addObserver:self
104
         selector:@selector(connectionRequestDidSucceed:)
105
             name:MKConnectedNotification
106
           object:nil];
107
 
108
  [nc addObserver:self
109
         selector:@selector(disconnected:)
110
             name:MKDisconnectedNotification
111
           object:nil];
112
 
113
  [nc addObserver:self
114
         selector:@selector(connectionRequestDidFail:)
115
             name:MKDisconnectErrorNotification
116
           object:nil];
117
 
118
  [nc addObserver:self
119
         selector:@selector(versionResponse:)
120
             name:MKVersionNotification
121
           object:nil];
122
 
123
 
124
  [super viewDidLoad];
125
}
126
 
127
-(void) viewDidAppear:(BOOL)animated {
128
  [self.tableView reloadData];
129
}
130
 
131
#pragma mark -
132
#pragma mark Memory management
133
 
134
- (void)didReceiveMemoryWarning {
135
  // Releases the view if it doesn't have a superview.
136
  [super didReceiveMemoryWarning];
137
 
138
  // Relinquish ownership any cached data, images, etc that aren't in use.
139
}
140
 
141
- (void)viewDidUnload {
142
 
143
  self.versionString=nil;
144
  self.controllers = nil;
145
  [super viewDidUnload];
146
}
147
 
148
- (void)dealloc {
149
  [_versionString release];
150
  [_controllers release];
151
 
152
  [super dealloc];
153
}
154
 
155
 
156
//////////////////////////////////////////////////////////////////////////////////////////////
157
#pragma mark -
158
#pragma mark Table view data source
159
 
160
 
161
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
162
    return 2;
163
}
164
 
165
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
166
 
167
  if(section==CONNECT_SECTIONID)
168
    return 1;
169
 
170
  return connectionState==MKConnectionStateConnected?[self.controllers count]:0;
171
}
172
 
173
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
174
{
175
  if (section!=CONNECT_SECTIONID)
176
    return nil;
177
 
178
  DLog(@"Footer for %d state %d",section,connectionState);
179
 
180
  if(connectionState==MKConnectionStateConnected && self.versionString!=nil )
181
    return self.versionString;
182
 
183
  return NSLocalizedString(@"Not Connected",@"Connection version string");
184
}
185
 
186
 
187
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
188
 
189
  DLog(@"%@ state %d",indexPath,connectionState);
190
 
191
  static NSString *CellIdentifier = @"Cell";
192
 
193
  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
194
  if (cell == nil) {
195
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
196
  }
197
 
198
  if (indexPath.section==CONNECT_SECTIONID) {
199
    [self initCellForConnectionButton:cell forRowAtIndexPath:indexPath];
200
  }
201
  else {
202
    [self initCellForMenu:cell forRowAtIndexPath:indexPath];
203
  }
204
 
205
  return cell;
206
}
207
 
208
 
209
//////////////////////////////////////////////////////////////////////////////////////////////
210
#pragma mark -
211
#pragma mark Table cell initialization
212
 
213
- (void)initCellForConnectionButton:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
214
{
215
  cell.textLabel.text=[[NSUserDefaults standardUserDefaults] stringForKey:kHostnameKey];
216
  cell.selectionStyle = UITableViewCellSelectionStyleNone;
217
 
218
  switch (connectionState) {
219
    case MKConnectionStateConnected:
220
      [self initCellForConnectionButtonConnected:cell forRowAtIndexPath:indexPath];
221
      break;
222
    case MKConnectionStateConnecting:
223
      [self initCellForConnectionButtonConnecting:cell forRowAtIndexPath:indexPath];
224
      break;
225
    default:
226
      [self initCellForConnectionButtonDisconnected:cell forRowAtIndexPath:indexPath];
227
      break;
228
  }
229
}
230
 
231
- (void)initCellForConnectionButtonDisconnected:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
232
{
233
  cell.selectionStyle = UITableViewCellSelectionStyleNone;
234
  cell.accessoryView = nil;
235
  cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
236
}
237
 
238
- (UIView *) initCancelButton
239
{
240
  UIView *accView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 94, 35)];
241
 
242
  GradientButton *cancelButton = [[GradientButton  alloc] initWithFrame:CGRectMake(0.0, 0.0, 63.0, 33.0)];
243
 
244
  [cancelButton useRedDeleteStyle];
245
  [cancelButton setTitle:NSLocalizedString(@"Abort",@"Conncetion abort button") forState:UIControlStateNormal];
246
  [cancelButton addTarget:self action:@selector(userDidCancelLastConnectRequest) forControlEvents:UIControlEventTouchDown];
247
  cancelButton.titleLabel.font = [UIFont boldSystemFontOfSize:14];
248
 
249
  UIActivityIndicatorView *gear = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray] autorelease];
250
  gear.frame = CGRectMake(68.0, 5.5, 22.0, 22.0);
251
  [gear startAnimating];
252
 
253
  [accView addSubview:gear];
254
  [accView addSubview:cancelButton];
255
  return accView;
256
}
257
 
258
- (void)initCellForConnectionButtonConnecting:(UITableViewCell *)cell
259
{
260
  UIView *accView;
261
  accView = [self initCancelButton];
262
 
263
  cell.accessoryView = accView;
264
  cell.accessoryType = UITableViewCellAccessoryNone;
265
  [accView release];
266
}
267
 
268
- (UIView *) initStopButton
269
{
270
  UIView *accView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 63, 35)];
271
 
272
  GradientButton *stoplButton = [[GradientButton  alloc] initWithFrame:CGRectMake(0.0, 0.0, 63.0, 33.0)];
273
 
274
  [stoplButton useAlertStyle];
275
  [stoplButton setTitle:NSLocalizedString(@"Stop",@"Conncetion stop button") forState:UIControlStateNormal];
276
  [stoplButton addTarget:self action:@selector(userDidDisconnect) forControlEvents:UIControlEventTouchDown];
277
  stoplButton.titleLabel.font = [UIFont boldSystemFontOfSize:14];
278
 
279
  [accView addSubview:stoplButton];
280
  return accView;
281
}
282
 
283
- (void)initCellForConnectionButtonConnected:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
284
{
285
  cell.detailTextLabel.text=@"Connected";
286
 
287
  UIView *accView;
288
  accView = [self initStopButton];
289
 
290
  cell.accessoryView = accView;
291
  cell.accessoryType = UITableViewCellAccessoryNone;
292
  [accView release];
293
}
294
 
295
//-----------------------------------------------------------------------
296
 
297
- (void)initCellForMenu:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath  
298
{
299
  // Configure the cell
300
  NSUInteger row = [indexPath row];
301
  UIViewController *controller =  [_controllers objectAtIndex:row];
302
 
303
  cell.textLabel.text = controller.title;
304
  cell.accessoryView = nil;
305
  cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
306
}
307
 
308
//////////////////////////////////////////////////////////////////////////////////////////////
309
#pragma mark -
310
#pragma mark Connection status actions (Statemachine)
311
 
312
- (void)userDidDisconnect;
313
{
314
  [[MKConnectionController sharedMKConnectionController] stop];
315
}
316
 
317
- (void)userDidCancelLastConnectRequest;
318
{
319
  [[MKConnectionController sharedMKConnectionController] stop];
320
  connectionState=MKConnectionStateDisconnected;
321
  [self.tableView reloadData];
322
}
323
- (void)connectionRequestDidFail:(NSNotification *)aNotification;
324
{
325
  NSError* err = [[aNotification userInfo] objectForKey:@"error"];
326
 
327
  UIAlertView *alert = [[UIAlertView alloc]
328
                        initWithTitle:NSLocalizedString(@"Server error", @"Server error")
329
                        message:[err localizedDescription]
330
                        delegate:self
331
                        cancelButtonTitle:@"Ok"
332
                        otherButtonTitles:nil];
333
  [alert show];
334
  [alert release];
335
 
336
  [self.navigationController popToRootViewControllerAnimated:YES];
337
 
338
  connectionState=MKConnectionStateDisconnected;
339
  [self.tableView reloadData];
340
}
341
- (void)connectionRequestDidSucceed:(NSNotification *)aNotification;
342
{
343
  DLog(@"Got connected");
344
 
345
  connectionState=MKConnectionStateConnected;
346
  [self.tableView beginUpdates];
347
  [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:1]  withRowAnimation:UITableViewRowAnimationFade];
348
  [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:CONNECT_SECTIONID]  withRowAnimation:UITableViewRowAnimationNone];
349
  [self.tableView endUpdates];
350
}
351
- (void)disconnected:(NSNotification *)aNotification;
352
{
353
  connectionState=MKConnectionStateDisconnected;
354
  [self.tableView beginUpdates];
355
  [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:1]  withRowAnimation:UITableViewRowAnimationBottom];
356
  [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:CONNECT_SECTIONID]  withRowAnimation:UITableViewRowAnimationNone];
357
  [self.tableView endUpdates];
358
}
359
 
360
- (void)versionResponse:(NSNotification *)aNotification;
361
{
362
  self.versionString = [[MKConnectionController sharedMKConnectionController]longVersionForAddress:MKAddressFC];
363
 
364
  [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:CONNECT_SECTIONID]  withRowAnimation:UITableViewRowAnimationNone];
365
}
366
 
367
//////////////////////////////////////////////////////////////////////////////////////////////
368
#pragma mark -
369
#pragma mark Table view delegate
370
 
371
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath{
372
 
373
  if (indexPath.section==CONNECT_SECTIONID) {
374
    return (connectionState==MKConnectionStateDisconnected)?indexPath:nil;
375
  }
376
 
377
  return (connectionState==MKConnectionStateConnected)?indexPath:nil;
378
}
379
 
380
 
381
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
382
 
383
  if (indexPath.section==CONNECT_SECTIONID && connectionState==MKConnectionStateDisconnected) {
384
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
385
    [self initCellForConnectionButtonConnecting:cell];
386
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
387
 
388
    [[MKConnectionController sharedMKConnectionController] start];
389
  }
390
  else {
391
 
392
    NSUInteger row = [indexPath row];
393
    UIViewController* controller=[self.controllers objectAtIndex:row];
394
 
395
    [self.navigationController pushViewController:controller animated:YES];
396
  }
397
 
398
}
399
 
400
 
401
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {
402
 
403
  if( [indexPath section]==0 && [indexPath row]==0 ) {
404
    InAppSettingsViewController *settings = [[InAppSettingsViewController alloc] init];
405
    [self.navigationController pushViewController:settings animated:YES];
406
  }
407
}
408
 
409
 
410
@end
411