Subversion Repositories Projects

Compare Revisions

Ignore whitespace Rev 804 → Rev 805

/iKopter/trunk/Classes/Settings/SettingViewController.h
0,0 → 1,40
// ///////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2010, Frank Blumenberg
//
// See License.txt for complete licensing and attribution information.
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
// ///////////////////////////////////////////////////////////////////////////////
 
#import <UIKit/UIKit.h>
#import "InAppSettings.h"
 
@interface SettingViewController : InAppSettingsViewController<InAppSettingsDatasource> {
 
NSMutableDictionary* _setting;
}
 
@property(nonatomic,retain) NSMutableDictionary* setting;
 
- (id)initWithSettingDatasource:(NSMutableDictionary*)aSetting;
 
- (void)saveSetting:(id)sender;
- (void)reloadSetting:(id)sender;
@end
/iKopter/trunk/Classes/Settings/SettingViewController.m
0,0 → 1,183
// ///////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2010, Frank Blumenberg
//
// See License.txt for complete licensing and attribution information.
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
// ///////////////////////////////////////////////////////////////////////////////
 
#import "SettingViewController.h"
#import "InAppSettings.h"
#import "MKConnectionController.h"
#import "NSData+MKCommandEncode.h"
#import "NSData+MKPayloadEncode.h"
#import "MKDatatypes.h"
#import "MKDataConstants.h"
 
@implementation SettingViewController
 
@synthesize setting=_setting;
 
#pragma mark -
 
- (id)initWithSettingDatasource:(NSMutableDictionary*)aSetting {
 
if ((self = [super initWithFile:@"Setting"])) {
self.setting = aSetting;
super.dataSource = self;
}
return self;
}
 
- (void)dealloc {
self.setting=nil;
[_setting release];
[super dealloc];
}
 
#pragma mark -
#pragma mark View lifecycle
 
- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem* renameButton;
renameButton = [[[UIBarButtonItem alloc]
initWithTitle:NSLocalizedString(@"Save",@"Save toolbar item")
style:UIBarButtonItemStyleDone
target:self
action:@selector(saveSetting:)] autorelease];
 
UIBarButtonItem* spacer;
spacer = [[[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil] autorelease];
UIBarButtonItem* reloadButton;
reloadButton = [[[UIBarButtonItem alloc]
initWithTitle:NSLocalizedString(@"Reload", @"Reload toolbar item")
style:UIBarButtonItemStyleBordered
target:self
action:@selector(reloadSetting:)] autorelease];
[self setToolbarItems:[NSArray arrayWithObjects:renameButton,spacer,reloadButton,nil]];
}
 
- (void)viewDidUnload {
[super viewDidUnload];
 
NSNotificationCenter * nc = [NSNotificationCenter defaultCenter];
[nc removeObserver:self];
DLog(@"unload");
}
 
#pragma mark -
 
// called after this controller's view will appear
- (void)viewWillAppear:(BOOL)animated
{
[self.navigationController setToolbarHidden:NO animated:NO];
NSNotificationCenter * nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self
selector:@selector(readSettingNotification:)
name:MKReadSettingNotification
object:nil];
[nc addObserver:self
selector:@selector(writeSettingNotification:)
name:MKWriteSettingNotification
object:nil];
}
 
// called after this controller's view will appear
- (void)viewWillDisappear:(BOOL)animated {
NSNotificationCenter * nc = [NSNotificationCenter defaultCenter];
[nc removeObserver:self];
}
 
 
#pragma mark -
#pragma mark Save Setting
 
- (void)saveSetting:(id)sender {
DLog(@"save setting");
MKConnectionController * cCtrl = [MKConnectionController sharedMKConnectionController];
 
NSData * payload = [NSData payloadForWriteSettingRequest:self.setting];
NSData * data = [payload dataWithCommand:MKCommandWriteSettingsRequest
forAddress:MKAddressFC];
[cCtrl sendRequest:data];
}
 
