summaryrefslogtreecommitdiff
path: root/UICaboodle/RVBook.mm
blob: e6cc1263159508aeb2594b9281e7828931b8bc79 (plain)
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
#import "RVBook.h"

#import <UIKit/UINavigationBar.h>
#import <UIKit/UINavigationItem.h>

#import <UIKit/UITransitionView.h>

#import <UIKit/UIView-Geometry.h>
#import <UIKit/UIView-Hierarchy.h>

#import "RVPage.h"

@implementation RVBook

- (void) dealloc {
    [navbar_ setDelegate:nil];

    [pages_ release];
    [navbar_ release];
    [transition_ release];
    [super dealloc];
}

- (void) navigationBar:(UINavigationBar *)navbar buttonClicked:(int)button {
    _assert([pages_ count] != 0);
    RVPage *page = [pages_ lastObject];
    switch (button) {
        case 0: [page _rightButtonClicked]; break;
        case 1: [page _leftButtonClicked]; break;
    }
}

- (void) navigationBar:(UINavigationBar *)navbar poppedItem:(UINavigationItem *)item {
    _assert([pages_ count] != 0);
    if (!resetting_)
        [[pages_ lastObject] setPageActive:NO];
    [pages_ removeLastObject];
    if (!resetting_)
        [self resetViewAnimated:YES toPage:[pages_ lastObject]];
}

- (id) initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame]) != nil) {
        pages_ = [[NSMutableArray arrayWithCapacity:4] retain];

        struct CGRect bounds = [self bounds];
        CGSize navsize = [UINavigationBar defaultSizeWithPrompt];
        CGRect navrect = {{0, 0}, navsize};

        navbar_ = [[UINavigationBar alloc] initWithFrame:navrect];
        [self addSubview:navbar_];

        [navbar_ setBarStyle:0];
        [navbar_ setDelegate:self];

        [navbar_ setPrompt:@""];

        transition_ = [[UITransitionView alloc] initWithFrame:CGRectMake(
            bounds.origin.x, bounds.origin.y + navsize.height, bounds.size.width, bounds.size.height - navsize.height
        )];

        [self addSubview:transition_];
    } return self;
}

- (void) setDelegate:(id)delegate {
    delegate_ = delegate;
}

- (void) setPage:(RVPage *)page {
    if ([pages_ count] != 0)
        [[pages_ lastObject] setPageActive:NO];

    [navbar_ disableAnimation];
    resetting_ = true;
    for (unsigned i(0), pages([pages_ count]); i != pages; ++i)
        [navbar_ popNavigationItem];
    resetting_ = false;

    [self pushPage:page];
    [navbar_ enableAnimation];
}

- (void) pushPage:(RVPage *)page {
    if ([pages_ count] != 0)
        [[pages_ lastObject] setPageActive:NO];

    NSString *title = [self getTitleForPage:page];

    NSString *backButtonTitle = [page backButtonTitle];
    if (backButtonTitle == nil)
        backButtonTitle = title;

    UINavigationItem *navitem = [[[UINavigationItem alloc] initWithTitle:title] autorelease];
    [navitem setBackButtonTitle:backButtonTitle];
    [navbar_ pushNavigationItem:navitem];

    BOOL animated = [pages_ count] == 0 ? NO : YES;
    [transition_ transition:(animated ? 1 : 0) toView:page];
    [page setPageActive:YES];

    [pages_ addObject:page];
    [self reloadButtonsForPage:page];

#ifdef __OBJC2__
    [navbar_ setAccessoryView:[page accessoryView] animate:animated removeOnPop:NO];
#else
    [navbar_ setAccessoryView:[page accessoryView] animate:animated goingBack:NO];
#endif
}

- (void) popPages:(unsigned)pages {
    if (pages == 0)
        return;

    [[pages_ lastObject] setPageActive:NO];

    resetting_ = true;
    for (unsigned i(0); i != pages; ++i)
        [navbar_ popNavigationItem];
    resetting_ = false;

    [self resetViewAnimated:YES toPage:[pages_ lastObject]];
}

- (void) setPrompt:(NSString *)prompt {
    [navbar_ setPrompt:prompt];
}

- (void) resetViewAnimated:(BOOL)animated {
    resetting_ = true;

    if ([pages_ count] > 1) {
        [navbar_ disableAnimation];
        while ([pages_ count] != (animated ? 2 : 1))
            [navbar_ popNavigationItem];
        [navbar_ enableAnimation];
        if (animated)
            [navbar_ popNavigationItem];
    }

    resetting_ = false;

    [self resetViewAnimated:animated toPage:[pages_ lastObject]];
}

- (void) resetViewAnimated:(BOOL)animated toPage:(RVPage *)page {
    [page resetViewAnimated:animated];
    [transition_ transition:(animated ? 2 : 0) toView:page];
    [page setPageActive:YES];
    [self reloadButtonsForPage:page];
#ifdef __OBJC2__
    [navbar_ setAccessoryView:[page accessoryView] animate:animated removeOnPop:NO];
#else
    [navbar_ setAccessoryView:[page accessoryView] animate:animated goingBack:YES];
#endif
}

- (void) setTitle:(NSString *)title forPage:(RVPage *)page {
    if ([pages_ count] == 0 || page != [pages_ lastObject])
        return;
    UINavigationItem *navitem = [navbar_ topItem];
    [navitem setTitle:title];
}

- (void) setBackButtonTitle:(NSString *)title forPage:(RVPage *)page {
    if ([pages_ count] == 0 || page != [pages_ lastObject])
        return;
    UINavigationItem *navitem = [navbar_ topItem];
    [navitem setBackButtonTitle:title];
}

- (void) reloadButtonsForPage:(RVPage *)page {
    if ([pages_ count] == 0 || page != [pages_ lastObject])
        return;
    NSString *leftButtonTitle([pages_ count] == 1 ? [page leftButtonTitle] : nil);
    [navbar_ showButtonsWithLeftTitle:leftButtonTitle rightTitle:[page rightButtonTitle]];
}

- (NSString *) getTitleForPage:(RVPage *)page {
    return [page title];
}

- (void) reloadData {
    for (int i(0), e([pages_ count]); i != e; ++i) {
        RVPage *page([pages_ objectAtIndex:(e - i - 1)]);
        [page reloadData];
    }
}

- (CGRect) pageBounds {
    return [transition_ bounds];
}

@end