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