Skip to content

Commit 0b1eb1a

Browse files
committed
add boot disks to app's quick actions
1 parent d93ae3c commit 0b1eb1a

File tree

2 files changed

+72
-2
lines changed

2 files changed

+72
-2
lines changed

Mini vMac/AppDelegate.m

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ - (void)initDefaults {
6161
@"runInBackground": @NO,
6262
@"autoSlow": @(sharedEmulator.initialAutoSlow),
6363
@"screenFilter": kCAFilterLinear,
64-
@"autoShowGestureHelp": @YES
64+
@"autoShowGestureHelp": @YES,
65+
@"recentDisks": @[]
6566
};
6667

6768
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
@@ -175,6 +176,19 @@ - (void)showAlertWithTitle:(NSString *)title message:(NSString *)message {
175176
[controller presentViewController:alert animated:YES completion:nil];
176177
}
177178

179+
- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler {
180+
BOOL success = NO;
181+
if ([shortcutItem.type isEqualToString:@"disk"] && sharedEmulator.isRunning) {
182+
NSString *fileName = (NSString*)shortcutItem.userInfo[@"disk"];
183+
NSString *filePath = [self.documentsPath stringByAppendingPathComponent:fileName];
184+
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath] && ![sharedEmulator isDiskInserted:filePath]) {
185+
success = YES;
186+
[sharedEmulator performSelector:@selector(insertDisk:) withObject:filePath afterDelay:1.0];
187+
}
188+
}
189+
completionHandler(success);
190+
}
191+
178192
#pragma mark - Settings / Insert Disk / Help
179193

180194
- (void)showSettings:(id)sender {

Mini vMac/InsertDiskViewController.m

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#import "AppDelegate.h"
1111
#import "UIImage+DiskImageIcon.h"
1212

13-
@interface InsertDiskViewController () <UITextFieldDelegate, UIContextMenuInteractionDelegate>
13+
@interface InsertDiskViewController () <UITextFieldDelegate>
1414

1515
@end
1616

@@ -241,9 +241,65 @@ - (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtInd
241241

242242
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
243243
NSString *filePath = [self fileAtIndexPath:indexPath];
244+
BOOL hadAnyDisksInserted = [AppDelegate sharedEmulator].anyDiskInserted;
244245
if ([self insertDisk:filePath]) {
245246
[self dismissViewControllerAnimated:YES completion:nil];
247+
if (!hadAnyDisksInserted) {
248+
// Add to quick actions if it was booted from (no disks inserted previously and still inserted after 10s)
249+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(10.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
250+
id<Emulator> emulator = [AppDelegate sharedEmulator];
251+
if (emulator.isRunning && [emulator isDiskInserted:filePath]) {
252+
[self updateApplicationShortcutsWithDisk:filePath];
253+
}
254+
});
255+
}
256+
}
257+
}
258+
259+
#pragma mark - Quick Actions
260+
261+
- (void)updateApplicationShortcutsWithDisk:(NSString*)filePath {
262+
// Update user defaults
263+
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
264+
NSString *fileName = filePath.lastPathComponent;
265+
NSMutableArray *recentDisks = [userDefaults arrayForKey:@"recentDisks"].mutableCopy;
266+
if ([recentDisks containsObject:fileName]) {
267+
return;
268+
}
269+
[recentDisks insertObject:fileName atIndex:0];
270+
if (recentDisks.count > 4) {
271+
[recentDisks removeObjectsInRange:NSMakeRange(4, recentDisks.count - 4)];
272+
}
273+
[userDefaults setObject:recentDisks forKey:@"recentDisks"];
274+
275+
// Update quick actions
276+
NSMutableArray *shortcutItems = [NSMutableArray arrayWithCapacity:4];
277+
for (NSString *diskName in recentDisks) {
278+
UIApplicationShortcutItem *shortcutItem = [self shortcutItemForDisk:diskName];
279+
if (shortcutItem) {
280+
[shortcutItems addObject:shortcutItem];
281+
}
282+
}
283+
[UIApplication sharedApplication].shortcutItems = shortcutItems;
284+
}
285+
286+
- (nullable UIApplicationShortcutItem*)shortcutItemForDisk:(NSString*)fileName {
287+
BOOL isDiskImage = [[AppDelegate sharedInstance].diskImageExtensions containsObject:fileName.pathExtension.lowercaseString];
288+
if (!isDiskImage) {
289+
return nil;
290+
}
291+
NSString *filePath = [basePath stringByAppendingPathComponent:fileName];
292+
NSString *title = [fileName stringByDeletingPathExtension];
293+
UIApplicationShortcutIcon *icon = nil;
294+
NSDictionary *attributes = [[NSURL fileURLWithPath:filePath] resourceValuesForKeys:@[NSURLTotalFileSizeKey, NSURLFileSizeKey] error:NULL];
295+
if (attributes && attributes[NSURLTotalFileSizeKey]) {
296+
NSInteger fileSize = [attributes[NSURLTotalFileSizeKey] integerValue];
297+
NSInteger numBlocks = fileSize / 512;
298+
icon = [UIApplicationShortcutIcon iconWithTemplateImageName:numBlocks == 800 || numBlocks == 1600 ? @"floppy" : @"floppyV"];
299+
} else {
300+
return nil;
246301
}
302+
return [[UIApplicationShortcutItem alloc] initWithType:@"disk" localizedTitle:title localizedSubtitle:nil icon:icon userInfo:@{@"disk": fileName}];
247303
}
248304

249305
#pragma mark - File Actions

0 commit comments

Comments
 (0)