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

enable state restoration #18

Merged
merged 5 commits into from
Nov 25, 2016
Merged
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
126 changes: 64 additions & 62 deletions Dash/Base.lproj/Main.storyboard

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions Dash/DHAppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,16 @@ - (void)application:(UIApplication *)application handleEventsForBackgroundURLSes
}];
}

#pragma mark - UIStateRestoration

- (BOOL)application:(UIApplication *)application shouldSaveApplicationState:(NSCoder *)coder {
return YES;
}

- (BOOL)application:(UIApplication *)application shouldRestoreApplicationState:(NSCoder *)coder {
return YES;
}

- (void)setDoNotBackUp
{
NSString *path = [homePath stringByAppendingPathComponent:@"Docsets"];
Expand Down
23 changes: 22 additions & 1 deletion Dash/DHEntryBrowser.m
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ - (void)viewDidLoad
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
if(!self.didLoad)
if(!self.didLoad && self.docset)
{
self.didLoad = YES;
self.tableView.allowsSelection = NO;
Expand Down Expand Up @@ -211,6 +211,11 @@ - (void)encodeRestorableStateWithCoder:(NSCoder *)coder
[coder encodeObject:self.type forKey:@"type"];
[coder encodeObject:self.title forKey:@"title"];
[coder encodeObject:self.entries forKey:@"entries"];
NSIndexPath *selectedIndexPath = [self.tableView indexPathForSelectedRow];
if(selectedIndexPath != nil)
{
[coder encodeObject:selectedIndexPath forKey:@"selectedIndexPath"];
}
[self.searchController encodeRestorableStateWithCoder:coder];
[super encodeRestorableStateWithCoder:coder];
}
Expand All @@ -225,6 +230,22 @@ - (void)decodeRestorableStateWithCoder:(NSCoder *)coder
[self viewDidLoad];
self.isRestoring = NO;
self.entries = [coder decodeObjectForKey:@"entries"];
for (int i = 0; i < self.entries.count; i++) {
if ([self.entries[i] isKindOfClass:[DHDBResult class]]) {
DHDBResult *result = (DHDBResult *)self.entries[i];
NSArray *components = [result.fullPath componentsSeparatedByString:@"/Library/Docsets"];
if (components.count == 2) {
result.fullPath = [@"file://" stringByAppendingFormat:@"%@%@%@",homePath,@"/Docsets",components[1]];
}
}
}
NSIndexPath *selectedIndexPath = [coder decodeObjectForKey:@"selectedIndexPath"];
if(selectedIndexPath != nil)
{
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView selectRowAtIndexPath:selectedIndexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
});
}
[self.searchController decodeRestorableStateWithCoder:coder];
[super decodeRestorableStateWithCoder:coder];
}
Expand Down
14 changes: 13 additions & 1 deletion Dash/DHNestedViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -186,15 +186,27 @@ - (void)encodeRestorableStateWithCoder:(NSCoder *)coder
[super encodeRestorableStateWithCoder:coder];
[coder encodeObject:self.result forKey:@"result"];
[coder encodeBool:self.hasMultipleDocsets forKey:@"hasMultipleDocsets"];
NSIndexPath *selectedIndexPath = [self.tableView indexPathForSelectedRow];
if(selectedIndexPath != nil)
{
[coder encodeObject:selectedIndexPath forKey:@"selectedIndexPath"];
}
}

- (void)decodeRestorableStateWithCoder:(NSCoder *)coder
{
[super decodeRestorableStateWithCoder:coder];
self.didDecode = YES;
self.result = [coder decodeObjectForKey:@"result"];
self.hasMultipleDocsets = [coder decodeBoolForKey:@"hasMultipleDocsets"];
self.title = self.result.name;
[super decodeRestorableStateWithCoder:coder];
NSIndexPath *selectedIndexPath = [coder decodeObjectForKey:@"selectedIndexPath"];
if(selectedIndexPath != nil)
{
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView selectRowAtIndexPath:selectedIndexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
});
}
}

@end
12 changes: 12 additions & 0 deletions Dash/DHPreferences.m
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ - (IBAction)dismissModal:(id)sender
}
if(toPopTo)
{
[(DHWebViewController *)toPopTo view].frame = rightNavController.view.bounds;
[rightNavController popToViewController:toPopTo animated:YES];
}
else
Expand Down Expand Up @@ -242,6 +243,17 @@ - (void)decodeRestorableStateWithCoder:(NSCoder *)coder
{
[self.tableView selectRowAtIndexPath:selectedIndexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
}
else
selectedIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
if (self.splitViewController.viewControllers.count == 2 && [selectedIndexPath compare:[NSIndexPath indexPathForRow:0 inSection:0]] == NSOrderedSame) {
UINavigationController *nav = [self.splitViewController.viewControllers lastObject];
if ([nav isKindOfClass:[UINavigationController class]] && ![nav.topViewController isKindOfClass:[DHDocsetDownloader class]]) {
NSMutableArray *newViewControllers = [NSMutableArray array];
[newViewControllers addObjectsFromArray:nav.viewControllers];
[newViewControllers addObject:[DHDocsetDownloader sharedDownloader]];
[nav setViewControllers:newViewControllers];
}
}
}

- (IBAction)getDashForMacOS:(id)sender
Expand Down
27 changes: 27 additions & 0 deletions Dash/DHRemoteBrowser.m
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,33 @@ - (DHNestedViewController *)nestedViewController
return nil;
}

