root / Classes / SharingAccountsViewController.m @ ef74c42f
History | View | Annotate | Download (6.7 kB)
1 |
// |
---|---|
2 |
// SharingAccountsViewController.m |
3 |
// pithos-ios |
4 |
// |
5 |
// Copyright 2011 GRNET S.A. All rights reserved. |
6 |
// |
7 |
// Redistribution and use in source and binary forms, with or |
8 |
// without modification, are permitted provided that the following |
9 |
// conditions are met: |
10 |
// |
11 |
// 1. Redistributions of source code must retain the above |
12 |
// copyright notice, this list of conditions and the following |
13 |
// disclaimer. |
14 |
// |
15 |
// 2. Redistributions in binary form must reproduce the above |
16 |
// copyright notice, this list of conditions and the following |
17 |
// disclaimer in the documentation and/or other materials |
18 |
// provided with the distribution. |
19 |
// |
20 |
// THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS |
21 |
// OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
22 |
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
23 |
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR |
24 |
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
25 |
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
26 |
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
27 |
// USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
28 |
// AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
29 |
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
30 |
// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
31 |
// POSSIBILITY OF SUCH DAMAGE. |
32 |
// |
33 |
// The views and conclusions contained in the software and |
34 |
// documentation are those of the authors and should not be |
35 |
// interpreted as representing official policies, either expressed |
36 |
// or implied, of GRNET S.A. |
37 |
|
38 |
#import "SharingAccountsViewController.h" |
39 |
#import "UIViewController+Conveniences.h" |
40 |
#import "ContainersViewController.h" |
41 |
#import "PithosImageViewController.h" |
42 |
#import "OpenStackRequest.h" |
43 |
#import "OpenStackAccount.h" |
44 |
#import "AccountManager.h" |
45 |
#import "ActivityIndicatorView.h" |
46 |
#import "AccountHomeViewController.h" |
47 |
#import "APICallback.h" |
48 |
|
49 |
@implementation SharingAccountsViewController |
50 |
|
51 |
@synthesize tableView, account, sharingAccounts, accountHomeViewController; |
52 |
|
53 |
#pragma mark - View lifecycle |
54 |
|
55 |
- (void)viewDidLoad { |
56 |
[super viewDidLoad]; |
57 |
self.navigationItem.title = @"Sharing Accounts"; |
58 |
sharingAccounts = [[NSMutableArray alloc] init]; |
59 |
} |
60 |
|
61 |
- (void)viewWillAppear:(BOOL)animated { |
62 |
[super viewWillAppear:animated]; |
63 |
|
64 |
if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) { |
65 |
PithosImageViewController *vc = [[PithosImageViewController alloc] initWithNibName:@"PithosImageViewController" bundle:nil]; |
66 |
[self presentPrimaryViewController:vc]; |
67 |
[vc release]; |
68 |
} |
69 |
|
70 |
[self refreshButtonPressed:nil]; |
71 |
} |
72 |
|
73 |
- (void)viewDidUnload { |
74 |
[super viewDidUnload]; |
75 |
account.sharingAccount = nil; |
76 |
} |
77 |
|
78 |
#pragma mark - Memory management |
79 |
|
80 |
- (void)dealloc { |
81 |
[tableView release]; |
82 |
[account release]; |
83 |
[sharingAccounts release]; |
84 |
[super dealloc]; |
85 |
} |
86 |
|
87 |
#pragma mark - Button Handlers |
88 |
|
89 |
- (void)refreshButtonPressed:(id)sender { |
90 |
__block ActivityIndicatorView *activityIndicatorView = [ActivityIndicatorView activityIndicatorViewWithText:@"Loading..." |
91 |
andAddToView:self.view]; |
92 |
[[self.account.manager getSharingAccounts] |
93 |
success:^(OpenStackRequest *request) { |
94 |
[sharingAccounts removeAllObjects]; |
95 |
[sharingAccounts addObjectsFromArray:[[request.userInfo objectForKey:@"sharingAccountsBuffer"] allKeys]]; |
96 |
[[self.account.manager userCatalogForDisplaynames:nil |
97 |
UUIDs:sharingAccounts] |
98 |
success:^(OpenStackRequest *request) { |
99 |
contentsLoaded = YES; |
100 |
[activityIndicatorView stopAnimatingAndRemoveFromSuperview]; |
101 |
[self.tableView reloadData]; |
102 |
} |
103 |
failure:^(OpenStackRequest *request) { |
104 |
contentsLoaded = YES; |
105 |
[activityIndicatorView stopAnimatingAndRemoveFromSuperview]; |
106 |
[self.tableView reloadData]; |
107 |
}]; |
108 |
} |
109 |
failure:^(OpenStackRequest *request) { |
110 |
contentsLoaded = NO; |
111 |
[activityIndicatorView stopAnimatingAndRemoveFromSuperview]; |
112 |
if (request.responseStatusCode != 0) { |
113 |
[self alert:@"There was a problem loading sharing accounts." request:request addAccountSettingsButton:YES]; |
114 |
} |
115 |
}]; |
116 |
} |
117 |
|
118 |
- (void)homeButtonPressed:(id)sender { |
119 |
[self.navigationController popToViewController:accountHomeViewController animated:YES]; |
120 |
} |
121 |
|
122 |
#pragma mark - UITableViewDataSource |
123 |
|
124 |
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
125 |
if ([sharingAccounts count] == 0) { |
126 |
self.tableView.allowsSelection = NO; |
127 |
self.tableView.scrollEnabled = NO; |
128 |
} else { |
129 |
self.tableView.allowsSelection = YES; |
130 |
self.tableView.scrollEnabled = YES; |
131 |
} |
132 |
return MAX(contentsLoaded ? 1 : 0, [sharingAccounts count]); |
133 |
} |
134 |
|
135 |
- (CGFloat)tableView:(UITableView *)aTableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { |
136 |
if ([sharingAccounts count] == 0) { |
137 |
return aTableView.frame.size.height; |
138 |
} else { |
139 |
return aTableView.rowHeight; |
140 |
} |
141 |
} |
142 |
|
143 |
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
144 |
if ([sharingAccounts count] == 0) { |
145 |
return [self tableView:tableView emptyCellWithImage:[UIImage imageNamed:@"OthersSharedLarge.png"] |
146 |
title:@"No sharing accounts" |
147 |
subtitle:@""]; |
148 |
} else { |
149 |
static NSString *CellIdentifier = @"Cell"; |
150 |
|
151 |
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; |
152 |
if (cell == nil) { |
153 |
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; |
154 |
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; |
155 |
} |
156 |
cell.textLabel.text = [account displaynameForUUID:[sharingAccounts objectAtIndex:indexPath.row] safe:YES]; |
157 |
cell.imageView.image = [UIImage imageNamed:@"myShared.png"]; |
158 |
|
159 |
return cell; |
160 |
} |
161 |
} |
162 |
|
163 |
#pragma mark - UITableViewDelegate |
164 |
|
165 |
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
166 |
if ([sharingAccounts count] == 0) |
167 |
return; |
168 |
|
169 |
ContainersViewController *vc = [[ContainersViewController alloc] initWithNibName:@"ContainersViewController" bundle:nil]; |
170 |
account.shared = NO; |
171 |
account.sharingAccount = [sharingAccounts objectAtIndex:indexPath.row]; |
172 |
vc.account = account; |
173 |
vc.accountHomeViewController = accountHomeViewController; |
174 |
[self.navigationController pushViewController:vc animated:YES]; |
175 |
[vc release]; |
176 |
[self.tableView deselectRowAtIndexPath:indexPath animated:YES]; |
177 |
} |
178 |
|
179 |
@end |