~urbanape/ubuntuone-ios-client/2.0-13-prerelease

« back to all changes in this revision

Viewing changes to musicstreaming/view_controllers/ArtistViewController.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:
31
31
#import "ArtistViewController.h"
32
32
#import "AlbumViewController.h"
33
33
#import "AlbumUITableViewCell.h"
34
 
#import "AsynchronousImageViewCached.h"
35
 
#import "ArtistParser.h"
36
34
#import "Artist.h"
37
35
#import "Album.h"
38
36
#import "Song.h"
39
 
#import "DataCache.h"
40
 
#import "NSMutableSet+Extras.h"
 
37
 
 
38
 
 
39
@interface ArtistViewController ()
 
40
- (void)configureCell:(UITableViewCell*)cell forAlbumAtIndexPath:(NSIndexPath*)indexPath;
 
41
@end
 
42
 
41
43
 
42
44
@implementation ArtistViewController
43
45
@synthesize artist, albums, songs;
59
61
        self.tableView.rowHeight = 60.0f;
60
62
        self.title = self.artist.name;
61
63
 
 
64
        loader = [[AlbumArtLoader alloc] init];
 
65
        loader.delegate = self;
 
66
        loader.imageSize = 120;
 
67
 
62
68
        [self loadData:NO];
63
69
}
64
70
 
 
71
- (void)viewDidUnload
 
72
{
 
73
    [super viewDidUnload];
 
74
    loader.delegate = nil;
 
75
    [loader release], loader = nil;
 
76
}
 
77
 
65
78
#pragma mark -
66
79
#pragma mark Data Loader
67
80
 
71
84
        {
72
85
                [self.albums removeAllObjects];
73
86
                [PerThreadManagedObjectContext() refreshObject:self.artist mergeChanges:YES]; //force the album to reload its set of songs from the persistent store.
74
 
                [self.albums addObjectsFromArray:[self.artist.albums allObjects]];
 
87
        NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:@"dearticlizedTitle" ascending:YES];
 
88
        NSArray *descriptors = [NSArray arrayWithObjects:descriptor, nil];
 
89
                [self.albums addObjectsFromArray:[self.artist.albums sortedArrayUsingDescriptors:descriptors]];
75
90
        }
76
91
        
77
 
        @synchronized(self.songs)
78
 
        {
79
 
                [self.songs removeAllObjects];
80
 
                [self.songs addObjectsFromArray:[self.artist.songs allObjects]];
81
 
        }
 
92
//      @synchronized(self.songs)
 
93
//      {
 
94
//              [self.songs removeAllObjects];
 
95
//        NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:@"dearticlizedTitle" ascending:YES];
 
96
//        NSArray *descriptors = [NSArray arrayWithObjects:descriptor, nil];
 
97
//              [self.songs addObjectsFromArray:[self.artist.songs sortedArrayUsingDescriptors:descriptors]];
 
98
//      }
82
99
        
83
100
        [self.tableView reloadData];
84
101
}
87
104
{
88
105
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
89
106
        
90
 
        NSMutableSet *parameters = [NSMutableSet set];
91
 
        [parameters addKeyValueObjectFromArray:[NSArray arrayWithObjects:@"id", self.artist.artistId, nil]];
92
 
        NSURL *url = [[Subsonic sharedSubsonic] getBaseURL:@"getMusicDirectory.view" parameters:parameters];
93
 
        NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];
94
 
        ArtistParser *parser = [[ArtistParser alloc] init];
95
 
        parser.artistManagedObjectID = [self.artist objectID];
96
 
        [xmlParser setDelegate:parser];
 
107
        NSError *error = [self.artist loadAlbums];
97
108
        
98
 
        if (![xmlParser parse])
 
109
        if (error)
99
110
        {
100
 
                [self performSelectorOnMainThread:@selector(failedLoadingData:) withObject:[xmlParser parserError] waitUntilDone:NO];
101
 
                [xmlParser release];
102
 
                [parser release];
 
111
                [self performSelectorOnMainThread:@selector(failedLoadingData:) withObject:error waitUntilDone:NO];
103
112
                [pool release];
104
 
                return;
 
113
                return;         
105
114
        }
106
 
                
107
 
        [xmlParser release];
108
 
        [parser release];
109
115
        
110
116
        [pool release];
111
117
        
117
123
{
118
124
        [self.tableView reloadData];
119
125
        [self hideLoadingUI];
 
126
        [super finishLoadingData];
120
127
}
121
128
 
122
129
#pragma mark -
130
137
        [self.tableView reloadData];
131
138
}
132
139
 
 
140
- (void)resetTableData:(id)sender
 
141
{
 
142
    [super resetTableData:sender];
 
143
    self.artist = nil;
 
144
    self.albums = [NSMutableArray array];
 
145
    self.songs = [NSMutableArray array];
 
146
    [self.tableView reloadData];
 
147
}
 
148
 
 
149
- (void)updateReachability:(Reachability *)reachability;
 
