Skip to content

Commit

Permalink
Improve open command
Browse files Browse the repository at this point in the history
  • Loading branch information
yury committed May 30, 2018
1 parent 75d432e commit 1c04f1b
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 40 deletions.
59 changes: 41 additions & 18 deletions Blink/Commands/open.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,31 +25,54 @@ int open_main(int argc, char *argv[]) {
return 1;
}


MCPSession *session = (__bridge MCPSession *)thread_context;

NSFileManager *fm = [[NSFileManager alloc] init];
NSMutableArray *urls = [[NSMutableArray alloc] init];
bool isDir = NO;
if ([[NSFileManager defaultManager] fileExistsAtPath:args isDirectory:&isDir]) {
if (!isDir) {
NSURL * currentDir = [NSURL fileURLWithPath: [[NSFileManager defaultManager] currentDirectoryPath]];
NSURL * url = [currentDir URLByAppendingPathComponent:args isDirectory:NO];

dispatch_async(dispatch_get_main_queue(), ^{
if (url) {
NSNotification *n = [[NSNotification alloc] initWithName:@"BlinkShare" object:session userInfo:@{@"url": url}];
[[NSNotificationCenter defaultCenter] postNotification:n];
}
});
if ([fm fileExistsAtPath:args isDirectory:&isDir]) {
NSURL * currentDir = [NSURL fileURLWithPath: [fm currentDirectoryPath]];
NSURL * url = [currentDir URLByAppendingPathComponent:args isDirectory:NO];
if (url) {
[urls addObject:url];
} else {
fprintf(thread_stderr, "Can't open file or dir");
return 1;
}
} else {
NSURL *url = [NSURL URLWithString:args];
dispatch_async(dispatch_get_main_queue(), ^{
if (url) {
NSNotification *n = [[NSNotification alloc] initWithName:@"BlinkShare" object:session userInfo:@{@"url": url}];
[[NSNotificationCenter defaultCenter] postNotification:n];
}
});

if (url) {
[urls addObject:url];
} else {
fprintf(thread_stderr, "%s", [NSString stringWithFormat:@"Can't open file or dir at path: %@", url].UTF8String);
return 1;
}
}

dispatch_semaphore_t dsema = dispatch_semaphore_create(0);

UIActivityViewControllerCompletionWithItemsHandler hander = ^(UIActivityType __nullable activityType, BOOL completed, NSArray * __nullable returnedItems, NSError * __nullable activityError) {
dispatch_semaphore_signal(dsema);
};

dispatch_async(dispatch_get_main_queue(), ^{
UIActivityViewController *ctrl = [[UIActivityViewController alloc] initWithActivityItems:urls applicationActivities:nil];

ctrl.completionWithItemsHandler = hander;

if (ctrl.popoverPresentationController) {
UIView *view = session.device.view;
ctrl.popoverPresentationController.sourceView = view;

CGRect rect = CGRectMake(0, view.bounds.size.height - 30, view.bounds.size.width, 30);
ctrl.popoverPresentationController.sourceRect = rect;
}

[session.device.delegate.viewController presentViewController:ctrl animated:YES completion:nil];
});

dispatch_semaphore_wait(dsema, DISPATCH_TIME_FOREVER);

return 0;
}
2 changes: 2 additions & 0 deletions Blink/Repl.m
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,8 @@ -(NSString *)_commandCompletionType:(NSString *)command {
return @"blink-theme";
} else if ([@"ls" isEqualToString:command]) {
return @"directory";
} else if ([@"open" isEqualToString:command]) {
return @"file";
} else if ([@"music" isEqualToString:command]) {
return @"blink-music";
} else if ([@[@"help", @"exit", @"whoami", @"config", @"clear", @"history", @"link-files"] indexOfObject:command] != NSNotFound) {
Expand Down
22 changes: 0 additions & 22 deletions Blink/SpaceController.m
Original file line number Diff line number Diff line change
Expand Up @@ -250,28 +250,6 @@ - (void)registerForNotifications
selector:@selector(keyboardFuncTriggerChanged:)
name:BKKeyboardFuncTriggerChanged
object:nil];

[defaultCenter addObserver: self selector:@selector(_blinkShare:) name:@"BlinkShare" object:nil];
}

- (void)_blinkShare: (NSNotification *)n {
NSURL *url = [n.userInfo objectForKey:@"url"];

if (@available(iOS 11.0, *)) {
UIActivityViewController *ctrl = [[UIActivityViewController alloc] initWithActivityItems:@[url] applicationActivities:nil];
if (ctrl.popoverPresentationController) {
ctrl.popoverPresentationController.sourceView = self.currentTerm.view;

CGRect rect = CGRectMake(0, self.view.bounds.size.height - 30, self.view.bounds.size.width, 30);
ctrl.popoverPresentationController.sourceRect = rect;
}

[self presentViewController:ctrl animated:YES completion:nil];

} else {
// Fallback on earlier versions
}

}

- (void)_appDidBecomeActive
Expand Down

0 comments on commit 1c04f1b

Please sign in to comment.