-
Notifications
You must be signed in to change notification settings - Fork 27
/
PALMoreAppsController.m
209 lines (177 loc) · 7.48 KB
/
PALMoreAppsController.m
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
//
// PALMoreAppsController.m
// Created by Hendrik Kueck on 11-05-22.
//
#import "PALMoreAppsController.h"
#import "PALManager.h"
#import "PALMoreAppsTableCellView.h"
#import "PALAppInfo.h"
static const int ROWHEIGHT = 86;
@interface PALMoreAppsController (PrivateStuff)
- (void)dismissWithLeavingApp:(BOOL)leavingApp;
@end
@implementation PALMoreAppsController
@synthesize delegate;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
additionalApps = [[[PALManager sharedPALManager] moreApps] retain];
}
return self;
}
- (id)init {
return [self initWithStyle:UITableViewStylePlain];
}
- (void)dealloc
{
[additionalApps release];
[super dealloc];
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
NSAssert(self.navigationController != nil, @"PALMoreAppsController must be presented in a UINavigationController");
self.tableView.allowsSelection = NO;
self.tableView.rowHeight = ROWHEIGHT;
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
self.tableView.backgroundColor = [UIColor colorWithWhite:0.5 alpha:1.0];
UIImage* shadowImage = [UIImage imageNamed:@"PAL_tableview_shadow.png"];
UIImageView* bottomShadowView = [[UIImageView alloc] initWithImage:shadowImage];
UIImageView* topShadowView = [[UIImageView alloc] initWithImage:shadowImage];
[topShadowView setTransform:CGAffineTransformMakeScale(1.0, -1.0)];
self.tableView.tableFooterView = bottomShadowView;
self.tableView.tableHeaderView = topShadowView;
[bottomShadowView release];
[topShadowView release];
self.tableView.contentInset = UIEdgeInsetsMake(-20, 0, -20, 0);
self.navigationItem.title = NSLocalizedString(@"Compatible Apps", @"PhotoAppLink");
BOOL isPresentedModally = (self.navigationController.parentViewController.modalViewController == self.navigationController);
BOOL weAreTheRootController = ([self.navigationController.viewControllers objectAtIndex:0] == self);
if (isPresentedModally && weAreTheRootController) {
UIBarButtonItem* cancelButton =
[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Cancel", @"PhotoAppLink")
style:UIBarButtonItemStyleBordered
target:self action:@selector(cancel)];
[[self navigationItem] setLeftBarButtonItem:cancelButton];
[cancelButton release];
}
}
// Called by the notification center if the app becomes active and this view is still visible
// This is necessary to reload the list of compatible apps as it can change when the app
// becomes active again (user might buy another compatible app, for example)
- (void)applicationDidBecomeActive
{
[additionalApps release];
additionalApps = [[[PALManager sharedPALManager] moreApps] retain];
[self.tableView reloadData];
}
- (void)viewWillAppear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(applicationDidBecomeActive)
name:UIApplicationDidBecomeActiveNotification
object:nil];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return YES;
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
[self.tableView reloadData];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [additionalApps count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"PALAppInfoCell";
static const int APPINFOVIEW_TAG = 1042;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
PALMoreAppsTableCellView* appInfoView;
UIButton* storeButton;
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"PAL_tablecell_background.png"]];
CGRect cellFrame = CGRectMake(0.0, 0.0, tableView.frame.size.width, ROWHEIGHT);
appInfoView= [[PALMoreAppsTableCellView alloc] initWithFrame:cellFrame];
appInfoView.tag = APPINFOVIEW_TAG;
[cell.contentView addSubview:appInfoView];
[appInfoView release];
// add button to go to App Store
UIImage* buttonBG = [UIImage imageNamed:@"PAL_button_background.png"];
UIImage* stretchableButtonBG = [buttonBG stretchableImageWithLeftCapWidth:5 topCapHeight:12];
// TODO add assert
storeButton = [UIButton buttonWithType:UIButtonTypeCustom];
[storeButton setBackgroundImage:stretchableButtonBG forState:UIControlStateNormal];
[storeButton addTarget:self action:@selector(storeButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
[storeButton.titleLabel setFont:[UIFont boldSystemFontOfSize:13]];
[storeButton.titleLabel setShadowColor:[UIColor blackColor]];
[storeButton.titleLabel setShadowOffset:CGSizeMake(0, -1)];
storeButton.frame = CGRectMake(0, 0, 50, 25);
[cell setAccessoryView:storeButton];
}
else {
appInfoView = (PALMoreAppsTableCellView*) [cell.contentView viewWithTag:APPINFOVIEW_TAG];
appInfoView.frame = CGRectMake(0, 0, tableView.frame.size.width, ROWHEIGHT);
storeButton = (UIButton*) cell.accessoryView;
}
PALAppInfo* appInfo = [additionalApps objectAtIndex:indexPath.row];
[appInfoView setAppInfo:appInfo];
storeButton.tag = indexPath.row;
NSString* storeButtonText;
if (appInfo.freeApp) {
storeButtonText = NSLocalizedString(@"FREE", "PhotoAppLink");
}
else {
storeButtonText = NSLocalizedString(@"Store", "PhotoAppLink");
}
[storeButton setTitle:storeButtonText forState:UIControlStateNormal];
return cell;
}
- (void)storeButtonTapped:(id)sender
{
UIButton* tappedButton = sender;
PALAppInfo* appInfo = [additionalApps objectAtIndex:tappedButton.tag];
NSURL* appStoreURL = [appInfo appStoreLink];
if (appStoreURL != nil) {
[self dismissWithLeavingApp:YES];
[[UIApplication sharedApplication] performSelector:@selector(openURL:) withObject:appStoreURL afterDelay:0.0];
}
}
- (void)dismissWithLeavingApp:(BOOL)leavingApp
{
if ([delegate respondsToSelector:@selector(finishedWithMoreAppsController:leavingApp:)]) {
[delegate finishedWithMoreAppsController:self leavingApp:leavingApp];
}
else {
// default behavior
BOOL isPresentedModally = (self.navigationController.parentViewController.modalViewController == self.navigationController);
BOOL useAnimation = !leavingApp;
if (isPresentedModally) {
[self dismissModalViewControllerAnimated:useAnimation];
}
else {
[self.navigationController popViewControllerAnimated:useAnimation];
}
}
}
- (void)cancel
{
[self dismissWithLeavingApp:NO];
}
@end