From 13e894e9a3ac3910d9de5b477b4da2c9ce551404 Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Sat, 11 Jul 2015 01:28:52 -0700 Subject: [PATCH] Refactor app list to be non-static and sorted in alphabetical order --- Limelight/Database/App.h | 2 + Limelight/Database/App.m | 4 ++ Limelight/Network/AppAssetManager.h | 2 +- Limelight/Network/AppAssetManager.m | 4 +- .../ViewControllers/MainFrameViewController.m | 38 +++++++++++-------- 5 files changed, 31 insertions(+), 19 deletions(-) diff --git a/Limelight/Database/App.h b/Limelight/Database/App.h index 92b94544..ae55976e 100644 --- a/Limelight/Database/App.h +++ b/Limelight/Database/App.h @@ -17,6 +17,8 @@ NS_ASSUME_NONNULL_BEGIN @property BOOL isRunning; +- (NSComparisonResult)compareName:(App *)other; + @end NS_ASSUME_NONNULL_END diff --git a/Limelight/Database/App.m b/Limelight/Database/App.m index 8ec42f5e..d1e28d40 100644 --- a/Limelight/Database/App.m +++ b/Limelight/Database/App.m @@ -13,4 +13,8 @@ @synthesize isRunning; +- (NSComparisonResult)compareName:(App *)other { + return [self.name caseInsensitiveCompare:other.name]; +} + @end diff --git a/Limelight/Network/AppAssetManager.h b/Limelight/Network/AppAssetManager.h index 8d544758..95f3a4be 100644 --- a/Limelight/Network/AppAssetManager.h +++ b/Limelight/Network/AppAssetManager.h @@ -20,7 +20,7 @@ @interface AppAssetManager : NSObject - (id) initWithCallback:(id)callback; -- (void) retrieveAssets:(NSArray*)appList fromHost:(Host*)host; +- (void) retrieveAssetsFromHost:(Host*)host; - (void) stopRetrieving; @end diff --git a/Limelight/Network/AppAssetManager.m b/Limelight/Network/AppAssetManager.m index 8a16bf77..6aaad796 100644 --- a/Limelight/Network/AppAssetManager.m +++ b/Limelight/Network/AppAssetManager.m @@ -28,8 +28,8 @@ static const int MAX_REQUEST_COUNT = 4; return self; } -- (void) retrieveAssets:(NSArray*)appList fromHost:(Host*)host { - for (App* app in appList) { +- (void) retrieveAssetsFromHost:(Host*)host { + for (App* app in host.appList) { if (app.image == nil) { AppAssetRetriever* retriever = [[AppAssetRetriever alloc] init]; retriever.app = app; diff --git a/Limelight/ViewControllers/MainFrameViewController.m b/Limelight/ViewControllers/MainFrameViewController.m index 40e13ba8..d1ad360a 100644 --- a/Limelight/ViewControllers/MainFrameViewController.m +++ b/Limelight/ViewControllers/MainFrameViewController.m @@ -35,9 +35,9 @@ UIAlertController* _pairAlert; UIScrollView* hostScrollView; int currentPosition; + NSArray* _sortedAppList; } static NSMutableSet* hostList; -static NSArray* appList; - (void)showPIN:(NSString *)PIN { dispatch_async(dispatch_get_main_queue(), ^{ @@ -74,7 +74,6 @@ static NSArray* appList; BOOL usingCachedAppList = false; if ([_selectedHost.appList count] > 0) { usingCachedAppList = true; - appList = [_selectedHost.appList allObjects]; dispatch_async(dispatch_get_main_queue(), ^{ _computerNameButton.title = _selectedHost.name; [self.navigationController.navigationBar setNeedsLayout]; @@ -112,46 +111,45 @@ static NSArray* appList; }); [_appManager stopRetrieving]; - [_appManager retrieveAssets:appList fromHost:_selectedHost]; + [_appManager retrieveAssetsFromHost:_selectedHost]; [self hideLoadingFrame]; } }); } - (void) mergeAppLists:(NSArray*) newList { - NSMutableArray* mergedList = [[NSMutableArray alloc] init]; for (App* app in newList) { - BOOL appAlreadyInList = NO; - for (App* savedApp in appList) { + BOOL appAlreadyInList = NO; + for (App* savedApp in _selectedHost.appList) { if ([app.id isEqualToString:savedApp.id]) { appAlreadyInList = YES; - [mergedList addObject:savedApp]; + break; } } if (!appAlreadyInList) { - [mergedList addObject:app]; - [_selectedHost addAppListObject:app]; app.host = _selectedHost; + [_selectedHost addAppListObject:app]; } } - for (App* app in appList) { + for (App* app in _selectedHost.appList) { BOOL appWasRemoved = YES; - for (App* mergedApp in mergedList) { + for (App* mergedApp in newList) { if ([mergedApp.id isEqualToString:app.id]) { appWasRemoved = NO; + break; } } if (appWasRemoved) { [_selectedHost removeAppListObject:app]; } } - appList = mergedList; } - (void)showHostSelectionView { - appList = [[NSArray alloc] init]; [_appManager stopRetrieving]; + [[[DataManager alloc] init] saveHosts]; + _selectedHost = nil; _computerNameButton.title = @"No Host Selected"; [self.collectionView reloadData]; [self.view addSubview:hostScrollView]; @@ -384,7 +382,7 @@ static NSArray* appList; } - (App*) findRunningApp { - for (App* app in appList) { + for (App* app in _selectedHost.appList) { if (app.isRunning) { return app; } @@ -555,6 +553,9 @@ static NSArray* appList; } - (void) updateApps { + _sortedAppList = [_selectedHost.appList allObjects]; + _sortedAppList = [_sortedAppList sortedArrayUsingSelector:@selector(compareName:)]; + [hostScrollView removeFromSuperview]; [self.collectionView reloadData]; } @@ -562,7 +563,7 @@ static NSArray* appList; - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)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]; [appView updateAppImage]; @@ -583,7 +584,12 @@ static NSArray* appList; } - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { - return appList.count; + if (_selectedHost != nil) { + return _selectedHost.appList.count; + } + else { + return 0; + } } - (void)didReceiveMemoryWarning