]> git.saurik.com Git - cydia.git/blame - UICaboodle/RVBook.mm
Factored out common code to common subproject.
[cydia.git] / UICaboodle / RVBook.mm
CommitLineData
77f175ac
JF
1#import "RVBook.h"
2
3#import <UIKit/UINavigationBar.h>
4#import <UIKit/UINavigationItem.h>
5
6#import <UIKit/UITransitionView.h>
7
8#import <UIKit/UIView-Geometry.h>
9#import <UIKit/UIView-Hierarchy.h>
10
11#import "RVPage.h"
12
13@implementation RVBook
14
15- (void) dealloc {
16 [navbar_ setDelegate:nil];
17
18 [pages_ release];
19 [navbar_ release];
20 [transition_ release];
21 [super dealloc];
22}
23
24- (void) navigationBar:(UINavigationBar *)navbar buttonClicked:(int)button {
25 _assert([pages_ count] != 0);
26 RVPage *page = [pages_ lastObject];
27 switch (button) {
28 case 0: [page _rightButtonClicked]; break;
29 case 1: [page _leftButtonClicked]; break;
30 }
31}
32
33- (void) navigationBar:(UINavigationBar *)navbar poppedItem:(UINavigationItem *)item {
34 _assert([pages_ count] != 0);
35 if (!resetting_)
36 [[pages_ lastObject] setPageActive:NO];
37 [pages_ removeLastObject];
38 if (!resetting_)
39 [self resetViewAnimated:YES toPage:[pages_ lastObject]];
40}
41
42- (id) initWithFrame:(CGRect)frame {
43 if ((self = [super initWithFrame:frame]) != nil) {
44 pages_ = [[NSMutableArray arrayWithCapacity:4] retain];
45
46 struct CGRect bounds = [self bounds];
47 CGSize navsize = [UINavigationBar defaultSizeWithPrompt];
48 CGRect navrect = {{0, 0}, navsize};
49
50 navbar_ = [[UINavigationBar alloc] initWithFrame:navrect];
51 [self addSubview:navbar_];
52
53 [navbar_ setBarStyle:1];
54 [navbar_ setDelegate:self];
55
56 [navbar_ setPrompt:@""];
57
58 transition_ = [[UITransitionView alloc] initWithFrame:CGRectMake(
59 bounds.origin.x, bounds.origin.y + navsize.height, bounds.size.width, bounds.size.height - navsize.height
60 )];
61
62 [self addSubview:transition_];
63 } return self;
64}
65
66- (void) setDelegate:(id)delegate {
67 delegate_ = delegate;
68}
69
70- (void) setPage:(RVPage *)page {
71 if ([pages_ count] != 0)
72 [[pages_ lastObject] setPageActive:NO];
73
74 [navbar_ disableAnimation];
75 resetting_ = true;
76 for (unsigned i(0), pages([pages_ count]); i != pages; ++i)
77 [navbar_ popNavigationItem];
78 resetting_ = false;
79
80 [self pushPage:page];
81 [navbar_ enableAnimation];
82}
83
84- (void) pushPage:(RVPage *)page {
85 if ([pages_ count] != 0)
86 [[pages_ lastObject] setPageActive:NO];
87
88 NSString *title = [self getTitleForPage:page];
89
90 NSString *backButtonTitle = [page backButtonTitle];
91 if (backButtonTitle == nil)
92 backButtonTitle = title;
93
94 UINavigationItem *navitem = [[[UINavigationItem alloc] initWithTitle:title] autorelease];
95 [navitem setBackButtonTitle:backButtonTitle];
96 [navbar_ pushNavigationItem:navitem];
97
98 BOOL animated = [pages_ count] == 0 ? NO : YES;
99 [transition_ transition:(animated ? 1 : 0) toView:page];
100 [page setPageActive:YES];
101
102 [pages_ addObject:page];
103 [self reloadButtonsForPage:page];
104
105 [navbar_ setAccessoryView:[page accessoryView] animate:animated goingBack:NO];
106}
107
108- (void) popPages:(unsigned)pages {
109 if (pages == 0)
110 return;
111
112 [[pages_ lastObject] setPageActive:NO];
113
114 resetting_ = true;
115 for (unsigned i(0); i != pages; ++i)
116 [navbar_ popNavigationItem];
117 resetting_ = false;
118
119 [self resetViewAnimated:YES toPage:[pages_ lastObject]];
120}
121
122- (void) setPrompt:(NSString *)prompt {
123 [navbar_ setPrompt:prompt];
124}
125
126- (void) resetViewAnimated:(BOOL)animated {
127 resetting_ = true;
128
129 if ([pages_ count] > 1) {
130 [navbar_ disableAnimation];
131 while ([pages_ count] != (animated ? 2 : 1))
132 [navbar_ popNavigationItem];
133 [navbar_ enableAnimation];
134 if (animated)
135 [navbar_ popNavigationItem];
136 }
137
138 resetting_ = false;
139
140 [self resetViewAnimated:animated toPage:[pages_ lastObject]];
141}
142
143- (void) resetViewAnimated:(BOOL)animated toPage:(RVPage *)page {
144 [page resetViewAnimated:animated];
145 [transition_ transition:(animated ? 2 : 0) toView:page];
146 [page setPageActive:YES];
147 [self reloadButtonsForPage:page];
148 [navbar_ setAccessoryView:[page accessoryView] animate:animated goingBack:YES];
149}
150
151- (void) setTitle:(NSString *)title forPage:(RVPage *)page {
152 if ([pages_ count] == 0 || page != [pages_ lastObject])
153 return;
154 UINavigationItem *navitem = [navbar_ topItem];
155 [navitem setTitle:title];
156}
157
158- (void) setBackButtonTitle:(NSString *)title forPage:(RVPage *)page {
159 if ([pages_ count] == 0 || page != [pages_ lastObject])
160 return;
161 UINavigationItem *navitem = [navbar_ topItem];
162 [navitem setBackButtonTitle:title];
163}
164
165- (void) reloadButtonsForPage:(RVPage *)page {
166 if ([pages_ count] == 0 || page != [pages_ lastObject])
167 return;
168 NSString *leftButtonTitle([pages_ count] == 1 ? [page leftButtonTitle] : nil);
169 [navbar_ showButtonsWithLeftTitle:leftButtonTitle rightTitle:[page rightButtonTitle]];
170}
171
172- (NSString *) getTitleForPage:(RVPage *)page {
173 return [page title];
174}
175
176- (void) reloadData {
177 for (int i(0), e([pages_ count]); i != e; ++i) {
178 RVPage *page([pages_ objectAtIndex:(e - i - 1)]);
179 [page reloadData];
180 }
181}
182
183- (CGRect) pageBounds {
184 return [transition_ bounds];
185}
186
187@end