~ubuntuone-ios-client-team/ubuntuone-ios-contacts/trunk

« back to all changes in this revision

Viewing changes to musicstreaming/view_controllers/SubsonicTableViewController.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:
20
20
#import "SubsonicTableViewController.h"
21
21
#import "StreamingPlayer.h"
22
22
#import "SongViewController.h"
23
 
#import "AsynchronousImageViewCached.h"
24
23
#import "Subsonic.h"
25
24
#import "Album.h"
26
25
#import "NSMutableSet+Extras.h"
 
26
#import "Reachability.h"
 
27
#import "UONetworkStatusCoordinator.h"
 
28
 
 
29
 
 
30
@interface SubsonicTableViewController ()
 
31
@property BOOL hasLoadedRemoteData;
 
32
- (void)updateReachability:(Reachability*)reachability;
 
33
@end
 
34
 
27
35
 
28
36
@implementation SubsonicTableViewController
29
37
@synthesize tableData, searchResults;
 
38
@synthesize hasLoadedRemoteData;
30
39
 
31
40
#pragma mark - Initialization
32
41
 
34
43
{
35
44
        if ((self = [super initWithNibName:@"SearchableTableViewController" bundle:nil]))
36
45
    {
37
 
                [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadCredentials:) name:@"ReloadAccountCredentials" object:nil];
 
46
                [[NSNotificationCenter defaultCenter] addObserver:self
 
47
                                                 selector:@selector(reloadCredentials:)
 
48
                                                     name:NOTIF_reloadAccountCredentials
 
49
                                                   object:nil];
38
50
        self.title = title;
39
51
        self.tableData = [NSMutableArray array];
40
52
        self.searchResults = [NSMutableArray array];
 
53
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(resetTableData:) name:NOTIF_removeCachedContent object:nil];
41
54
    }
42
55
    return self;
43
56
}
47
60
- (void)viewDidLoad
48
61
{
49
62
    [super viewDidLoad];
50
 
        showProgressHUD = YES;
51
 
        respondToRemoteTimer = NULL;
 
63
 
 
64
    showProgressHUD = YES;
 
65
    respondToRemoteTimer = NULL;
52
66
 
53
67
    if (0 == [self.tableData count])
54
 
        {
55
 
                [self loadData:NO];
56
 
        }
 
68
    {
 
69
        [self loadData:NO];
 
70
    }
 
71
 
 
72
        if (self.searchDisplayController)
 
73
                [self.tableView setContentOffset:CGPointMake(0,self.searchDisplayController.searchBar.frame.size.height) animated:NO];
57
74
}
58
75
 
59
76
- (void)viewWillAppear:(BOOL)animated
67
84
                                                                                                                                                                  target:self
68
85
                                                                                                                                                                  action:@selector(nowPlayingAction:)] autorelease];
69
86
        }
 
87
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
 
88
        [self updateReachability:[[Subsonic sharedSubsonic] reachability]];
 
89
}
 
90
 
 
91
- (void)viewDidAppear:(BOOL)animated
 
92
{
 
93
        [super viewDidAppear:animated];
 
94
        
 
95
    if ([[UIApplication sharedApplication] respondsToSelector:@selector(beginReceivingRemoteControlEvents)])
 
96
    {
 
97
        [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
 
98
    }
 
99
    [self becomeFirstResponder]; // this enables listening for events       
 
100
    // update the UI in case we were in the background
 
101
        
 
102
        if (![Subsonic sharedSubsonic].hasCredentials)
 
103
        {
 
104
                UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Ubuntu One",@"")
 
105
                                                                                                                 message:NSLocalizedString(@"Ubuntu One Music works with Ubuntu One. Find out more at http://one.ubuntu.com/. Have an account? Sign-in below.",@"")
 
106
                                                                                                                delegate:self
 
107
                                                                                           cancelButtonTitle:NSLocalizedString(@"Cancel",@"")
 
108
                                                                                           otherButtonTitles:NSLocalizedString(@"Sign In",@""),nil] autorelease];
 
109
                alert.tag = 1010101;
 
110
                [alert show];
 
111
        }
 
112
        
 
113
        [[NSNotificationCenter defaultCenter] postNotificationName:ASStatusChangedNotification object:self];
 
114
}
 
115
 
 
116
- (void)viewWillDisappear:(BOOL)animated;
 
117
{
 
118
        [super viewWillDisappear:animated];
 
119
        [[NSNotificationCenter defaultCenter] removeObserver:self name:kReachabilityChangedNotification object:nil];
 
120
        [[UIApplication sharedApplication] endReceivingRemoteControlEvents];
 
121
        [self resignFirstResponder];
70
122
}
71
123
 
72
124
#pragma mark -
84
136
        [self.tableView reloadData];
85
137
}
86
138
 
 
139
- (void)resetTableData:(id)sender
 
