Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge #69 #70

Merged
merged 10 commits into from
Jan 14, 2018
118 changes: 76 additions & 42 deletions Dash/Base.lproj/Main.storyboard

Large diffs are not rendered by default.

4 changes: 1 addition & 3 deletions Dash/DHDocsetBrowser.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,10 @@

@property (assign) BOOL didFirstReload;
@property (strong) DHDBSearchController *searchController;
@property (strong) NSMutableArray *shownDocsets;
@property (strong) NSArray *sections;
@property (strong, readonly) NSArray<DHDocset *> *shownDocsets;
@property (assign) BOOL didLoad;
@property (assign) BOOL isSearching;
@property (assign) BOOL needsToReloadWhenDoneSearching;
@property (strong) NSMutableArray *keyDocsets;

- (IBAction)openSettings:(id)sender;
+ (NSAttributedString *)titleBarItemAttributedStringTemplate;
Expand Down
47 changes: 24 additions & 23 deletions Dash/DHDocsetBrowser.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,36 @@
#import "DHDocsetDownloader.h"
#import "DHRemoteBrowser.h"
#import "DHWebView.h"
#import "DHDocsetBrowserViewModel.h"

static NSAttributedString *_titleBarItemAttributedStringTemplate = nil;

@interface DHDocsetBrowser ()
@property (nonatomic, strong) DHDocsetBrowserViewModel *viewModel;
@end

@implementation DHDocsetBrowser

- (NSArray<DHDocset *> *)shownDocsets {
return self.viewModel.shownDocsets;
}

- (NSArray *)sections {
return self.viewModel.sections;
}

- (void)viewDidLoad
{
[super viewDidLoad];
self.viewModel = [[DHDocsetBrowserViewModel alloc] init];
self.clearsSelectionOnViewWillAppear = NO;
self.searchController = [DHDBSearchController searchControllerWithDocsets:nil typeLimit:nil viewController:self];

[self.tableView registerNib:[UINib nibWithNibName:@"DHBrowserCell" bundle:nil] forCellReuseIdentifier:@"DHBrowserCell"];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reload:) name:DHDocsetsChangedNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reload:) name:DHRemotesChangedNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reload:) name:DHSettingsChangedNotification object:nil];
self.tableView.rowHeight = 44;
self.navigationController.delegate = self;
self.navigationController.interactivePopGestureRecognizer.delegate = (id<UIGestureRecognizerDelegate>)self;
Expand Down Expand Up @@ -179,7 +194,7 @@ - (void)performURLSearch:(NSNotification *)notification
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.00 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
if(keywordDocsets.count)
{
self.keyDocsets = [NSMutableArray arrayWithArray:[keywordDocsets array]];
self.viewModel.keyDocsets = [NSMutableArray arrayWithArray:[keywordDocsets array]];
}
[self.searchDisplayController setActive:YES animated:NO];
self.searchDisplayController.searchBar.text = query;
Expand Down Expand Up @@ -264,21 +279,7 @@ - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

