]> git.saurik.com Git - cydia.git/blame - CyteKit/ListController.mm
libapt-pkg needs to be statically linked on armv6.
[cydia.git] / CyteKit / ListController.mm
CommitLineData
3a6f9c89
JF
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 <Foundation/Foundation.h>
25#include <UIKit/UIKit.h>
26
27#include "CyteKit/ListController.h"
28#include "CyteKit/extern.h"
29
30#include "iPhonePrivate.h"
31#include <Menes/ObjectHandle.h>
32
33static CGFloat CYStatusBarHeight() {
34 CGSize size([[UIApplication sharedApplication] statusBarFrame].size);
35 return UIInterfaceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation]) ? size.height : size.width;
36}
37
38@implementation CyteListController {
39 _H<UITableView, 2> list_;
40}
41
42- (bool) shouldYield {
43 return false;
44}
45
46- (void) deselectWithAnimation:(BOOL)animated {
47 [list_ deselectRowAtIndexPath:[list_ indexPathForSelectedRow] animated:animated];
48}
49
50- (void) resizeForKeyboardBounds:(CGRect)bounds duration:(NSTimeInterval)duration curve:(UIViewAnimationCurve)curve {
51 CGRect base = [[self view] bounds];
52 base.size.height -= bounds.size.height;
53 base.origin = [list_ frame].origin;
54
55 [UIView beginAnimations:nil context:NULL];
56 [UIView setAnimationBeginsFromCurrentState:YES];
57 [UIView setAnimationCurve:curve];
58 [UIView setAnimationDuration:duration];
59 [list_ setFrame:base];
60 [UIView commitAnimations];
61}
62
63- (void) resizeForKeyboardBounds:(CGRect)bounds duration:(NSTimeInterval)duration {
64 [self resizeForKeyboardBounds:bounds duration:duration curve:UIViewAnimationCurveLinear];
65}
66
67- (void) resizeForKeyboardBounds:(CGRect)bounds {
68 [self resizeForKeyboardBounds:bounds duration:0];
69}
70
71- (void) getKeyboardCurve:(UIViewAnimationCurve *)curve duration:(NSTimeInterval *)duration forNotification:(NSNotification *)notification {
72 if (&UIKeyboardAnimationCurveUserInfoKey == NULL)
73 *curve = UIViewAnimationCurveEaseInOut;
74 else
75 [[[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:curve];
76
77 if (&UIKeyboardAnimationDurationUserInfoKey == NULL)
78 *duration = 0.3;
79 else
80 [[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:duration];
81}
82
83- (void) keyboardWillShow:(NSNotification *)notification {
84 CGRect bounds;
85 CGPoint center;
86 [[[notification userInfo] objectForKey:UIKeyboardBoundsUserInfoKey] getValue:&bounds];
87 [[[notification userInfo] objectForKey:UIKeyboardCenterEndUserInfoKey] getValue:&center];
88
89 NSTimeInterval duration;
90 UIViewAnimationCurve curve;
91 [self getKeyboardCurve:&curve duration:&duration forNotification:notification];
92
93 CGRect kbframe = CGRectMake(Retina(center.x - bounds.size.width / 2), Retina(center.y - bounds.size.height / 2), bounds.size.width, bounds.size.height);
94 UIViewController *base([self rootViewController]);
95 CGRect viewframe = [[base view] convertRect:[list_ frame] fromView:[list_ superview]];
96 CGRect intersection = CGRectIntersection(viewframe, kbframe);
97
98 if (kCFCoreFoundationVersionNumber < kCFCoreFoundationVersionNumber_iPhoneOS_3_0) // XXX: _UIApplicationLinkedOnOrAfter(4)
99 intersection.size.height += CYStatusBarHeight();
100
101 [self resizeForKeyboardBounds:intersection duration:duration curve:curve];
102}
103
104- (void) keyboardWillHide:(NSNotification *)notification {
105 NSTimeInterval duration;
106 UIViewAnimationCurve curve;
107 [self getKeyboardCurve:&curve duration:&duration forNotification:notification];
108
109 [self resizeForKeyboardBounds:CGRectZero duration:duration curve:curve];
110}
111
112- (void) viewWillAppear:(BOOL)animated {
113 [super viewWillAppear:animated];
114
115 [self resizeForKeyboardBounds:CGRectZero];
116 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
117 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
118}
119
120- (void) viewWillDisappear:(BOOL)animated {
121 [super viewWillDisappear:animated];
122
123 [self resizeForKeyboardBounds:CGRectZero];
124 [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
125 [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
126}
127
128- (void) viewDidAppear:(BOOL)animated {
129 [super viewDidAppear:animated];
130 [self deselectWithAnimation:animated];
131}
132
70d35e13
JF
133- (id) initWithTitle:(NSString *)title {
134 if ((self = [super init]) != nil) {
135 [[self navigationItem] setTitle:title];
136 } return self;
137}
138
3a6f9c89
JF
139- (void) releaseSubviews {
140 list_ = nil;
141 [super releaseSubviews];
142}
143
144- (CGFloat) rowHeight {
145 return 0;
146}
147
148- (void) updateHeight {
149 [list_ setRowHeight:[self rowHeight]];
150}
151
152- (void) loadView {
153 UIView *view([[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease]);
154 [view setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
155 [self setView:view];
156
157 list_ = [[[UITableView alloc] initWithFrame:[[self view] bounds] style:UITableViewStylePlain] autorelease];
158 [list_ setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
159 [view addSubview:list_];
160
161 // XXX: is 20 the most optimal number here?
162 [list_ setSectionIndexMinimumDisplayRowCount:20];
163
164 [list_ setDataSource:(id)self];
165 [list_ setDelegate:(id)self];
166
167 [self updateHeight];
168}
169
170- (void) _reloadData {
171 [self updateHeight];
172
173 [list_ setDataSource:(id)self];
174 [list_ reloadData];
175}
176
177- (void) reloadData {
178 [super reloadData];
179
180 if ([self shouldYield])
181 [self performSelector:@selector(_reloadData) withObject:nil afterDelay:0];
182 else
183 [self _reloadData];
184}
185
186- (void) resetCursor {
187 [list_ scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:NO];
188}
189
190- (void) clearData {
191 [self updateHeight];
192
193 [list_ setDataSource:nil];
194 [list_ reloadData];
195
196 [self resetCursor];
197}
198
199@end