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 "SettingsSelectionViewController.h"
26
#import "SettingViewController.h"
27
#import "MKConnectionController.h"
28
#import "NSData+MKCommandEncode.h"
29
#import "NSData+MKPayloadEncode.h"
30
#import "MKDatatypes.h"
31
#import "MKDataConstants.h"
32
#import "InAppSettings.h"
33
#import "MixerViewController.h"
34
#import "ChannelsViewController.h"
35
#import "EngineTestViewController.h"
36
 
37
static NSUInteger kNumberOfPages = 5;
38
 
39
@interface SettingsSelectionViewController (Private)
40
 
41
@end
42
 
43
// ///////////////////////////////////////////////////////////////////////////////
44
 
45
@implementation SettingsSelectionViewController
46
 
47
@synthesize settings=_settings;
48
 
49
// ///////////////////////////////////////////////////////////////////////////////
50
#pragma mark -
51
#pragma mark View lifecycle
52
 
53
 
54
- (id)init {
55
 
56
  if ((self = [super initWithStyle:UITableViewStyleGrouped])) {
57
    self.title = NSLocalizedString(@"Settings",@"Settings controller title");
58
    self.hidesBottomBarWhenPushed=NO;
59
  }
60
 
61
  return self;
62
}
63
 
64
- (void)viewDidLoad {
65
  [super viewDidLoad];
66
 
67
  NSNotificationCenter * nc = [NSNotificationCenter defaultCenter];
68
  [nc addObserver:self
69
         selector:@selector(readSettingNotification:)
70
             name:MKReadSettingNotification
71
           object:nil];
72
 
73
  [nc addObserver:self
74
         selector:@selector(changeSettingNotification:)
75
             name:MKChangeSettingNotification
76
           object:nil];
77
 
78
 
79
  [self.tableView setAllowsSelectionDuringEditing:YES];
80
 
81
}
82
 
83
#pragma mark -
84
#pragma mark Memory management
85
 
86
- (void)didReceiveMemoryWarning {
87
  // Releases the view if it doesn't have a superview.
88
  [super didReceiveMemoryWarning];
89
 
90
  // Relinquish ownership any cached data, images, etc that aren't in use.
91
}
92
 
93
- (void)viewDidUnload {
94
  NSNotificationCenter * nc = [NSNotificationCenter defaultCenter];
95
  [nc removeObserver:self];
96
 
97
  self.settings=nil;
98
  [_settings release];
99
}
100
 
101
 
102
- (void)dealloc {
103
  [super dealloc];
104
  [_settings release];
105
}
106
 
107
// ////////////////////////////////////////////////////////////////////////////////////////////
108
#pragma mark -
109
- (void) viewWillAppear:(BOOL)animated {
110
  [self.navigationController setToolbarHidden:NO animated:NO];
111
  [[MKConnectionController sharedMKConnectionController] activateFlightCtrl];
112
}
113
 
114
- (void) viewDidAppear:(BOOL)animated {
115
  [self cancelEditActiveSetting:self];
116
  [self reloadAllSettings];
117
 
118
  UIBarButtonItem* actionButton;
119
 
120
  actionButton =  [[[UIBarButtonItem alloc]
121
                    initWithTitle:@"Change Active"
122
                            style:UIBarButtonItemStyleBordered|UIBarButtonItemStyleDone
123
                            target:self
124
                            action:@selector(saveActiveSetting:)] autorelease];
125
 
126
 
127
        [self setToolbarItems:[NSArray arrayWithObject:actionButton] animated:YES];
128
  //[actionButton release];
129
}
130
 
131
// ////////////////////////////////////////////////////////////////////////////////////////////
132
#pragma mark -
133
 
134
- (IBAction) reloadAllSettings {
135
 
136
  NSMutableArray * settings = [[NSMutableArray alloc] init];
137
  for (unsigned i = 0; i < kNumberOfPages; i++) {
138
    [settings addObject:[NSNull null]];
139
  }
140
  self.settings = settings;
141
  [settings release];
142
 
143
  activeSetting=0xFF;
144
  [self requestSettingForIndex:0xFF];
145
 
146
}
147
 
148
 
149
// theIndex 1-5 or 0xFF
150
- (void) requestSettingForIndex:(NSInteger)theIndex {
151
  MKConnectionController * cCtrl = [MKConnectionController sharedMKConnectionController];
152
  uint8_t index = theIndex;
153
 
154
  NSData * data = [NSData dataWithCommand:MKCommandReadSettingsRequest
155
                               forAddress:MKAddressFC
156
                         payloadWithBytes:&index
157
                                   length:1];
158
 
159
  [cCtrl sendRequest:data];
160
}
161
 
