Skip to content

Commit

Permalink
Improve user activity restoration.
Browse files Browse the repository at this point in the history
  • Loading branch information
yury committed Sep 10, 2018
1 parent 3988a24 commit 88a77ec
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 33 deletions.
15 changes: 9 additions & 6 deletions Blink/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -110,23 +110,26 @@ - (BOOL)application:(UIApplication *)application willContinueUserActivityWithTyp
return YES;
}

- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray * _Nullable))restorationHandler
{
restorationHandler(@[[[ScreenController shared] mainScreenRootViewController]]);
return YES;
- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray<id<UIUserActivityRestoring>> * _Nullable))restorationHandler {
restorationHandler(@[[[ScreenController shared] mainScreenRootViewController]]);
return YES;
}

//- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray * _Nullable))restorationHandler
//{
// restorationHandler(@[[[ScreenController shared] mainScreenRootViewController]]);
// return YES;
//}

#pragma mark - State saving and restoring

- (void)applicationProtectedDataWillBecomeUnavailable:(UIApplication *)application
{

[self _suspendApplicationOnProtectedDataWillBecomeUnavailable];
}

- (void)applicationWillTerminate:(UIApplication *)application
{

[self _suspendApplicationOnWillTerminate];
}

Expand Down
52 changes: 38 additions & 14 deletions Blink/SpaceController.m
Original file line number Diff line number Diff line change
Expand Up @@ -844,32 +844,56 @@ - (void)restoreUserActivityState:(NSUserActivity *)activity
return;
}

// 1. Try find term with same activity key

// 1. Find terminal with excact command that is running right now
NSInteger targetIdx = [_viewports indexOfObjectPassingTest:^BOOL(TermController *term, NSUInteger idx, BOOL * _Nonnull stop) {
return [activity.title isEqualToString:term.activityKey];
return [activity.title isEqualToString:term.activityKey] && [term isRunningCmd];
}];

// 2. No term with same activity key, so we create one or use current
if (targetIdx == NSNotFound) {
if (self.currentTerm.activityKey == nil) {
[self.currentTerm restoreUserActivityState:activity];

if (targetIdx != NSNotFound) {
// current terminal is running this command, so just stay here.
if (idx == targetIdx) {
[self _attachInputToCurrentTerm];
} else {
[self _createShellWithUserActivity:activity sessionStateKey:nil animated:YES completion:nil];
return;
}

// switch to terminal that is running command.
UIPageViewControllerNavigationDirection direction =
idx < targetIdx ? UIPageViewControllerNavigationDirectionForward : UIPageViewControllerNavigationDirectionReverse;

[self switchShellIdx: targetIdx
direction: direction
animated: NO];
return;
}

// 3. We are already showing required term. So do nothing.
if (idx == targetIdx) {
// 2. Check if current term can run command.
if ([self.currentTerm canRestoreUserActivityState:activity]) {
[self _attachInputToCurrentTerm];
[self.currentTerm.termDevice focus];
[self.currentTerm restoreUserActivityState:activity];
return;
}

// 4. Switch to found term index.

// 3. Find terminal that can run this command.
targetIdx = [_viewports indexOfObjectPassingTest:^BOOL(TermController *term, NSUInteger idx, BOOL * _Nonnull stop) {
return [term canRestoreUserActivityState:activity];
}];

// No running terminals can run this command, so we creating new one.
if (targetIdx == NSNotFound) {
[self _createShellWithUserActivity:activity sessionStateKey:nil animated:YES completion:nil];
return;
}

// Switch to terminal and run command.
UIPageViewControllerNavigationDirection direction =
idx < targetIdx ? UIPageViewControllerNavigationDirectionForward : UIPageViewControllerNavigationDirectionReverse;


TermController *term = _viewports[targetIdx];
[term.termDevice attachInput:_termInput];
[term.termDevice focus];
[term restoreUserActivityState:activity];
[self switchShellIdx: targetIdx
direction: direction
animated: NO];
Expand Down
2 changes: 2 additions & 0 deletions Blink/TermController.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,7 @@
- (void)suspend;
- (void)resume;
- (void)scaleWithPich:(UIPinchGestureRecognizer *)pinch;
- (bool)canRestoreUserActivityState:(NSUserActivity *)activity;
- (bool)isRunningCmd;

@end
23 changes: 19 additions & 4 deletions Blink/TermController.m
Original file line number Diff line number Diff line change
Expand Up @@ -103,17 +103,32 @@ - (void)updateUserActivityState:(NSUserActivity *)activity
[activity setRequiredUserInfoKeys:[NSSet setWithArray:_activityUserInfo.allKeys]];
}

- (bool)canRestoreUserActivityState:(NSUserActivity *)activity {
return ![_session isRunningCmd] || [activity.title isEqualToString:self.activityKey];
}

- (bool)isRunningCmd {
return [_session isRunningCmd];
}

- (void)restoreUserActivityState:(NSUserActivity *)activity
{
[super restoreUserActivityState:activity];

if (![activity.activityType isEqualToString: BKUserActivityTypeCommandLine]) {
[super restoreUserActivityState:activity];
return;
}

NSString *cmdLine = [activity.userInfo objectForKey:BKUserActivityCommandLineKey];
if (cmdLine) {
// TODO: investigate lost first char on iPad
[_termDevice write:[NSString stringWithFormat:@" %@\n", cmdLine]];

if ([_session isRunningCmd] || !cmdLine) {
return;
}
char ctrlA = 'a' - 'a' + 1;
char ctrlK = 'k' - 'a' + 1;
// delete all input on current line - ctrl+a ctrl+k
// run command
[_termDevice write:[NSString stringWithFormat:@"%c%c%@\n", ctrlA, ctrlK, cmdLine]];
}

- (void)viewDidLoad
Expand Down
2 changes: 2 additions & 0 deletions Sessions/MCPSession.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
@property (strong, readonly) Repl *repl;
@property (weak) SSHClient *sshClient;

- (bool)isRunningCmd;

- (void)updateAllowedPaths;

@end
13 changes: 4 additions & 9 deletions Sessions/MCPSession.m
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,6 @@
#import "BKUserConfigurationManager.h"
#import "BlinkPaths.h"



// from ios_system:

#include <ios_system/ios_system.h>

#include "ios_error.h"
Expand Down Expand Up @@ -112,7 +108,6 @@ - (int)main:(int)argc argv:(char **)argv
} else if ([cmd isEqualToString:@"ssh-copy-id"]) {
[self _runSSHCopyIDWithArgs:cmdline];
} else {

[self.delegate indexCommand:cmdline];
_currentCmd = cmdline;
thread_stdout = nil;
Expand All @@ -133,6 +128,10 @@ - (int)main:(int)argc argv:(char **)argv
return 0;
}

- (bool)isRunningCmd {
return _childSession != nil || _currentCmd != nil;
}

- (NSArray<NSString *> *)_symlinksInHomeDirectory
{
NSFileManager *fm = [NSFileManager defaultManager];
Expand Down Expand Up @@ -203,10 +202,6 @@ - (void)sigwinch
{
[_repl sigwinch];
[_childSession sigwinch];
if (_currentCmd) {
// pthread_t tid = ios_getLastThreadId();
// pthread_kill(tid, SIGWINCH);
}
[_sshClient sigwinch];
}

Expand Down

0 comments on commit 88a77ec

Please sign in to comment.