Preserve app art when updating the app list

This commit is contained in:
Cameron Gutman
2016-01-21 14:03:07 -05:00
parent ec3748b26e
commit b8a4093c75
6 changed files with 92 additions and 28 deletions

View File

@@ -18,6 +18,8 @@
- (NSArray*) getHosts;
- (void) updateHost:(TemporaryHost*)host;
- (void) updateAppsForExistingHost:(TemporaryHost *)host;
- (void) updateIconForExistingApp:(TemporaryApp*)app;
- (void) removeHost:(TemporaryHost*)host;
- (TemporarySettings*) getSettings;
@@ -25,7 +27,4 @@
- (void) updateUniqueId:(NSString*)uniqueId;
- (NSString*) getUniqueId;
// Don't call this outside of database code
- (App*) getAppForTemporaryApp:(TemporaryApp*)tempApp;
@end

View File

@@ -68,7 +68,49 @@
}
// Push changes from the temp host to the persistent one
[host propagateChangesToParent:parent withDm:self];
[host propagateChangesToParent:parent];
[self saveData];
}];
}
- (void) updateAppsForExistingHost:(TemporaryHost *)host {
[_managedObjectContext performBlockAndWait:^{
Host* parent = [self getHostForTemporaryHost:host];
if (parent == nil) {
// The host must exist to be updated
return;
}
NSMutableSet *applist = [[NSMutableSet alloc] init];
for (TemporaryApp* app in host.appList) {
// Add a new persistent managed object if one doesn't exist
App* parentApp = [self getAppForTemporaryApp:app];
if (parentApp == nil) {
NSEntityDescription* entity = [NSEntityDescription entityForName:@"App" inManagedObjectContext:_managedObjectContext];
parentApp = [[App alloc] initWithEntity:entity insertIntoManagedObjectContext:_managedObjectContext];
}
[app propagateChangesToParent:parentApp withHost:parent];
[applist addObject:parentApp];
}
parent.appList = applist;
[self saveData];
}];
}
- (void) updateIconForExistingApp:(TemporaryApp*)app {
[_managedObjectContext performBlockAndWait:^{
App* parentApp = [self getAppForTemporaryApp:app];
if (parentApp == nil) {
// The app must exist to be updated
return;
}
parentApp.image = app.image;
[self saveData];
}];

View File

@@ -24,7 +24,6 @@
- (void) propagateChangesToParent:(App*)parent withHost:(Host*)host {
parent.id = self.id;
parent.image = self.image;
parent.name = self.name;
parent.host = host;
}

View File

@@ -10,8 +10,6 @@
#import "Utils.h"
#import "Host.h"
@class DataManager;
@interface TemporaryHost : NSObject
@property (nonatomic) BOOL online;
@@ -26,13 +24,13 @@ NS_ASSUME_NONNULL_BEGIN
@property (nonatomic, retain) NSString *mac;
@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSString *uuid;
@property (nonatomic, retain) NSSet *appList;
@property (nonatomic, retain) NSMutableSet *appList;
- (id) initFromHost:(Host*)host;
- (NSComparisonResult)compareName:(TemporaryHost *)other;
- (void) propagateChangesToParent:(Host*)host withDm:(DataManager*)dm;
- (void) propagateChangesToParent:(Host*)host;
NS_ASSUME_NONNULL_END

View File

@@ -37,7 +37,7 @@
return self;
}
- (void) propagateChangesToParent:(Host*)parentHost withDm:(DataManager*)dm {
- (void) propagateChangesToParent:(Host*)parentHost {
parentHost.address = self.address;
parentHost.externalAddress = self.externalAddress;
parentHost.localAddress = self.localAddress;
@@ -45,22 +45,6 @@
parentHost.name = self.name;
parentHost.uuid = self.uuid;
parentHost.pairState = [NSNumber numberWithInt:self.pairState];
NSMutableSet *applist = [[NSMutableSet alloc] init];
for (TemporaryApp* app in self.appList) {
// Add a new persistent managed object if one doesn't exist
App* parentApp = [dm getAppForTemporaryApp:app];
if (parentApp == nil) {
NSEntityDescription* entity = [NSEntityDescription entityForName:@"App" inManagedObjectContext:parentHost.managedObjectContext];
parentApp = [[App alloc] initWithEntity:entity insertIntoManagedObjectContext:parentHost.managedObjectContext];
}
[app propagateChangesToParent:parentApp withHost:parentHost];
[applist addObject:parentApp];
}
parentHost.appList = applist;
}
- (NSComparisonResult)compareName:(TemporaryHost *)other {

View File

@@ -161,9 +161,48 @@ static NSMutableSet* hostList;
- (void) updateApplist:(NSSet*) newList forHost:(TemporaryHost*)host {
DataManager* database = [[DataManager alloc] init];
host.appList = newList;
for (TemporaryApp* app in newList) {
BOOL appAlreadyInList = NO;
for (TemporaryApp* savedApp in host.appList) {
if ([app.id isEqualToString:savedApp.id]) {
savedApp.name = app.name;
savedApp.isRunning = app.isRunning;
appAlreadyInList = YES;
break;
}
}
if (!appAlreadyInList) {
app.host = host;
[host.appList addObject:app];
}
}
BOOL appWasRemoved;
do {
appWasRemoved = NO;
for (TemporaryApp* app in host.appList) {
appWasRemoved = YES;
for (TemporaryApp* mergedApp in newList) {
if ([mergedApp.id isEqualToString:app.id]) {
appWasRemoved = NO;
break;
}
}
if (appWasRemoved) {
// Removing the app mutates the list we're iterating (which isn't legal).
// We need to jump out of this loop and restart enumeration.
[host.appList removeObject:app];
break;
}
}
// Keep looping until the list is no longer being mutated
} while (appWasRemoved);
[database updateHost:host];
[database updateAppsForExistingHost:host];
}
- (void)showHostSelectionView {
@@ -179,6 +218,9 @@ static NSMutableSet* hostList;
// on the main thread
[self updateBoxArtCacheForApp:app];
DataManager* dataManager = [[DataManager alloc] init];
[dataManager updateIconForExistingApp: app];
dispatch_async(dispatch_get_main_queue(), ^{
[self.collectionView reloadData];
});