162
 
163
- (void) readSettingNotification:(NSNotification *)aNotification {
164
 
165
  NSDictionary* d=[aNotification userInfo];
166
 
167
  NSInteger index = [[d objectForKey:kMKDataKeyIndex] integerValue]-1;
168
 
169
  if (activeSetting==0xFF) {
170
    activeSetting=index;
171
  }
172
 
173
  [self.settings replaceObjectAtIndex:index withObject:d];
174
 
175
  for (int i=0; i<kNumberOfPages; i++) {
176
    if ([self.settings objectAtIndex:i] == [NSNull null]) {
177
      [self requestSettingForIndex:i+1];
178
      break;
179
    }  
180
  }
181
  [self.tableView reloadData];
182
}
183
 
184
 
185
//////////////////////////////////////////////////////////////////////////////////////////////
186
#pragma mark -
187
#pragma mark Table view data source
188
 
189
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
190
  return self.tableView.editing?1:2;
191
}
192
 
193
 
194
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
195
  if(section==0)
196
    return [self.settings count];
197
 
198
  return 3;
199
}
200
 
201
//////////////////////////////////////////////////////////////////////////////////////////////
202
 
203
- (UITableViewCell *) cellForSetting: (UITableView *) tableView indexPath: (NSIndexPath *) indexPath  {
204
  static NSString *CellIdentifier = @"SettingsSelectionCell";
205
 
206
  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
207
  if (cell == nil) {
208
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
209
  }
210
 
211
  NSUInteger row = [indexPath row];
212
  NSDictionary* setting=[self.settings objectAtIndex:row];
213
 
214
  if ((NSNull *)setting == [NSNull null]) {
215
    cell.textLabel.text = [NSString stringWithFormat:NSLocalizedString(@"Setting #%d",@"Setting i"), row];
216
 
217
    UIActivityIndicatorView *activityView =
218
    [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
219
    [activityView startAnimating];
220
    [cell setAccessoryView:activityView];
221
    [activityView release];
222
  }
223
  else {
224
 
225
    cell.textLabel.text = [setting objectForKey:kKeyName];
226
    cell.accessoryView = nil;
227
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
228
  }
229
 
230
 
231
  int currActiveSetting= self.tableView.editing? newActiveSetting:activeSetting;
232
 
233
  if( row == currActiveSetting ){
234
    UIImage *image = [UIImage imageNamed:@"star.png"];
235
    cell.imageView.image = image;
236
  }
237
  else {
238
    cell.imageView.image = nil;
239
  }
240
 
241
 
242
  return cell;
243
 
244
}
245
 
246
//////////////////////////////////////////////////////////////////////////////////////////////
247
 
248
- (UITableViewCell *) cellForExtra: (UITableView *) tableView indexPath: (NSIndexPath *) indexPath  {
249
  static NSString *CellIdentifier = @"SettingsMixerCell";
250
 
251
  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
252
  if (cell == nil) {
253
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
254
  }
255
 
256
  switch (indexPath.row) {
257
    case 0:
258
      cell.textLabel.text = NSLocalizedString(@"Mixer",@"Mixer cell");
259
      break;
260
    case 1:
261
      cell.textLabel.text = NSLocalizedString(@"Motor test",@"Motor test cell");
262
      break;
263
    case 2:
264
      cell.textLabel.text = NSLocalizedString(@"Channels",@"Channels cell");
265
      break;
266
  }
267
  cell.accessoryView = nil;
268
  cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
269
  cell.imageView.image = nil;
270
 
271
 
272
  return cell;
273
 
274
}
275
 
276
//////////////////////////////////////////////////////////////////////////////////////////////
277
 
278
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
279
 
280
  if(indexPath.section==0)
281
    return [self cellForSetting: tableView indexPath: indexPath];
282
 
283
  return [self cellForExtra: tableView indexPath: indexPath];
284
}
285
 
286
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
287
  return UITableViewCellEditingStyleNone;
288
}
289
 
290
- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath
291
{
292
  return NO;
293
}
294
 
295
// Override to support conditional editing of the table view.
296
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
297
    return indexPath.section==0;
298
}
299
 
300
 
301
/*
302
// Override to support editing the table view.
303
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
304
 
305
    if (editingStyle == UITableViewCellEditingStyleDelete) {
306
        // Delete the row from the data source
307
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
308
    }  
309
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
310
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
311
    }  
312
}
313
*/
314
 
315
 
316
/*
317
// Override to support rearranging the table view.
318
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
319
}
320
*/
321
 
