/
BFRefreshBriefCellController.m
240 lines (184 loc) · 8.58 KB
/
BFRefreshBriefCellController.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
//
// BFRefreshBriefCellController.m
// Briefs
//
// Created by Rob Rhyne on 5/1/10.
// Copyright Digital Arch Design, 2009-2010. See LICENSE file for details.
//
#import "BFRefreshBriefCellController.h"
#import "BFSceneManager.h"
#import "BFPresentationDispatch.h"
#import "BFDataManager.h"
#import "BFConfig.h"
@implementation BFRefreshBriefCellController
@synthesize brief, navigation;
///////////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark NSObject overrides
- (id)initWithBrief:(BriefRef *)item
{
self = [self init];
if (self != nil) {
self.brief = item;
cell = nil;
}
return self;
}
- (void)dealloc
{
[self.brief release];
[super dealloc];
}
///////////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark BFLoadingViewDelegate Methods
- (IBAction)updateBriefData
{
NSLog(@"Is the accessory view registering the taps?");
// setup the loading controller
NSString *location = self.brief.fromURL;
BFLoadingViewController *loadingController = [[BFLoadingViewController alloc] initWithNibName:@"CellRefreshLoad" bundle:nil];
loadingController.view.frame = CGRectOffset(loadingController.view.frame, 60.0f, 5.0f);
loadingController.view.alpha = 0.0;
[cell addSubview:[loadingController view]];
[UIView beginAnimations:@"fade-in refresh view" context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationDuration:0.5f];
name.alpha = 0.0f;
desc.alpha = 0.0f;
[(UIButton *)cell.accessoryView setEnabled:NO];
loadingController.view.alpha = 1.0f;
[UIView commitAnimations];
[loadingController setDelegate:self];
[loadingController load:location withStatus:@"Changing underwear..."];
}
- (void)loadingFadeDidStop:(NSString *)animationId finished:(NSNumber *)finished context:(void *)context
{
BFLoadingViewController *controller = context;
[[controller view] removeFromSuperview];
}
- (void)beginLoadingFadeOutAnimation:(id)loading
{
BFLoadingViewController *controller = loading;
[UIView beginAnimations:@"fade-out refresh view" context:controller];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationDuration:0.5f];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(loadingFadeDidStop:finished:context:)];
name.alpha = 1.0f;
desc.alpha = 1.0f;
controller.view.alpha = 0.0f;
[(UIButton *)cell.accessoryView setEnabled:YES];
[UIView commitAnimations];
}
- (void)loadingView:(BFLoadingViewController *)controller didCompleteWithData:(NSData *)data
{
[self performSelector:@selector(beginLoadingFadeOutAnimation:) withObject:controller afterDelay:1.0f];
// refresh data
self.brief = [[BFDataManager sharedBFDataManager] updateBrief:self.brief usingData:data];
}
- (void)loadingView:(BFLoadingViewController *)controller didNotCompleteWithError:(NSError *)error
{
// TODO: handle error
[self performSelector:@selector(beginLoadingFadeOutAnimation:) withObject:controller afterDelay:1.0f];
}
- (void)loadingView:(BFLoadingViewController *)controller didCancelConnection:(NSString *)url
{
// TODO: handle cancelation
}
///////////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark Cell Controller methods
- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
cell = [tv dequeueReusableCellWithIdentifier:@"BriefsReloadCell"];
if (cell == nil) {
NSArray *nibArray = [[NSBundle mainBundle] loadNibNamed:@"BFRefreshBriefCell" owner:self options:nil];
cell = (UITableViewCell *) [nibArray objectAtIndex:0];
}
name.text = [self.brief title];
desc.text = [NSString stringWithFormat:@"%@ scenes, last opened %@",
self.brief.totalNumberOfScenes, [BFConfig shortDateStringFromDate:self.brief.dateLastOpened]];
// Miserable hack to get custom accessory view
UIImage *refreshImage = [UIImage imageNamed:@"refresh-accessory.png"];
UIButton *refresh = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
// configure target
[refresh addTarget:self action:@selector(updateBriefData) forControlEvents:UIControlEventTouchUpInside];
// configure images
[refresh setImage:refreshImage forState:UIControlStateNormal];
[refresh setImage:refreshImage forState:UIControlStateSelected];
[refresh setImage:refreshImage forState:UIControlStateHighlighted];
[refresh setImage:[UIImage imageNamed:@"refresh-accessory-disabled.png"] forState:UIControlStateDisabled];
// disable if the brief is local
if ([brief.briefcast isEqual:[[BFDataManager sharedBFDataManager] localBriefcastRefMarker]])
[refresh setEnabled:NO];
cell.accessoryView = refresh;
// calculate background
UIImage *bgImage, *selectedBgImage;
if (indexPath.row == 0) {
// top
bgImage = [[UIImage imageNamed:@"cell-top.png"] stretchableImageWithLeftCapWidth:10 topCapHeight:0];
selectedBgImage = [[UIImage imageNamed:@"cell-top-sel.png"] stretchableImageWithLeftCapWidth:10 topCapHeight:0];
}
else if (indexPath.row == [tv numberOfRowsInSection:indexPath.section]-1) {
// bottom
bgImage = [[UIImage imageNamed:@"cell-bottom.png"] stretchableImageWithLeftCapWidth:10 topCapHeight:0];
selectedBgImage = [[UIImage imageNamed:@"cell-bottom-sel.png"] stretchableImageWithLeftCapWidth:10 topCapHeight:0];
}
else {
// middle
bgImage = [[UIImage imageNamed:@"cell-middle.png"] stretchableImageWithLeftCapWidth:10 topCapHeight:0];
selectedBgImage = [[UIImage imageNamed:@"cell-middle-sel.png"] stretchableImageWithLeftCapWidth:10 topCapHeight:0];
}
cell.backgroundView = [[UIImageView alloc] initWithImage:bgImage];
cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:selectedBgImage];
return cell;
}
- (void)tableView:(UITableView *)tv didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *pathToDictionary = [[[BFDataManager sharedBFDataManager] documentDirectory] stringByAppendingPathComponent:[self.brief filePath]];
BFSceneManager *manager = [[BFSceneManager alloc] initWithPathToDictionary:pathToDictionary];
BFSceneViewController *sceneController = [[BFSceneViewController alloc] initWithSceneManager:manager];
// wire dispatch
if ([[BFPresentationDispatch sharedBFPresentationDispatch] viewController] != nil)
[BFPresentationDispatch sharedBFPresentationDispatch].viewController = nil;
[[BFPresentationDispatch sharedBFPresentationDispatch] setViewController:sceneController];
[BFPresentationDispatch sharedBFPresentationDispatch].viewController.delegate = self;
// get parent View
UIView *parentView = [self.navigation view];
[UIView beginAnimations:@"display brief" context:nil];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(briefShowDidStop:finished:context:)];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:parentView cache:YES];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationDuration:0.8f];
// swap loader for brief
[parentView addSubview:[BFPresentationDispatch sharedBFPresentationDispatch].viewController.view];
[UIView commitAnimations];
[manager release];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 50.0f;
}
///////////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark BFSceneViewDelegate Methods
- (void)briefShowDidStop:(NSString *)animationId finished:(NSNumber *)finished context:(void *)context
{
[[UIApplication sharedApplication] setStatusBarHidden:YES animated:YES];
}
- (void)sceneView:(BFSceneViewController *)controller shouldDismissView:(BOOL)animated
{
[[UIApplication sharedApplication] setStatusBarHidden:NO animated:YES];
[cell setSelected:NO animated:YES];
[UIView beginAnimations:@"remove brief" context:nil];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.navigation.view cache:YES];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationDuration:0.8f];
// swap brief for save view
[controller.view removeFromSuperview];
[UIView commitAnimations];
}
///////////////////////////////////////////////////////////////////////////////
@end