diff --git a/Limelight/Network/AppManager.h b/Limelight/Network/AppManager.h index 262bdb8..e0d4fba 100644 --- a/Limelight/Network/AppManager.h +++ b/Limelight/Network/AppManager.h @@ -19,8 +19,8 @@ @interface AppManager : NSObject -- (id) initWithHost:(Host*)host andCallback:(id)callback; -- (void) retrieveAssets:(NSArray*)appList; +- (id) initWithCallback:(id)callback; +- (void) retrieveAssets:(NSArray*)appList fromHost:(Host*)host; - (void) stopRetrieving; @end diff --git a/Limelight/Network/AppManager.m b/Limelight/Network/AppManager.m index 8188821..13455dc 100644 --- a/Limelight/Network/AppManager.m +++ b/Limelight/Network/AppManager.m @@ -11,39 +11,60 @@ #import "Utils.h" @implementation AppManager { - NSOperationQueue* opQueue; + NSOperationQueue* _opQueue; id _callback; Host* _host; NSString* _uniqueId; NSData* _cert; + NSMutableDictionary* _imageCache; } -- (id) initWithHost:(Host*)host andCallback:(id)callback { +- (id) initWithCallback:(id)callback { self = [super init]; _callback = callback; - _host = host; - opQueue = [[NSOperationQueue alloc] init]; + _opQueue = [[NSOperationQueue alloc] init]; + _imageCache = [[NSMutableDictionary alloc] init]; + _uniqueId = [CryptoManager getUniqueID]; return self; } -- (void) retrieveAssets:(NSArray*)appList { +- (void) retrieveAssets:(NSArray*)appList fromHost:(Host*)host { + Host* oldHost = _host; + _host = host; + BOOL useCache = [oldHost.uuid isEqualToString:_host.uuid]; + NSLog(@"Using cached app images: %d", useCache); + if (!useCache) { + [_imageCache removeAllObjects]; + } for (App* app in appList) { - [opQueue addOperationWithBlock:^{ - [self retrieveAssetForApp:app]; + [_opQueue addOperationWithBlock:^{ + [self retrieveAssetForApp:app useCache:useCache]; }]; } } - (void) stopRetrieving { - [opQueue cancelAllOperations]; + [_opQueue cancelAllOperations]; } -- (void) retrieveAssetForApp:(App*)app { - HttpManager* hMan = [[HttpManager alloc] initWithHost:_host.address uniqueId:_uniqueId deviceName:deviceName cert:_cert]; - NSData* appAsset = [hMan executeRequestSynchronously:[hMan newAppAssetRequestWithAppId:app.appId]]; - UIImage* appImage = [UIImage imageWithData:appAsset]; - app.appImage = appImage; - NSLog(@"App Name: %@ id:%@ image: %@", app.appName, app.appId, app.appImage); +- (void) retrieveAssetForApp:(App*)app useCache:(BOOL)useCache { + UIImage* appImage = nil; + if (useCache) { + UIImage* cachedImage = [_imageCache objectForKey:app.appId]; + if (cachedImage != nil) { + appImage = cachedImage; + app.appImage = appImage; + } + } + if (appImage == nil) { + HttpManager* hMan = [[HttpManager alloc] initWithHost:_host.address uniqueId:_uniqueId deviceName:deviceName cert:_cert]; + NSData* appAsset = [hMan executeRequestSynchronously:[hMan newAppAssetRequestWithAppId:app.appId]]; + appImage = [UIImage imageWithData:appAsset]; + app.appImage = appImage; + if (appImage != nil) { + [_imageCache setObject:appImage forKey:app.appId]; + } + } [self performSelectorOnMainThread:@selector(sendCallBackForApp:) withObject:app waitUntilDone:NO]; } diff --git a/Limelight/ViewControllers/MainFrameViewController.m b/Limelight/ViewControllers/MainFrameViewController.m index e38cf81..aa413e8 100644 --- a/Limelight/ViewControllers/MainFrameViewController.m +++ b/Limelight/ViewControllers/MainFrameViewController.m @@ -78,11 +78,9 @@ static StreamConfiguration* streamConfig; dispatch_async(dispatch_get_main_queue(), ^{ [self updateApps]; }); - if (_appManager != nil) { - [_appManager stopRetrieving]; - } - _appManager = [[AppManager alloc] initWithHost:_selectedHost andCallback:self]; - [_appManager retrieveAssets:appList]; + + [_appManager stopRetrieving]; + [_appManager retrieveAssets:appList fromHost:_selectedHost]; }); } @@ -299,6 +297,8 @@ static StreamConfiguration* streamConfig; _uniqueId = [CryptoManager getUniqueID]; _cert = [CryptoManager readCertFromFile]; + _appManager = [[AppManager alloc] initWithCallback:self]; + // Only initialize the host picker list once if (hostList == nil) { hostList = [[NSMutableSet alloc] init];