- (void)updateSections:(BOOL)withTitleUpdate
{
NSMutableArray *sections = [NSMutableArray array];
if([DHRemoteServer sharedServer].remotes.count)
{
if(!self.isEditing && !self.isSearching)
{
[sections addObject:[DHRemoteServer sharedServer].remotes];
}
}
NSMutableArray *docsets = (self.isEditing) ? [DHDocsetManager sharedManager].docsets : (self.keyDocsets) ? self.keyDocsets : [DHDocsetManager sharedManager].enabledDocsets;
self.shownDocsets = docsets;
if(docsets.count)
{
[sections addObject:docsets];
}
self.sections = sections;
[self.viewModel updateSectionsForEditing:self.isEditing andSearching:self.isSearching];
if(withTitleUpdate)
{
[self updateTitle];
Expand Down Expand Up @@ -335,7 +336,7 @@ - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fro

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
return self.viewModel.canMoveRows;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
Expand Down Expand Up @@ -386,8 +387,8 @@ - (void)tableViewDidBeginEditing:(UITableView *)tableView
}

// NSArray *current = self.shownDocsets;
NSMutableArray *new = [DHDocsetManager sharedManager].docsets;
self.shownDocsets = new;
NSArray *new = [self.viewModel docsetsForEditing:YES];
self.viewModel.shownDocsets = new;
NSMutableArray *toInsert = [NSMutableArray array];
for(NSInteger i = 0; i < new.count; i++)
{
Expand Down Expand Up @@ -419,7 +420,7 @@ - (void)tableViewDidBeginEditing:(UITableView *)tableView
- (void)tableViewDidEndEditing:(UITableView *)tableView
{
NSArray *current = self.shownDocsets;
self.shownDocsets = [DHDocsetManager sharedManager].enabledDocsets;
self.viewModel.shownDocsets = [self.viewModel docsetsForEditing:NO];
NSMutableArray *toDelete = [NSMutableArray array];
BOOL docsetsShouldBeShown = NO;
for(int i = 0; i < current.count; i++)
Expand Down Expand Up @@ -455,7 +456,7 @@ - (void)tableViewDidEndEditing:(UITableView *)tableView

- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller
{
if(self.keyDocsets)
if(self.viewModel.keyDocsets)
{
[self reload:nil];
}
Expand All @@ -478,9 +479,9 @@ - (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)cont
- (void)searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller
{
self.isSearching = NO;
if(self.needsToReloadWhenDoneSearching || self.keyDocsets)
if(self.needsToReloadWhenDoneSearching || self.viewModel.keyDocsets)
{
self.keyDocsets = nil;
self.viewModel.keyDocsets = nil;
self.needsToReloadWhenDoneSearching = NO;
[self reload:nil];
}
Expand Down
11 changes: 11 additions & 0 deletions Dash/DHDocsetBrowserViewModel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

#import <Foundation/Foundation.h>

@interface DHDocsetBrowserViewModel : NSObject
@property (nonatomic, strong, readonly) NSArray *sections;
@property (nonatomic, strong) NSMutableArray *keyDocsets;
@property (nonatomic, strong) NSArray<DHDocset *> *shownDocsets;
@property (nonatomic, readonly) BOOL canMoveRows;
- (void)updateSectionsForEditing:(BOOL)editing andSearching:(BOOL)searching;
- (NSArray<DHDocset *> *)docsetsForEditing:(BOOL)editing;
@end
52 changes: 52 additions & 0 deletions Dash/DHDocsetBrowserViewModel.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@

#import "DHDocsetBrowserViewModel.h"
#import "DHDocsetManager.h"
#import "DHDocsetDownloader.h"

@interface DHDocsetBrowserViewModel ()
@property (nonatomic, strong) NSArray *sections;
@end

@implementation DHDocsetBrowserViewModel

- (BOOL)alphabetizing {
return [NSUserDefaults.standardUserDefaults boolForKey:DHDocsetDownloader.defaultsAlphabetizingKey];
}

- (BOOL)canMoveRows {
return !self.alphabetizing;
}

- (NSArray *)sort:(NSArray *)array {
if (self.alphabetizing) {
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];
array = [array sortedArrayUsingDescriptors:@[sortDescriptor]];
}
return array;
}

- (NSArray<DHDocset *> *)docsetsForEditing:(BOOL)editing {
NSArray<DHDocset *> *docsets = (editing) ? [DHDocsetManager sharedManager].docsets : (self.keyDocsets) ? self.keyDocsets : [DHDocsetManager sharedManager].enabledDocsets;
return [self sort:docsets];
}

- (void)updateSectionsForEditing:(BOOL)editing andSearching:(BOOL)searching
{
NSMutableArray *sections = [NSMutableArray array];
if([DHRemoteServer sharedServer].remotes.count)
{
if(!editing && !searching)
{
[sections addObject:[self sort:DHRemoteServer.sharedServer.remotes]];
}
}
NSArray<DHDocset *> *docsets = [self docsetsForEditing:editing];
self.shownDocsets = docsets;
if(docsets.count)
{
[sections addObject:docsets];
}
self.sections = sections;
}

@end
3 changes: 3 additions & 0 deletions Dash/DHPreferences.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
@interface DHPreferences : UITableViewController

@property (assign) IBOutlet UISwitch *updatesSwitch;
@property (assign) IBOutlet UISwitch *alphabetizingSwitch;
@property (assign) IBOutlet UITableViewCell *updatesCell;
@property (assign) IBOutlet UITableViewCell *alphabetizingCell;
@property (assign) BOOL didSetUpdateLabelBefore;

- (IBAction)updatesSwitchValueChanged:(id)sender;
Expand Down
36 changes: 31 additions & 5 deletions Dash/DHPreferences.m
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ - (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.updatesSwitch setOn:[[NSUserDefaults standardUserDefaults] boolForKey:[[DHDocsetDownloader sharedDownloader] defaultsAutomaticallyCheckForUpdatesKey]]];
[self.alphabetizingSwitch setOn:[NSUserDefaults.standardUserDefaults boolForKey:DHDocsetDownloader.defaultsAlphabetizingKey]];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self updateAlphabetizingSwitchFooterView:nil];
[self updateUpdatesSwitchFooterView:nil];
});
}

