Skip to content

Commit b8e7719

Browse files
committed
Slider persists in settings
1 parent 09d15cb commit b8e7719

File tree

4 files changed

+31
-37
lines changed

4 files changed

+31
-37
lines changed

tips/Base.lproj/Main.storyboard

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -162,13 +162,6 @@
162162
<rect key="frame" x="0.0" y="0.0" width="320" height="568"/>
163163
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
164164
<subviews>
165-
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="Default Tip Amount:" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="zRs-pv-2OM">
166-
<rect key="frame" x="20" y="108" width="153" height="21"/>
167-
<animations/>
168-
<fontDescription key="fontDescription" type="system" pointSize="17"/>
169-
<color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="calibratedRGB"/>
170-
<nil key="highlightedColor"/>
171-
</label>
172165
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="Dark Theme" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="B9C-dq-fmJ">
173166
<rect key="frame" x="20" y="196" width="94" height="21"/>
174167
<animations/>
@@ -183,8 +176,15 @@
183176
<action selector="hitSwitch" destination="gAk-LZ-1lC" eventType="valueChanged" id="Kyd-CY-WRX"/>
184177
</connections>
185178
</switch>
179+
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="Default Tip Amount:" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="zRs-pv-2OM">
180+
<rect key="frame" x="20" y="93" width="280" height="21"/>
181+
<animations/>
182+
<fontDescription key="fontDescription" type="system" pointSize="17"/>
183+
<color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="calibratedRGB"/>
184+
<nil key="highlightedColor"/>
185+
</label>
186186
<view contentMode="scaleToFill" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="2sf-d2-OIb">
187-
<rect key="frame" x="0.0" y="137" width="320" height="50"/>
187+
<rect key="frame" x="0.0" y="122" width="320" height="50"/>
188188
<subviews>
189189
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="5" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="5tG-Ui-FyL">
190190
<rect key="frame" x="29" y="0.0" width="11" height="21"/>
@@ -211,6 +211,7 @@
211211
<rect key="frame" x="18" y="18" width="284" height="31"/>
212212
<animations/>
213213
<connections>
214+
<action selector="changedTip:" destination="gAk-LZ-1lC" eventType="valueChanged" id="G16-nW-oCY"/>
214215
<action selector="sliderChanged:" destination="BYZ-38-t0r" eventType="valueChanged" id="rIb-qw-HXJ"/>
215216
</connections>
216217
</slider>
@@ -232,13 +233,6 @@
232233
<animations/>
233234
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
234235
</view>
235-
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="25%" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="vhC-vJ-lpk">
236-
<rect key="frame" x="181" y="108" width="33" height="21"/>
237-
<animations/>
238-
<fontDescription key="fontDescription" type="system" pointSize="17"/>
239-
<color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="calibratedRGB"/>
240-
<nil key="highlightedColor"/>
241-
</label>
242236
</subviews>
243237
<animations/>
244238
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>

