|
10 | 10 | #import "AppDelegate.h" |
11 | 11 | #import "UIImage+DiskImageIcon.h" |
12 | 12 |
|
13 | | -@interface InsertDiskViewController () <UITextFieldDelegate, UIContextMenuInteractionDelegate> |
| 13 | +@interface InsertDiskViewController () <UITextFieldDelegate> |
14 | 14 |
|
15 | 15 | @end |
16 | 16 |
|
@@ -241,9 +241,65 @@ - (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtInd |
241 | 241 |
|
242 | 242 | - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
243 | 243 | NSString *filePath = [self fileAtIndexPath:indexPath]; |
| 244 | + BOOL hadAnyDisksInserted = [AppDelegate sharedEmulator].anyDiskInserted; |
244 | 245 | if ([self insertDisk:filePath]) { |
245 | 246 | [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; |
246 | 301 | } |
| 302 | + return [[UIApplicationShortcutItem alloc] initWithType:@"disk" localizedTitle:title localizedSubtitle:nil icon:icon userInfo:@{@"disk": fileName}]; |
247 | 303 | } |
248 | 304 |
|
249 | 305 | #pragma mark - File Actions |
|
0 commit comments