-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathItemsViewController.m
269 lines (225 loc) · 9.47 KB
/
ItemsViewController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
//
// ItemsViewController.m
// Homepwner
//
// Created by Jonathan Ballerano on 3/2/13.
// Copyright (c) 2013 jballer. All rights reserved.
//
#import "BNRItem.h"
#import "BNRItemStore.h"
#import "BNRImageStore.h"
#import "DetailViewController.h"
#import "HomepwnerItemCell.h"
#import "ImageViewController.h"
#import "ItemsViewController.h"
@implementation ItemsViewController
- (id)init
{
self = [super initWithStyle:UITableViewStyleGrouped];
if (self) {
[[self navigationItem] setTitle:@"Homepwner"];
[[self navigationItem] setRightBarButtonItem:[[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self action:@selector(addNewItem:)]];
[[self navigationItem] setLeftBarButtonItem:[self editButtonItem]];
}
return self;
}
- (id)initWithStyle:(UITableViewStyle)style
{
return [self init];
}
- (void)viewDidLoad
{
// Load the NIB file
UINib *nib = [UINib nibWithNibName:@"HomepwnerItemCell" bundle:nil];
// Register it with the TableView
[[self tableView] registerNib:nib forCellReuseIdentifier:@"HomepwnerItemCell"];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
// Load data that was changed by the detail view
[[self tableView] reloadData];
}
#pragma mark -
#pragma mark Custom Methods
- (void)addNewItem:(id)sender
{
// Make a new index path for the last row of the 0th section
BNRItem *newItem = [[BNRItemStore sharedStore] createItem];
DetailViewController *detailViewController = [[DetailViewController alloc] initForNewItem:YES];
[detailViewController setItem:newItem];
[detailViewController setTitle:@"New Item"];
[detailViewController setDismissBlock:^{
[[self tableView] reloadData];
}];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:detailViewController];
[navController setModalPresentationStyle:UIModalPresentationFullScreen];
[navController setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
[self presentViewController:navController animated:YES completion:nil];
}
- (void)showImage:(id)sender atIndexPath:(NSIndexPath *)indexPath
{
// My initial guess:
// UIViewController *ivc = [[UIViewController alloc] init];
// [self presentViewController:ivc
// animated:YES
// completion:…]
// get the item for the indexpath
BNRItem *item = [[[BNRItemStore sharedStore] allItems] objectAtIndex:[indexPath row]];
UIImage *img = [[BNRImageStore sharedStore] imageForKey:[item imageKey]];
if (!img)
return;
// Make a new ImageViewController and set its image
ImageViewController *ivc = [[ImageViewController alloc] init];
[ivc setImage:img];
// Use a popover if this is on an iPad
if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)
{
// Make a rect of the button relative to this view
CGRect rect = [[self view] convertRect:[sender bounds] fromView:sender];
// Present a 600x600 popover from the rect
imagePopover = [[UIPopoverController alloc] initWithContentViewController:ivc];
[imagePopover setDelegate:self];
[imagePopover setPopoverContentSize:CGSizeMake(600, 600)];
[imagePopover presentPopoverFromRect:rect
inView:[self view]
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
}
else
{
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(dismissModalViewController:)];
UINavigationController *nav = [[UINavigationController alloc] init];
[nav setViewControllers:[NSArray arrayWithObject:ivc]];
[[ivc navigationItem] setRightBarButtonItem:doneButton];
[self presentViewController:nav animated:YES completion:nil];
}
}
- (void)dismissModalViewController:(UIViewController *)sender
{
[self dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark -
#pragma mark <UIPopoverControllerDelegate>
- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController
{
[imagePopover dismissPopoverAnimated:YES];
imagePopover = nil;
}
#pragma mark <UITableViewDataSource>
// The following two methods are required to implement a Header View
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
return [[[BNRItemStore sharedStore] allItems] count] + 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// The last item in the list is static
if ([indexPath row] == ([[self tableView] numberOfRowsInSection:[indexPath section]] - 1))
{
// Last item (static)
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"];
[[cell textLabel] setText:@"No more items!"];
return cell;
}
// The rest of the items come from the BNRItemStore
else
{
BNRItem *currentItem = [[[BNRItemStore sharedStore] allItems] objectAtIndex:[indexPath row]];
HomepwnerItemCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HomepwnerItemCell"];
[[cell nameLabel] setText:[currentItem itemName]];
[[cell serialNumberLabel] setText:[currentItem serialNumber]];
// [[cell valueLabel] setText:[NSString stringWithFormat:@"$%d", [currentItem valueInDollars]]];
NSString *currencySymbol = [[NSLocale currentLocale] objectForKey:NSLocaleCurrencySymbol];
cell.valueLabel.text = [NSString stringWithFormat:@"%@%d", currencySymbol, [currentItem valueInDollars]];
[[cell thumbnailView] setImage:[currentItem thumbnail]];
// BRONZE CHALLENGE
[[cell valueLabel] setTextColor:([currentItem valueInDollars] > 50) ? [UIColor greenColor] : [UIColor redColor]];
// Tell the cell about the controller and view so it can act on them
[cell setController:self];
[cell setTableView:tableView];
return cell;
}
}
// Delete object when deleted by user in Edit mode
- (void)tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([indexPath row] >= [[self tableView] numberOfRowsInSection:[indexPath section]] - 1){
return;
}
else if (editingStyle == UITableViewCellEditingStyleDelete)
{
BNRItem *currentItem = [[[BNRItemStore sharedStore] allItems] objectAtIndex:[indexPath row]];
[[BNRItemStore sharedStore] removeItem:currentItem];
[[self tableView] deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
}
// Move object when user reorders
- (void)tableView:(UITableView *)tableView
moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath
toIndexPath:(NSIndexPath *)destinationIndexPath
{
[[BNRItemStore sharedStore] moveItemFromIndex:[sourceIndexPath row] toIndex:[destinationIndexPath row]];
}
#pragma mark <UITableViewDelegate>
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView
editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Don't allow editing for the last row
if ([indexPath row] >= [[self tableView] numberOfRowsInSection:[indexPath section]] - 1)
return UITableViewCellEditingStyleNone;
else
return UITableViewCellEditingStyleDelete;
}
// BRONZE CHALLENGE
- (NSString *) tableView:(UITableView *)tableView
titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
return @"Remove";
}
// SILVER CHALLENGE
- (BOOL) tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
BOOL canEdit = true;
if ([indexPath row] == ([[self tableView] numberOfRowsInSection:[indexPath section]] - 1))
{
canEdit = false;
}
return canEdit;
}
// GOLD CHALLENGE
- (NSIndexPath *) tableView:(UITableView *)tableView
targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath
toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
int maxRow = [[self tableView] numberOfRowsInSection:[proposedDestinationIndexPath section]] - 2;
if (([proposedDestinationIndexPath row] >= maxRow))
{
// This means it went past the static row. Cap it at the second-to-last row.
proposedDestinationIndexPath = [NSIndexPath indexPathForRow:maxRow
inSection:[proposedDestinationIndexPath section]];
// NSLog(@"dragged too far");
}
return proposedDestinationIndexPath;
}
- (void) tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([indexPath row] != [[self tableView] numberOfRowsInSection:[indexPath section]] - 1)
{
DetailViewController *detail = [[DetailViewController alloc] initForNewItem:NO];
[detail setItem:[[[BNRItemStore sharedStore] allItems] objectAtIndex:[indexPath row]]];
[[self navigationController] pushViewController:detail animated:YES];
}
else
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
}
@end