minivmac4ios/Mini vMac/SettingsViewController.m

217 lines
9.3 KiB
Mathematica
Raw Normal View History

2016-05-11 21:04:49 +00:00
//
// SettingsViewController.m
// Mini vMac
//
// Created by Jesús A. Álvarez on 07/05/2016.
// Copyright © 2016 namedfork. All rights reserved.
//
#import "SettingsViewController.h"
#import "AppDelegate.h"
@interface SettingsViewController ()
@end
typedef enum : NSInteger {
SettingsSectionSpeed,
SettingsSectionMouse,
SettingsSectionKeyboard,
SettingsSectionMachine,
SettingsSectionAbout
} SettingsSection;
2016-05-11 21:04:49 +00:00
@implementation SettingsViewController
2016-05-14 11:06:42 +00:00
{
NSArray *keyboardLayouts;
NSArray<NSBundle*> *emulatorBundles;
2016-05-22 15:09:33 +00:00
NSString *aboutTitle;
NSArray<NSDictionary<NSString*,NSString*>*> *aboutItems;
UITextView *footerView;
2016-05-14 11:06:42 +00:00
}
2016-05-11 21:04:49 +00:00
- (void)viewDidLoad {
[super viewDidLoad];
2016-05-14 11:06:42 +00:00
keyboardLayouts = [[NSBundle mainBundle] pathsForResourcesOfType:@"nfkeyboardlayout" inDirectory:@"Keyboard Layouts"];
emulatorBundles = [AppDelegate sharedInstance].emulatorBundles;
2016-05-22 15:09:33 +00:00
[self loadCredits];
}
- (void)loadCredits {
NSDictionary *aboutData = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"about" ofType:@"plist"]];
aboutTitle = aboutData[@"title"];
aboutItems = aboutData[@"items"];
footerView = [[UITextView alloc] initWithFrame:CGRectZero];
NSAttributedString *str = [[NSMutableAttributedString alloc] initWithData:[aboutData[@"footer.html"] dataUsingEncoding:NSUTF8StringEncoding]
options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)}
documentAttributes:nil
error:NULL];
footerView.attributedText = str;
[footerView sizeToFit];
footerView.editable = NO;
footerView.textAlignment = NSTextAlignmentCenter;
footerView.textColor = [UIColor darkGrayColor];
footerView.font = [UIFont systemFontOfSize:[UIFont smallSystemFontSize]];
footerView.backgroundColor = [UIColor clearColor];
2016-05-11 21:04:49 +00:00
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
2016-05-28 11:01:13 +00:00
[AppDelegate sharedEmulator].running = NO;
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
2016-05-28 11:01:13 +00:00
[AppDelegate sharedEmulator].running = YES;
}
2016-05-11 21:04:49 +00:00
- (void)showInsertDisk:(id)sender {
[[AppDelegate sharedInstance] showInsertDisk:sender];
}
- (void)dismiss:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)changeSpeed:(UISegmentedControl*)sender {
if ([sender isKindOfClass:[UISegmentedControl class]]) {
[[NSUserDefaults standardUserDefaults] setInteger:sender.selectedSegmentIndex forKey:@"speedValue"];
}
2016-05-11 21:04:49 +00:00
}
- (IBAction)changeMouseType:(UISegmentedControl*)sender {
if ([sender isKindOfClass:[UISegmentedControl class]]) {
[[NSUserDefaults standardUserDefaults] setBool:sender.selectedSegmentIndex == 1 forKey:@"trackpad"];
}
2016-05-11 21:04:49 +00:00
}
#pragma mark - Table view data source
2016-05-11 21:04:49 +00:00
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 5;
2016-05-11 21:04:49 +00:00
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
switch (section) {
case SettingsSectionKeyboard:
return keyboardLayouts.count;
case SettingsSectionMachine:
return emulatorBundles.count;
case SettingsSectionAbout:
return aboutItems.count;
default:
return 1;
}
2016-05-11 21:04:49 +00:00
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
switch (section) {
case SettingsSectionSpeed:
return NSLocalizedString(@"Speed", nil);
case SettingsSectionMouse:
return NSLocalizedString(@"Mouse Type", nil);
case SettingsSectionMachine:
return NSLocalizedString(@"Emulated Machine", nil);
case SettingsSectionKeyboard:
return NSLocalizedString(@"Keyboard Layout", nil);
case SettingsSectionAbout:
return aboutTitle;
default:return nil;
}
2016-05-11 21:04:49 +00:00
}
2016-05-22 15:09:33 +00:00
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
if (section == SettingsSectionAbout) {
2016-05-22 15:09:33 +00:00
return footerView;
} else {
return nil;
}
}
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
if (section == SettingsSectionMachine) {
return NSLocalizedString(@"Changing the emulated machine requires to relaunch Mini vMac", nil);
} else {
return nil;
}
}
2016-05-22 15:09:33 +00:00
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
if (section == SettingsSectionAbout) {
2016-05-22 15:09:33 +00:00
return footerView.bounds.size.height;
} else {
return UITableViewAutomaticDimension;
2016-05-22 15:09:33 +00:00
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = nil;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSInteger section = indexPath.section;
if (section == SettingsSectionSpeed) {
cell = [tableView dequeueReusableCellWithIdentifier:@"speed" forIndexPath:indexPath];
UISegmentedControl *speedControl = (UISegmentedControl*)[cell viewWithTag:128];
speedControl.selectedSegmentIndex = [defaults integerForKey:@"speedValue"];
} else if (section == SettingsSectionMouse) {
cell = [tableView dequeueReusableCellWithIdentifier:@"mouse" forIndexPath:indexPath];
UISegmentedControl *mouseControl = (UISegmentedControl*)[cell viewWithTag:128];
mouseControl.selectedSegmentIndex = [defaults boolForKey:@"trackpad"] ? 1 : 0;
} else if (section == SettingsSectionKeyboard) {
cell = [tableView dequeueReusableCellWithIdentifier:@"keyboard" forIndexPath:indexPath];
2016-05-14 11:06:42 +00:00
NSString *layout = keyboardLayouts[indexPath.row];
cell.textLabel.text = layout.lastPathComponent.stringByDeletingPathExtension;
BOOL selected = [[defaults stringForKey:@"keyboardLayout"] isEqualToString:layout.lastPathComponent];
cell.accessoryType = selected ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
} else if (section == SettingsSectionMachine) {
NSBundle *bundle = emulatorBundles[indexPath.row];
NSString *bundleName = bundle.bundlePath.lastPathComponent.stringByDeletingPathExtension;
cell = [tableView dequeueReusableCellWithIdentifier:@"machine" forIndexPath:indexPath];
cell.textLabel.text = [bundle objectForInfoDictionaryKey:@"CFBundleDisplayName"];
cell.detailTextLabel.text = [bundle objectForInfoDictionaryKey:@"CFBundleGetInfoString"];
NSString *iconName = [NSString stringWithFormat:@"PlugIns/%@.mnvm/Icon", bundleName];
cell.imageView.image = [UIImage imageNamed:iconName];
BOOL selected = [[defaults stringForKey:@"machine"] isEqualToString:bundleName];
cell.accessoryType = selected ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
} else if (section == SettingsSectionAbout) {
cell = [tableView dequeueReusableCellWithIdentifier:@"about" forIndexPath:indexPath];
2016-05-22 15:09:33 +00:00
NSDictionary<NSString*,NSString*> *item = aboutItems[indexPath.row];
cell.textLabel.text = item[@"text"];
NSString *detailText = item[@"detailText"];
if ([detailText isEqualToString:@"$version"]) {
NSString *versionString = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
NSString *commitString = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"GitVersion"];
cell.detailTextLabel.text = commitString.length > 0 ? [NSString stringWithFormat:@"%@ (%@)", versionString, commitString] : versionString;
} else {
cell.detailTextLabel.text = detailText;
}
cell.accessoryType = item[@"link"] == nil ? UITableViewCellAccessoryNone : UITableViewCellAccessoryDisclosureIndicator;
}
return cell;
2016-05-11 21:04:49 +00:00
}
2016-05-14 11:06:42 +00:00
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
2016-05-22 15:09:33 +00:00
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if (indexPath.section == SettingsSectionKeyboard) {
2016-05-22 15:09:33 +00:00
// selected keyboard layout
2016-05-14 11:06:42 +00:00
NSString *layout = keyboardLayouts[indexPath.row];
[defaults setValue:layout.lastPathComponent forKey:@"keyboardLayout"];
2016-05-22 15:09:33 +00:00
[tableView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationAutomatic];
} else if (indexPath.section == SettingsSectionMachine) {
// selected emulated machine
NSBundle *bundle = emulatorBundles[indexPath.row];
NSString *bundleName = bundle.bundlePath.lastPathComponent.stringByDeletingPathExtension;
[defaults setValue:bundleName forKey:@"machine"];
[tableView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationAutomatic];
} else if (indexPath.section == SettingsSectionAbout) {
2016-05-22 15:09:33 +00:00
// links in about
NSString *linkURL = aboutItems[indexPath.row][@"link"];
if (linkURL != nil) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:linkURL]];
}
2016-05-14 11:06:42 +00:00
}
}
2016-05-11 21:04:49 +00:00
@end