20
20
#import "SubsonicTableViewController.h"
21
21
#import "StreamingPlayer.h"
22
22
#import "SongViewController.h"
23
#import "AsynchronousImageViewCached.h"
24
23
#import "Subsonic.h"
26
25
#import "NSMutableSet+Extras.h"
26
#import "Reachability.h"
27
#import "UONetworkStatusCoordinator.h"
30
@interface SubsonicTableViewController ()
31
@property BOOL hasLoadedRemoteData;
32
- (void)updateReachability:(Reachability*)reachability;
28
36
@implementation SubsonicTableViewController
29
37
@synthesize tableData, searchResults;
38
@synthesize hasLoadedRemoteData;
31
40
#pragma mark - Initialization
35
44
if ((self = [super initWithNibName:@"SearchableTableViewController" bundle:nil]))
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
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];
47
60
- (void)viewDidLoad
49
62
[super viewDidLoad];
50
showProgressHUD = YES;
51
respondToRemoteTimer = NULL;
64
showProgressHUD = YES;
65
respondToRemoteTimer = NULL;
53
67
if (0 == [self.tableData count])
72
if (self.searchDisplayController)
73
[self.tableView setContentOffset:CGPointMake(0,self.searchDisplayController.searchBar.frame.size.height) animated:NO];
59
76
- (void)viewWillAppear:(BOOL)animated
68
85
action:@selector(nowPlayingAction:)] autorelease];
87
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
88
[self updateReachability:[[Subsonic sharedSubsonic] reachability]];
91
- (void)viewDidAppear:(BOOL)animated
93
[super viewDidAppear:animated];
95
if ([[UIApplication sharedApplication] respondsToSelector:@selector(beginReceivingRemoteControlEvents)])
97
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
99
[self becomeFirstResponder]; // this enables listening for events
100
// update the UI in case we were in the background
102
if (![Subsonic sharedSubsonic].hasCredentials)
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.",@"")
107
cancelButtonTitle:NSLocalizedString(@"Cancel",@"")
108
otherButtonTitles:NSLocalizedString(@"Sign In",@""),nil] autorelease];
113
[[NSNotificationCenter defaultCenter] postNotificationName:ASStatusChangedNotification object:self];
116
- (void)viewWillDisappear:(BOOL)animated;
118
[super viewWillDisappear:animated];
119
[[NSNotificationCenter defaultCenter] removeObserver:self name:kReachabilityChangedNotification object:nil];
120
[[UIApplication sharedApplication] endReceivingRemoteControlEvents];
121
[self resignFirstResponder];
84
136
[self.tableView reloadData];
139
- (void)resetTableData:(id)sender
141
self.tableData = [NSMutableArray array];
142
[self.tableView reloadData];
145
- (void)reachabilityChanged:(NSNotification*)notification
147
Reachability *reachability = [notification object];
148
[self updateReachability:reachability];
151
- (void)updateReachability:(Reachability*)reachability
153
canCache = [reachability currentReachabilityStatus] == ReachableViaWiFi;
154
canStream = [reachability currentReachabilityStatus] != NotReachable;
155
if (canStream && !hasLoadedRemoteData)
88
162
#pragma mark Remote Control
152
//xxx todo - it behooves us to figure out where to stick the commensurate -endReceivingRemoteControlEvents call.
153
- (void)viewDidAppear:(BOOL)animated
155
[super viewDidAppear:animated];
157
if (![Subsonic sharedSubsonic].hasCredentials)
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.",@"")
162
cancelButtonTitle:NSLocalizedString(@"Cancel",@"")
163
otherButtonTitles:NSLocalizedString(@"Sign In",@""),nil] autorelease];
168
if ([[UIApplication sharedApplication] respondsToSelector:@selector(beginReceivingRemoteControlEvents)])
170
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
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];
178
226
#pragma mark - Alert handling
180
228
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
203
251
[self showLoadingUI];
204
252
[self loadLocalData];
206
if ([Subsonic sharedSubsonic].hasCredentials)
254
if ([Subsonic sharedSubsonic].hasCredentials && canStream)
256
hasLoadedRemoteData = YES;
257
[UONetworkStatusCoordinator addNetworkActivity];
208
258
[self performSelectorInBackground:@selector(beginLoadingRemoteData:) withObject:(force ? @"YES" : @"NO")];
262
[self finishLoadingData];
212
266
- (void)loadLocalData
217
271
- (void)beginLoadingRemoteData:(NSString*)force
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.
225
279
- (void)finishLoadingData
281
[UONetworkStatusCoordinator removeNetworkActivity];
227
282
[self.tableView reloadData];
228
283
[self hideLoadingUI];
231
286
- (void)failedLoadingData:(NSError*)error
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.",@"")];
237
293
#pragma mark - IBActions