cocoaTouch/SettingsViewController.m
changeset 2803 1f446fc5c8ec
parent 2799 558b29bf00c5
child 2805 36a8cebb91e8
equal deleted inserted replaced
2802:28cd5b87bce9 2803:1f446fc5c8ec
    26 }
    26 }
    27 
    27 
    28 -(void) viewDidLoad {
    28 -(void) viewDidLoad {
    29 	NSString *filePath = [[SDLUIKitDelegate sharedAppDelegate] dataFilePath:@"settings.plist"];
    29 	NSString *filePath = [[SDLUIKitDelegate sharedAppDelegate] dataFilePath:@"settings.plist"];
    30 	
    30 	
    31 	needsReset = NO;
       
    32 	if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {	
    31 	if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {	
    33 		NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
    32 		NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
    34 		username.text = [data objectForKey:@"username"];
    33 		username.text = [data objectForKey:@"username"];
    35 		password.text = [data objectForKey:@"password"];
    34 		password.text = [data objectForKey:@"password"];
    36 		if (1 == [[data objectForKey:@"music"] intValue]) {
    35 		if (1 == [[data objectForKey:@"music"] intValue]) {
    73 	self.settingsTable = nil;
    72 	self.settingsTable = nil;
    74 	self.buttonContainer = nil;
    73 	self.buttonContainer = nil;
    75 	[super viewDidUnload];
    74 	[super viewDidUnload];
    76 }
    75 }
    77 
    76 
    78 //- (void)applicationWillTerminate:(NSNotification *)notification {
       
    79 -(void) viewWillDisappear:(BOOL)animated {
       
    80 	if (!needsReset) {
       
    81 		NSMutableDictionary *saveDict = [[NSMutableDictionary alloc] init];
       
    82 		NSString *tmpMus = (musicSwitch.on) ? @"1" : @"0";
       
    83 		NSString *tmpEff = (soundsSwitch.on) ? @"1" : @"0";
       
    84 		NSString *tmpAlt = (altDamageSwitch.on) ? @"1" : @"0";
       
    85 		
       
    86 		[saveDict setObject:username.text forKey:@"username"];
       
    87 		[saveDict setObject:password.text forKey:@"password"];
       
    88 		[saveDict setObject:tmpMus forKey:@"music"];
       
    89 		[saveDict setObject:tmpEff forKey:@"sounds"];
       
    90 		[saveDict setObject:tmpAlt forKey:@"alternate"];
       
    91 		
       
    92 		[saveDict writeToFile:[[SDLUIKitDelegate sharedAppDelegate] dataFilePath:@"settings.plist"] atomically:YES];
       
    93 		[saveDict release];
       
    94 	}
       
    95 	[super viewWillDisappear:animated];
       
    96 }
       
    97 
       
    98 -(void) dealloc {
    77 -(void) dealloc {
    99 	[username release];
    78 	[username release];
   100 	[password release];
    79 	[password release];
   101 	[musicSwitch release];
    80 	[musicSwitch release];
   102 	[soundsSwitch release];
    81 	[soundsSwitch release];
   110 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    89 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
   111     // Return YES for supported orientations
    90     // Return YES for supported orientations
   112     return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
    91     return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
   113 }
    92 }
   114 
    93 
   115 
       
   116 // makes the keyboard go away when background is tapped
    94 // makes the keyboard go away when background is tapped
   117 -(IBAction) backgroundTap: (id)sender {
    95 -(IBAction) backgroundTap: (id)sender {
   118 	[username resignFirstResponder];
    96 	[username resignFirstResponder];
   119 	[password resignFirstResponder];
    97 	[password resignFirstResponder];
   120 }
    98 }
   134 // don't enable music when sound is off
   112 // don't enable music when sound is off
   135 -(void) checkValueSwitch {
   113 -(void) checkValueSwitch {
   136 	if (NO == self.soundsSwitch.on) {
   114 	if (NO == self.soundsSwitch.on) {
   137 		[musicSwitch setOn:!musicSwitch.on animated:YES];
   115 		[musicSwitch setOn:!musicSwitch.on animated:YES];
   138 	}
   116 	}
       
   117 }
       
   118 
       
   119 #pragma mark -
       
   120 #pragma mark Return to mainView
       
   121 -(void) flushData {
       
   122 	NSLog(@"writing preferences to file");
       
   123 
       
   124 	NSMutableDictionary *saveDict = [[NSMutableDictionary alloc] init];
       
   125 	NSString *tmpMus = (musicSwitch.on) ? @"1" : @"0";
       
   126 	NSString *tmpEff = (soundsSwitch.on) ? @"1" : @"0";
       
   127 	NSString *tmpAlt = (altDamageSwitch.on) ? @"1" : @"0";
       
   128 	
       
   129 	[saveDict setObject:username.text forKey:@"username"];
       
   130 	[saveDict setObject:password.text forKey:@"password"];
       
   131 	[saveDict setObject:tmpMus forKey:@"music"];
       
   132 	[saveDict setObject:tmpEff forKey:@"sounds"];
       
   133 	[saveDict setObject:tmpAlt forKey:@"alternate"];
       
   134 	
       
   135 	[saveDict writeToFile:[[SDLUIKitDelegate sharedAppDelegate] dataFilePath:@"settings.plist"] atomically:YES];
       
   136 	[saveDict release];
       
   137 }
       
   138 
       
   139 -(void) returnMainView {
       
   140 	[self flushData];
       
   141 
       
   142 	[UIView beginAnimations:@"Get Back" context:NULL];
       
   143 	[UIView setAnimationDuration:1];
       
   144 	
       
   145 	self.view.frame = CGRectMake(0, -320, 480, 320);
       
   146 	self.parentView.frame = CGRectMake(0, 0, 480, 320);
       
   147 	[UIView commitAnimations];
       
   148 	
       
   149 	[self.view performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:1];
       
   150 	self.parentView = nil;
   139 }
   151 }
   140 
   152 
   141 #pragma mark -
   153 #pragma mark -
   142 #pragma mark UIActionSheet Methods
   154 #pragma mark UIActionSheet Methods
   143 -(IBAction) deleteData: (id)sender {
   155 -(IBAction) deleteData: (id)sender {
   148 						   destructiveButtonTitle:NSLocalizedString(@"As sure as I can be!", @"")
   160 						   destructiveButtonTitle:NSLocalizedString(@"As sure as I can be!", @"")
   149 							otherButtonTitles:nil];
   161 							otherButtonTitles:nil];
   150 	[actionSheet showInView:self.view];
   162 	[actionSheet showInView:self.view];
   151 	[actionSheet release];
   163 	[actionSheet release];
   152 	 */
   164 	 */
   153 	[UIView beginAnimations:@"Get Back" context:NULL];
   165 	[self returnMainView];
   154 	[UIView setAnimationDuration:1];
       
   155 	//[UIView setAnimationDuration:UIViewAnimationCurveEaseOut];
       
   156 	
       
   157 	self.view.frame = CGRectMake(0, -320, 480, 320);
       
   158 	self.parentView.frame = CGRectMake(0, 0, 480, 320);
       
   159 	[UIView commitAnimations];
       
   160 
       
   161 	[self.view performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:1];
       
   162 	self.parentView = nil;
       
   163 }
   166 }
   164 
   167 
   165 -(void) actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger) buttonIndex {
   168 -(void) actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger) buttonIndex {
   166 	if ([actionSheet cancelButtonIndex] != buttonIndex) {
   169 	if ([actionSheet cancelButtonIndex] != buttonIndex) {
   167 		needsReset = YES;
       
   168 		
       
   169 		// get the documents dirctory
   170 		// get the documents dirctory
   170 		NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
   171 		NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
   171 		NSString *documentsDirectory = [paths objectAtIndex:0];
   172 		NSString *documentsDirectory = [paths objectAtIndex:0];
   172 		
   173 		
   173 		// get the content of the directory
   174 		// get the content of the directory