Skip to content

Commit 3fd70eb

Browse files
authored
Add reveal password button and use a rounded checkbox (#6268)
* Adds the reveal password icon to RoundedBorderTextField. * Use a rounded checkmark for the terms toggle style.
1 parent 10f250d commit 3fd70eb

File tree

9 files changed

+138
-44
lines changed

9 files changed

+138
-44
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"images" : [
3+
{
4+
"filename" : "authentication_reveal_password.svg",
5+
"idiom" : "universal"
6+
}
7+
],
8+
"info" : {
9+
"author" : "xcode",
10+
"version" : 1
11+
},
12+
"properties" : {
13+
"preserves-vector-representation" : true
14+
}
15+
}
Loading

Riot/Generated/Images.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ internal class Asset: NSObject {
3333
internal static let authenticationEmailIcon = ImageAsset(name: "authentication_email_icon")
3434
internal static let authenticationMsisdnIcon = ImageAsset(name: "authentication_msisdn_icon")
3535
internal static let authenticationPasswordIcon = ImageAsset(name: "authentication_password_icon")
36+
internal static let authenticationRevealPassword = ImageAsset(name: "authentication_reveal_password")
3637
internal static let authenticationServerSelectionIcon = ImageAsset(name: "authentication_server_selection_icon")
3738
internal static let authenticationSsoIconApple = ImageAsset(name: "authentication_sso_icon_apple")
3839
internal static let authenticationSsoIconFacebook = ImageAsset(name: "authentication_sso_icon_facebook")

RiotSwiftUI/Modules/Authentication/Terms/View/AuthenticationTermsToggleStyle.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,9 @@ struct AuthenticationTermsToggleStyle: ToggleStyle {
2222

2323
func makeBody(configuration: Configuration) -> some View {
2424
Button { configuration.isOn.toggle() } label: {
25-
Image(systemName: configuration.isOn ? "checkmark.square.fill" : "square")
25+
Image(systemName: configuration.isOn ? "checkmark.circle.fill" : "circle")
2626
.font(.title3.weight(.regular))
27-
.imageScale(.large)
28-
.foregroundColor(configuration.isOn ? theme.colors.accent : theme.colors.tertiaryContent)
27+
.foregroundColor(theme.colors.accent)
2928
}
3029
.buttonStyle(.plain)
3130
}

RiotSwiftUI/Modules/Common/Util/ClearViewModifier.swift

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,34 +16,14 @@
1616

1717
import SwiftUI
1818

19-
@available(iOS 14.0, *)
20-
extension ThemableTextField {
21-
/// Adds a clear button to the text field
22-
/// - Parameters:
23-
/// - show: A boolean that can be used to dynamically show/hide the button. Defaults to `true`.
24-
/// - text: The text for the clear button to clear.
25-
/// - alignment: The vertical alignment of the button in the text field. Default to `center`
26-
@ViewBuilder
27-
func showClearButton(_ show: Bool = true, text: Binding<String>, alignment: VerticalAlignment = .center) -> some View {
28-
if show {
29-
modifier(ClearViewModifier(alignment: alignment, text: text))
30-
} else {
31-
self
32-
}
33-
}
34-
}
35-
36-
@available(iOS 14.0, *)
3719
extension ThemableTextEditor {
3820
func showClearButton(text: Binding<String>, alignment: VerticalAlignment = .top) -> some View {
3921
return modifier(ClearViewModifier(alignment: alignment, text: text))
4022
}
4123
}
4224

4325
/// `ClearViewModifier` aims to add a clear button (e.g. `x` button) on the right side of any text editing view
44-
@available(iOS 14.0, *)
45-
struct ClearViewModifier: ViewModifier
46-
{
26+
struct ClearViewModifier: ViewModifier {
4727
// MARK: - Properties
4828

4929
let alignment: VerticalAlignment
@@ -58,8 +38,7 @@ struct ClearViewModifier: ViewModifier
5838

5939
// MARK: - Public
6040

61-
public func body(content: Content) -> some View
62-
{
41+
public func body(content: Content) -> some View {
6342
HStack(alignment: alignment) {
6443
content
6544
if !text.isEmpty {
@@ -70,7 +49,9 @@ struct ClearViewModifier: ViewModifier
7049
.renderingMode(.template)
7150
.foregroundColor(theme.colors.quarterlyContent)
7251
}
73-
.padding(EdgeInsets(top: alignment == .top ? 8 : 0, leading: 0, bottom: alignment == .bottom ? 8 : 0, trailing: 8))
52+
.padding(.top, alignment == .top ? 8 : 0)
53+
.padding(.bottom, alignment == .bottom ? 8 : 0)
54+
.padding(.trailing, 12)
7455
}
7556
}
7657
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//
2+
// Copyright 2021 New Vector Ltd
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
//
16+
17+
import SwiftUI
18+
19+
/// Adds a reveal password button (e.g. an eye button) on the
20+
/// right side of the view. For use with `ThemableTextField`.
21+
struct PasswordButtonModifier: ViewModifier {
22+
23+
// MARK: - Properties
24+
25+
let text: String
26+
@Binding var isSecureTextVisible: Bool
27+
let alignment: VerticalAlignment
28+
29+
// MARK: - Private
30+
31+
@Environment(\.theme) private var theme: ThemeSwiftUI
32+
@ScaledMetric private var iconSize = 16
33+
34+
// MARK: - Public
35+
36+
public func body(content: Content) -> some View {
37+
HStack(alignment: .center) {
38+
content
39+
40+
if !text.isEmpty {
41+
Button { isSecureTextVisible.toggle() } label: {
42+
Image(Asset.Images.authenticationRevealPassword.name)
43+
.renderingMode(.template)
44+
.resizable()
45+
.frame(width: iconSize, height: iconSize)
46+
.foregroundColor(theme.colors.secondaryContent)
47+
}
48+
.padding(.top, alignment == .top ? 8 : 0)
49+
.padding(.bottom, alignment == .bottom ? 8 : 0)
50+
.padding(.trailing, 12)
51+
}
52+
}
53+
}
54+
}

RiotSwiftUI/Modules/Common/Util/RoundedBorderTextField.swift

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
import SwiftUI
1818

19-
20-
@available(iOS 14.0, *)
2119
struct RoundedBorderTextField: View {
2220

2321
// MARK: - Properties
@@ -30,14 +28,15 @@ struct RoundedBorderTextField: View {
3028
var isFirstResponder = false
3129

3230
var configuration: UIKitTextInputConfiguration = UIKitTextInputConfiguration()
31+
@State var isSecureTextVisible = false
3332

3433
var onTextChanged: ((String) -> Void)? = nil
3534
var onEditingChanged: ((Bool) -> Void)? = nil
3635
var onCommit: (() -> Void)? = nil
3736

3837
// MARK: Private
3938

40-
@State private var editing = false
39+
@State private var isEditing = false
4140

4241
@Environment(\.theme) private var theme: ThemeSwiftUI
4342
@Environment(\.isEnabled) private var isEnabled
@@ -51,7 +50,7 @@ struct RoundedBorderTextField: View {
5150
.foregroundColor(theme.colors.primaryContent)
5251
.font(theme.fonts.subheadline)
5352
.multilineTextAlignment(.leading)
54-
.padding(EdgeInsets(top: 0, leading: 0, bottom: 8, trailing: 0))
53+
.padding(.bottom, 8)
5554
}
5655

5756
ZStack(alignment: .leading) {
@@ -63,14 +62,17 @@ struct RoundedBorderTextField: View {
6362
.accessibilityHidden(true)
6463
}
6564

66-
ThemableTextField(placeholder: "", text: $text, configuration: configuration, onEditingChanged: { edit in
67-
self.editing = edit
68-
onEditingChanged?(edit)
69-
}, onCommit: {
65+
ThemableTextField(placeholder: "",
66+
text: $text,
67+
configuration: configuration,
68+
isSecureTextVisible: $isSecureTextVisible) { isEditing in
69+
self.isEditing = isEditing
70+
onEditingChanged?(isEditing)
71+
} onCommit: {
7072
onCommit?()
71-
})
73+
}
7274
.makeFirstResponder(isFirstResponder)
73-
.showClearButton(isEnabled, text: $text)
75+
.addButton(isEnabled)
7476
.onChange(of: text) { newText in
7577
onTextChanged?(newText)
7678
}
@@ -81,25 +83,39 @@ struct RoundedBorderTextField: View {
8183
}
8284
.padding(EdgeInsets(top: 8, leading: 8, bottom: 8, trailing: text.isEmpty ? 8 : 0))
8385
.background(RoundedRectangle(cornerRadius: 8).fill(theme.colors.background))
84-
.overlay(RoundedRectangle(cornerRadius: 8)
85-
.stroke(editing ? theme.colors.accent : (footerText != nil && isError ? theme.colors.alert : theme.colors.quinaryContent), lineWidth: editing || (footerText != nil && isError) ? 2 : 1))
86+
.overlay(RoundedRectangle(cornerRadius: 8).stroke(borderColor, lineWidth: borderWidth))
8687

8788
if let footerText = self.footerText {
8889
Text(footerText)
8990
.foregroundColor(isError ? theme.colors.alert : theme.colors.tertiaryContent)
9091
.font(theme.fonts.footnote)
9192
.multilineTextAlignment(.leading)
92-
.padding(EdgeInsets(top: 8, leading: 0, bottom: 0, trailing: 0))
93+
.padding(.top, 8)
9394
.transition(.opacity)
9495
}
9596
}
9697
.animation(.easeOut(duration: 0.2))
9798
}
99+
100+
/// The text field's border color.
101+
private var borderColor: Color {
102+
if isEditing {
103+
return theme.colors.accent
104+
} else if footerText != nil && isError {
105+
return theme.colors.alert
106+
} else {
107+
return theme.colors.quinaryContent
108+
}
109+
}
110+
111+
/// The text field's border width.
112+
private var borderWidth: CGFloat {
113+
isEditing || (footerText != nil && isError) ? 2 : 1
114+
}
98115
}
99116

100117
// MARK: - Previews
101118

102-
@available(iOS 14.0, *)
103119
struct TextFieldWithError_Previews: PreviewProvider {
104120
static var previews: some View {
105121

@@ -118,6 +134,11 @@ struct TextFieldWithError_Previews: PreviewProvider {
118134
RoundedBorderTextField(title: "A title", placeHolder: "A placeholder", text: .constant("Some very long text used to check overlapping with the delete button"), footerText: "Some normal text", isError: false)
119135
RoundedBorderTextField(title: "A title", placeHolder: "A placeholder", text: .constant("Some very long text used to check overlapping with the delete button"), footerText: "Some normal text", isError: false)
120136
.disabled(true)
137+
138+
Spacer().frame(height: 0)
139+
140+
RoundedBorderTextField(title: "Password", placeHolder: "Enter your password", text: .constant(""), configuration: UIKitTextInputConfiguration(isSecureTextEntry: true))
141+
RoundedBorderTextField(title: "Password", placeHolder: "Enter your password", text: .constant("password"), configuration: UIKitTextInputConfiguration(isSecureTextEntry: true))
121142
}
122143
}
123144
}

RiotSwiftUI/Modules/Common/Util/ThemableTextField.swift

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ struct UIKitTextInputConfiguration {
2424
var autocorrectionType: UITextAutocorrectionType = .default
2525
}
2626

27-
@available(iOS 14.0, *)
2827
struct ThemableTextField: UIViewRepresentable {
2928

3029
// MARK: Properties
3130

3231
@State var placeholder: String?
3332
@Binding var text: String
3433
@State var configuration: UIKitTextInputConfiguration = UIKitTextInputConfiguration()
34+
@Binding var isSecureTextVisible: Bool
3535
var onEditingChanged: ((_ edit: Bool) -> Void)?
3636
var onCommit: (() -> Void)?
3737

@@ -47,11 +47,13 @@ struct ThemableTextField: UIViewRepresentable {
4747
init(placeholder: String? = nil,
4848
text: Binding<String>,
4949
configuration: UIKitTextInputConfiguration = UIKitTextInputConfiguration(),
50+
isSecureTextVisible: Binding<Bool> = .constant(false),
5051
onEditingChanged: ((_ edit: Bool) -> Void)? = nil,
5152
onCommit: (() -> Void)? = nil) {
5253
self._text = text
5354
self._placeholder = State(initialValue: placeholder)
5455
self._configuration = State(initialValue: configuration)
56+
self._isSecureTextVisible = isSecureTextVisible
5557
self.onEditingChanged = onEditingChanged
5658
self.onCommit = onCommit
5759

@@ -89,7 +91,7 @@ struct ThemableTextField: UIViewRepresentable {
8991

9092
uiView.keyboardType = configuration.keyboardType
9193
uiView.returnKeyType = configuration.returnKeyType
92-
uiView.isSecureTextEntry = configuration.isSecureTextEntry
94+
uiView.isSecureTextEntry = configuration.isSecureTextEntry ? !isSecureTextVisible : false
9395
uiView.autocapitalizationType = configuration.autocapitalizationType
9496
uiView.autocorrectionType = configuration.autocorrectionType
9597
}
@@ -149,7 +151,6 @@ struct ThemableTextField: UIViewRepresentable {
149151

150152
// MARK: - modifiers
151153

152-
@available(iOS 14.0, *)
153154
extension ThemableTextField {
154155
func makeFirstResponder() -> ThemableTextField {
155156
return makeFirstResponder(true)
@@ -159,4 +160,22 @@ extension ThemableTextField {
159160
internalParams.isFirstResponder = isFirstResponder
160161
return self
161162
}
163+
164+
/// Adds a button button to the text field
165+
/// - Parameters:
166+
/// - show: A boolean that can be used to dynamically show/hide the button. Defaults to `true`.
167+
/// - alignment: The vertical alignment of the button in the text field. Default to `center`
168+
@ViewBuilder
169+
func addButton(_ show: Bool, alignment: VerticalAlignment = .center) -> some View {
170+
if show && configuration.isSecureTextEntry {
171+
modifier(PasswordButtonModifier(text: text,
172+
isSecureTextVisible: $isSecureTextVisible,
173+
alignment: alignment))
174+
} else if show {
175+
modifier(ClearViewModifier(alignment: alignment,
176+
text: $text))
177+
} else {
178+
self
179+
}
180+
}
162181
}

changelog.d/pr-6268.wip

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Authentication: Add reveal password button and use a rounded checkbox

0 commit comments

Comments
 (0)