-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathOSIABNavigationView.swift
More file actions
175 lines (156 loc) · 6.64 KB
/
OSIABNavigationView.swift
File metadata and controls
175 lines (156 loc) · 6.64 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import SwiftUI
private struct OSIABNavigationButton: View {
/// Handler to trigger when the button is pressed.
private let buttonPressed: () -> Void
/// The icon to set the button with.
private let iconName: String
/// Indicates if the button should appeared as enabled or not.
private let isDisabled: Bool
/// Constructor method.
/// - Parameters:
/// - buttonPressed: Handler to trigger when the button is pressed.
/// - iconName: The icon to set the button with.
/// - isDisabled: Indicates if the button should appeared as enabled or not.
init(_ buttonPressed: @escaping () -> Void, iconName: String, isDisabled: Bool) {
self.buttonPressed = buttonPressed
self.iconName = iconName
self.isDisabled = isDisabled
}
var body: some View {
Button(action: buttonPressed, label: {
Image(systemName: iconName)
})
.buttonStyle(.plain)
.disabled(isDisabled)
}
}
/// View related with displaying the Navigation items (buttons + URL address)
struct OSIABNavigationView: View {
/// Indicates if the navigations should be displayed on the toolbar.
private let showNavigationButtons: Bool
/// Handler to trigger when the back button is pressed.
private let backButtonPressed: () -> Void
/// Indicates if the back button is available for pressing.
private let backButtonEnabled: Bool
/// Handler to trigger when the forward button is pressed.
private let forwardButtonPressed: () -> Void
/// Indicates if the forward button is available for pressing.
private let forwardButtonEnabled: Bool
/// The current adress label being displayed on the screen. Empty string indicates that the address will not be displayed.
private let addressLabel: String
/// Alignment to apply to the URL address.
private let addressLabelAlignment: Alignment
/// Indicates if the navigation buttons should stay on the order defined or adapt.
private let buttonLayoutDirection: OSIABLayoutDirectionEnum
/// Constructor method.
/// - Parameters:
/// - showNavigationButtons: Indicates if the navigations should be displayed on the toolbar.
/// - backButtonPressed: Handler to trigger when the back button is pressed.
/// - backButtonEnabled: Indicates if the back button is available for pressing.
/// - forwardButtonPressed: Handler to trigger when the forward button is pressed.
/// - forwardButtonEnabled: Indicates if the forward button is available for pressing.
/// - addressLabel: The current adress label being displayed on the screen. Empty string indicates that the address will not be displayed.
/// - addressLabelAlignment: Alignment to apply to the URL address.
init(
showNavigationButtons: Bool,
backButtonPressed: @escaping () -> Void,
backButtonEnabled: Bool,
forwardButtonPressed: @escaping () -> Void,
forwardButtonEnabled: Bool,
addressLabel: String,
addressLabelAlignment: Alignment,
buttonLayoutDirection: OSIABLayoutDirectionEnum
) {
self.showNavigationButtons = showNavigationButtons
self.backButtonPressed = backButtonPressed
self.backButtonEnabled = backButtonEnabled
self.forwardButtonPressed = forwardButtonPressed
self.forwardButtonEnabled = forwardButtonEnabled
self.addressLabel = addressLabel
self.addressLabelAlignment = addressLabelAlignment
self.buttonLayoutDirection = buttonLayoutDirection
}
var body: some View {
HStack {
if showNavigationButtons {
HStack {
OSIABNavigationButton(
backButtonPressed,
iconName: "chevron.backward",
isDisabled: !backButtonEnabled
)
OSIABNavigationButton(
forwardButtonPressed,
iconName: "chevron.forward",
isDisabled: !forwardButtonEnabled
)
}
.layoutDirection(buttonLayoutDirection)
Spacer()
}
if !addressLabel.isEmpty {
Text(addressLabel)
.lineLimit(1)
.allowsHitTesting(false)
.frame(maxWidth: .infinity, alignment: addressLabelAlignment)
}
}
}
}
// MARK: - Preview Helper View
private struct OSIABTestNavigationView: View {
@State private var buttonPressedText = "No Button Pressed."
private let showNavigationButtons: Bool
private let backButtonEnabled: Bool
private let forwardButtonEnabled: Bool
private let addressLabel: String = "URL Address"
init(
showNavigationButtons: Bool = true,
backButtonEnabled: Bool = false,
forwardButtonEnabled: Bool = false
) {
self.showNavigationButtons = showNavigationButtons
self.backButtonEnabled = backButtonEnabled
self.forwardButtonEnabled = forwardButtonEnabled
}
var body: some View {
VStack {
OSIABNavigationView(
showNavigationButtons: showNavigationButtons,
backButtonPressed: {
buttonPressedText = "Back Button Pressed."
},
backButtonEnabled: backButtonEnabled,
forwardButtonPressed: {
buttonPressedText = "Forward Button Pressed."
},
forwardButtonEnabled: forwardButtonEnabled,
addressLabel: addressLabel,
addressLabelAlignment: showNavigationButtons ? .trailing : .center,
buttonLayoutDirection: .fixed(value: .leftToRight)
)
.padding()
.background(Color.secondary.opacity(0.3))
Spacer()
Text(buttonPressedText)
Spacer()
}
}
}
struct OSIABNavigationView_Previews: PreviewProvider {
static var previews: some View {
// Default - Light Mode
OSIABTestNavigationView()
// Default - Dark Mode
OSIABTestNavigationView()
.preferredColorScheme(.dark)
// Show Navigation Buttons Disabled
OSIABTestNavigationView(showNavigationButtons: false)
// Back Button Enabled
OSIABTestNavigationView(backButtonEnabled: true)
// Forward Button Enabled
OSIABTestNavigationView(forwardButtonEnabled: true)
// Both Buttons Enabled
OSIABTestNavigationView(backButtonEnabled: true, forwardButtonEnabled: true)
}
}