- (void) writeSettingNotification:(NSNotification *)aNotification {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Setting" message:@"Setting saved"
delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
[alert release];
}
 
#pragma mark -
#pragma mark Reload Setting
 
- (void)reloadSetting:(id)sender {
MKConnectionController * cCtrl = [MKConnectionController sharedMKConnectionController];
NSNumber* theIndex = [self.setting objectForKey:kMKDataKeyIndex];
uint8_t index = [theIndex unsignedCharValue];
NSData * data = [NSData dataWithCommand:MKCommandReadSettingsRequest
forAddress:MKAddressFC
payloadWithBytes:&index
length:1];
[cCtrl sendRequest:data];
}
 
- (void) readSettingNotification:(NSNotification *)aNotification {
NSDictionary* d=[aNotification userInfo];
self.setting = [d mutableCopy];
[super.settingsTableView reloadData];
}
 
#pragma mark -
#pragma mark InAppSettingsDatasource
 
- (id) objectForKey:(id)aKey {
return [self.setting objectForKey:aKey];
}
 
- (void) setObject:(id)anObject forKey:(id)aKey {
[self.setting setObject:anObject forKey:aKey];
}
 
 
@end
/iKopter/trunk/Classes/Settings/SettingsSelectionViewController.h
0,0 → 1,46
// ///////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2010, Frank Blumenberg
//
// See License.txt for complete licensing and attribution information.
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
// ///////////////////////////////////////////////////////////////////////////////
 
#import <UIKit/UIKit.h>
 
 
@interface SettingsSelectionViewController : UITableViewController {
 
NSMutableArray* _settings;
int activeSetting;
int newActiveSetting;
}
 
@property (nonatomic, retain) NSMutableArray* settings;
 
- (void) requestSettingForIndex:(NSInteger)theIndex;
 
- (IBAction) reloadAllSettings;
 
 
- (void)saveActiveSetting:(id)sender;
- (void)editActiveSetting:(id)sender;
- (void)cancelEditActiveSetting:(id)sender;
 
@end
/iKopter/trunk/Classes/Settings/SettingsSelectionViewController.m
0,0 → 1,454
// ///////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2010, Frank Blumenberg
//
// See License.txt for complete licensing and attribution information.
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
// ///////////////////////////////////////////////////////////////////////////////
 
#import "SettingsSelectionViewController.h"
#import "SettingViewController.h"
#import "MKConnectionController.h"
#import "NSData+MKCommandEncode.h"
#import "NSData+MKPayloadEncode.h"
#import "MKDatatypes.h"
#import "MKDataConstants.h"
#import "InAppSettings.h"
#import "MixerViewController.h"
#import "ChannelsViewController.h"
#import "EngineTestViewController.h"
 
static NSUInteger kNumberOfPages = 5;
 
@interface SettingsSelectionViewController (Private)
 
@end
 
// ///////////////////////////////////////////////////////////////////////////////
 
@implementation SettingsSelectionViewController
 
@synthesize settings=_settings;
 
// ///////////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark View lifecycle
 
 
- (id)init {
if ((self = [super initWithStyle:UITableViewStyleGrouped])) {
self.title = NSLocalizedString(@"Settings",@"Settings controller title");
self.hidesBottomBarWhenPushed=NO;
}
return self;
}
 
- (void)viewDidLoad {
[super viewDidLoad];
NSNotificationCenter * nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self
selector:@selector(readSettingNotification:)
name:MKReadSettingNotification
object:nil];
 
[nc addObserver:self
selector:@selector(changeSettingNotification:)
name:MKChangeSettingNotification
object:nil];
[self.tableView setAllowsSelectionDuringEditing:YES];
}
 
#pragma mark -
#pragma mark Memory management
 
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Relinquish ownership any cached data, images, etc that aren't in use.
}
 
- (void)viewDidUnload {
NSNotificationCenter * nc = [NSNotificationCenter defaultCenter];
[nc removeObserver:self];
self.settings=nil;
[_settings release];
}
 
 
- (void)dealloc {
[super dealloc];
[_settings release];
}
 
