handle input error

This commit is contained in:
Ryan Zhao 2023-08-21 14:13:08 +10:00
parent 90b2a4545c
commit 73ab8af072
3 changed files with 66 additions and 47 deletions

View File

@ -9,6 +9,7 @@ struct DisplayNameView: View {
@EnvironmentObject var host: HostWrapper
@State private var displayName: String = ""
@State private var error: String? = nil
private let flow: Onboarding.Flow
@ -43,7 +44,8 @@ struct DisplayNameView: View {
SessionTextField(
$displayName,
placeholder: "onboarding_display_name_hint".localized()
placeholder: "onboarding_display_name_hint".localized(),
error: $error
)
Spacer()
@ -79,23 +81,14 @@ struct DisplayNameView: View {
}
private func register() {
func showError(title: String, message: String = "") {
let modal: ConfirmationModal = ConfirmationModal(
info: ConfirmationModal.Info(
title: title,
body: .text(message),
cancelTitle: "BUTTON_OK".localized(),
cancelStyle: .alert_text
)
)
self.host.controller?.present(modal, animated: true)
}
let displayName = self.displayName.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
guard !displayName.isEmpty else {
return showError(title: "vc_display_name_display_name_missing_error".localized())
error = "vc_display_name_display_name_missing_error".localized()
return
}
guard !ProfileManager.isToLong(profileName: displayName) else {
return showError(title: "vc_display_name_display_name_too_long_error".localized())
error = "vc_display_name_display_name_too_long_error".localized()
return
}
// Try to save the user name but ignore the result
@ -109,7 +102,8 @@ struct DisplayNameView: View {
guard self.flow == .register else {
self.flow.completeRegistration()
// TODO: Go to the loading screen
let homeVC: HomeVC = HomeVC(flow: self.flow)
self.host.controller?.navigationController?.setViewControllers([ homeVC ], animated: true)
return
}

View File

@ -10,6 +10,7 @@ struct LoadAccountView: View {
@State var tabIndex = 0
@State private var recoveryPassword: String = ""
@State private var error: String? = nil
var body: some View {
NavigationView {
@ -26,7 +27,10 @@ struct LoadAccountView: View {
.frame(maxWidth: .infinity)
if tabIndex == 0 {
EnterRecoveryPasswordView($recoveryPassword)
EnterRecoveryPasswordView(
$recoveryPassword,
error: $error
)
}
else {
ScanQRCodeView()
@ -133,9 +137,11 @@ struct CustomTopTabBar: View {
struct EnterRecoveryPasswordView: View{
@Binding var recoveryPassword: String
@Binding var error: String?
init(_ recoveryPassword: Binding<String>) {
init(_ recoveryPassword: Binding<String>, error: Binding<String?>) {
self._recoveryPassword = recoveryPassword
self._error = error
}
var body: some View{
@ -158,7 +164,8 @@ struct EnterRecoveryPasswordView: View{
SessionTextField(
$recoveryPassword,
placeholder: "onboarding_recovery_password_hint".localized()
placeholder: "onboarding_recovery_password_hint".localized(),
error: $error
)
Spacer()

View File

@ -4,51 +4,69 @@ import SwiftUI
public struct SessionTextField: View {
@Binding var text: String
@Binding var error: String?
let placeholder: String
static let height: CGFloat = isIPhone5OrSmaller ? CGFloat(48) : CGFloat(80)
static let cornerRadius: CGFloat = 13
public init(_ text: Binding<String>, placeholder: String) {
public init(_ text: Binding<String>, placeholder: String, error: Binding<String?>) {
self._text = text
self.placeholder = placeholder
self._error = error
}
public var body: some View {
ZStack(alignment: .topLeading) {
if text.isEmpty {
Text(placeholder)
.font(.system(size: Values.mediumFontSize))
.foregroundColor(themeColor: .textSecondary)
}
SwiftUI.TextField(
"",
text: $text
)
.font(.system(size: Values.mediumFontSize))
.foregroundColor(themeColor: .textPrimary)
}
.padding(.horizontal, Values.largeSpacing)
.frame(
maxWidth: .infinity,
maxHeight: Self.height
)
.overlay(
RoundedRectangle(
cornerSize: CGSize(
width: Self.cornerRadius,
height: Self.cornerRadius
VStack (
alignment: .center,
spacing: Values.smallSpacing
) {
ZStack(alignment: .topLeading) {
if text.isEmpty {
Text(placeholder)
.font(.system(size: Values.mediumFontSize))
.foregroundColor(themeColor: .textSecondary)
}
SwiftUI.TextField(
"",
text: $text
)
.font(.system(size: Values.mediumFontSize))
.foregroundColor(themeColor: (error?.isEmpty == false) ? .danger : .textPrimary)
.onReceive(text.publisher, perform: { _ in
error = nil
})
}
.padding(.horizontal, Values.largeSpacing)
.frame(
maxWidth: .infinity,
maxHeight: Self.height
)
.stroke(themeColor: .borderSeparator)
)
.overlay(
RoundedRectangle(
cornerSize: CGSize(
width: Self.cornerRadius,
height: Self.cornerRadius
)
)
.stroke(themeColor: (error?.isEmpty == false) ? .danger : .borderSeparator)
)
Text(error ?? " ")
.bold()
.font(.system(size: Values.mediumFontSize))
.foregroundColor(themeColor: .danger)
}
}
}
struct SessionTextField_Previews: PreviewProvider {
@State static var text: String = ""
@State static var text: String = "test"
@State static var error: String? = "test error"
static var previews: some View {
SessionTextField($text, placeholder: "Placeholder")
SessionTextField($text, placeholder: "Placeholder", error: $error)
}
}