From 37f25c1f9716b647c827e14c61a3634f8e2723d7 Mon Sep 17 00:00:00 2001 From: Diego Waxemberg Date: Fri, 10 Jul 2015 22:19:25 -0700 Subject: [PATCH] fixed some bugs with saving app list to database --- .../Moonlight v1.0.xcdatamodel/contents | 4 +-- Limelight/Network/AppListResponse.h | 2 ++ Limelight/Network/AppListResponse.m | 3 +- .../ViewControllers/MainFrameViewController.m | 29 +++++++++++++------ 4 files changed, 26 insertions(+), 12 deletions(-) diff --git a/Limelight/Limelight.xcdatamodeld/Moonlight v1.0.xcdatamodel/contents b/Limelight/Limelight.xcdatamodeld/Moonlight v1.0.xcdatamodel/contents index 4c3eafd..3c95341 100644 --- a/Limelight/Limelight.xcdatamodeld/Moonlight v1.0.xcdatamodel/contents +++ b/Limelight/Limelight.xcdatamodeld/Moonlight v1.0.xcdatamodel/contents @@ -2,7 +2,7 @@ - + @@ -24,7 +24,7 @@ - + diff --git a/Limelight/Network/AppListResponse.h b/Limelight/Network/AppListResponse.h index 9769ea9..5fdc01f 100644 --- a/Limelight/Network/AppListResponse.h +++ b/Limelight/Network/AppListResponse.h @@ -10,6 +10,8 @@ @interface AppListResponse : NSObject +@property Host* host; + - (void)populateWithData:(NSData *)data; - (NSArray*) getAppList; - (BOOL) isStatusOk; diff --git a/Limelight/Network/AppListResponse.m b/Limelight/Network/AppListResponse.m index 9c3296a..71fda0f 100644 --- a/Limelight/Network/AppListResponse.m +++ b/Limelight/Network/AppListResponse.m @@ -14,7 +14,7 @@ @implementation AppListResponse { NSMutableArray* _appList; } -@synthesize data, statusCode, statusMessage; +@synthesize data, statusCode, statusMessage, host; static const char* TAG_APP = "App"; static const char* TAG_APP_TITLE = "AppTitle"; @@ -98,6 +98,7 @@ static const char* TAG_APP_IS_RUNNING = "IsRunning"; App* app = [dataMan createApp]; app.name = appName; app.id = appId; + app.host = host; app.isRunning = appIsRunning; if (app.id != nil) { [_appList addObject:app]; diff --git a/Limelight/ViewControllers/MainFrameViewController.m b/Limelight/ViewControllers/MainFrameViewController.m index c80e056..f70c040 100644 --- a/Limelight/ViewControllers/MainFrameViewController.m +++ b/Limelight/ViewControllers/MainFrameViewController.m @@ -72,10 +72,10 @@ static NSArray* appList; - (void)alreadyPaired { BOOL usingCachedAppList = false; - if (_selectedHost.appList != nil) { + if ([_selectedHost.appList count] > 0) { usingCachedAppList = true; + appList = [_selectedHost.appList allObjects]; dispatch_async(dispatch_get_main_queue(), ^{ - appList = [_selectedHost.appList allObjects]; _computerNameButton.title = _selectedHost.name; [self.navigationController.navigationBar setNeedsLayout]; [self updateApps]; @@ -87,6 +87,7 @@ static NSArray* appList; HttpManager* hMan = [[HttpManager alloc] initWithHost:_selectedHost.activeAddress uniqueId:_uniqueId deviceName:deviceName cert:_cert]; AppListResponse* appListResp = [[AppListResponse alloc] init]; + appListResp.host = _selectedHost; [hMan executeRequestSynchronously:[HttpRequest requestForResponse:appListResp withUrlRequest:[hMan newAppListRequest]]]; if (appListResp == nil || ![appListResp isStatusOk] || [appListResp getAppList] == nil) { Log(LOG_W, @"Failed to get applist: %@", appListResp.statusMessage); @@ -101,10 +102,6 @@ static NSArray* appList; [self showHostSelectionView]; }); } else { - if (!usingCachedAppList) { - appList = [[NSArray alloc] init]; - [_selectedHost addAppList:[NSSet setWithArray:appList]]; - } [self mergeAppLists:[appListResp getAppList]]; dispatch_async(dispatch_get_main_queue(), ^{ @@ -122,17 +119,31 @@ static NSArray* appList; } - (void) mergeAppLists:(NSArray*) newList { - NSMutableArray* mergedList = [NSMutableArray arrayWithArray:appList]; + NSMutableArray* mergedList = [[NSMutableArray alloc] init]; for (App* app in newList) { BOOL appAlreadyInList = NO; - for (App* savedApp in mergedList) { - if (app.id == savedApp.id) { + for (App* savedApp in appList) { + if ([app.id isEqualToString:savedApp.id]) { appAlreadyInList = YES; + [mergedList addObject:savedApp]; } } if (!appAlreadyInList) { [mergedList addObject:app]; [_selectedHost addAppListObject:app]; + app.host = _selectedHost; + } + } + + for (App* app in appList) { + BOOL appWasRemoved = YES; + for (App* mergedApp in mergedList) { + if ([mergedApp.id isEqualToString:app.id]) { + appWasRemoved = NO; + } + } + if (appWasRemoved) { + [_selectedHost removeAppListObject:app]; } } appList = mergedList;