Skip to content

Commit

Permalink
update about view to save settings
Browse files Browse the repository at this point in the history
  • Loading branch information
cmelbye-sc committed Nov 13, 2018
1 parent afff360 commit bc5fc0d
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 9 deletions.
10 changes: 8 additions & 2 deletions app/About.storyboard
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="14313.18" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="0yy-Uo-cZb">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="14460.31" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="0yy-Uo-cZb">
<device id="ipad9_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14283.14"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14460.20"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
Expand Down Expand Up @@ -37,6 +37,9 @@
<switch opaque="NO" contentMode="scaleToFill" horizontalHuggingPriority="750" verticalHuggingPriority="750" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" on="YES" translatesAutoresizingMaskIntoConstraints="NO" id="d8L-Q5-dsF">
<rect key="frame" x="473" y="6" width="49" height="31"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<connections>
<action selector="didSwitchCapsLock:" destination="yJz-O7-jlW" eventType="valueChanged" id="oqY-SQ-J1m"/>
</connections>
</switch>
</subviews>
</tableViewCellContentView>
Expand Down Expand Up @@ -187,6 +190,9 @@
</connections>
</navigationItem>
<connections>
<outlet property="backgroundColorCell" destination="3YF-i1-D5D" id="8Ki-CN-5VZ"/>
<outlet property="fontSizeCell" destination="CTf-zd-xjl" id="AvD-Zl-Y54"/>
<outlet property="foregroundColorCell" destination="dqn-Cd-cNm" id="qLU-S2-Nn7"/>
<outlet property="openGithub" destination="F4i-eC-hQ6" id="66M-sQ-RJR"/>
<outlet property="openTwitter" destination="bge-GA-6p8" id="r0A-lN-0e0"/>
<outlet property="sendFeedback" destination="gMm-4C-5X3" id="nhX-ZH-zyP"/>
Expand Down
5 changes: 1 addition & 4 deletions app/AboutViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@
//

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN
#import "UserPreferences.h"

@interface AboutViewController : UITableViewController

@end

NS_ASSUME_NONNULL_END
70 changes: 67 additions & 3 deletions app/AboutViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,50 @@

#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];
// Do any additional setup after loading the view.
[self _addObservers];
}

- (void)_addObservers {
UserPreferences *prefs = [UserPreferences shared];
NSKeyValueObservingOptions opts = NSKeyValueObservingOptionInitial | 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)openLink:(id)sender {
- (IBAction)didSwitchCapsLock:(id)sender {

}

Expand All @@ -34,7 +62,43 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
[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

0 comments on commit bc5fc0d

Please sign in to comment.