diff --git a/Mini vMac/AppDelegate.m b/Mini vMac/AppDelegate.m index e069dd7..c2ac758 100644 --- a/Mini vMac/AppDelegate.m +++ b/Mini vMac/AppDelegate.m @@ -61,7 +61,8 @@ NSString *DocumentsChangedNotification = @"documentsChanged"; @"runInBackground": @NO, @"autoSlow": @(sharedEmulator.initialAutoSlow), @"screenFilter": kCAFilterLinear, - @"autoShowGestureHelp": @YES + @"autoShowGestureHelp": @YES, + @"recentDisks": @[] }; NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; @@ -175,6 +176,19 @@ NSString *DocumentsChangedNotification = @"documentsChanged"; [controller presentViewController:alert animated:YES completion:nil]; } +- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler { + BOOL success = NO; + if ([shortcutItem.type isEqualToString:@"disk"] && sharedEmulator.isRunning) { + NSString *fileName = (NSString*)shortcutItem.userInfo[@"disk"]; + NSString *filePath = [self.documentsPath stringByAppendingPathComponent:fileName]; + if ([[NSFileManager defaultManager] fileExistsAtPath:filePath] && ![sharedEmulator isDiskInserted:filePath]) { + success = YES; + [sharedEmulator performSelector:@selector(insertDisk:) withObject:filePath afterDelay:1.0]; + } + } + completionHandler(success); +} + #pragma mark - Settings / Insert Disk / Help - (void)showSettings:(id)sender { diff --git a/Mini vMac/InsertDiskViewController.m b/Mini vMac/InsertDiskViewController.m index df2efb7..a510848 100644 --- a/Mini vMac/InsertDiskViewController.m +++ b/Mini vMac/InsertDiskViewController.m @@ -10,7 +10,7 @@ #import "AppDelegate.h" #import "UIImage+DiskImageIcon.h" -@interface InsertDiskViewController () +@interface InsertDiskViewController () @end @@ -241,11 +241,67 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSString *filePath = [self fileAtIndexPath:indexPath]; + BOOL hadAnyDisksInserted = [AppDelegate sharedEmulator].anyDiskInserted; if ([self insertDisk:filePath]) { [self dismissViewControllerAnimated:YES completion:nil]; + if (!hadAnyDisksInserted) { + // Add to quick actions if it was booted from (no disks inserted previously and still inserted after 10s) + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(10.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ + id emulator = [AppDelegate sharedEmulator]; + if (emulator.isRunning && [emulator isDiskInserted:filePath]) { + [self updateApplicationShortcutsWithDisk:filePath]; + } + }); + } } } +#pragma mark - Quick Actions + +- (void)updateApplicationShortcutsWithDisk:(NSString*)filePath { + // Update user defaults + NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; + NSString *fileName = filePath.lastPathComponent; + NSMutableArray *recentDisks = [userDefaults arrayForKey:@"recentDisks"].mutableCopy; + if ([recentDisks containsObject:fileName]) { + return; + } + [recentDisks insertObject:fileName atIndex:0]; + if (recentDisks.count > 4) { + [recentDisks removeObjectsInRange:NSMakeRange(4, recentDisks.count - 4)]; + } + [userDefaults setObject:recentDisks forKey:@"recentDisks"]; + + // Update quick actions + NSMutableArray *shortcutItems = [NSMutableArray arrayWithCapacity:4]; + for (NSString *diskName in recentDisks) { + UIApplicationShortcutItem *shortcutItem = [self shortcutItemForDisk:diskName]; + if (shortcutItem) { + [shortcutItems addObject:shortcutItem]; + } + } + [UIApplication sharedApplication].shortcutItems = shortcutItems; +} + +- (nullable UIApplicationShortcutItem*)shortcutItemForDisk:(NSString*)fileName { + BOOL isDiskImage = [[AppDelegate sharedInstance].diskImageExtensions containsObject:fileName.pathExtension.lowercaseString]; + if (!isDiskImage) { + return nil; + } + NSString *filePath = [basePath stringByAppendingPathComponent:fileName]; + NSString *title = [fileName stringByDeletingPathExtension]; + UIApplicationShortcutIcon *icon = nil; + NSDictionary *attributes = [[NSURL fileURLWithPath:filePath] resourceValuesForKeys:@[NSURLTotalFileSizeKey, NSURLFileSizeKey] error:NULL]; + if (attributes && attributes[NSURLTotalFileSizeKey]) { + NSInteger fileSize = [attributes[NSURLTotalFileSizeKey] integerValue]; + NSInteger numBlocks = fileSize / 512; + icon = [UIApplicationShortcutIcon iconWithTemplateImageName:numBlocks == 800 || numBlocks == 1600 ? @"floppy" : @"floppyV"]; + } else { + return nil; + } + return [[UIApplicationShortcutItem alloc] initWithType:@"disk" localizedTitle:title localizedSubtitle:nil icon:icon userInfo:@{@"disk": fileName}]; +} + #pragma mark - File Actions - (BOOL)insertDisk:(NSString*)filePath {