322
 
323
/*
324
// Override to support conditional rearranging of the table view.
325
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
326
    // Return NO if you do not want the item to be re-orderable.
327
    return YES;
328
}
329
*/
330
 
331
- (void) changeSettingNotification:(NSNotification *)aNotification {
332
 
333
  NSDictionary* d=[aNotification userInfo];
334
 
335
  NSInteger index = [[d objectForKey:kMKDataKeyIndex] integerValue]-1;
336
 
337
  activeSetting=index;
338
 
339
  [self cancelEditActiveSetting:self];
340
}
341
 
342
- (void)saveActiveSetting:(id)sender
343
{
344
  MKConnectionController * cCtrl = [MKConnectionController sharedMKConnectionController];
345
  uint8_t index = newActiveSetting+1;
346
 
347
  NSData * data = [NSData dataWithCommand:MKCommandChangeSettingsRequest
348
                               forAddress:MKAddressFC
349
                         payloadWithBytes:&index
350
                                   length:1];
351
 
352
  [cCtrl sendRequest:data];
353
}
354
 
355
- (void)editActiveSetting:(id)sender
356
{
357
  [self.navigationController setToolbarHidden:NO animated:YES];
358
 
359
  UIBarButtonItem *cancelButton = [[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
360
                                                                                target:self
361
                                                                                action:@selector(cancelEditActiveSetting:)]autorelease];
362
 
363
  [self.navigationItem setRightBarButtonItem:cancelButton animated:NO];
364
 
365
  newActiveSetting = activeSetting;
366
 
367
  [self.tableView setEditing:YES animated:YES];
368
  [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationFade];
369
}
370
 
371
- (void)cancelEditActiveSetting:(id)sender
372
{
373
  [self.navigationController setToolbarHidden:YES animated:YES];
374
 
375
  UIBarButtonItem *editButton = [[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemEdit
376
                                                                             target:self
377
                                                                             action:@selector(editActiveSetting:)]autorelease];
378
 
379
  [self.navigationItem setRightBarButtonItem:editButton animated:NO];
380
  [self.tableView setEditing:NO animated:YES];
381
 
382
  //[self.tableView insertSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:YES];
383
 
384
  [self.tableView reloadData];
385
}
386
 
387
 
388
#pragma mark -
389
#pragma mark Table view delegate
390
 
391
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
392
  if( self.tableView.editing && indexPath.section!=0 )
393
    return nil;
394
  return indexPath;
395
}
396
 
397
 
398
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
399
 
400
  [tableView deselectRowAtIndexPath:indexPath animated:YES];
401
 
402
  NSUInteger row = [indexPath row];
403
 
404
  if (self.tableView.editing ) {
405
 
406
    if(indexPath.section==0 && newActiveSetting != row) {
407
      NSArray *row0 = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:newActiveSetting inSection:0]];  
408
      newActiveSetting = row;
409
      NSArray *row1 = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:newActiveSetting inSection:0]];  
410
 
411
      [self.tableView beginUpdates];
412
      [self.tableView reloadRowsAtIndexPaths:row0 withRowAnimation:UITableViewRowAnimationFade];
413
      [self.tableView reloadRowsAtIndexPaths:row1 withRowAnimation:UITableViewRowAnimationFade];
414
      [self.tableView endUpdates];
415
    }
416
  }
417
  else {
418
    if( indexPath.section==0 ) {
419
      NSMutableDictionary* setting=[self.settings objectAtIndex:row];
420
      SettingViewController* settingView = [[SettingViewController alloc] initWithSettingDatasource:setting];
421
 
422
      [self.navigationController setToolbarHidden:NO animated:NO];
423
 
424
      [self.navigationController pushViewController:settingView animated:YES];
425
      [settingView release];
426
    }
427
    else {
428
      MixerViewController* extraView=nil;
429
 
430
      switch (indexPath.row) {
431
        case 0:
432
          extraView = [[MixerViewController alloc] initWithStyle:UITableViewStylePlain];
433
          break;
434
        case 1:
435
          extraView = [[EngineTestViewController alloc] initWithStyle:UITableViewStyleGrouped];
436
          break;
437
        case 2:
438
          extraView = [[ChannelsViewController alloc] initWithStyle:UITableViewStylePlain];
439
          break;
440
      }
441
 
442
      [self.navigationController setToolbarHidden:NO animated:NO];
443
 
444
      [self.navigationController pushViewController:extraView animated:YES];
445
      [extraView release];
446
    }
447
  }
448
}
449
 
450
 
451
 
452
 
453
@end
454