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

Cache package list #325

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Alcatraz.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
8AF670C919C2DE8A00E1C168 /* ATZSegmentedControl.m in Sources */ = {isa = PBXBuildFile; fileRef = 8AF670C819C2DE8A00E1C168 /* ATZSegmentedControl.m */; };
F0DF961E1B40416400DF68CC /* ATZSegmentedCell.m in Sources */ = {isa = PBXBuildFile; fileRef = F0DF961D1B40416400DF68CC /* ATZSegmentedCell.m */; };
F07E55521B551BA800161E61 /* ATZXcodePrefsManager.m in Sources */ = {isa = PBXBuildFile; fileRef = F07E55511B551BA800161E61 /* ATZXcodePrefsManager.m */; };
F0C202121B519CCC001867CF /* ATZCache.m in Sources */ = {isa = PBXBuildFile; fileRef = F0C202111B519CCC001867CF /* ATZCache.m */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand Down Expand Up @@ -148,6 +149,8 @@
F0DF961D1B40416400DF68CC /* ATZSegmentedCell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ATZSegmentedCell.m; sourceTree = "<group>"; };
F07E55501B551BA800161E61 /* ATZXcodePrefsManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ATZXcodePrefsManager.h; sourceTree = "<group>"; };
F07E55511B551BA800161E61 /* ATZXcodePrefsManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ATZXcodePrefsManager.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 @@ -305,6 +308,8 @@
89B4F2BD172FF5AC001FD2E3 /* ATZPBXProjParser.m */,
F07E55501B551BA800161E61 /* ATZXcodePrefsManager.h */,
F07E55511B551BA800161E61 /* ATZXcodePrefsManager.m */,
F0C202101B519CCC001867CF /* ATZCache.h */,
F0C202111B519CCC001867CF /* ATZCache.m */,
);
path = Helpers;
sourceTree = "<group>";
Expand Down Expand Up @@ -442,6 +447,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
2 changes: 2 additions & 0 deletions Alcatraz/Controllers/ATZPackageTableViewDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@

- (instancetype)initWithPackages:(NSArray*)packages tableViewOwner:(id)owner;

- (void)updatePackages:(NSArray*)packages;

- (void)configureTableView:(NSTableView*)tableView;
- (void)filterUsingPredicate:(NSPredicate*)predicate;

Expand Down
4 changes: 4 additions & 0 deletions Alcatraz/Controllers/ATZPackageTableViewDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ - (instancetype)initWithPackages:(NSArray*)packages tableViewOwner:(id)owner {
return self;
}

- (void)updatePackages:(NSArray*)packages {
_packages = packages;
}

- (void)configureTableView:(NSTableView *)tableView {
NSNib* nib = [[NSNib alloc] initWithNibNamed:NSStringFromClass([ATZPackageListTableCellView class]) bundle:[Alcatraz sharedPlugin].bundle];
[tableView registerNib:nib forIdentifier:packageCellIdentifier];
Expand Down
33 changes: 26 additions & 7 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 "ATZStyleKit.h"
#import "ATZCache.h"

#import "ATZPlugin.h"
#import "ATZColorScheme.h"
Expand Down Expand Up @@ -141,19 +142,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 All @@ -176,12 +188,19 @@ - (IBAction)resetPackageRepoPath:(id)sender {
}

- (void)reloadTableView {
self.tableViewDelegate = [[ATZPackageTableViewDelegate alloc] initWithPackages:self.packages tableViewOwner:self];
self.tableView.delegate = self.tableViewDelegate;
self.tableView.dataSource = self.tableViewDelegate;
[self.tableViewDelegate configureTableView:self.tableView];
if (!self.tableViewDelegate) {
self.tableViewDelegate = [[ATZPackageTableViewDelegate alloc] initWithPackages:self.packages
tableViewOwner:self];
self.tableView.delegate = self.tableViewDelegate;
self.tableView.dataSource = self.tableViewDelegate;

[self.tableViewDelegate configureTableView:self.tableView];
}
else {
[self.tableViewDelegate updatePackages:self.packages];
}

[self updatePredicate];
[self.tableView reloadData];
}

#pragma mark - Private
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