- (void)viewDidAppear:(BOOL)animated
Expand Down Expand Up @@ -238,16 +243,37 @@ - (IBAction)updatesSwitchValueChanged:(id)sender
[self updateUpdatesSwitchFooterView:nil];
}

- (void)tableView:(UITableView *)tableView willDisplayFooterView:(UITableViewHeaderFooterView *)view forSection:(NSInteger)section
- (IBAction)alphabetizingSwitchValueChanged:(id)sender
{
[self updateUpdatesSwitchFooterView:view];
[[NSUserDefaults standardUserDefaults] setBool:[sender isOn] forKey:DHDocsetDownloader.defaultsAlphabetizingKey];
[NSNotificationCenter.defaultCenter postNotificationName:DHSettingsChangedNotification object:DHDocsetDownloader.defaultsAlphabetizingKey];
[self updateAlphabetizingSwitchFooterView:nil];
}

- (void)tableView:(UITableView *)tableView willDisplayFooterView:(UITableViewHeaderFooterView *)footer forSection:(NSInteger)section
{
if(section == [tableView indexPathForCell:self.updatesCell].section)
{
[self updateUpdatesSwitchFooterView:footer];
}
else if(section == [tableView indexPathForCell:self.alphabetizingCell].section)
{
[self updateAlphabetizingSwitchFooterView:footer];
}
}

- (void)updateUpdatesSwitchFooterView:(UITableViewHeaderFooterView *)footer
{
footer = (footer) ?: [self.tableView footerViewForSection:1];
[footer textLabel].text = [NSString stringWithFormat:@"Dash %@ notify you when docset updates are available. ", (self.updatesSwitch.isOn) ? @"will" : @"won't"];

footer = (footer) ? footer : [self.tableView footerViewForSection:[self.tableView indexPathForCell:self.updatesCell].section];
[footer textLabel].text = [NSString stringWithFormat:@"Dash %@ notify you when docset updates are available.", (self.updatesSwitch.isOn) ? @"will" : @"won't"];
[[footer textLabel] sizeToFit];
}

- (void)updateAlphabetizingSwitchFooterView:(UITableViewHeaderFooterView *)footer
{
footer = (footer) ? footer : [self.tableView footerViewForSection:[self.tableView indexPathForCell:self.alphabetizingCell].section];
[footer textLabel].text = [NSString stringWithFormat:@"Docsets %@ be sorted alphabetically in the docset browser.", (self.alphabetizingSwitch.isOn) ? @"will" : @"won't"];
[[footer textLabel] sizeToFit];
}

- (void)prepareForURLSearch:(id)sender
Expand Down
3 changes: 3 additions & 0 deletions Dash/DHRepo.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
- (void)backgroundCheckForUpdatesIfNeeded;
- (NSString *)repoIdentifier; // Used to find a corresponding repo for a installed docset
- (NSString *)defaultsAutomaticallyCheckForUpdatesKey;
@property (nonatomic, strong, readonly, class) NSString *defaultsAlphabetizingKey;
- (void)emptyTrashAtPath:(NSString *)trashPath;
- (NSString *)docsetInstallFolderPath;
- (NSString *)uniqueTrashPath;
Expand All @@ -67,3 +68,5 @@
@end

NSInteger compareFeeds(id feed1, id feed2, void *context);

#define DHSettingsChangedNotification @"DHSettingsChangedNotification"
4 changes: 4 additions & 0 deletions Dash/DHRepo.m
Original file line number Diff line number Diff line change
Expand Up @@ -864,6 +864,10 @@ - (NSString *)defaultsAutomaticallyCheckForUpdatesKey
return @"AutomaticallyCheckForUpdates";
}

+ (NSString *)defaultsAlphabetizingKey {
return @"DocSetAlphabetizing";
}

