git.y1.nz

SameBoy

Accurate GB/GBC emulator
download: https://git.y1.nz/archives/sameboy.tar.gz
README | Files | Log | Refs | LICENSE

iOS/GBStatesViewController.m

      1 #import "GBStatesViewController.h"
      2 #import "GBSlotButton.h"
      3 #import "GBROMManager.h"
      4 #import "GBViewController.h"
      5 
      6 @implementation GBStatesViewController
      7 
      8 - (void)updateSlotView:(GBSlotButton *)view
      9 {
     10     NSString *stateFile = [[GBROMManager sharedManager] stateFile:view.tag];
     11     if ([[NSFileManager defaultManager] fileExistsAtPath:stateFile]) {
     12         NSDate *date = [[[NSFileManager defaultManager] attributesOfItemAtPath:stateFile error:nil] fileModificationDate];
     13         if (@available(iOS 13.0, *)) {
     14             if ((uint64_t)(date.timeIntervalSince1970) == (uint64_t)([NSDate now].timeIntervalSince1970)) {
     15                 view.slotSubtitleLabel.text = @"Just now";
     16             }
     17             else {
     18                 NSRelativeDateTimeFormatter *formatter = [[NSRelativeDateTimeFormatter alloc] init];
     19                 view.slotSubtitleLabel.text = [formatter localizedStringForDate:date relativeToDate:[NSDate now]];
     20             }
     21         }
     22         else {
     23             NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
     24             formatter.timeStyle = NSDateFormatterShortStyle;
     25             formatter.dateStyle = NSDateFormatterShortStyle;
     26             formatter.doesRelativeDateFormatting = true;
     27             view.slotSubtitleLabel.text = [formatter stringFromDate:date];
     28         }
     29         
     30         view.imageView.image = [UIImage imageWithContentsOfFile:[stateFile stringByAppendingPathExtension:@"png"]];
     31     }
     32     else {
     33         view.slotSubtitleLabel.text = @"Empty";
     34         view.imageView.image = nil;
     35     }
     36 }
     37 
     38 
     39 - (void)viewDidLoad
     40 {
     41     [super viewDidLoad];
     42     UIView *root = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0x300, 0x300)];
     43     [self.view addSubview:root];
     44     for (unsigned i = 0; i < 9; i++) {
     45         unsigned x = i % 3;
     46         unsigned y = i / 3;
     47         GBSlotButton *slotView = [GBSlotButton buttonWithLabelText:[NSString stringWithFormat:@"Slot %u", i + 1]];
     48         
     49         slotView.frame = CGRectMake(0x100 * x,
     50                                     0x100 * y,
     51                                     0x100,
     52                                     0x100);
     53         
     54         slotView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin |
     55                                     UIViewAutoresizingFlexibleWidth |
     56                                     UIViewAutoresizingFlexibleRightMargin |
     57                                     UIViewAutoresizingFlexibleTopMargin |
     58                                     UIViewAutoresizingFlexibleHeight |
     59                                     UIViewAutoresizingFlexibleBottomMargin;
     60 
     61         slotView.tag = i + 1;
     62         [self updateSlotView:slotView];
     63         [slotView addTarget:self
     64                      action:@selector(slotSelected:)
     65            forControlEvents:UIControlEventTouchUpInside];
     66         [root addSubview:slotView];
     67     }
     68     self.edgesForExtendedLayout = 0;
     69 }
     70 
     71 - (void)slotSelected:(GBSlotButton *)slot
     72 {
     73     
     74     UIAlertController *controller = [UIAlertController alertControllerWithTitle:slot.label.text
     75                                                                         message:nil
     76                                                                  preferredStyle:UIAlertControllerStyleActionSheet];
     77     
     78     NSString *stateFile = [[GBROMManager sharedManager] stateFile:slot.tag];
     79     GBViewController *delegate = (typeof(delegate))[UIApplication sharedApplication].delegate;
     80     
     81     void (^saveState)(UIAlertAction *action) = ^(UIAlertAction *action) {
     82         [delegate saveStateToFile:stateFile];
     83         [self updateSlotView:slot];
     84         [self.presentingViewController dismissViewControllerAnimated:true completion:nil];
     85         slot.showingMenu = false;
     86     };
     87     
     88     if (![[NSFileManager defaultManager] fileExistsAtPath:stateFile]) {
     89         saveState(nil);
     90         return;
     91     }
     92 
     93     [controller addAction:[UIAlertAction actionWithTitle:@"Save state"
     94                                                    style:UIAlertActionStyleDefault
     95                                                  handler:saveState]];
     96     
     97     [controller addAction:[UIAlertAction actionWithTitle:@"Load state"
     98                                                    style:UIAlertActionStyleDefault
     99                                                  handler:^(UIAlertAction *action) {
    100         [delegate loadStateFromFile:stateFile];
    101         [self updateSlotView:slot];
    102         [self.presentingViewController dismissViewControllerAnimated:true completion:nil];
    103         slot.showingMenu = false;
    104     }]];
    105     
    106     [controller addAction:[UIAlertAction actionWithTitle:@"Cancel"
    107                                                    style:UIAlertActionStyleCancel
    108                                                  handler:^(UIAlertAction *action) {
    109         slot.showingMenu = false;
    110     }]];
    111         
    112     slot.showingMenu = true;
    113     controller.popoverPresentationController.sourceView = slot;
    114     [self presentViewController:controller animated:true completion:nil];
    115 }
    116 
    117 - (void)viewWillLayoutSubviews
    118 {
    119     [super viewWillLayoutSubviews];
    120     self.view.subviews.firstObject.frame = [self.view.safeAreaLayoutGuide layoutFrame];
    121 }
    122 
    123 - (NSString *)title
    124 {
    125     return @"Save States";
    126 }
    127 @end

This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.