From 9c6b718be28830a4daa84130edfb58b9793bf543 Mon Sep 17 00:00:00 2001 From: Diego Waxemberg Date: Sat, 11 Jul 2015 18:47:05 -0700 Subject: [PATCH] fixed issue with apps duplicating in app list --- Limelight/Database/DataManager.h | 3 ++- Limelight/Database/DataManager.m | 8 ++++++-- Limelight/Network/AppListResponse.h | 2 -- Limelight/Network/AppListResponse.m | 3 +-- Limelight/Network/DiscoveryManager.m | 2 +- Limelight/ViewControllers/MainFrameViewController.m | 12 ++++++++---- 6 files changed, 18 insertions(+), 12 deletions(-) diff --git a/Limelight/Database/DataManager.h b/Limelight/Database/DataManager.h index f5fc9b87..da23ec95 100644 --- a/Limelight/Database/DataManager.h +++ b/Limelight/Database/DataManager.h @@ -19,9 +19,10 @@ - (void) saveSettingsWithBitrate:(NSInteger)bitrate framerate:(NSInteger)framerate height:(NSInteger)height width:(NSInteger)width onscreenControls:(NSInteger)onscreenControls; - (Settings*) retrieveSettings; - (NSArray*) retrieveHosts; -- (void) saveHosts; +- (void) saveData; - (Host*) createHost; - (void) removeHost:(Host*)host; - (App*) createApp; +- (void) removeApp:(App*)app; @end diff --git a/Limelight/Database/DataManager.m b/Limelight/Database/DataManager.m index c7edef6d..ffea1eea 100644 --- a/Limelight/Database/DataManager.m +++ b/Limelight/Database/DataManager.m @@ -53,10 +53,10 @@ - (void) removeHost:(Host*)host { [[self.appDelegate managedObjectContext] deleteObject:host]; - [self saveHosts]; + [self saveData]; } -- (void) saveHosts { +- (void) saveData { NSError* error; if (![[self.appDelegate managedObjectContext] save:&error]) { Log(LOG_E, @"Unable to save hosts to database: %@", error); @@ -73,6 +73,10 @@ return [[App alloc] initWithEntity:entity insertIntoManagedObjectContext:[self.appDelegate managedObjectContext]]; } +- (void) removeApp:(App*)app { + [[self.appDelegate managedObjectContext] deleteObject:app]; +} + - (NSArray*) fetchRecords:(NSString*)entityName { NSFetchRequest* fetchRequest = [[NSFetchRequest alloc] init]; NSEntityDescription* entity = [NSEntityDescription entityForName:entityName inManagedObjectContext:[self.appDelegate managedObjectContext]]; diff --git a/Limelight/Network/AppListResponse.h b/Limelight/Network/AppListResponse.h index 5fdc01f6..9769ea97 100644 --- a/Limelight/Network/AppListResponse.h +++ b/Limelight/Network/AppListResponse.h @@ -10,8 +10,6 @@ @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 1cc94b6b..7c86cbab 100644 --- a/Limelight/Network/AppListResponse.m +++ b/Limelight/Network/AppListResponse.m @@ -14,7 +14,7 @@ @implementation AppListResponse { NSMutableArray* _appList; } -@synthesize data, statusCode, statusMessage, host; +@synthesize data, statusCode, statusMessage; static const char* TAG_APP = "App"; static const char* TAG_APP_TITLE = "AppTitle"; @@ -99,7 +99,6 @@ static const char* TAG_APP_IS_RUNNING = "IsRunning"; App* app = [dataMan createApp]; app.name = appName; app.id = appId; - app.host = host; app.isRunning = appIsRunning; [_appList addObject:app]; } diff --git a/Limelight/Network/DiscoveryManager.m b/Limelight/Network/DiscoveryManager.m index 9cd34b12..0757e20f 100644 --- a/Limelight/Network/DiscoveryManager.m +++ b/Limelight/Network/DiscoveryManager.m @@ -125,7 +125,7 @@ [dataMan removeHost:host]; } } - [dataMan saveHosts]; + [dataMan saveData]; }); } diff --git a/Limelight/ViewControllers/MainFrameViewController.m b/Limelight/ViewControllers/MainFrameViewController.m index 5be5de13..a4fa8570 100644 --- a/Limelight/ViewControllers/MainFrameViewController.m +++ b/Limelight/ViewControllers/MainFrameViewController.m @@ -86,7 +86,6 @@ static NSMutableSet* hostList; 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); @@ -118,6 +117,7 @@ static NSMutableSet* hostList; } - (void) mergeAppLists:(NSArray*) newList { + DataManager* database = [[DataManager alloc] init]; for (App* app in newList) { BOOL appAlreadyInList = NO; for (App* savedApp in _selectedHost.appList) { @@ -129,6 +129,8 @@ static NSMutableSet* hostList; if (!appAlreadyInList) { app.host = _selectedHost; [_selectedHost addAppListObject:app]; + } else { + [database removeApp:app]; } } @@ -142,13 +144,15 @@ static NSMutableSet* hostList; } if (appWasRemoved) { [_selectedHost removeAppListObject:app]; + [database removeApp:app]; } } + [database saveData]; } - (void)showHostSelectionView { [_appManager stopRetrieving]; - [[[DataManager alloc] init] saveHosts]; + [[[DataManager alloc] init] saveData]; _selectedHost = nil; _computerNameButton.title = @"No Host Selected"; [self.collectionView reloadData]; @@ -265,7 +269,7 @@ static NSMutableSet* hostList; [_discMan discoverHost:hostAddress withCallback:^(Host* host, NSString* error){ if (host != nil) { DataManager* dataMan = [[DataManager alloc] init]; - [dataMan saveHosts]; + [dataMan saveData]; dispatch_async(dispatch_get_main_queue(), ^{ @synchronized(hostList) { [hostList addObject:host]; @@ -472,7 +476,7 @@ static NSMutableSet* hostList; [_discMan stopDiscovery]; // In case the host objects were updated in the background - [[[DataManager alloc] init] saveHosts]; + [[[DataManager alloc] init] saveData]; } - (void) retrieveSavedHosts {