1 /* Cydia - iPhone UIKit Front-End for Debian APT
2 * Copyright (C) 2008-2015 Jay Freeman (saurik)
5 /* GNU General Public License, Version 3 {{{ */
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.
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.
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/>.
22 #include "CyteKit/UCPlatform.h"
24 #include <Foundation/Foundation.h>
25 #include <UIKit/UIKit.h>
27 #include "CyteKit/ListController.h"
28 #include "CyteKit/extern.h"
30 #include "iPhonePrivate.h"
31 #include <Menes/ObjectHandle.h>
33 static CGFloat CYStatusBarHeight() {
34 CGSize size([[UIApplication sharedApplication] statusBarFrame].size);
35 return UIInterfaceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation]) ? size.height : size.width;
38 @implementation CyteListController {
39 _H<UITableView, 2> list_;
42 - (bool) shouldYield {
46 - (void) deselectWithAnimation:(BOOL)animated {
47 [list_ deselectRowAtIndexPath:[list_ indexPathForSelectedRow] animated:animated];
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;
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];
63 - (void) resizeForKeyboardBounds:(CGRect)bounds duration:(NSTimeInterval)duration {
64 [self resizeForKeyboardBounds:bounds duration:duration curve:UIViewAnimationCurveLinear];
67 - (void) resizeForKeyboardBounds:(CGRect)bounds {
68 [self resizeForKeyboardBounds:bounds duration:0];
71 - (void) getKeyboardCurve:(UIViewAnimationCurve *)curve duration:(NSTimeInterval *)duration forNotification:(NSNotification *)notification {
72 if (&UIKeyboardAnimationCurveUserInfoKey == NULL)
73 *curve = UIViewAnimationCurveEaseInOut;
75 [[[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:curve];
77 if (&UIKeyboardAnimationDurationUserInfoKey == NULL)
80 [[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:duration];
83 - (void) keyboardWillShow:(NSNotification *)notification {
86 [[[notification userInfo] objectForKey:UIKeyboardBoundsUserInfoKey] getValue:&bounds];
87 [[[notification userInfo] objectForKey:UIKeyboardCenterEndUserInfoKey] getValue:¢er];
89 NSTimeInterval duration;
90 UIViewAnimationCurve curve;
91 [self getKeyboardCurve:&curve duration:&duration forNotification:notification];
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);
98 if (kCFCoreFoundationVersionNumber < kCFCoreFoundationVersionNumber_iPhoneOS_3_0) // XXX: _UIApplicationLinkedOnOrAfter(4)
99 intersection.size.height += CYStatusBarHeight();
101 [self resizeForKeyboardBounds:intersection duration:duration curve:curve];
104 - (void) keyboardWillHide:(NSNotification *)notification {
105 NSTimeInterval duration;
106 UIViewAnimationCurve curve;
107 [self getKeyboardCurve:&curve duration:&duration forNotification:notification];
109 [self resizeForKeyboardBounds:CGRectZero duration:duration curve:curve];
112 - (void) viewWillAppear:(BOOL)animated {
113 [super viewWillAppear:animated];
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];
120 - (void) viewWillDisappear:(BOOL)animated {
121 [super viewWillDisappear:animated];
123 [self resizeForKeyboardBounds:CGRectZero];
124 [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
125 [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
128 - (void) viewDidAppear:(BOOL)animated {
129 [super viewDidAppear:animated];
130 [self deselectWithAnimation:animated];
133 - (void) releaseSubviews {
135 [super releaseSubviews];
138 - (CGFloat) rowHeight {
142 - (void) updateHeight {
143 [list_ setRowHeight:[self rowHeight]];
147 UIView *view([[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease]);
148 [view setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
151 list_ = [[[UITableView alloc] initWithFrame:[[self view] bounds] style:UITableViewStylePlain] autorelease];
152 [list_ setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
153 [view addSubview:list_];
155 // XXX: is 20 the most optimal number here?
156 [list_ setSectionIndexMinimumDisplayRowCount:20];
158 [list_ setDataSource:(id)self];
159 [list_ setDelegate:(id)self];
164 - (void) _reloadData {
167 [list_ setDataSource:(id)self];
171 - (void) reloadData {
174 if ([self shouldYield])
175 [self performSelector:@selector(_reloadData) withObject:nil afterDelay:0];
180 - (void) resetCursor {
181 [list_ scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:NO];
187 [list_ setDataSource:nil];