140
{
 
141
    self.tableData = [NSMutableArray array];
 
142
    [self.tableView reloadData];
 
143
}
 
144
 
 
145
- (void)reachabilityChanged:(NSNotification*)notification
 
146
{
 
147
        Reachability *reachability = [notification object];
 
148
        [self updateReachability:reachability];
 
149
}
 
150
 
 
151
- (void)updateReachability:(Reachability*)reachability
 
152
{
 
153
        canCache = [reachability currentReachabilityStatus] == ReachableViaWiFi;
 
154
        canStream = [reachability currentReachabilityStatus] != NotReachable;
 
155
        if (canStream && !hasLoadedRemoteData)
 
156
        {
 
157
                [self loadData:NO];
 
158
        }
 
159
}
 
160
 
87
161
#pragma mark -
88
162
#pragma mark Remote Control
89
163
 
149
223
        [event release];
150
224
}
151
225
 
152
 
//xxx todo - it behooves us to figure out where to stick the commensurate -endReceivingRemoteControlEvents call.
153
 
- (void)viewDidAppear:(BOOL)animated
154
 
{
155
 
        [super viewDidAppear:animated];
156
 
        
157
 
        if (![Subsonic sharedSubsonic].hasCredentials)
158
 
        {
159
 
                UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Ubuntu One",@"")
160
 
                                                                                                                 message:NSLocalizedString(@"Ubuntu One Music works with Ubuntu One. Find out more at http://one.ubuntu.com/. Have an account? Sign-in below.",@"")
161
 
                                                                                                                delegate:self
162
 
                                                                                           cancelButtonTitle:NSLocalizedString(@"Cancel",@"")
163
 
                                                                                           otherButtonTitles:NSLocalizedString(@"Sign In",@""),nil] autorelease];
164
 
                alert.tag = 1010101;
165
 
                [alert show];
166
 
        }
167
 
 
168
 
    if ([[UIApplication sharedApplication] respondsToSelector:@selector(beginReceivingRemoteControlEvents)])
169
 
        {
170
 
                [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
171
 
        }
172
 
        
173
 
        [self becomeFirstResponder]; // this enables listening for events       
174
 
        // update the UI in case we were in the background
175
 
        [[NSNotificationCenter defaultCenter] postNotificationName:ASStatusChangedNotification object:self];
176
 
}
177
 
 
178
226
#pragma mark - Alert handling
179
227
 
180
228
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
203
251
        [self showLoadingUI];
204
252
        [self loadLocalData];
205
253
        
206
 
        if ([Subsonic sharedSubsonic].hasCredentials)
 
254
        if ([Subsonic sharedSubsonic].hasCredentials && canStream)
207
255
        {
 
256
                hasLoadedRemoteData = YES;
 
257
                [UONetworkStatusCoordinator addNetworkActivity];
208
258
                [self performSelectorInBackground:@selector(beginLoadingRemoteData:) withObject:(force ? @"YES" : @"NO")];
209
259
        }
 
260
        else
 
261
        {
 
262
                [self finishLoadingData];
 
263
        }
210
264
}
211
265
 
212
266
- (void)loadLocalData
217
271
- (void)beginLoadingRemoteData:(NSString*)force
218
272
{
219
273
        //Starts with an NSAutoreleasePool
220
 
        //Loads data either from DataCache or from an XML feed. If force == @"YES"
 
274
        //Loads data either from CoreData or from an XML feed. If force == @"YES"
221
275
        //Release the pool
222
276
        //Call finishLoadingData on the main thread.
223
277
}
224
278
 
225
279
- (void)finishLoadingData
226
280
{
 
281
        [UONetworkStatusCoordinator removeNetworkActivity];
227
282
        [self.tableView reloadData];
228
283
        [self hideLoadingUI];
229
284
}
231
286
- (void)failedLoadingData:(NSError*)error
232
287
{
233
288
        [self hideLoadingUI];
234
 
        [self showError:NSLocalizedString(@"Unable to load your music. Please check your Internet connection or account credentials and try again.",@"")];
 
289
    NSLog(@"Error loading data: %@", error);
 
290
//      [self showError:NSLocalizedString(@"Unable to load your music. Please check your Internet connection or account credentials and try again.",@"")];
235
291
}
236
292
 
237
293
#pragma mark - IBActions
304
360
        [self showMessage:errorMessage withTitle:NSLocalizedString(@"Error",@"")];
305
361
}
306
362
 
307
 
#pragma mark - MBProgressHUD
 
363
#pragma mark - MiniHudView
308
364
 
309
365
- (MiniHudView*)hudView
310
366
{
326
382
 
327
383
- (void)showLoadingUIWithText:(NSString*)text
328
384
{
329
 
        [self.hudView show:YES];
 
385
        // TODO: remove the hud code altogether once we're satisfied
 
386
        // with other status indicators
 
387
//      [self.hudView show:YES];
330
388
}
331
389
 
332
390
- (void)hideLoadingUI