Skip to content

Commit

Permalink
Reorganize appearance settings
Browse files Browse the repository at this point in the history
  • Loading branch information
tbodt committed Jan 2, 2019
1 parent 01ecfa0 commit d1de392
Show file tree
Hide file tree
Showing 6 changed files with 204 additions and 181 deletions.
14 changes: 2 additions & 12 deletions app/AboutViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@

@interface AboutViewController ()
@property (weak, nonatomic) IBOutlet UITableViewCell *capsLockMappingCell;
@property (weak, nonatomic) IBOutlet UILabel *fontSizeLabel;
@property (weak, nonatomic) IBOutlet UIStepper *fontSizeStepper;
@property (weak, nonatomic) IBOutlet UITableViewCell *themeCell;
@property (weak, nonatomic) IBOutlet UISwitch *disableDimmingSwitch;

Expand All @@ -39,15 +37,13 @@ - (void)_addObservers {

[prefs addObserver:self forKeyPath:@"capsLockMapping" options:opts context:nil];
[prefs addObserver:self forKeyPath:@"fontSize" options:opts context:nil];
[prefs addObserver:self forKeyPath:@"theme" options:opts context:nil];
}

- (void)_removeObservers {
@try {
UserPreferences *prefs = [UserPreferences shared];
[prefs removeObserver:self forKeyPath:@"capsLockMapping"];
[prefs removeObserver:self forKeyPath:@"fontSize"];
[prefs removeObserver:self forKeyPath:@"theme"];
} @catch (NSException * __unused exception) {}
}

Expand All @@ -60,9 +56,7 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N
}

- (void)_updatePreferenceUI {
UserPreferences *prefs = [UserPreferences shared];
self.fontSizeLabel.text = prefs.fontSize.stringValue;
self.fontSizeStepper.value = prefs.fontSize.doubleValue;
UserPreferences *prefs = UserPreferences.shared;
self.themeCell.detailTextLabel.text = prefs.theme.presetName;
NSString *capsLockMappingDescr;
switch (prefs.capsLockMapping) {
Expand All @@ -80,7 +74,7 @@ - (void)_updatePreferenceUI {
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell == self.sendFeedback) {
[UIApplication openURL:@"mailto:tblodt@icloud.com"];
[UIApplication openURL:@"mailto:tblodt@icloud.com?subject=Feedback%20for%20iSH"];
} else if (cell == self.openGithub) {
[UIApplication openURL:@"https://github.com/tbodt/ish"];
} else if (cell == self.openTwitter) {
Expand All @@ -89,10 +83,6 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (IBAction)fontSizeChanged:(id)sender {
UserPreferences.shared.fontSize = @((int) self.fontSizeStepper.value);
}

- (IBAction)disableDimmingChanged:(id)sender {
UserPreferences.shared.shouldDisableDimming = self.disableDimmingSwitch.on;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

NS_ASSUME_NONNULL_BEGIN

@interface ThemeViewController : UITableViewController
@interface AppearanceViewController : UITableViewController

@end

Expand Down
131 changes: 131 additions & 0 deletions app/AppearanceViewController.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
//
// ThemeViewController.m
// iSH
//
// Created by Charlie Melbye on 11/12/18.
//

#import "AppearanceViewController.h"
#import "UserPreferences.h"

static NSString *const ThemeNameCellIdentifier = @"Theme Name";
static NSString *const FontSizeCellIdentifier = @"Font Size";
static NSString *const PreviewCellIdentifier = @"Preview";

@interface AppearanceViewController ()

@end

@implementation AppearanceViewController

- (void)viewDidLoad {
[super viewDidLoad];
[[UserPreferences shared] addObserver:self forKeyPath:@"theme" options:NSKeyValueObservingOptionNew context:nil];
[[UserPreferences shared] addObserver:self forKeyPath:@"fontSize" options:NSKeyValueObservingOptionNew context:nil];
}

- (void)dealloc {
@try {
[[UserPreferences shared] removeObserver:self forKeyPath:@"theme"];
[[UserPreferences shared] removeObserver:self forKeyPath:@"fontSize"];
} @catch (NSException * __unused exception) {}
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
[self.tableView reloadData];
[self setNeedsStatusBarAppearanceUpdate];
}

#pragma mark - Table view data source

enum {
ThemeNameSection,
FontSizeSection,
PreviewSection,
NumberOfSections,
};

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return NumberOfSections;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
switch (section) {
case ThemeNameSection: return Theme.presetNames.count;
case FontSizeSection: return 1;
case PreviewSection: return 1;
default: NSAssert(NO, @"unhandled section"); return 0;
}
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
switch (section) {
case ThemeNameSection: return @"Theme";
case PreviewSection: return @"Preview";
default: return nil;
}
}

- (Theme *)_themeForRow:(NSUInteger)row {
return [Theme presetThemeNamed:Theme.presetNames[row]];
}

- (NSString *)reuseIdentifierForSection:(NSInteger)section {
switch (section) {
case ThemeNameSection: return @"Theme Name";
case FontSizeSection: return @"Font Size";
case PreviewSection: return @"Preview";
default: return nil;
}
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UserPreferences *prefs = [UserPreferences shared];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[self reuseIdentifierForSection:indexPath.section] forIndexPath:indexPath];

switch (indexPath.section) {
case ThemeNameSection:
cell.textLabel.text = Theme.presetNames[indexPath.row];
if ([prefs.theme isEqual:[self _themeForRow:indexPath.row]]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
break;

case FontSizeSection: {
UserPreferences *prefs = [UserPreferences shared];
UILabel *label = [cell viewWithTag:1];
UIStepper *stepper = [cell viewWithTag:2];
label.text = prefs.fontSize.stringValue;
stepper.value = prefs.fontSize.doubleValue;
break;
}

case PreviewSection:
cell.backgroundColor = prefs.theme.backgroundColor;
cell.textLabel.textColor = prefs.theme.foregroundColor;
cell.textLabel.font = [UIFont fontWithName:@"Menlo-Regular" size:prefs.fontSize.doubleValue];
cell.textLabel.text = [NSString stringWithFormat:@"%@:~# ps aux", [UIDevice currentDevice].name];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
break;
}

return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];

switch (indexPath.section) {
case ThemeNameSection:
UserPreferences.shared.theme = [self _themeForRow:indexPath.row];
break;
}
}

- (IBAction)fontSizeChanged:(UIStepper *)sender {
UserPreferences.shared.fontSize = @((int) sender.value);
}

@end
Loading

0 comments on commit d1de392

Please sign in to comment.