]>
Commit | Line | Data |
---|---|---|
7e9a36b6 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 | ||
2388b078 | 53 | [navbar_ setBarStyle:0]; |
7e9a36b6 JF |
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 | ||
c15d236a JF |
105 | if ([navbar_ respondsToSelector:@selector(setAccessoryView:animate:goingBack:)]) |
106 | [navbar_ setAccessoryView:[page accessoryView] animate:animated goingBack:NO]; | |
107 | else | |
108 | [navbar_ setAccessoryView:[page accessoryView] animate:animated removeOnPop:NO]; | |
7e9a36b6 JF |
109 | } |
110 | ||
111 | - (void) popPages:(unsigned)pages { | |
112 | if (pages == 0) | |
113 | return; | |
114 | ||
115 | [[pages_ lastObject] setPageActive:NO]; | |
116 | ||
117 | resetting_ = true; | |
118 | for (unsigned i(0); i != pages; ++i) | |
119 | [navbar_ popNavigationItem]; | |
120 | resetting_ = false; | |
121 | ||
122 | [self resetViewAnimated:YES toPage:[pages_ lastObject]]; | |
123 | } | |
124 | ||
125 | - (void) setPrompt:(NSString *)prompt { | |
126 | [navbar_ setPrompt:prompt]; | |
127 | } | |
128 | ||
129 | - (void) resetViewAnimated:(BOOL)animated { | |
130 | resetting_ = true; | |
131 | ||
132 | if ([pages_ count] > 1) { | |
133 | [navbar_ disableAnimation]; | |
134 | while ([pages_ count] != (animated ? 2 : 1)) | |
135 | [navbar_ popNavigationItem]; | |
136 | [navbar_ enableAnimation]; | |
137 | if (animated) | |
138 | [navbar_ popNavigationItem]; | |
139 | } | |
140 | ||
141 | resetting_ = false; | |
142 | ||
143 | [self resetViewAnimated:animated toPage:[pages_ lastObject]]; | |
144 | } | |
145 | ||
146 | - (void) resetViewAnimated:(BOOL)animated toPage:(RVPage *)page { | |
147 | [page resetViewAnimated:animated]; | |
148 | [transition_ transition:(animated ? 2 : 0) toView:page]; | |
149 | [page setPageActive:YES]; | |
150 | [self reloadButtonsForPage:page]; | |
c15d236a JF |
151 | |
152 | if ([navbar_ respondsToSelector:@selector(setAccessoryView:animate:goingBack:)]) | |
153 | [navbar_ setAccessoryView:[page accessoryView] animate:animated goingBack:YES]; | |
154 | else | |
155 | [navbar_ setAccessoryView:[page accessoryView] animate:animated removeOnPop:NO]; | |
7e9a36b6 JF |
156 | } |
157 | ||
158 | - (void) setTitle:(NSString *)title forPage:(RVPage *)page { | |
159 | if ([pages_ count] == 0 || page != [pages_ lastObject]) | |
160 | return; | |
161 | UINavigationItem *navitem = [navbar_ topItem]; | |
162 | [navitem setTitle:title]; | |
163 | } | |
164 | ||
165 | - (void) setBackButtonTitle:(NSString *)title forPage:(RVPage *)page { | |
166 | if ([pages_ count] == 0 || page != [pages_ lastObject]) | |
167 | return; | |
168 | UINavigationItem *navitem = [navbar_ topItem]; | |
169 | [navitem setBackButtonTitle:title]; | |
170 | } | |
171 | ||
172 | - (void) reloadButtonsForPage:(RVPage *)page { | |
173 | if ([pages_ count] == 0 || page != [pages_ lastObject]) | |
174 | return; | |
7b0ce2da JF |
175 | NSString *leftButtonTitle([page leftButtonTitle]); |
176 | RVUINavBarButtonStyle leftButtonStyle = [page leftButtonStyle]; | |
177 | RVUINavBarButtonStyle rightButtonStyle = [page rightButtonStyle]; | |
178 | //[navbar_ showButtonsWithLeftTitle:leftButtonTitle rightTitle:[page rightButtonTitle] leftBack:(leftButtonTitle == nil)]; | |
179 | ||
180 | [navbar_ | |
181 | showLeftButton:leftButtonTitle | |
182 | withStyle:leftButtonStyle | |
183 | rightButton:[page rightButtonTitle] | |
184 | withStyle:rightButtonStyle | |
185 | ]; | |
7e9a36b6 JF |
186 | } |
187 | ||
188 | - (NSString *) getTitleForPage:(RVPage *)page { | |
189 | return [page title]; | |
190 | } | |
191 | ||
192 | - (void) reloadData { | |
193 | for (int i(0), e([pages_ count]); i != e; ++i) { | |
194 | RVPage *page([pages_ objectAtIndex:(e - i - 1)]); | |
195 | [page reloadData]; | |
196 | } | |
197 | } | |
198 | ||
199 | - (CGRect) pageBounds { | |
200 | return [transition_ bounds]; | |
201 | } | |
202 | ||
203 | @end |