Moved some login code into its own file within the Login module.

This commit is contained in:
Javier Cicchelli 2022-12-02 20:55:13 +01:00
parent 9ed753b2fe
commit a98c5df41e
5 changed files with 100 additions and 53 deletions

View File

@ -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)

View File

@ -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
)
}
}
}

View File

@ -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()
}
}

View File

@ -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()
}
}

View File

@ -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 {