1 #import "MasterViewController.h"
2 #import "DetailViewController.h"
5 #import "CustomSegue.h"
8 @interface MasterViewController () {
9 NSMutableArray *_objects;
18 @implementation MasterViewController
20 @synthesize detailViewController = _detailViewController;
24 if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
25 self.clearsSelectionOnViewWillAppear = NO;
26 self.contentSizeForViewInPopover = CGSizeMake(320.0, 600.0);
34 // Do any additional setup after loading the view, typically from a nib.
35 self.navigationItem.leftBarButtonItem = self.editButtonItem;
37 UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject:)];
38 self.navigationItem.rightBarButtonItem = addButton;
39 self.detailViewController = (DetailViewController *)[[self.splitViewController.viewControllers lastObject] topViewController];
44 storage = [Storage init];
46 // Load initial items.
52 [super viewDidUnload];
53 // Release any retained subviews of the main view.
57 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
59 if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
60 return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
66 - (void)insertNewObject:(id)sender
68 FileItem *item = [FileItem allocWithName: [[NSDate date] description]];
69 [self insertItem: item];
72 #pragma mark - Item operations
75 for (FileItem *file in [storage listItems]) {
76 [self insertItem: file];
78 if (!storage.isTopDir) {
79 FileItem *up = [FileItem allocWithName:@"UP"];
81 [self insertItem: up];
85 - (void)removeAllItems {
86 NSUInteger n = [_objects count];
88 NSMutableArray *rmItems = [[NSMutableArray alloc] initWithCapacity:n];
89 for (int i = 0; i < n; i++) {
90 NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
91 [rmItems addObject: indexPath];
93 [_objects removeAllObjects];
94 [self.tableView deleteRowsAtIndexPaths: rmItems withRowAnimation:UITableViewRowAnimationAutomatic];
97 - (void)insertItem:(FileItem *)item {
99 _objects = [[NSMutableArray alloc] init];
101 [_objects insertObject:item atIndex:0];
102 NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
103 [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
104 withRowAnimation:UITableViewRowAnimationAutomatic];
107 #pragma mark - Table View
109 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
114 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
116 return _objects.count;
119 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
121 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
123 FileItem *fItem = [_objects objectAtIndex:indexPath.row];
124 cell.textLabel.text = [fItem name];
127 imgUp = [UIImage imageNamed:@"sub_blue_up-32"];
129 cell.imageView.image = imgUp;
130 } else if (fItem.isDirectory) {
132 imgFolder = [UIImage imageNamed:@"Folder-32"];
135 cell.imageView.image = imgFolder;
138 imgDocument = [UIImage imageNamed:@"TextDocument-32"];
140 cell.imageView.image = imgDocument;
145 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
147 // Return NO if you do not want the specified item to be editable.
151 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
153 if (editingStyle == UITableViewCellEditingStyleDelete) {
154 [_objects removeObjectAtIndex:indexPath.row];
155 [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
156 } else if (editingStyle == UITableViewCellEditingStyleInsert) {
157 // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
162 // Override to support rearranging the table view.
163 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
169 // Override to support conditional rearranging of the table view.
170 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
172 // Return NO if you do not want the item to be re-orderable.
177 - (void)transitDetailView:(NSIndexPath *)indexPath controller:(DetailViewController*)controller {
178 FileItem *item = [_objects objectAtIndex:indexPath.row];
181 [self removeAllItems];
184 } else if (item.isDirectory) {
186 [self removeAllItems];
187 [storage chdir: item.name];
191 [controller setDetailItem:item];
196 // Select Row(iPhone/iPad)
198 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
200 if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
201 [self transitDetailView:indexPath controller:self.detailViewController];
205 // set item (for iPhone)
206 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
208 if ([[segue identifier] isEqualToString:@"showDetail"]) {
209 NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
210 FileItem *item = [_objects objectAtIndex:indexPath.row];
211 if (item.isUp || item.isDirectory) {
212 CustomSegue *customSegue = (CustomSegue*)segue;
213 customSegue.isStop = YES;
215 [self transitDetailView:indexPath controller:[segue destinationViewController]];