Refactor app list to be non-static and sorted in alphabetical order

This commit is contained in:
Cameron Gutman
2015-07-11 01:28:52 -07:00
parent 4c95773131
commit 13e894e9a3
5 changed files with 31 additions and 19 deletions
+2
View File
@@ -17,6 +17,8 @@ NS_ASSUME_NONNULL_BEGIN
@property BOOL isRunning; @property BOOL isRunning;
- (NSComparisonResult)compareName:(App *)other;
@end @end
NS_ASSUME_NONNULL_END NS_ASSUME_NONNULL_END
+4
View File
@@ -13,4 +13,8 @@
@synthesize isRunning; @synthesize isRunning;
- (NSComparisonResult)compareName:(App *)other {
return [self.name caseInsensitiveCompare:other.name];
}
@end @end
+1 -1
View File
@@ -20,7 +20,7 @@
@interface AppAssetManager : NSObject @interface AppAssetManager : NSObject
- (id) initWithCallback:(id<AppAssetCallback>)callback; - (id) initWithCallback:(id<AppAssetCallback>)callback;
- (void) retrieveAssets:(NSArray*)appList fromHost:(Host*)host; - (void) retrieveAssetsFromHost:(Host*)host;
- (void) stopRetrieving; - (void) stopRetrieving;
@end @end
+2 -2
View File
@@ -28,8 +28,8 @@ static const int MAX_REQUEST_COUNT = 4;
return self; return self;
} }
- (void) retrieveAssets:(NSArray*)appList fromHost:(Host*)host { - (void) retrieveAssetsFromHost:(Host*)host {
for (App* app in appList) { for (App* app in host.appList) {
if (app.image == nil) { if (app.image == nil) {
AppAssetRetriever* retriever = [[AppAssetRetriever alloc] init]; AppAssetRetriever* retriever = [[AppAssetRetriever alloc] init];
retriever.app = app; retriever.app = app;
@@ -35,9 +35,9 @@
UIAlertController* _pairAlert; UIAlertController* _pairAlert;
UIScrollView* hostScrollView; UIScrollView* hostScrollView;
int currentPosition; int currentPosition;
NSArray* _sortedAppList;
} }
static NSMutableSet* hostList; static NSMutableSet* hostList;
static NSArray* appList;
- (void)showPIN:(NSString *)PIN { - (void)showPIN:(NSString *)PIN {
dispatch_async(dispatch_get_main_queue(), ^{ dispatch_async(dispatch_get_main_queue(), ^{
@@ -74,7 +74,6 @@ static NSArray* appList;
BOOL usingCachedAppList = false; BOOL usingCachedAppList = false;
if ([_selectedHost.appList count] > 0) { if ([_selectedHost.appList count] > 0) {
usingCachedAppList = true; usingCachedAppList = true;
appList = [_selectedHost.appList allObjects];
dispatch_async(dispatch_get_main_queue(), ^{ dispatch_async(dispatch_get_main_queue(), ^{
_computerNameButton.title = _selectedHost.name; _computerNameButton.title = _selectedHost.name;
[self.navigationController.navigationBar setNeedsLayout]; [self.navigationController.navigationBar setNeedsLayout];
@@ -112,46 +111,45 @@ static NSArray* appList;
}); });
[_appManager stopRetrieving]; [_appManager stopRetrieving];
[_appManager retrieveAssets:appList fromHost:_selectedHost]; [_appManager retrieveAssetsFromHost:_selectedHost];
[self hideLoadingFrame]; [self hideLoadingFrame];
} }
}); });
} }
- (void) mergeAppLists:(NSArray*) newList { - (void) mergeAppLists:(NSArray*) newList {
NSMutableArray* mergedList = [[NSMutableArray alloc] init];
for (App* app in newList) { for (App* app in newList) {
BOOL appAlreadyInList = NO; BOOL appAlreadyInList = NO;
for (App* savedApp in appList) { for (App* savedApp in _selectedHost.appList) {
if ([app.id isEqualToString:savedApp.id]) { if ([app.id isEqualToString:savedApp.id]) {
appAlreadyInList = YES; appAlreadyInList = YES;
[mergedList addObject:savedApp]; break;
} }
} }
if (!appAlreadyInList) { if (!appAlreadyInList) {
[mergedList addObject:app];
[_selectedHost addAppListObject:app];
app.host = _selectedHost; app.host = _selectedHost;
[_selectedHost addAppListObject:app];
} }
} }
for (App* app in appList) { for (App* app in _selectedHost.appList) {
BOOL appWasRemoved = YES; BOOL appWasRemoved = YES;
for (App* mergedApp in mergedList) { for (App* mergedApp in newList) {
if ([mergedApp.id isEqualToString:app.id]) { if ([mergedApp.id isEqualToString:app.id]) {
appWasRemoved = NO; appWasRemoved = NO;
break;
} }
} }
if (appWasRemoved) { if (appWasRemoved) {
[_selectedHost removeAppListObject:app]; [_selectedHost removeAppListObject:app];
} }
} }
appList = mergedList;
} }
- (void)showHostSelectionView { - (void)showHostSelectionView {
appList = [[NSArray alloc] init];
[_appManager stopRetrieving]; [_appManager stopRetrieving];
[[[DataManager alloc] init] saveHosts];
_selectedHost = nil;
_computerNameButton.title = @"No Host Selected"; _computerNameButton.title = @"No Host Selected";
[self.collectionView reloadData]; [self.collectionView reloadData];
[self.view addSubview:hostScrollView]; [self.view addSubview:hostScrollView];
@@ -384,7 +382,7 @@ static NSArray* appList;
} }
- (App*) findRunningApp { - (App*) findRunningApp {
for (App* app in appList) { for (App* app in _selectedHost.appList) {
if (app.isRunning) { if (app.isRunning) {
return app; return app;
} }
@@ -555,6 +553,9 @@ static NSArray* appList;
} }
- (void) updateApps { - (void) updateApps {
_sortedAppList = [_selectedHost.appList allObjects];
_sortedAppList = [_sortedAppList sortedArrayUsingSelector:@selector(compareName:)];
[hostScrollView removeFromSuperview]; [hostScrollView removeFromSuperview];
[self.collectionView reloadData]; [self.collectionView reloadData];
} }
@@ -562,7 +563,7 @@ static NSArray* appList;
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"AppCell" forIndexPath:indexPath]; UICollectionViewCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"AppCell" forIndexPath:indexPath];
App* app = appList[indexPath.row]; App* app = _sortedAppList[indexPath.row];
UIAppView* appView = [[UIAppView alloc] initWithApp:app andCallback:self]; UIAppView* appView = [[UIAppView alloc] initWithApp:app andCallback:self];
[appView updateAppImage]; [appView updateAppImage];
@@ -583,7 +584,12 @@ static NSArray* appList;
} }
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return appList.count; if (_selectedHost != nil) {
return _selectedHost.appList.count;
}
else {
return 0;
}
} }
- (void)didReceiveMemoryWarning - (void)didReceiveMemoryWarning