Basic ROM management functionality

This commit is contained in:
Lior Halphon 2023-01-19 00:47:35 +02:00
parent 21f21f6bc7
commit d32bff58a4
4 changed files with 55 additions and 2 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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];