From d32bff58a432888ba0b519e71029dd2cac0354e1 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Thu, 19 Jan 2023 00:47:35 +0200 Subject: [PATCH] Basic ROM management functionality --- iOS/GBLoadROMTableViewController.m | 31 +++++++++++++++++++++++++++++- iOS/GBROMManager.h | 1 + iOS/GBROMManager.m | 7 +++++++ iOS/GBViewController.m | 18 ++++++++++++++++- 4 files changed, 55 insertions(+), 2 deletions(-) diff --git a/iOS/GBLoadROMTableViewController.m b/iOS/GBLoadROMTableViewController.m index 977c94c..d873eb4 100644 --- a/iOS/GBLoadROMTableViewController.m +++ b/iOS/GBLoadROMTableViewController.m @@ -11,6 +11,7 @@ - (instancetype)init { self = [super initWithStyle:UITableViewStyleGrouped]; + self.navigationItem.rightBarButtonItem = self.editButtonItem; return self; } @@ -55,11 +56,16 @@ return 64; } -- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section +- (NSString *)title { return @"ROM Library"; } +- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section +{ + return @"Import ROMs using AirDrop or by opening them in SameBoy using the Files app"; +} + - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [GBROMManager sharedManager].currentROM = [GBROMManager sharedManager].allROMs[[indexPath indexAtPosition:1]]; @@ -73,4 +79,27 @@ return UIModalPresentationFormSheet; } +- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath +{ + if (editingStyle != UITableViewCellEditingStyleDelete) return; + NSString *rom = [GBROMManager sharedManager].allROMs[[indexPath indexAtPosition:1]]; + UIAlertController *alert = [UIAlertController alertControllerWithTitle:[NSString stringWithFormat:@"Delete ROM “%@”?", rom] + message: @"Save data for this ROM will also be deleted." + preferredStyle:UIAlertControllerStyleAlert]; + [alert addAction:[UIAlertAction actionWithTitle:@"Delete" + style:UIAlertActionStyleDestructive + handler:^(UIAlertAction * _Nonnull action) { + [[GBROMManager sharedManager] deleteROM:rom]; + [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; + if ([[GBROMManager sharedManager].currentROM isEqualToString:rom]) { + [GBROMManager sharedManager].currentROM = nil; + [[NSNotificationCenter defaultCenter] postNotificationName:@"GBROMChanged" object:nil]; + } + }]]; + [alert addAction:[UIAlertAction actionWithTitle:@"Cancel" + style:UIAlertActionStyleCancel + handler:nil]]; + [self presentViewController:alert animated:true completion:nil]; +} + @end diff --git a/iOS/GBROMManager.h b/iOS/GBROMManager.h index ed12821..5d2c225 100644 --- a/iOS/GBROMManager.h +++ b/iOS/GBROMManager.h @@ -16,4 +16,5 @@ - (NSString *)autosaveStateFileForROM:(NSString *)rom; - (NSString *)stateFile:(unsigned)index forROM:(NSString *)rom; - (NSString *)importROM:(NSString *)romFile keepOriginal:(bool)keep; +- (void)deleteROM:(NSString *)rom; @end diff --git a/iOS/GBROMManager.m b/iOS/GBROMManager.m index 0b7e38b..73c7d2b 100644 --- a/iOS/GBROMManager.m +++ b/iOS/GBROMManager.m @@ -160,4 +160,11 @@ return friendlyName; } +- (void)deleteROM:(NSString *)rom +{ + NSString *root = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, true).firstObject; + NSString *romDirectory = [root stringByAppendingPathComponent:rom]; + [[NSFileManager defaultManager] removeItemAtPath:romDirectory error:nil]; +} + @end diff --git a/iOS/GBViewController.m b/iOS/GBViewController.m index e511702..0e02cbb 100644 --- a/iOS/GBViewController.m +++ b/iOS/GBViewController.m @@ -107,6 +107,7 @@ static void rumbleCallback(GB_gameboy_t *gb, double amp) [self initGameBoy]; _gbView = _backgroundView.gbView; + _gbView.hidden = true; _gbView.gb = &_gb; [_gbView screenSizeChanged]; @@ -149,6 +150,10 @@ static void rumbleCallback(GB_gameboy_t *gb, double amp) [self loadState:[GBROMManager sharedManager].autosaveStateFile]; } } + else { + _romLoaded = false; + } + _gbView.hidden = !_romLoaded; } - (void)applicationDidBecomeActive:(UIApplication *)application @@ -174,7 +179,13 @@ static void rumbleCallback(GB_gameboy_t *gb, double amp) - (void)openLibrary { - [self presentViewController:[[GBLoadROMTableViewController alloc] init] + UINavigationController *controller = [[UINavigationController alloc] initWithRootViewController:[[GBLoadROMTableViewController alloc] init]]; + UIBarButtonItem *close = [[UIBarButtonItem alloc] initWithTitle:@"Close" + style:UIBarButtonItemStylePlain + target:self + action:@selector(dismissViewController)]; + [controller.visibleViewController.navigationItem setLeftBarButtonItem:close]; + [self presentViewController:controller animated:true completion:nil]; } @@ -227,6 +238,11 @@ static void rumbleCallback(GB_gameboy_t *gb, double amp) }]; } +- (void)dismissViewController +{ + [self dismissViewControllerAnimated:true completion:nil]; +} + - (void)applicationWillResignActive:(UIApplication *)application { [self stop];