~threeve/ubuntuone-ios-music/fix-play-on-call

« back to all changes in this revision

Viewing changes to musicstreaming/view_controllers/AlbumListViewController.m

  • Committer: Jason Foreman
  • Date: 2011-06-16 18:33:42 UTC
  • mfrom: (191.1.60 master)
  • Revision ID: jason.foreman@canonical.com-20110616183342-vl7a4804xsf0s1b5
Merging branches for v 2.0.

lp:~threeve/ubuntuone-ios-client/master
lp:~urbanape/ubuntuone-ios-client/downloader

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
19
 
20
20
#import "AlbumListViewController.h"
21
 
#import "DataCache.h"
22
21
#import "AlbumListParser.h"
23
22
#import "AlbumArtistUITableViewCell.h"
24
23
#import "Album.h"
25
24
#import "AlbumViewController.h"
26
 
#import "AsynchronousImageViewCached.h"
 
25
#import "NSString+Extras.h"
 
26
 
 
27
@interface AlbumListViewController ()
 
28
- (void)configureCell:(AlbumArtistUITableViewCell*)cell forTableView:(UITableView*)tableView indexPath:(NSIndexPath*)indexPath;
 
29
@end
27
30
 
28
31
@implementation AlbumListViewController
29
32
 
32
35
    AlbumListViewController *albumListViewController = [[[AlbumListViewController alloc] initWithTitle:NSLocalizedString(@"Albums", @"")] autorelease];
33
36
    UINavigationController *nav = [[[UINavigationController alloc] initWithRootViewController:albumListViewController] autorelease];
34
37
    nav.navigationBar.barStyle = UIBarStyleBlack;
 
38
    nav.tabBarItem.image = [UIImage imageNamed:@"albums"];
35
39
    return nav;
36
40
}
37
41
 
42
46
        self.tableView.rowHeight = 60.0f;
43
47
    [super viewDidLoad];
44
48
        
45
 
        loader = [[ImageLoader alloc] init];
 
49
        loader = [[AlbumArtLoader alloc] init];
46
50
        loader.delegate = self;
47
51
        loader.imageSize = 120;
48
52
}
49
53
 
 
54
- (void)viewDidUnload;
 
55
{
 
56
    [super viewDidUnload];
 
57
    loader.delegate = nil;
 
58
    [loader release], loader = nil;
 
59
}
 
60
 
50
61
#pragma mark - Data Loading
51
62
 
52
63
- (void)loadLocalData
63
74
                
64
75
                for (Album *a in self.tableData)
65
76
                {
66
 
                        NSString *index = [[a.title substringToIndex:1] uppercaseString];
 
77
                        NSString *index = [[a.title dearticlizedIndex] uppercaseString];
67
78
                        NSMutableArray *group = [self.groupedTableData objectForKey:index];
68
79
                        
69
80
                        if (nil == group)
111
122
        [pool release];
112
123
}
113
124
 
 
125
 
 
126
- (void)updateReachability:(Reachability*)reachability;
 
127
{
 
128
        [super updateReachability:reachability];
 
129
        
 
130
        for (AlbumArtistUITableViewCell *cell in [self.tableView visibleCells])
 
131
        {
 
132
                [self configureCell:cell forTableView:self.tableView indexPath:[self.tableView indexPathForCell:cell]];
 
133
        }
 
134
}
 
135
 
114
136
#pragma mark - UITableView
115
137
 
116
138
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
121
143
        if (nil == cell)
122
144
        {
123
145
                cell = [[[AlbumArtistUITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:AlbumArtistCellIdentifier] autorelease];
124
 
                cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
125
146
        }
 
147
        [self configureCell:cell forTableView:tableView indexPath:indexPath];
 
148
        return cell;
 
149
}
126
150
 
127
 
    Album *a = nil;
 
151
- (void)configureCell:(AlbumArtistUITableViewCell*)cell forTableView:(UITableView*)tableView indexPath:(NSIndexPath*)indexPath;
 
