From a98c5df41eab5d59c990872a490bd88578d25f67 Mon Sep 17 00:00:00 2001 From: Javier Cicchelli Date: Fri, 2 Dec 2022 20:55:13 +0100 Subject: [PATCH] Moved some login code into its own file within the Login module. --- .../Login/UI/Components/LoginForm.swift | 14 +++- .../UI/Components/ViewHeightGeometry.swift | 22 +++++++ .../ViewHeightPreferenceKey.swift | 17 +++++ .../Login/UI/Styles/LogInLabelStyle.swift | 34 ++++++++++ .../Sources/Login/UI/Views/LoginView.swift | 66 +++++-------------- 5 files changed, 100 insertions(+), 53 deletions(-) create mode 100644 Modules/Sources/Login/UI/Components/ViewHeightGeometry.swift create mode 100644 Modules/Sources/Login/UI/Preference Keys/ViewHeightPreferenceKey.swift create mode 100644 Modules/Sources/Login/UI/Styles/LogInLabelStyle.swift diff --git a/Modules/Sources/Login/UI/Components/LoginForm.swift b/Modules/Sources/Login/UI/Components/LoginForm.swift index c5250f9..c40fc6c 100644 --- a/Modules/Sources/Login/UI/Components/LoginForm.swift +++ b/Modules/Sources/Login/UI/Components/LoginForm.swift @@ -1,6 +1,6 @@ // // LoginForm.swift -// BeReal +// Login // // Created by Javier Cicchelli on 01/12/2022. // Copyright © 2022 Röck+Cöde. All rights reserved. @@ -32,7 +32,11 @@ struct LoginForm: View { spacing: 16 ) { TextField( - "login.text_field.username.placeholder", + NSLocalizedString( + "login.text_field.username.placeholder", + bundle: .module, + comment: "The placeholder for the username text field." + ), text: $username ) { isBeginEditing in guard isBeginEditing, errorMessage != nil else { return } @@ -52,7 +56,11 @@ struct LoginForm: View { Divider() SecureField( - "login.text_field.password.placeholder", + NSLocalizedString( + "login.text_field.password.placeholder", + bundle: .module, + comment: "The placeholder for the password text field." + ), text: $password ) .textContentType(.password) diff --git a/Modules/Sources/Login/UI/Components/ViewHeightGeometry.swift b/Modules/Sources/Login/UI/Components/ViewHeightGeometry.swift new file mode 100644 index 0000000..7f0672a --- /dev/null +++ b/Modules/Sources/Login/UI/Components/ViewHeightGeometry.swift @@ -0,0 +1,22 @@ +// +// ViewHeightGeometry.swift +// Login +// +// Created by Javier Cicchelli on 02/12/2022. +// Copyright © 2022 Röck+Cöde. All rights reserved. +// + +import SwiftUI + +struct ViewHeightGeometry: View { + var body: some View { + GeometryReader { proxy in + Color.clear.preference( + key: ViewHeightPreferenceKey.self, + value: proxy.size.height + + proxy.safeAreaInsets.top + + proxy.safeAreaInsets.bottom + ) + } + } +} diff --git a/Modules/Sources/Login/UI/Preference Keys/ViewHeightPreferenceKey.swift b/Modules/Sources/Login/UI/Preference Keys/ViewHeightPreferenceKey.swift new file mode 100644 index 0000000..2dfc680 --- /dev/null +++ b/Modules/Sources/Login/UI/Preference Keys/ViewHeightPreferenceKey.swift @@ -0,0 +1,17 @@ +// +// ViewHeightPreferenceKey.swift +// Login +// +// Created by Javier Cicchelli on 02/12/2022. +// Copyright © 2022 Röck+Cöde. All rights reserved. +// + +import SwiftUI + +struct ViewHeightPreferenceKey: PreferenceKey { + static var defaultValue: CGFloat = 0 + + static func reduce(value: inout CGFloat, nextValue: () -> CGFloat) { + value = nextValue() + } +} diff --git a/Modules/Sources/Login/UI/Styles/LogInLabelStyle.swift b/Modules/Sources/Login/UI/Styles/LogInLabelStyle.swift new file mode 100644 index 0000000..a025527 --- /dev/null +++ b/Modules/Sources/Login/UI/Styles/LogInLabelStyle.swift @@ -0,0 +1,34 @@ +// +// LogInLabelStyle.swift +// Login +// +// Created by Javier Cicchelli on 02/12/2022. +// Copyright © 2022 Röck+Cöde. All rights reserved. +// + +import SwiftUI + +struct LogInLabelStyle: LabelStyle { + func makeBody(configuration: Configuration) -> some View { + HStack(spacing: 8) { + Spacer() + + configuration.title + .font(.body) + .foregroundColor(.primary) + + configuration.icon + .tint(.primary) + + Spacer() + } + } +} + +// MARK: - LabelStyle + +extension LabelStyle where Self == LogInLabelStyle { + static var logIn: Self { + LogInLabelStyle() + } +} diff --git a/Modules/Sources/Login/UI/Views/LoginView.swift b/Modules/Sources/Login/UI/Views/LoginView.swift index 45cb4df..392073b 100644 --- a/Modules/Sources/Login/UI/Views/LoginView.swift +++ b/Modules/Sources/Login/UI/Views/LoginView.swift @@ -1,6 +1,6 @@ // // LoginView.swift -// BeReal +// Login // // Created by Javier Cicchelli on 30/11/2022. // Copyright © 2022 Röck+Cöde. All rights reserved. @@ -54,10 +54,14 @@ fileprivate extension LoginView { var body: some View { VStack(spacing: 32) { - Text("login.title.text", bundle: .module) - .font(.largeTitle) - .fontWeight(.bold) - .foregroundColor(.primary) + Text( + "login.title.text", + bundle: .module, + comment: "Login view title text." + ) + .font(.largeTitle) + .fontWeight(.bold) + .foregroundColor(.primary) LoginForm( username: $username, @@ -71,8 +75,12 @@ fileprivate extension LoginView { // TODO: login with the username and password. } label: { Label { - Text("login.button.log_in.text", bundle: .module) - .fontWeight(.semibold) + Text( + "login.button.log_in.text", + bundle: .module, + comment: "Log in button text." + ) + .fontWeight(.semibold) } icon: { if isAuthenticating { ProgressView() @@ -80,7 +88,7 @@ fileprivate extension LoginView { EmptyView() } } - .labelStyle(LogInLabelStyle()) + .labelStyle(.logIn) } .tint(.orange) .buttonStyle(.borderedProminent) @@ -90,38 +98,6 @@ fileprivate extension LoginView { } } } - - struct ViewHeightGeometry: View { - var body: some View { - GeometryReader { proxy in - Color.clear.preference( - key: ViewHeightPreferenceKey.self, - value: proxy.size.height + proxy.safeAreaInsets.top + proxy.safeAreaInsets.bottom - ) - } - } - } -} - -// MARK: - Label styles - -private extension LoginView.LoginContainer { - struct LogInLabelStyle: LabelStyle { - func makeBody(configuration: Configuration) -> some View { - HStack(spacing: 8) { - Spacer() - - configuration.title - .font(.body) - .foregroundColor(.primary) - - configuration.icon - .tint(.primary) - - Spacer() - } - } - } } // MARK: - Helpers @@ -132,16 +108,6 @@ private extension LoginView.LoginContainer { } } -// MARK: - Preference keys - -struct ViewHeightPreferenceKey: PreferenceKey { - static var defaultValue: CGFloat = 0 - - static func reduce(value: inout CGFloat, nextValue: () -> CGFloat) { - value = nextValue() - } -} - // MARK: - Previews struct LoginView_Previews: PreviewProvider {