// ////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark -
- (void) viewWillAppear:(BOOL)animated {
[self.navigationController setToolbarHidden:NO animated:NO];
[[MKConnectionController sharedMKConnectionController] activateFlightCtrl];
}
 
- (void) viewDidAppear:(BOOL)animated {
[self cancelEditActiveSetting:self];
[self reloadAllSettings];
UIBarButtonItem* actionButton;
actionButton = [[[UIBarButtonItem alloc]
initWithTitle:@"Change Active"
style:UIBarButtonItemStyleBordered|UIBarButtonItemStyleDone
target:self
action:@selector(saveActiveSetting:)] autorelease];
[self setToolbarItems:[NSArray arrayWithObject:actionButton] animated:YES];
//[actionButton release];
}
 
// ////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark -
 
- (IBAction) reloadAllSettings {
 
NSMutableArray * settings = [[NSMutableArray alloc] init];
for (unsigned i = 0; i < kNumberOfPages; i++) {
[settings addObject:[NSNull null]];
}
self.settings = settings;
[settings release];
activeSetting=0xFF;
[self requestSettingForIndex:0xFF];
}
 
 
// theIndex 1-5 or 0xFF
- (void) requestSettingForIndex:(NSInteger)theIndex {
MKConnectionController * cCtrl = [MKConnectionController sharedMKConnectionController];
uint8_t index = theIndex;
NSData * data = [NSData dataWithCommand:MKCommandReadSettingsRequest
forAddress:MKAddressFC
payloadWithBytes:&index
length:1];
[cCtrl sendRequest:data];
}
 
 
- (void) readSettingNotification:(NSNotification *)aNotification {
 
NSDictionary* d=[aNotification userInfo];
 
NSInteger index = [[d objectForKey:kMKDataKeyIndex] integerValue]-1;
if (activeSetting==0xFF) {
activeSetting=index;
}
[self.settings replaceObjectAtIndex:index withObject:d];
for (int i=0; i<kNumberOfPages; i++) {
if ([self.settings objectAtIndex:i] == [NSNull null]) {
[self requestSettingForIndex:i+1];
break;
}
}
[self.tableView reloadData];
}
 
 
//////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark Table view data source
 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return self.tableView.editing?1:2;
}
 
 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if(section==0)
return [self.settings count];
return 3;
}
 
//////////////////////////////////////////////////////////////////////////////////////////////
 
- (UITableViewCell *) cellForSetting: (UITableView *) tableView indexPath: (NSIndexPath *) indexPath {
static NSString *CellIdentifier = @"SettingsSelectionCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
 
NSUInteger row = [indexPath row];
NSDictionary* setting=[self.settings objectAtIndex:row];
 
if ((NSNull *)setting == [NSNull null]) {
cell.textLabel.text = [NSString stringWithFormat:NSLocalizedString(@"Setting #%d",@"Setting i"), row];
UIActivityIndicatorView *activityView =
[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[activityView startAnimating];
[cell setAccessoryView:activityView];
[activityView release];
}
else {
 
cell.textLabel.text = [setting objectForKey:kKeyName];
cell.accessoryView = nil;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
 
int currActiveSetting= self.tableView.editing? newActiveSetting:activeSetting;
if( row == currActiveSetting ){
UIImage *image = [UIImage imageNamed:@"star.png"];
cell.imageView.image = image;
}
else {
cell.imageView.image = nil;
}
return cell;
 
}
 
//////////////////////////////////////////////////////////////////////////////////////////////
 
- (UITableViewCell *) cellForExtra: (UITableView *) tableView indexPath: (NSIndexPath *) indexPath {
static NSString *CellIdentifier = @"SettingsMixerCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
switch (indexPath.row) {
case 0:
cell.textLabel.text = NSLocalizedString(@"Mixer",@"Mixer cell");
break;
case 1:
cell.textLabel.text = NSLocalizedString(@"Motor test",@"Motor test cell");
break;
case 2:
cell.textLabel.text = NSLocalizedString(@"Channels",@"Channels cell");
break;
}
cell.accessoryView = nil;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.imageView.image = nil;
return cell;
}
 
//////////////////////////////////////////////////////////////////////////////////////////////
 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if(indexPath.section==0)
return [self cellForSetting: tableView indexPath: indexPath];
 
return [self cellForExtra: tableView indexPath: indexPath];
}
 
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleNone;
}
 
- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
return NO;
}
 
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return indexPath.section==0;
}
 
 
/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
*/
 
 
/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/
 
 
/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the item to be re-orderable.
return YES;
}
*/
 
- (void) changeSettingNotification:(NSNotification *)aNotification {
NSDictionary* d=[aNotification userInfo];
NSInteger index = [[d objectForKey:kMKDataKeyIndex] integerValue]-1;
activeSetting=index;
[self cancelEditActiveSetting:self];
}
 
- (void)saveActiveSetting:(id)sender
{
MKConnectionController * cCtrl = [MKConnectionController sharedMKConnectionController];
uint8_t index = newActiveSetting+1;
NSData * data = [NSData dataWithCommand:MKCommandChangeSettingsRequest
forAddress:MKAddressFC
payloadWithBytes:&index
length:1];
[cCtrl sendRequest:data];
}
 
- (void)editActiveSetting:(id)sender
{
[self.navigationController setToolbarHidden:NO animated:YES];
UIBarButtonItem *cancelButton = [[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
target:self
action:@selector(cancelEditActiveSetting:)]autorelease];
[self.navigationItem setRightBarButtonItem:cancelButton animated:NO];
newActiveSetting = activeSetting;
[self.tableView setEditing:YES animated:YES];
[self.tableView deleteSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationFade];
}
 
- (void)cancelEditActiveSetting:(id)sender
{
[self.navigationController setToolbarHidden:YES animated:YES];
UIBarButtonItem *editButton = [[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemEdit
target:self
action:@selector(editActiveSetting:)]autorelease];
[self.navigationItem setRightBarButtonItem:editButton animated:NO];
[self.tableView setEditing:NO animated:YES];
//[self.tableView insertSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:YES];
[self.tableView reloadData];
}
 
 
#pragma mark -
#pragma mark Table view delegate
 
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if( self.tableView.editing && indexPath.section!=0 )
return nil;
return indexPath;
}
 
 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSUInteger row = [indexPath row];
if (self.tableView.editing ) {
if(indexPath.section==0 && newActiveSetting != row) {
NSArray *row0 = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:newActiveSetting inSection:0]];
newActiveSetting = row;
NSArray *row1 = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:newActiveSetting inSection:0]];
[self.tableView beginUpdates];
[self.tableView reloadRowsAtIndexPaths:row0 withRowAnimation:UITableViewRowAnimationFade];
[self.tableView reloadRowsAtIndexPaths:row1 withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];
}
}
else {
if( indexPath.section==0 ) {
NSMutableDictionary* setting=[self.settings objectAtIndex:row];
SettingViewController* settingView = [[SettingViewController alloc] initWithSettingDatasource:setting];
[self.navigationController setToolbarHidden:NO animated:NO];
[self.navigationController pushViewController:settingView animated:YES];
[settingView release];
}
else {
MixerViewController* extraView=nil;
 
switch (indexPath.row) {
case 0:
extraView = [[MixerViewController alloc] initWithStyle:UITableViewStylePlain];
break;
case 1:
extraView = [[EngineTestViewController alloc] initWithStyle:UITableViewStyleGrouped];
break;
case 2:
extraView = [[ChannelsViewController alloc] initWithStyle:UITableViewStylePlain];
break;
}
[self.navigationController setToolbarHidden:NO animated:NO];
[self.navigationController pushViewController:extraView animated:YES];
[extraView release];
}
}
}
 
 
 
 
@end