Skip to content
This repository was archived by the owner on Oct 21, 2022. It is now read-only.

Commit 4e19bfd

Browse files
committed
Configs
1 parent 58895d8 commit 4e19bfd

File tree

6 files changed

+121
-56
lines changed

6 files changed

+121
-56
lines changed

TimePicker.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
1EACE58B1EC9ECFF00A26022 /* TimeFormat.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1EACE5871EC9ECFF00A26022 /* TimeFormat.swift */; };
1717
1EACE58E1EC9ED1800A26022 /* UIGestureRecognizer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1EACE58D1EC9ED1800A26022 /* UIGestureRecognizer.swift */; };
1818
1EACE5901EC9EDB000A26022 /* TimeInterval.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1EACE58F1EC9EDB000A26022 /* TimeInterval.swift */; };
19+
1ECB62581EDD67BA00612A25 /* TimePickerConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1ECB62571EDD67BA00612A25 /* TimePickerConfig.swift */; };
1920
9E3F66051E55E7AC007509DF /* TimePicker.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E3F66011E55E7AC007509DF /* TimePicker.swift */; };
2021
9E3F66081E55E83A007509DF /* TimePicker.h in Headers */ = {isa = PBXBuildFile; fileRef = 9E3F66071E55E83A007509DF /* TimePicker.h */; settings = {ATTRIBUTES = (Public, ); }; };
2122
9E3F661F1E55E8B6007509DF /* TimePickerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E3F661E1E55E8B6007509DF /* TimePickerTests.swift */; };
@@ -67,6 +68,7 @@
6768
1EACE5871EC9ECFF00A26022 /* TimeFormat.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TimeFormat.swift; sourceTree = "<group>"; };
6869
1EACE58D1EC9ED1800A26022 /* UIGestureRecognizer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UIGestureRecognizer.swift; sourceTree = "<group>"; };
6970
1EACE58F1EC9EDB000A26022 /* TimeInterval.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TimeInterval.swift; sourceTree = "<group>"; };
71+
1ECB62571EDD67BA00612A25 /* TimePickerConfig.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TimePickerConfig.swift; sourceTree = "<group>"; };
7072
9E3F65E31E55E727007509DF /* TimePicker.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = TimePicker.framework; sourceTree = BUILT_PRODUCTS_DIR; };
7173
9E3F66011E55E7AC007509DF /* TimePicker.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TimePicker.swift; sourceTree = "<group>"; };
7274
9E3F66031E55E7AC007509DF /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
@@ -178,6 +180,7 @@
178180
1EACE5831EC9ECFF00A26022 /* Extensions */,
179181
1EACE5851EC9ECFF00A26022 /* Utils */,
180182
9E3F66011E55E7AC007509DF /* TimePicker.swift */,
183+
1ECB62571EDD67BA00612A25 /* TimePickerConfig.swift */,
181184
);
182185
path = Classes;
183186
sourceTree = "<group>";
@@ -377,6 +380,7 @@
377380
buildActionMask = 2147483647;
378381
files = (
379382
9E3F66051E55E7AC007509DF /* TimePicker.swift in Sources */,
383+
1ECB62581EDD67BA00612A25 /* TimePickerConfig.swift in Sources */,
380384
1EACE5901EC9EDB000A26022 /* TimeInterval.swift in Sources */,
381385
1EACE58A1EC9ECFF00A26022 /* TimeCalculator.swift in Sources */,
382386
1EACE58E1EC9ED1800A26022 /* UIGestureRecognizer.swift in Sources */,

TimePicker/Classes/Extensions/NSLayoutConstraints.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,13 @@ extension NSLayoutConstraint {
4646
)
4747
}
4848

49+
static func alignVertically(view: UIView, above bottomView: UIView, distance: CGFloat = 0) -> [NSLayoutConstraint] {
50+
return constraints(
51+
withVisualFormat: "V:[view]-distance-[bottomView]",
52+
options: NSLayoutFormatOptions(),
53+
metrics: ["distance": distance],
54+
views: ["view": view, "bottomView": bottomView]
55+
)
56+
}
57+
4958
}

TimePicker/Classes/TimePicker.swift

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,33 +16,24 @@ open class TimePicker: UIView {
1616
return calculator.time
1717
}
1818