152
{
 
153
    Album *album = nil;
128
154
    
129
155
        if (self.searchDisplayController.searchResultsTableView == tableView)
130
156
        {
131
 
                a = [self.searchResults objectAtIndex:indexPath.row];
 
157
                album = [self.searchResults objectAtIndex:indexPath.row];
132
158
                cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
133
159
        }
134
160
        else
138
164
        if (indexed)
139
165
        {
140
166
            NSString *index = [self.indexes objectAtIndex:indexPath.section];
141
 
            a = [[self.groupedTableData objectForKey:index] objectAtIndex:indexPath.row]; 
 
167
            album = [[self.groupedTableData objectForKey:index] objectAtIndex:indexPath.row]; 
142
168
        }
143
169
        else
144
170
        {
145
 
            a = [self.tableData objectAtIndex:indexPath.row];
 
171
            album = [self.tableData objectAtIndex:indexPath.row];
146
172
        }
147
173
        }
148
174
 
149
 
    cell.albumNameLabel.text = a.title;
150
 
    cell.artistNameLabel.text = a.artist;
 
175
    cell.albumNameLabel.text = album.title;
 
176
    cell.artistNameLabel.text = album.artist;
151
177
        
152
 
        if (a.coverArtId)
 
178
    NSError *error = nil;
 
179
    cell.coverArtView.image = [loader imageForArtID:album.coverArtId error:&error];
 
180
    
 
181
    if ([error code]) {
 
182
        // Let's see if we need to wait for non-dragging/decelerating
 
183
//        if (self.tableView.dragging == NO && self.tableView.decelerating == NO)
 
184
//        {
 
185
            [loader download:album.coverArtId];
 
186
//        }
 
187
    }
 
188
        if (canStream || [album hasCachedSongs])
153
189
        {
154
 
                UIImage *img = [loader imageForArtID:a.coverArtId];
155
 
                
156
 
                if (img)
157
 
                {
158
 
                        cell.coverArtView.image = img;
159
 
                }
160
 
                else
161
 
                {
162
 
                        cell.coverArtView.image = [UIImage imageNamed:@"default-album-art.png"];
163
 
                        
164
 
                        if (self.tableView.dragging == NO && self.tableView.decelerating == NO)
165
 
                        {
166
 
                                [loader download:a.coverArtId];
167
 
                        }
168
 
                }               
 
190
                cell.albumNameLabel.textColor = [UIColor blackColor];
169
191
        }
170
192
        else
171
193
        {
172
 
                cell.coverArtView.image = [UIImage imageNamed:@"default-album-art.png"];
 
194
                cell.albumNameLabel.textColor = [UIColor lightGrayColor];
 
195
                cell.accessoryType = UITableViewCellAccessoryNone;
173
196
        }
174
 
        
175
 
        return cell;
176
197
}
177
198
 
178
199
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
192
213
    {
193
214
        selection = [self.tableData objectAtIndex:indexPath.row];
194
215
    }
195
 
                
196
 
        AlbumViewController *albumViewController = [[AlbumViewController alloc] initWithStyle:UITableViewStylePlain];
197
 
        albumViewController.album = selection;
198
 
    albumViewController.artist = selection.artist;
199
 
        [self.navigationController pushViewController:albumViewController animated:YES];
200
 
        [albumViewController release];
 
216
 
 
217
        if (canStream || [selection hasCachedSongs])
 
218
        {
 
219
                AlbumViewController *albumViewController = [[AlbumViewController alloc] initWithStyle:UITableViewStylePlain];
 
220
                albumViewController.album = selection;
 
221
                albumViewController.artist = selection.artist;
 
222
                [self.navigationController pushViewController:albumViewController animated:YES];
 
223
                [albumViewController release];
 
224
        }
 
225
        else
 
226
        {
 
227
                [tableView deselectRowAtIndexPath:indexPath animated:YES];
 
228
        }
201
229
}
202
230
 
203
231
 
214
242
        for (NSIndexPath *indexPath in [self.tableView indexPathsForVisibleRows])
215
243
        {
216
244
                Album *album = [self.tableData objectAtIndex:indexPath.row];
 
245
        // This will prompt the album art to download, but doesn't try to load the image when it's done?
217
246
                [loader download:album.coverArtId];
218
247
        }
219
248
}
220
249
 
221
 
 
222
250
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
223
251
{       
224
252
        [super scrollViewDidEndDragging:scrollView willDecelerate:decelerate];