cocoaTouch/SchemeSettingsViewController.m
changeset 3479 972ae3ec178a
parent 3356 3ae3fccb439e
child 3490 016b3172b645
equal deleted inserted replaced
3478:cbf71e938164 3479:972ae3ec178a
     5 //  Created by Vittorio on 19/04/10.
     5 //  Created by Vittorio on 19/04/10.
     6 //  Copyright 2010 __MyCompanyName__. All rights reserved.
     6 //  Copyright 2010 __MyCompanyName__. All rights reserved.
     7 //
     7 //
     8 
     8 
     9 #import "SchemeSettingsViewController.h"
     9 #import "SchemeSettingsViewController.h"
    10 
    10 #import "CommodityFunctions.h"
       
    11 #import "SingleSchemeViewController.h"
    11 
    12 
    12 @implementation SchemeSettingsViewController
    13 @implementation SchemeSettingsViewController
       
    14 @synthesize listOfSchemes;
    13 
    15 
    14 
    16 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    15 #pragma mark -
    17     return rotationManager(interfaceOrientation);
    16 #pragma mark Initialization
       
    17 
       
    18 /*
       
    19 - (id)initWithStyle:(UITableViewStyle)style {
       
    20     // Override initWithStyle: if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
       
    21     if ((self = [super initWithStyle:style])) {
       
    22     }
       
    23     return self;
       
    24 }
    18 }
    25 */
       
    26 
    19 
    27 
    20 
    28 #pragma mark -
    21 #pragma mark -
    29 #pragma mark View lifecycle
    22 #pragma mark View lifecycle
    30 
    23 -(void) viewDidLoad {
    31 /*
       
    32 - (void)viewDidLoad {
       
    33     [super viewDidLoad];
    24     [super viewDidLoad];
    34 
    25     
    35     // Uncomment the following line to preserve selection between presentations.
    26     UIBarButtonItem *editButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Edit",@"from the scheme panel")
    36     self.clearsSelectionOnViewWillAppear = NO;
    27                                                                    style:UIBarButtonItemStyleBordered
    37  
    28                                                                   target:self
    38     // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    29                                                                   action:@selector(toggleEdit:)];
    39     // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    30     self.navigationItem.rightBarButtonItem = editButton;
    40 }
    31     [editButton release];
    41 */
    32     
    42 
       
    43 /*
       
    44 - (void)viewWillAppear:(BOOL)animated {
       
    45     [super viewWillAppear:animated];
       
    46 }
       
    47 */
       
    48 /*
       
    49 - (void)viewDidAppear:(BOOL)animated {
       
    50     [super viewDidAppear:animated];
       
    51 }
       
    52 */
       
    53 /*
       
    54 - (void)viewWillDisappear:(BOOL)animated {
       
    55     [super viewWillDisappear:animated];
       
    56 }
       
    57 */
       
    58 /*
       
    59 - (void)viewDidDisappear:(BOOL)animated {
       
    60     [super viewDidDisappear:animated];
       
    61 }
       
    62 */
       
    63 
       
    64 
       
    65 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
       
    66     // Override to allow orientations other than the default portrait orientation.
       
    67     return YES;
       
    68 }
    33 }
    69 
    34 
       
    35 -(void) viewWillAppear:(BOOL) animated {
       
    36     [super viewWillAppear:animated];
       
    37     
       
    38     NSArray *contentsOfDir = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:SCHEMES_DIRECTORY() error:NULL];
       
    39     NSMutableArray *array = [[NSMutableArray alloc] initWithArray:contentsOfDir copyItems:YES];
       
    40     self.listOfSchemes = array;
       
    41     [array release];
       
    42     
       
    43     [self.tableView reloadData];
       
    44 }
       
    45 
       
    46 // modifies the navigation bar to add the "Add" and "Done" buttons
       
    47 -(void) toggleEdit:(id) sender {
       
    48     BOOL isEditing = self.tableView.editing;
       
    49     [self.tableView setEditing:!isEditing animated:YES];
       
    50     
       
    51     if (isEditing) {
       
    52         [self.navigationItem.rightBarButtonItem setTitle:NSLocalizedString(@"Edit",@"from the scheme panel")];
       
    53         [self.navigationItem.rightBarButtonItem setStyle: UIBarButtonItemStyleBordered];
       
    54         self.navigationItem.leftBarButtonItem = self.navigationItem.backBarButtonItem;
       
    55     } else {
       
    56         [self.navigationItem.rightBarButtonItem setTitle:NSLocalizedString(@"Done",@"from the scheme panel")];
       
    57         [self.navigationItem.rightBarButtonItem setStyle:UIBarButtonItemStyleDone];
       
    58         UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Add",@"from the scheme panel")
       
    59                                                                       style:UIBarButtonItemStyleBordered
       
    60                                                                      target:self
       
    61                                                                      action:@selector(addScheme:)];
       
    62         self.navigationItem.leftBarButtonItem = addButton;
       
    63         [addButton release];
       
    64     }
       
    65 }
       
    66 
       
    67 -(void) addScheme:(id) sender {
       
    68     NSString *fileName = [[NSString alloc] initWithFormat:@"Scheme %u.plist", [self.listOfSchemes count]];
       
    69     
       
    70     createSchemeNamed([fileName stringByDeletingPathExtension]);
       
    71     
       
    72     [self.listOfSchemes addObject:fileName];
       
    73     [fileName release];
       
    74     
       
    75     // order the array alphabetically, so schemes will keep their position
       
    76     [self.listOfSchemes sortUsingSelector:@selector(compare:)];
       
    77     
       
    78     [self.tableView reloadData];
       
    79 }
    70 
    80 
    71 #pragma mark -
    81 #pragma mark -
    72 #pragma mark Table view data source
    82 #pragma mark Table view data source
    73 
    83 -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    74 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
       
    75     // Return the number of sections.
       
    76     return 1;
    84     return 1;
    77 }
    85 }
    78 
    86 
    79 
    87 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    80 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    88     return [self.listOfSchemes count];
    81     // Return the number of rows in the section.
       
    82     return 1;
       
    83 }
    89 }
    84 
    90 
    85 
    91 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    86 // Customize the appearance of table view cells.
       
    87 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
       
    88     
       
    89     static NSString *CellIdentifier = @"Cell";
    92     static NSString *CellIdentifier = @"Cell";
    90     
    93     
    91     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    94     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    92     if (cell == nil) {
    95     if (cell == nil) {
    93         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    96         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    94     }
    97     }
    95     
    98     
    96     // Configure the cell...
    99     NSUInteger row = [indexPath row]; 
       
   100     NSString *rowString = [[self.listOfSchemes objectAtIndex:row] stringByDeletingPathExtension]; 
       
   101     cell.textLabel.text = rowString; 
       
   102     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    97     
   103     
    98     return cell;
   104     return cell;
    99 }
   105 }
   100 
   106 
   101 
   107 // delete the row and the file
   102 /*
   108 -(void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
   103 // Override to support conditional editing of the table view.
   109     NSUInteger row = [indexPath row];
   104 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
   110     
   105     // Return NO if you do not want the specified item to be editable.
   111     NSString *schemeFile = [[NSString alloc] initWithFormat:@"%@/%@",SCHEMES_DIRECTORY(),[self.listOfSchemes objectAtIndex:row]];
   106     return YES;
   112     [[NSFileManager defaultManager] removeItemAtPath:schemeFile error:NULL];
       
   113     [schemeFile release];
       
   114     
       
   115     [self.listOfSchemes removeObjectAtIndex:row];
       
   116     [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
   107 }
   117 }
   108 */
       
   109 
       
   110 
       
   111 /*
       
   112 // Override to support editing the table view.
       
   113 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
       
   114     
       
   115     if (editingStyle == UITableViewCellEditingStyleDelete) {
       
   116         // Delete the row from the data source
       
   117         [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
       
   118     }   
       
   119     else if (editingStyle == UITableViewCellEditingStyleInsert) {
       
   120         // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
       
   121     }   
       
   122 }
       
   123 */
       
   124 
       
   125 
       
   126 /*
       
   127 // Override to support rearranging the table view.
       
   128 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
       
   129 }
       
   130 */
       
   131 
       
   132 
       
   133 /*
       
   134 // Override to support conditional rearranging of the table view.
       
   135 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
       
   136     // Return NO if you do not want the item to be re-orderable.
       
   137     return YES;
       
   138 }
       
   139 */
       
   140 
       
   141 
   118 
   142 #pragma mark -
   119 #pragma mark -
   143 #pragma mark Table view delegate
   120 #pragma mark Table view delegate
   144 
   121 
   145 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   122 -(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   146     // Navigation logic may go here. Create and push another view controller.
   123     if (childController == nil) {
   147 	/*
   124         childController = [[SingleSchemeViewController alloc] initWithStyle:UITableViewStyleGrouped];
   148 	 <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
   125     }
   149      // ...
   126     
   150      // Pass the selected object to the new view controller.
   127     NSInteger row = [indexPath row];
   151 	 [self.navigationController pushViewController:detailViewController animated:YES];
   128     NSString *selectedSchemeFile = [self.listOfSchemes objectAtIndex:row];
   152 	 [detailViewController release];
   129     
   153 	 */
   130     // this must be set so childController can load the correct plist
       
   131     childController.title = [selectedSchemeFile stringByDeletingPathExtension];
       
   132     [childController.tableView setContentOffset:CGPointMake(0,0) animated:NO];
       
   133 
       
   134     [self.navigationController pushViewController:childController animated:YES];
   154 }
   135 }
   155 
   136 
   156 
   137 
   157 #pragma mark -
   138 #pragma mark -
   158 #pragma mark Memory management
   139 #pragma mark Memory management
   159 
   140 -(void)didReceiveMemoryWarning {
   160 - (void)didReceiveMemoryWarning {
       
   161     // Releases the view if it doesn't have a superview.
       
   162     [super didReceiveMemoryWarning];
   141     [super didReceiveMemoryWarning];
   163     
       
   164     // Relinquish ownership any cached data, images, etc that aren't in use.
       
   165 }
   142 }
   166 
   143 
   167 - (void)viewDidUnload {
   144 -(void) viewDidUnload {
   168     // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
   145     self.listOfSchemes = nil;
   169     // For example: self.myOutlet = nil;
   146     childController = nil;
   170 }
   147 }
   171 
   148 
   172 
   149 
   173 - (void)dealloc {
   150 -(void) dealloc {
       
   151     [self.listOfSchemes release];
       
   152     [childController release];
   174     [super dealloc];
   153     [super dealloc];
   175 }
   154 }
   176 
   155 
   177 
   156 
   178 @end
   157 @end