-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathDeterminedStepper.swift
100 lines (87 loc) · 2.88 KB
/
DeterminedStepper.swift
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
//
// DeterminedStepper.swift
//
// Made with ❤️ by Novum
//
// Copyright © Telefonica. All rights reserved.
//
import Foundation
import SwiftUI
public struct DeterminedStepper: View {
private enum Constants {
static let height: CGFloat = 24
static let originalHeight: CGFloat = 64
static let borderWidth: CGFloat = 1.5
// The border width should be 1.5, however as we are scaling down the animation,
// we need to adjust the border width. Otherwise, sizes do not match.
static let scaleFactor: CGFloat = Constants.originalHeight / Constants.height
static let scaledBorderWidth: CGFloat = Constants.borderWidth * Constants.scaleFactor
}
@Binding var step: Int
var steps: Int
public init(
step: Binding<Int>,
steps: Int
) {
_step = step
self.steps = steps
}
public var body: some View {
GeometryReader { _ in
HStack {
ForEach(0 ..< steps, id: \.self) { stepIndex in
step(for: stepIndex)
.frame(width: Constants.height, height: Constants.height)
undeterminedStepper(for: stepIndex)
}
}
}
.expandHorizontally()
.frame(height: Constants.height)
}
}
// MARK: Private
private extension DeterminedStepper {
func stepBorderWidth(for step: Int) -> CGFloat {
self.step == step ? 0 : Constants.borderWidth
}
func stepForegroundColor(for step: Int) -> Color {
self.step == step ? .textPrimaryInverse : .textPrimary
}
func stepBackgroundColor(for step: Int) -> Color {
self.step == step ? .controlActivated : .clear
}
@ViewBuilder
func undeterminedStepper(for step: Int) -> some View {
if step + 1 != steps {
UndeterminedStepper(
step: .constant(self.step > step ? 1 : 0),
steps: 1
)
}
}
@ViewBuilder
func step(for step: Int) -> some View {
if self.step > step {
LottieView(
loopMode: .playOnce,
asset: .successAnimation,
color: .controlActivated,
borderWidth: Constants.scaledBorderWidth,
scaleToFit: true
)
.transition(.opacity.animation(.misticaTimingCurve))
} else {
ZStack {
Rectangle()
.fill(stepBackgroundColor(for: step))
.round(radiusStyle: .bar)
Text("\(step + 1)")
.font(.textPreset1(weight: .regular))
.foregroundColor(stepForegroundColor(for: step))
}
.border(radiusStyle: .bar, borderColor: .borderHigh, lineWidth: stepBorderWidth(for: step))
.transition(.opacity.animation(.misticaTimingCurve))
}
}
}