- (void)encodeRestorableStateWithCoder:(NSCoder *)coder
{
[super encodeRestorableStateWithCoder:coder];
[coder encodeObject:self.remote forKey:@"remote"];
[coder encodeObject:self.results forKey:@"results"];
NSIndexPath *selectedIndexPath = [self.tableView indexPathForSelectedRow];
if(selectedIndexPath != nil)
{
[coder encodeObject:selectedIndexPath forKey:@"selectedIndexPath"];
}
}

- (void)decodeRestorableStateWithCoder:(NSCoder *)coder
{
[super decodeRestorableStateWithCoder:coder];
self.remote = [coder decodeObjectForKey:@"remote"];
[self.remote connect];
self.results = [coder decodeObjectForKey:@"results"];
NSIndexPath *selectedIndexPath = [coder decodeObjectForKey:@"selectedIndexPath"];
if(selectedIndexPath != nil)
{
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView selectRowAtIndexPath:selectedIndexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
});
}
}

- (void)dealloc
{

Expand Down
8 changes: 8 additions & 0 deletions Dash/DHRepo.m
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,8 @@ - (BOOL)canInstallFeed:(DHFeed *)feed

- (void)viewDidLoad
{
if(!self.navigationController)
return;
[super viewDidLoad];
[self.tableView registerNib:[UINib nibWithNibName:@"DHRepoCell" bundle:nil] forCellReuseIdentifier:@"DHRepoCell"];
}
Expand Down Expand Up @@ -888,6 +890,12 @@ - (NSInteger)indexOfFeedWithFeedURL:(NSString *)feedURL
return NSNotFound;
}

- (void)decodeRestorableStateWithCoder:(NSCoder *)coder
{
[super decodeRestorableStateWithCoder:coder];
[self viewDidLoad];
}

@end

NSInteger compareFeeds(id feed1, id feed2, void *context)
Expand Down
3 changes: 3 additions & 0 deletions Dash/DHSplitViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ - (void)viewDidLoad
{
[super viewDidLoad];
self.delegate = (id)self;
self.preferredPrimaryColumnWidthFraction = (iPad) ? 0.39 : 0.35;
self.maximumPrimaryColumnWidth = 320;

}

- (BOOL)splitViewController:(UISplitViewController *)splitViewController collapseSecondaryViewController:(UINavigationController *)secondaryViewController ontoPrimaryViewController:(UIViewController *)primaryViewController
Expand Down
2 changes: 1 addition & 1 deletion Dash/DHTypeBrowser.m
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ - (void)viewDidLoad
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
if(!self.didLoad)
if(!self.didLoad && self.docset)
{
self.didLoad = YES;
if(isRegularHorizontalClass && self.isActive)
Expand Down
4 changes: 3 additions & 1 deletion Dash/DHWebViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@
@property (assign) BOOL didLoadInitialRequest;
@property (assign) BOOL didLoadOnce;
@property (strong) NSString *urlToLoad;
@property (assign) BOOL isRestoreScroll;
@property (assign) CGPoint webViewOffset;

+ (instancetype)sharedWebViewController;
- (void)loadURL:(NSString *)urlString;
Expand All @@ -66,4 +68,4 @@
- (IBAction)tocButtonPressed:(id)sender;
- (void)reload;

@end
@end
23 changes: 20 additions & 3 deletions Dash/DHWebViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@ - (void)viewDidLoad
self.progressView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;

[self.splitViewController setPreferredDisplayMode:UISplitViewControllerDisplayModeAllVisible];
self.splitViewController.preferredPrimaryColumnWidthFraction = (iPad) ? 0.39 : 0.35;
self.splitViewController.maximumPrimaryColumnWidth = 320;
self.splitViewController.presentsWithGesture = NO;

if(isRegularHorizontalClass)
Expand Down Expand Up @@ -297,6 +295,10 @@ - (void)webViewDidFinishLoad:(UIWebView *)webView
[self setUpScripts];
[self setUpTOC];
[self.progressView setProgress:1.0 animated:YES];
if (self.isRestoreScroll) {
[self.webView.scrollView setContentOffset:self.webViewOffset animated:NO];
self.isRestoreScroll = NO;
}
}

- (void)setUpTOC
Expand Down Expand Up @@ -738,15 +740,30 @@ - (BOOL)dismissMethodsPopoverIfVisible:(BOOL)animated
- (void)encodeRestorableStateWithCoder:(NSCoder *)coder
{
[super encodeRestorableStateWithCoder:coder];
[coder encodeObject:self.webView.request.URL.absoluteString forKey:@"webViewURL"];
[coder encodeObject:homePath forKey:@"homePath"];
[coder encodeCGPoint:self.webView.scrollView.contentOffset forKey:@"webViewOffset"];
}

- (void)decodeRestorableStateWithCoder:(NSCoder *)coder
{
[super decodeRestorableStateWithCoder:coder];
NSString *loadURL = [coder decodeObjectForKey:@"webViewURL"];
NSString *lastHomePath = [coder decodeObjectForKey:@"homePath"];
self.webViewOffset = [coder decodeCGPointForKey:@"webViewOffset"];
if (lastHomePath) {
loadURL = [[loadURL stringByReplacingOccurrencesOfString:lastHomePath withString:homePath] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
}

self.isRestoring = YES;
[self viewDidLoad];
self.isRestoring = NO;
self.isDecoding = YES;
[super decodeRestorableStateWithCoder:coder];
[self loadURL:loadURL];
if (isRegularHorizontalClass) {
[self.splitViewController setPreferredDisplayMode:UISplitViewControllerDisplayModeAllVisible];
}
self.isRestoreScroll = YES;
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
Expand Down