- (NSString *)defaultsUpdateLastCheckDateKey
{
return [[self defaultsKey] stringByAppendingString:@"LastUpdateCheck"];
Expand Down
8 changes: 8 additions & 0 deletions Dash/Dash iOS.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@
43EE932A19CD079600FFF30C /* libarchive.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 43EE932919CD079600FFF30C /* libarchive.a */; };
43EED91C1B607DB800F3DC35 /* on_page_load.js in Resources */ = {isa = PBXBuildFile; fileRef = 43EED91B1B607DB800F3DC35 /* on_page_load.js */; };
43F25F9C19EBDEF30025CDC4 /* DHBlockProtocol.m in Sources */ = {isa = PBXBuildFile; fileRef = 43F25F9B19EBDEF30025CDC4 /* DHBlockProtocol.m */; };
43F31AAE200BB52F0082066C /* DHDocsetBrowserViewModel.m in Sources */ = {isa = PBXBuildFile; fileRef = F5C39BFF200A836D00734571 /* DHDocsetBrowserViewModel.m */; };
43F70AC019CBBD20000849C8 /* Defaults.plist in Resources */ = {isa = PBXBuildFile; fileRef = 43F70ABF19CBBD20000849C8 /* Defaults.plist */; };
43F7527319CD1C2000D76F58 /* UIButton+DHUtils.m in Sources */ = {isa = PBXBuildFile; fileRef = 43F7527219CD1C2000D76F58 /* UIButton+DHUtils.m */; };
43F7527519CD31D100D76F58 /* DHRepoCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = 43F7527419CD31D100D76F58 /* DHRepoCell.xib */; };
Expand All @@ -249,6 +250,7 @@
43FD2F0219BB8D87007E594D /* DHWebViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 43FD2F0119BB8D87007E594D /* DHWebViewController.m */; };
43FD2F0419BB8D87007E594D /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 43FD2F0319BB8D87007E594D /* Images.xcassets */; };
45D28F8ABD1FC392B1930467 /* libPods-Dash.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 9A2364AD2032B0714BCD2265 /* libPods-Dash.a */; };
F5C39C00200A836D00734571 /* DHDocsetBrowserViewModel.m in Sources */ = {isa = PBXBuildFile; fileRef = F5C39BFF200A836D00734571 /* DHDocsetBrowserViewModel.m */; };
/* End PBXBuildFile section */

/* Begin PBXCopyFilesBuildPhase section */
Expand Down Expand Up @@ -486,6 +488,8 @@
9A2364AD2032B0714BCD2265 /* libPods-Dash.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-Dash.a"; sourceTree = BUILT_PRODUCTS_DIR; };
9FB91D369AD27465BED375AD /* libPods-Dash-Dash App Store.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-Dash-Dash App Store.a"; sourceTree = BUILT_PRODUCTS_DIR; };
BAE47FF5DEE33300EBFF9AEA /* Pods-Dash.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Dash.debug.xcconfig"; path = "../Pods/Target Support Files/Pods-Dash/Pods-Dash.debug.xcconfig"; sourceTree = "<group>"; };
F5C39BFE200A836D00734571 /* DHDocsetBrowserViewModel.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = DHDocsetBrowserViewModel.h; sourceTree = "<group>"; };
F5C39BFF200A836D00734571 /* DHDocsetBrowserViewModel.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = DHDocsetBrowserViewModel.m; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -664,6 +668,8 @@
438D58C119E575D600012881 /* DHLoadingCell.xib */,
43FD2EFD19BB8D87007E594D /* DHDocsetBrowser.h */,
43FD2EFE19BB8D87007E594D /* DHDocsetBrowser.m */,
F5C39BFE200A836D00734571 /* DHDocsetBrowserViewModel.h */,
F5C39BFF200A836D00734571 /* DHDocsetBrowserViewModel.m */,
437FE06419E2AC0D0022ADF4 /* DHTypeBrowser.h */,
437FE06519E2AC0D0022ADF4 /* DHTypeBrowser.m */,
439051F719E684160083A55E /* DHEntryBrowser.h */,
Expand Down Expand Up @@ -1368,6 +1374,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
43F31AAE200BB52F0082066C /* DHDocsetBrowserViewModel.m in Sources */,
43AB65341ED19D2F00A6EC18 /* DHAppDelegate.m in Sources */,
43AB65351ED19D2F00A6EC18 /* DHType.m in Sources */,
43AB65361ED19D2F00A6EC18 /* UIViewController+DHUtils.m in Sources */,
Expand Down Expand Up @@ -1473,6 +1480,7 @@
43F25F9C19EBDEF30025CDC4 /* DHBlockProtocol.m in Sources */,
43B0956C19BFD57500F8611B /* DHRepo.m in Sources */,
43D7E4F01BB4C62E00FC1DEF /* DHWindow.m in Sources */,
F5C39C00200A836D00734571 /* DHDocsetBrowserViewModel.m in Sources */,
43BD6BF41DCE379B0005A1E7 /* DHAppUpdateChecker.m in Sources */,
439051FC19E68FE20083A55E /* NSString+GTM.m in Sources */,
43D4F06719E7D68100C22AF5 /* DHDBSearcher.m in Sources */,
Expand Down