]> git.saurik.com Git - cydia.git/blob - CyteKit/TabBarController.mm
Objective-C seriously didn't notice my mistake :/.
[cydia.git] / CyteKit / TabBarController.mm
1 /* Cydia - iPhone UIKit Front-End for Debian APT
2 * Copyright (C) 2008-2015 Jay Freeman (saurik)
3 */
4
5 /* GNU General Public License, Version 3 {{{ */
6 /*
7 * Cydia is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published
9 * by the Free Software Foundation, either version 3 of the License,
10 * or (at your option) any later version.
11 *
12 * Cydia is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with Cydia. If not, see <http://www.gnu.org/licenses/>.
19 **/
20 /* }}} */
21
22 #include "CyteKit/UCPlatform.h"
23
24 #include "CyteKit/TabBarController.h"
25
26 #include "iPhonePrivate.h"
27 #include <Menes/ObjectHandle.h>
28
29 @implementation CyteTabBarController {
30 _transient UIViewController *transient_;
31 _H<UIViewController> remembered_;
32 }
33
34 - (NSArray *) navigationURLCollection {
35 NSMutableArray *items([NSMutableArray array]);
36
37 // XXX: Should this deal with transient view controllers?
38 for (id navigation in [self viewControllers]) {
39 NSArray *stack = [navigation performSelector:@selector(navigationURLCollection)];
40 if (stack != nil)
41 [items addObject:stack];
42 }
43
44 return items;
45 }
46
47 - (void) addViewControllers:(id)no, ... {
48 va_list args;
49 va_start(args, no);
50
51 NSMutableArray *controllers([NSMutableArray array]);
52
53 for (;;) {
54 auto title(va_arg(args, NSString *));
55 if (title == nil)
56 break;
57
58 UINavigationController *controller([[[UINavigationController alloc] init] autorelease]);
59 [controllers addObject:controller];
60
61 auto legacy(va_arg(args, NSString *));
62 auto normal(va_arg(args, NSString *));
63 auto select(va_arg(args, NSString *));
64
65 if (kCFCoreFoundationVersionNumber < 800)
66 [controller setTabBarItem:[[[UITabBarItem alloc] initWithTitle:title image:[UIImage imageNamed:legacy] tag:0] autorelease]];
67 else
68 [controller setTabBarItem:[[[UITabBarItem alloc] initWithTitle:title image:[UIImage imageNamed:normal] selectedImage:[UIImage imageNamed:select]] autorelease]];
69 }
70
71 va_end(args);
72
73 [self setViewControllers:controllers];
74 }
75
76 - (void) didReceiveMemoryWarning {
77 [super didReceiveMemoryWarning];
78
79 // presenting a UINavigationController on 2.x does not update its transitionView
80 // it thereby will not allow its topViewController to be unloaded by memory pressure
81 if (kCFCoreFoundationVersionNumber < kCFCoreFoundationVersionNumber_iPhoneOS_3_0) {
82 UIViewController *selected([self selectedViewController]);
83 for (UINavigationController *controller in [self viewControllers])
84 if (controller != selected)
85 if (UIViewController *top = [controller topViewController])
86 [top unloadView];
87 }
88 }
89
90 - (void) tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
91 if ([self unselectedViewController])
92 [self setUnselectedViewController:nil];
93
94 // presenting a UINavigationController on 2.x does not update its transitionView
95 // if this view was unloaded, the tranitionView may currently be presenting nothing
96 if (kCFCoreFoundationVersionNumber < kCFCoreFoundationVersionNumber_iPhoneOS_3_0) {
97 UINavigationController *navigation((UINavigationController *) viewController);
98 [navigation pushViewController:[[[UIViewController alloc] init] autorelease] animated:NO];
99 [navigation popViewControllerAnimated:NO];
100 }
101 }
102
103 - (void) dismissModalViewControllerAnimated:(BOOL)animated {
104 if ([self modalViewController] == nil && [self unselectedViewController] != nil)
105 [self setUnselectedViewController:nil];
106 else
107 [super dismissModalViewControllerAnimated:YES];
108 }
109
110 - (void) setUnselectedViewController:(UIViewController *)transient {
111 if (kCFCoreFoundationVersionNumber < kCFCoreFoundationVersionNumber_iPhoneOS_3_0) {
112 if (transient != nil) {
113 [[[self viewControllers] objectAtIndex:0] pushViewController:transient animated:YES];
114 [self setSelectedIndex:0];
115 } return;
116 }
117
118 NSMutableArray *controllers = [[[self viewControllers] mutableCopy] autorelease];
119 if (transient != nil) {
120 UINavigationController *navigation([[[UINavigationController alloc] init] autorelease]);
121 [navigation setViewControllers:[NSArray arrayWithObject:transient]];
122 transient = navigation;
123
124 if (transient_ == nil)
125 remembered_ = [controllers objectAtIndex:0];
126 transient_ = transient;
127 [transient_ setTabBarItem:[remembered_ tabBarItem]];
128 [controllers replaceObjectAtIndex:0 withObject:transient_];
129 [self setSelectedIndex:0];
130 [self setViewControllers:controllers];
131 [self concealTabBarSelection];
132 } else if (remembered_ != nil) {
133 [remembered_ setTabBarItem:[transient_ tabBarItem]];
134 transient_ = transient;
135 [controllers replaceObjectAtIndex:0 withObject:remembered_];
136 remembered_ = nil;
137 [self setViewControllers:controllers];
138 [self revealTabBarSelection];
139 }
140 }
141
142 - (UIViewController *) unselectedViewController {
143 return transient_;
144 }
145
146 - (void) unloadData {
147 [super unloadData];
148
149 for (UINavigationController *controller in [self viewControllers])
150 [controller unloadData];
151
152 if (UIViewController *selected = [self selectedViewController])
153 [selected reloadData];
154
155 if (UIViewController *unselected = [self unselectedViewController]) {
156 [unselected unloadData];
157 [unselected reloadData];
158 }
159 }
160
161 #include "InterfaceOrientation.h"
162
163 @end