Skip to content
This repository has been archived by the owner on Sep 4, 2018. It is now read-only.

Commit

Permalink
Cache package list and load it on Alcatraz startup
Browse files Browse the repository at this point in the history
  • Loading branch information
guillaumealgis committed Sep 27, 2015
1 parent 3902c70 commit 5c43f84
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 2 deletions.
6 changes: 6 additions & 0 deletions Alcatraz.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
8ADC22341A2AD5B800DB7BCA /* ATZPreviewImageButton.m in Sources */ = {isa = PBXBuildFile; fileRef = 8ADC22331A2AD5B800DB7BCA /* ATZPreviewImageButton.m */; };
8AF670C919C2DE8A00E1C168 /* ATZSegmentedControl.m in Sources */ = {isa = PBXBuildFile; fileRef = 8AF670C819C2DE8A00E1C168 /* ATZSegmentedControl.m */; };
F0DF961E1B40416400DF68CC /* ATZSegmentedCell.m in Sources */ = {isa = PBXBuildFile; fileRef = F0DF961D1B40416400DF68CC /* ATZSegmentedCell.m */; };
F0C202121B519CCC001867CF /* ATZCache.m in Sources */ = {isa = PBXBuildFile; fileRef = F0C202111B519CCC001867CF /* ATZCache.m */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand Down Expand Up @@ -146,6 +147,8 @@
8AF670C819C2DE8A00E1C168 /* ATZSegmentedControl.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = ATZSegmentedControl.m; path = Views/ATZSegmentedControl.m; sourceTree = "<group>"; };
F0DF961C1B40416300DF68CC /* ATZSegmentedCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ATZSegmentedCell.h; sourceTree = "<group>"; };
F0DF961D1B40416400DF68CC /* ATZSegmentedCell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ATZSegmentedCell.m; sourceTree = "<group>"; };
F0C202101B519CCC001867CF /* ATZCache.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ATZCache.h; sourceTree = "<group>"; };
F0C202111B519CCC001867CF /* ATZCache.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ATZCache.m; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -302,6 +305,8 @@
8917DA171726B63B00F0B2D2 /* ATZShell.m */,
89B4F2BC172FF5AC001FD2E3 /* ATZPBXProjParser.h */,
89B4F2BD172FF5AC001FD2E3 /* ATZPBXProjParser.m */,
F0C202101B519CCC001867CF /* ATZCache.h */,
F0C202111B519CCC001867CF /* ATZCache.m */,
);
path = Helpers;
sourceTree = "<group>";
Expand Down Expand Up @@ -439,6 +444,7 @@
files = (
890A4B4E171F031300AFE577 /* Alcatraz.m in Sources */,
89D40AFC1721CBD600A67BB3 /* ATZPluginWindowController.m in Sources */,
F0C202121B519CCC001867CF /* ATZCache.m in Sources */,
8917D9F31726B4EE00F0B2D2 /* ATZColorScheme.m in Sources */,
8917D9F41726B4EE00F0B2D2 /* ATZFileTemplate.m in Sources */,
8917D9F51726B4EE00F0B2D2 /* ATZPackage.m in Sources */,
Expand Down
16 changes: 14 additions & 2 deletions Alcatraz/Controllers/ATZPluginWindowController.m
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#import "Alcatraz.h"
#import "ATZPackageFactory.h"
#import "ATZVersion.h"
#import "ATZCache.h"

#import "ATZPlugin.h"
#import "ATZColorScheme.h"
Expand Down Expand Up @@ -132,19 +133,30 @@ - (void)keyDown:(NSEvent *)event {
}

- (IBAction)reloadPackages:(id)sender {
if (!self.packages) {
// No packages loaded yet, display from cache in the meantime
NSDictionary *packageList = [ATZCache loadPackagesFromCache];
[self displayPackages:packageList];
}

ATZDownloader *downloader = [ATZDownloader new];
[downloader downloadPackageListWithCompletion:^(NSDictionary *packageList, NSError *error) {

if (error) {
NSLog(@"Error while downloading packages! %@", error);
} else {
self.packages = [ATZPackageFactory createPackagesFromDicts:packageList];
[self reloadTableView];
[self displayPackages:packageList];
[ATZCache cachePackages:packageList];
[self updatePackages];
}
}];
}

- (void)displayPackages:(NSDictionary *)packageList {
self.packages = [ATZPackageFactory createPackagesFromDicts:packageList];
[self reloadTableView];
}

- (IBAction)updatePackageRepoPath:(id)sender {
// present dialog with text field, update repo path, redownload package list
NSAlert *alert = [NSAlert new];
Expand Down
31 changes: 31 additions & 0 deletions Alcatraz/Helpers/ATZCache.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//
// ATZCache.h
//
// Copyright (c) 2014 Marin Usalj | supermar.in
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

#import <Foundation/Foundation.h>

@interface ATZCache : NSObject

+ (NSDictionary *)loadPackagesFromCache;
+ (BOOL)cachePackages:(NSDictionary *)packages;

@end
76 changes: 76 additions & 0 deletions Alcatraz/Helpers/ATZCache.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
//
// ATZCache.m
//
// Copyright (c) 2014 Marin Usalj | supermar.in
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

#import "ATZCache.h"
#import "Alcatraz.h"

static NSString *const PACKAGES_CACHE_FILENAME = @"packages.plist";

@implementation ATZCache

+ (NSDictionary *)loadPackagesFromCache {
return [NSDictionary dictionaryWithContentsOfURL:[self packagesCacheURL]];
}

+ (BOOL)cachePackages:(NSDictionary *)packages {
NSError *error;

BOOL directoryExists = [self createCacheDirectoryIfNeeded:&error];
if (!directoryExists) {
NSLog(@"Error while caching packages: %@", error.localizedDescription);
return NO;
}

NSData *plistData = [NSPropertyListSerialization dataWithPropertyList:packages format:NSPropertyListBinaryFormat_v1_0 options:0 error:&error];
if (!plistData) {
NSLog(@"Error while serializing packages for caching: %@", error.localizedDescription);
return NO;
}

return [plistData writeToURL:[self packagesCacheURL] atomically:YES];
}

+ (NSURL *)cacheDirectoryURL {
NSURL *mainCacheDirectory = [[[NSFileManager defaultManager] URLsForDirectory:NSCachesDirectory inDomains:NSUserDomainMask] firstObject];
NSString *bundleIdentifier = [Alcatraz sharedPlugin].bundle.bundleIdentifier;
NSURL *cacheDirectory = [mainCacheDirectory URLByAppendingPathComponent:bundleIdentifier];

return cacheDirectory;
}

+ (NSURL *)packagesCacheURL {
return [[self cacheDirectoryURL] URLByAppendingPathComponent:PACKAGES_CACHE_FILENAME];
}

+ (BOOL)createCacheDirectoryIfNeeded:(NSError **)error {
BOOL created = [[NSFileManager defaultManager] createDirectoryAtURL:[self cacheDirectoryURL] withIntermediateDirectories:YES attributes:nil error:error];

if (!created) {
NSString *errorDescription = error ? (*error).localizedDescription : nil;
NSLog(@"Could not find a valid caching directory: %@", errorDescription);
}

return created;
}

@end

0 comments on commit 5c43f84

Please sign in to comment.