forked from ish-app/ish
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AboutViewController.m
105 lines (90 loc) · 4.03 KB
/
AboutViewController.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
//
// AboutViewController.m
// iSH
//
// Created by Theodore Dubois on 9/23/18.
//
#import "UIApplication+OpenURL.h"
#import "AboutViewController.h"
#import "UserPreferences.h"
@interface AboutViewController ()
@property (weak, nonatomic) IBOutlet UITableViewCell *sendFeedback;
@property (weak, nonatomic) IBOutlet UITableViewCell *openGithub;
@property (weak, nonatomic) IBOutlet UITableViewCell *openTwitter;
@property (weak, nonatomic) IBOutlet UITableViewCell *fontSizeCell;
@property (weak, nonatomic) IBOutlet UITableViewCell *foregroundColorCell;
@property (weak, nonatomic) IBOutlet UITableViewCell *backgroundColorCell;
@end
@implementation AboutViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self _addObservers];
[self _updatePreferenceUI];
}
- (void)_addObservers {
UserPreferences *prefs = [UserPreferences shared];
NSKeyValueObservingOptions opts = NSKeyValueObservingOptionNew;
[prefs addObserver:self forKeyPath:@"mapCapsLockAsControl" options:opts context:nil];
[prefs addObserver:self forKeyPath:@"fontSize" options:opts context:nil];
[prefs addObserver:self forKeyPath:@"foregroundColor" options:opts context:nil];
[prefs addObserver:self forKeyPath:@"backgroundColor" options:opts context:nil];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if ([object isKindOfClass:[UserPreferences class]]) {
[self _updatePreferenceUI];
} else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
- (void)_updatePreferenceUI {
UserPreferences *prefs = [UserPreferences shared];
_fontSizeCell.detailTextLabel.text = prefs.fontSize.stringValue;
_foregroundColorCell.detailTextLabel.text = prefs.foregroundColor;
_backgroundColorCell.detailTextLabel.text = prefs.backgroundColor;
}
- (IBAction)didSwitchCapsLock:(id)sender {
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell == self.sendFeedback) {
[UIApplication openURL:@"mailto:tblodt@icloud.com"];
} else if (cell == self.openGithub) {
[UIApplication openURL:@"https://github.com/tbodt/ish"];
} else if (cell == self.openTwitter) {
[UIApplication openURL:@"https://twitter.com/tblodt"];
} else if (cell == self.fontSizeCell ||
cell == self.foregroundColorCell ||
cell == self.backgroundColorCell) {
[self _showInputForCell:cell];
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (void)_showInputForCell:(UITableViewCell *)cell {
UIAlertController *alert = [UIAlertController
alertControllerWithTitle:cell.textLabel.text
message:@"(Enter a CSS compatible value)"
preferredStyle:UIAlertControllerStyleAlert];
[alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder = cell.detailTextLabel.text;
}];
[alert addAction:[UIAlertAction actionWithTitle:@"Save" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSString *value = [[[alert textFields] firstObject] text];
if ([value isEqualToString:@""]) {
return;
}
UserPreferences *prefs = [UserPreferences shared];
if (cell == self.fontSizeCell) {
NSUInteger fontSize = [value integerValue];
if (fontSize) {
[prefs setFontSize:@(fontSize)];
}
} else if (cell == self.foregroundColorCell) {
[prefs setForegroundColor:value];
} else if (cell == self.backgroundColorCell) {
[prefs setBackgroundColor:value];
}
}]];
[alert addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil]];
[self presentViewController:alert animated:YES completion:nil];
}
@end