150
{
 
151
        [super updateReachability:reachability];
 
152
        
 
153
        for (AlbumUITableViewCell *cell in [self.tableView visibleCells])
 
154
        {
 
155
                [self configureCell:cell forAlbumAtIndexPath:[self.tableView indexPathForCell:cell]];
 
156
        }
 
157
}
 
158
 
133
159
#pragma mark Table view methods
134
160
 
135
161
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
164
190
        if (nil == cell)
165
191
        {
166
192
                cell = [[[AlbumUITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:AlbumCellIdentifier] autorelease];
167
 
                cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
168
193
        }
169
 
        
 
194
        [self configureCell:cell forAlbumAtIndexPath:indexPath];
 
195
        return cell;
 
196
}
 
197
 
 
198
- (void)configureCell:(AlbumUITableViewCell*)cell forAlbumAtIndexPath:(NSIndexPath*)indexPath;
 
199
{
 
200
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
 
201
 
170
202
        if (0 == indexPath.section)
171
203
        {
172
204
                Album *album = [self.albums objectAtIndex:indexPath.row];
173
205
                cell.albumNameLabel.text = album.title;
174
 
        [cell.coverArtView loadImageId:album.coverArtId forSize:120];
 
206
 
 
207
        NSError *error = nil;
 
208
        cell.coverArtView.image = [loader imageForArtID:album.coverArtId error:&error];
 
209
        
 
210
        if ([error code]) {
 
211
            // Let's see if we need to wait for non-dragging/decelerating
 
212
    //        if (self.tableView.dragging == NO && self.tableView.decelerating == NO)
 
213
    //        {
 
214
                [loader download:album.coverArtId];
 
215
    //        }
 
216
        }
 
217
                if (canStream || [album hasCachedSongs])
 
218
                {
 
219
                        cell.albumNameLabel.textColor = [UIColor blackColor];
 
220
                }
 
221
                else
 
222
                {
 
223
                        cell.albumNameLabel.textColor = [UIColor lightGrayColor];
 
224
                        cell.accessoryType = UITableViewCellAccessoryNone;
 
225
                }
175
226
        }
176
227
        else
177
228
        {
178
229
                Song *song = [self.songs objectAtIndex:indexPath.row];
179
230
                cell.textLabel.text = song.title;
180
231
        }
181
 
 
182
 
        return cell;
183
232
}
184
233
 
185
234
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
188
237
        {
189
238
                Album *selection = [self.albums objectAtIndex:indexPath.row];
190
239
 
191
 
                AlbumViewController *albumViewController = [[[AlbumViewController alloc] initWithStyle:UITableViewStylePlain] autorelease];
192
 
                albumViewController.album = selection;
193
 
                albumViewController.artist = self.artist.name;
194
 
                [self.navigationController pushViewController:albumViewController animated:YES];
 
240
                if (canStream || [selection hasCachedSongs])
 
241
                {
 
242
                        AlbumViewController *albumViewController = [[[AlbumViewController alloc] initWithStyle:UITableViewStylePlain] autorelease];
 
243
                        albumViewController.album = selection;
 
244
                        albumViewController.artist = self.artist.name;
 
245
                        [self.navigationController pushViewController:albumViewController animated:YES];
 
246
                }
 
247
                else
 
248
                {
 
249
                        [tableView deselectRowAtIndexPath:indexPath animated:YES];
 
250
                }
195
251
        }
196
252
        else
197
253
        {
198
254
                Song *song = [self.songs objectAtIndex:indexPath.row];
199
 
                [self didSelectSong:song withPlaylist:self.songs];
 
255
                if (canStream || [song cachedSongExists])
 
256
                {
 
257
                        [self didSelectSong:song withPlaylist:self.songs];
 
258
                }
 
259
                else
 
260
                {
 
261
                        [tableView deselectRowAtIndexPath:indexPath animated:YES];
 
262
                }
200
263
        }
201
264
}
202
265
 
218
281
        [super dealloc];
219
282
}
220
283
 
 
284
#pragma mark -
 
285
#pragma mark Deferred Image Loading
 
286
 
 
287
- (void)reloadImages
 
288
{
 
289
        [self.tableView reloadData];
 
290
}
 
291
 
 
292
- (void)loadImagesForOnscreenRows
 
293
{
 
294
        for (NSIndexPath *indexPath in [self.tableView indexPathsForVisibleRows])
 
295
        {
 
296
                Album *album = [self.tableData objectAtIndex:indexPath.row];
 
297
        // This will prompt the album art to download, but doesn't try to load the image when it's done?
 
298
                [loader download:album.coverArtId];
 
299
        }
 
300
}
 
301
 
 
302
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
 
303
{       
 
304
        [super scrollViewDidEndDragging:scrollView willDecelerate:decelerate];
 
305
        
 
306
    if (!decelerate)
 
307
        {
 
308
        [self loadImagesForOnscreenRows];
 
309
    }
 
310
}
 
311
 
 
312
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
 
313
{       
 
314
    [self loadImagesForOnscreenRows];
 
315
}
 
316
 
221
317
@end