OSDN Git Service

Refactoring
[tombo/Tombo.git] / iOS / Tombo / Tombo / MasterViewController.m
1 #import "MasterViewController.h"
2 #import "DetailViewController.h"
3
4 #import "Storage.h"
5 #import "CustomSegue.h"
6 #import "FileItem.h"
7
8 @interface MasterViewController () {
9     NSMutableArray *_objects;
10     Storage *storage;
11     
12     UIImage *imgFolder;
13     UIImage *imgDocument;
14     UIImage *imgUp;
15 }
16 @end
17
18 @implementation MasterViewController
19
20 @synthesize detailViewController = _detailViewController;
21
22 - (void)awakeFromNib
23 {
24     if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
25         self.clearsSelectionOnViewWillAppear = NO;
26         self.contentSizeForViewInPopover = CGSizeMake(320.0, 600.0);
27     }
28     [super awakeFromNib];
29 }
30
31 - (void)viewDidLoad
32 {
33     [super viewDidLoad];
34         // Do any additional setup after loading the view, typically from a nib.
35     self.navigationItem.leftBarButtonItem = self.editButtonItem;
36
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];
40     
41     imgFolder = nil;
42     imgDocument = nil;
43     if (!storage) {
44         storage = [Storage init];
45     }
46     // Load initial items.
47     [self insertItems];
48 }
49
50 - (void)viewDidUnload
51 {
52     [super viewDidUnload];
53     // Release any retained subviews of the main view.
54     storage = nil;
55 }
56
57 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
58 {
59     if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
60         return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
61     } else {
62         return YES;
63     }
64 }
65
66 - (void)insertNewObject:(id)sender
67 {
68     FileItem *item = [FileItem allocWithName: [[NSDate date] description]];
69     [self insertItem: item];
70 }
71
72 #pragma mark - Item operations
73
74 - (void)insertItems {
75     for (FileItem *file in [storage listItems]) {
76         [self insertItem: file];
77     }    
78     if (!storage.isTopDir) {
79         FileItem *up = [FileItem allocWithName:@"UP"];
80         up.isUp = YES;
81         [self insertItem: up];
82     }
83 }
84
85 - (void)removeAllItems {
86     NSUInteger n = [_objects count];
87     
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];
92     }
93     [_objects removeAllObjects];
94     [self.tableView deleteRowsAtIndexPaths: rmItems withRowAnimation:UITableViewRowAnimationAutomatic];
95 }
96
97 - (void)insertItem:(FileItem *)item {
98     if (!_objects) {
99         _objects = [[NSMutableArray alloc] init];
100     }
101     [_objects insertObject:item atIndex:0];
102     NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
103     [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
104                           withRowAnimation:UITableViewRowAnimationAutomatic];
105 }
106
107 #pragma mark - Table View
108
109 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
110 {
111     return 1;
112 }
113
114 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
115 {
116     return _objects.count;
117 }
118
119 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
120 {
121     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
122
123     FileItem *fItem = [_objects objectAtIndex:indexPath.row];
124     cell.textLabel.text = [fItem name];
125     if (fItem.isUp) {
126         if (!imgUp) {
127             imgUp = [UIImage imageNamed:@"sub_blue_up-32"];
128         }
129         cell.imageView.image = imgUp;
130     } else if (fItem.isDirectory) {
131         if (!imgFolder) {
132             imgFolder = [UIImage imageNamed:@"Folder-32"];
133
134         }
135         cell.imageView.image = imgFolder;
136     } else {
137         if (!imgDocument) {
138             imgDocument = [UIImage imageNamed:@"TextDocument-32"];
139         }
140         cell.imageView.image = imgDocument;
141     }
142     return cell;
143 }
144
145 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
146 {
147     // Return NO if you do not want the specified item to be editable.
148     return YES;
149 }
150
151 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
152 {
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.
158     }
159 }
160
161 /*
162 // Override to support rearranging the table view.
163 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
164 {
165 }
166 */
167
168 /*
169 // Override to support conditional rearranging of the table view.
170 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
171 {
172     // Return NO if you do not want the item to be re-orderable.
173     return YES;
174 }
175 */
176
177 - (void)transitDetailView:(NSIndexPath *)indexPath controller:(DetailViewController*)controller {
178     FileItem *item = [_objects objectAtIndex:indexPath.row];
179     if (item.isUp) {
180         // switch view items
181         [self removeAllItems];
182         [storage updir];
183         [self insertItems];
184     } else if (item.isDirectory) {
185         // switch view items
186         [self removeAllItems];
187         [storage chdir: item.name];
188         [self insertItems];
189         
190     } else {
191         [controller setDetailItem:item];
192     }
193     
194 }
195
196 // Select Row(iPhone/iPad)
197 // set item for iPad
198 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
199 {
200     if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
201         [self transitDetailView:indexPath controller:self.detailViewController];
202     }
203 }
204
205 // set item (for iPhone)
206 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
207 {
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;            
214         }
215         [self transitDetailView:indexPath controller:[segue destinationViewController]];
216     }
217 }
218
219 @end