19-
@IBInspectable public var textColor: UIColor = .black {
20-
didSet { updateColors() }
21-
}
22-
@IBInspectable public var textFont: UIFont = .systemFont(ofSize: 20) {
23-
didSet { updateFonts() }
24-
}
25-
@IBInspectable public var isShakeToResetEnabled: Bool = false {
26-
didSet {}
27-
}
28-
@IBInspectable public var shakeToResetColor: UIColor = .lightGray {
29-
didSet {}
30-
}
31-
@IBInspectable public var shakeToResetFont: UIFont = .systemFont(ofSize: 15) {
32-
didSet {}
33-
}
34-
@IBInspectable public var shakeToResetText: String = "Shake to reset" {
35-
didSet {}
19+
var config = TimePickerConfig() {
20+
didSet {
21+
calculator.config = config.time
22+
23+
updateColors()
24+
updateFonts()
25+
updateTime()
26+
}
3627
}
37-
@IBInspectable public var isHapticFeedbackEnabled: Bool = true
3828

3929
fileprivate let hourLabel = UILabel()
4030
fileprivate let timeLabel = UILabel()
4131
fileprivate let colonLabel = UILabel()
4232
fileprivate let periodLabel = UILabel()
33+
fileprivate let resetLabel = UILabel()
4334

4435
fileprivate lazy var calculator: TimeCalculator = {
45-
let calculator = TimeCalculator()
36+
let calculator = TimeCalculator(config: self.config.time)
4637

4738
calculator.didUpdateTime = {
4839
[unowned self]
@@ -129,9 +120,23 @@ extension TimePicker {
129120
}
130121

131122
fileprivate func updateColors() {
123+
hourLabel.textColor = config.text.color
124+
timeLabel.textColor = config.text.color
125+
colonLabel.textColor = config.text.color
126+
periodLabel.textColor = config.text.color
127+
resetLabel.textColor = config.reset?.color
132128
}
133129

134130
fileprivate func updateFonts() {
131+
hourLabel.font = config.text.font
132+
timeLabel.font = config.text.font
133+
colonLabel.font = config.text.font
134+
periodLabel.font = config.text.font
135+
resetLabel.font = config.reset?.font
136+
}
137+
138+
fileprivate func updateTexts() {
139+
resetLabel.text = config.reset?.label
135140
}
136141

137142
fileprivate func updateTime() {
@@ -175,6 +180,13 @@ extension TimePicker {
175180
NSLayoutConstraint.verticallyCentered(view: periodLabel, in: self)
176181
)
177182
)
183+
addSubview(
184+
resetLabel,
185+
constraints: (
186+
NSLayoutConstraint.horizontallyCentered(view: resetLabel, in: self) +
187+
NSLayoutConstraint.alignVertically(view: resetLabel, above: self, distance: 40)
188+
)
189+
)
178190
}
179191

180192
private func addSubview(_ view: UIView, constraints: [NSLayoutConstraint]) {
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
//
2+
// TimePickerConfig.swift
3+
// TimePicker
4+
//
5+
// Created by Oleh Stasula on 30/05/2017.
6+
// Copyright © 2017 Oleh Stasula. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
struct TimePickerConfig {
12+
struct Reset {
13+
let color: UIColor
14+
let font: UIFont
15+
let label: String
16+
17+
static let reset = Reset(color: .gray, font: .systemFont(ofSize: 15), label: "Shake to reset")
18+
}
19+
struct Text {
20+
let color: UIColor
21+
let font: UIFont
22+
23+
static let text = Text(color: .black, font: .systemFont(ofSize: 28, weight: UIFontWeightSemibold))
24+
}
25+
struct Time {
26+
static let timeRange = TimeInterval(0)...(24 * 60 * 60)
27+
static let timeStepRange = TimeInterval(1)...30
28+
static let initialTime = TimeInterval(8) * 60 * 60
29+
30+
enum Format {
31+
case auto, international, period
32+
}
33+
34+
let initial: TimeInterval
35+
let step: TimeInterval
36+
let format: Format
37+
38+
init(initial: TimeInterval = Time.initialTime, step: TimeInterval = Time.timeStepRange.lowerBound, format: Format = .auto) {
39+
self.initial = max(Time.timeRange.lowerBound, min(initial, Time.timeRange.upperBound))
40+
self.step = max(Time.timeStepRange.lowerBound, min(step, Time.timeStepRange.upperBound))
41+
self.format = format
42+
}
43+
44+
static let time = Time(initial: Time.initialTime, step: Time.timeStepRange.lowerBound, format: .auto)
45+
}
46+
47+
let reset: Reset? = .reset
48+
let text: Text = .text
49+
let time: Time = .time
50+
}

TimePicker/Classes/Utils/TimeCalculator.swift

Lines changed: 25 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,68 +8,58 @@
88

99
import Foundation
1010

11-
fileprivate let TimeRange = TimeInterval(0)...(24 * 60 * 60)
12-
fileprivate let TimeStepRange = TimeInterval(1)...30
13-
fileprivate let InitialTime = TimeInterval(8) * 60 * 60
14-
1511
final class TimeCalculator {
1612

1713
var didUpdateTime: ((TimeInterval) -> ())?
1814

19-
fileprivate(set) var time: TimeInterval = InitialTime {
15+
var config: TimePickerConfig.Time {
2016
didSet {
21-
guard oldValue.timeFormat() != time.timeFormat() else { return }
22-
23-
didUpdateTime?(time)
24-
}
25-
}
26-
27-
private var initialTimeValue = InitialTime
28-
var initialTime: TimeInterval {
29-
get {
30-
return initialTimeValue
31-
}
32-
set {
33-
let updateTime = time == initialTimeValue
34-
35-
initialTimeValue = max(TimeRange.lowerBound, min(newValue, TimeRange.upperBound))
17+
if time == oldValue.initial {
18+
time = config.initial
19+
}
3620

37-
if updateTime {
38-
time = initialTimeValue
21+
if step == oldValue.step {
22+
step = config.step
23+
}
24+
else if step == -oldValue.step {
25+
step = -config.step
3926
}
4027
}
4128
}
4229

43-
private var minTimeChangeStepValue = TimeStepRange.lowerBound
44-
var minTimeChangeStep: TimeInterval {
45-
get {
46-
return minTimeChangeStepValue
47-
}
48-
set {
49-
minTimeChangeStepValue = max(TimeStepRange.lowerBound, min(newValue, TimeStepRange.upperBound))
30+
fileprivate(set) var time: TimeInterval {
31+
didSet {
32+
guard oldValue.timeFormat() != time.timeFormat() else { return }
33+
34+
didUpdateTime?(time)
5035
}
5136
}
5237

53-
fileprivate var timer: Timer?
5438
fileprivate var step: TimeInterval?
39+
fileprivate var timer: Timer?
40+
41+
init(config: TimePickerConfig.Time) {
42+
self.config = config
43+
self.time = config.initial
44+
}
5545

5646
}
5747

5848
extension TimeCalculator {
5949

6050
func increment() {
61-
time += minTimeChangeStep.minutes
51+
time += config.step.minutes
6252

6353
if timer != nil {
64-
step = minTimeChangeStep
54+
step = config.step
6555
}
6656
}
6757

6858
func decrement() {
69-
time -= minTimeChangeStep.minutes
59+
time -= config.step.minutes
7060

7161
if timer != nil {
72-
step = -minTimeChangeStep
62+
step = -config.step
7363
}
7464
}
7565

@@ -116,7 +106,7 @@ extension TimeCalculator {
116106
}
117107

118108
var t = time + TimeInterval(change * 100 * multiplier).minutes
119-
t.normalize(min: TimeRange.lowerBound, max: TimeRange.upperBound)
109+
t.normalize(min: TimePickerConfig.Time.timeRange.lowerBound, max: TimePickerConfig.Time.timeRange.upperBound)
120110
time = t
121111
}
122112

TimePickerExample/Resources/Base.lproj/Main.storyboard

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="11762" systemVersion="16D32" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="12120" systemVersion="16E195" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
33
<device id="retina4_7" orientation="portrait">
44
<adaptation id="fullscreen"/>
55
</device>
66
<dependencies>
77
<deployment identifier="iOS"/>
8-
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="11757"/>
8+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="12088"/>
99
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
1010
</dependencies>
1111
<scenes>

0 commit comments

Comments
 (0)