From 6c32910c0cbed34c831ce90a94c66baa45d9286d Mon Sep 17 00:00:00 2001 From: nate-parrott Date: Tue, 11 Nov 2014 18:27:18 -0500 Subject: [PATCH] fix some potential bugs --- FlashlightApp/EasySIMBL/PluginInstallTask.m | 2 +- .../EasySIMBL/PluginListController.m | 29 ++++++++----------- FlashlightApp/EasySIMBL/Updater.m | 16 ++++++---- 3 files changed, 23 insertions(+), 24 deletions(-) diff --git a/FlashlightApp/EasySIMBL/PluginInstallTask.m b/FlashlightApp/EasySIMBL/PluginInstallTask.m index 27842d4f..2d5001d3 100644 --- a/FlashlightApp/EasySIMBL/PluginInstallTask.m +++ b/FlashlightApp/EasySIMBL/PluginInstallTask.m @@ -20,7 +20,7 @@ - (id)initWithPlugin:(PluginModel *)plugin { } - (void)startInstallationIntoPluginsDirectory:(NSString *)directory withCallback:(void(^)(BOOL success, NSError *error))callback { [[[NSURLSession sharedSession] dataTaskWithURL:self.plugin.zipURL completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { - if (data) { + if (data && !error) { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{ NSError *zipError = nil; ZZArchive *archive = [ZZArchive archiveWithData:data error:&zipError]; diff --git a/FlashlightApp/EasySIMBL/PluginListController.m b/FlashlightApp/EasySIMBL/PluginListController.m index fbf15111..2ec864c0 100644 --- a/FlashlightApp/EasySIMBL/PluginListController.m +++ b/FlashlightApp/EasySIMBL/PluginListController.m @@ -116,10 +116,12 @@ - (IBAction)reloadPluginsFromWeb:(id)sender { NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://raw.githubusercontent.com/nate-parrott/flashlight/master/PluginDirectories/1/index.json"]]; [[[NSURLSession sharedSession] dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { - NSDictionary *d = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; NSMutableArray *plugins = [NSMutableArray new]; - for (NSDictionary *dict in d[@"plugins"]) { - [plugins addObject:[PluginModel fromJson:dict baseURL:url]]; + if (data) { + NSDictionary *d = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; + for (NSDictionary *dict in d[@"plugins"]) { + [plugins addObject:[PluginModel fromJson:dict baseURL:url]]; + } } dispatch_async(dispatch_get_main_queue(), ^{ [self setPluginsFromWeb:plugins]; @@ -205,10 +207,13 @@ - (void)reloadFromDisk { NSMutableArray *models = [NSMutableArray new]; for (NSString *itemName in contents) { if ([[itemName pathExtension] isEqualToString:@"bundle"]) { - NSDictionary *json = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:[[[self localPluginsPath] stringByAppendingPathComponent:itemName] stringByAppendingPathComponent:@"info.json"]] options:0 error:nil]; - PluginModel *model = [PluginModel fromJson:json baseURL:nil]; - model.installed = YES; - [models addObject:model]; + NSData *data = [NSData dataWithContentsOfFile:[[[self localPluginsPath] stringByAppendingPathComponent:itemName] stringByAppendingPathComponent:@"info.json"]]; + if (data) { + NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; + PluginModel *model = [PluginModel fromJson:json baseURL:nil]; + model.installed = YES; + [models addObject:model]; + } } } self.installedPlugins = models; @@ -234,16 +239,6 @@ - (void)installPlugin:(PluginModel *)plugin { self.installTasksInProgress = self.installTasksInProgress ? [self.installTasksInProgress setByAddingObject:task] : [NSSet setWithObject:task]; [task startInstallationIntoPluginsDirectory:[self localPluginsPath] withCallback:^(BOOL success, NSError *error) { dispatch_async(dispatch_get_main_queue(), ^{ - /*if (!success) { - NSAlert *alert = [NSAlert new]; - NSMutableString *errorMsg = [NSMutableString stringWithFormat:@"Couldn't install \"%@\".", plugin.displayName]; - if (error) { - [errorMsg appendFormat:@"\n(%@)", error.localizedFailureReason]; - } - alert.messageText = errorMsg; - [alert addButtonWithTitle:@"Okay"]; - [alert runModal]; - }*/ if (!success) { NSAlert *alert = error ? [NSAlert alertWithError:error] : [NSAlert alertWithMessageText:@"Couldn't install plugin." defaultButton:@"Okay" alternateButton:nil otherButton:nil informativeTextWithFormat:nil]; alert.alertStyle = NSWarningAlertStyle; diff --git a/FlashlightApp/EasySIMBL/Updater.m b/FlashlightApp/EasySIMBL/Updater.m index 95cf0a82..1328a173 100644 --- a/FlashlightApp/EasySIMBL/Updater.m +++ b/FlashlightApp/EasySIMBL/Updater.m @@ -21,14 +21,18 @@ - (void)checkForUpdates:(void(^)())callback { NSURL *updateURL = [NSURL URLWithString:@"https://raw.githubusercontent.com/nate-parrott/flashlight/master/UpdateInfo.json"]; double currentBuild = [[[NSBundle mainBundle] infoDictionary][@"CFBundleVersion"] doubleValue]; [[[NSURLSession sharedSession] dataTaskWithURL:updateURL completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { - self.updateJson = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; - if ([self.updateJson[@"build"] doubleValue] > currentBuild) { - self.updatedVersionName = self.updateJson[@"name"]; - self.updateURL = self.updateJson[@"updateURL"]; - } else { + dispatch_async(dispatch_get_main_queue(), ^{ self.updatedVersionName = nil; self.updateURL = nil; - } + if (data) { + self.updateJson = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; + if ([self.updateJson[@"build"] doubleValue] > currentBuild) { + self.updatedVersionName = self.updateJson[@"name"]; + self.updateURL = self.updateJson[@"updateURL"]; + } + } + callback(); + }); }] resume]; }