tips/SettingsViewController.swift

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,18 @@ import UIKit
1212
class SettingsViewController: UIViewController {
1313

1414
@IBOutlet weak var tipLabel: UILabel!
15-
@IBOutlet weak var tipSeg: UISegmentedControl!
1615
@IBOutlet weak var themeLabel: UILabel!
1716
@IBOutlet weak var themeSwitch: UISwitch!
17+
@IBOutlet weak var sliderView: UIView!
18+
@IBOutlet weak var tipSlider: UISlider!
1819

1920
private func applyTheme() {
20-
view.backgroundColor = getThemeBackgroundColor()
21-
let foregroundColor = getThemeForegroundColor()
22-
tipLabel.textColor = foregroundColor
23-
tipSeg.tintColor = getThemeSegColor()
24-
themeLabel.textColor = foregroundColor
21+
let backgroundColor = getThemeBackgroundColor()
22+
view.backgroundColor = backgroundColor
23+
sliderView.backgroundColor = backgroundColor
24+
25+
updateForegrounds(view)
26+
tipSlider.minimumTrackTintColor = getThemeSegColor()
2527
}
2628

2729
override func viewWillAppear(animated: Bool) {
@@ -32,7 +34,9 @@ class SettingsViewController: UIViewController {
3234
super.viewDidLoad()
3335

3436
// Do any additional setup after loading the view.
35-
tipSeg.selectedSegmentIndex = getDefaultTipIndex()
37+
tipSlider.value = getDefaultTip()
38+
changedTip(tipSlider)
39+
3640
themeSwitch.setOn(isThemeDark(), animated: false)
3741
}
3842

@@ -41,8 +45,11 @@ class SettingsViewController: UIViewController {
4145
// Dispose of any resources that can be recreated.
4246
}
4347

44-
@IBAction func changedTip(sender: AnyObject) {
45-
setTipIndex(tipSeg.selectedSegmentIndex)
48+
@IBAction func changedTip(slider: UISlider) {
49+
let newValue = roundf(slider.value)
50+
slider.setValue(newValue, animated: true)
51+
tipLabel.text = "Default Tip Amount: \(newValue)%"
52+
setDefaultTip(newValue)
4653
}
4754

4855
@IBAction func hitSwitch() {

tips/Utilities.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ private let cacheExpiryMins = 10.0
1919
private let tipPercentages = [5.0, 10.0, 15.0, 20.0, 25.0]
2020
let defaults = NSUserDefaults.standardUserDefaults()
2121

22-
func getDefaultTipIndex() -> Int {
23-
return defaults.integerForKey(DEFAULT_TIP_KEY)
22+
func getDefaultTip() -> Float {
23+
return defaults.floatForKey(DEFAULT_TIP_KEY)
2424
}
2525

26-
func setTipIndex(index: Int) {
27-
defaults.setInteger(index, forKey: DEFAULT_TIP_KEY)
26+
func setDefaultTip(tip: Float) {
27+
defaults.setFloat(tip, forKey: DEFAULT_TIP_KEY)
2828
defaults.synchronize()
2929
}
3030

tips/ViewController.swift

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ class ViewController: UIViewController {
1717
@IBOutlet weak var separatorView: UIView!
1818
@IBOutlet weak var totalTitleLabel: UILabel!
1919
@IBOutlet weak var totalLabel: UILabel!
20-
// @IBOutlet weak var tipSeg: UISegmentedControl!
2120
@IBOutlet weak var tipSlider: UISlider!
2221
@IBOutlet weak var sliderView: UIView!
2322

@@ -31,7 +30,6 @@ class ViewController: UIViewController {
3130
self.separatorView.alpha = alpha
3231
self.totalTitleLabel.alpha = alpha
3332
self.totalLabel.alpha = alpha
34-
// self.tipSeg.alpha = alpha
3533
self.sliderView.alpha = alpha
3634
}
3735
}
@@ -42,11 +40,8 @@ class ViewController: UIViewController {
4240
sliderView.backgroundColor = backgroundColor
4341

4442
updateForegrounds(view)
45-
43+
billField.attributedPlaceholder = NSAttributedString(string: currencyFormatter.currencySymbol, attributes: [NSForegroundColorAttributeName:UIColor ( red: 0.498, green: 0.498, blue: 0.498, alpha: 1.0 )])
4644
tipSlider.minimumTrackTintColor = getThemeSegColor()
47-
48-
let foregroundColor = getThemeForegroundColor()
49-
billField.attributedPlaceholder = NSAttributedString(string: currencyFormatter.currencySymbol, attributes: [NSForegroundColorAttributeName:foregroundColor])
5045
}
5146

5247
override func viewWillAppear(animated: Bool) {
@@ -70,7 +65,7 @@ class ViewController: UIViewController {
7065
}
7166

7267
override func viewDidAppear(animated: Bool) {
73-
// tipSeg.selectedSegmentIndex = getDefaultTipIndex()
68+
tipSlider.value = getDefaultTip()
7469
billChanged()
7570
billField.becomeFirstResponder()
7671
}
@@ -86,8 +81,6 @@ class ViewController: UIViewController {
8681
if (valuesHidden != billText.isEmpty) {
8782
valuesHidden = !valuesHidden
8883
updateVisibility(valuesHidden)
89-
90-
billField.attributedPlaceholder = NSAttributedString(string: currencyFormatter.currencySymbol, attributes: [NSForegroundColorAttributeName:getThemeForegroundColor()])
9184
}
9285

9386
let billAmount = (billText as NSString).doubleValue

0 commit comments

Comments
 (0)