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