Skip to content
Repos

WO_DevPlaceIOS

Swift 4.0 KB154 lines Opens in site

Directory Raw Download

import SwiftUI

struct LogInView: View {
    @Environment(\.api) var api

    var body: some View {
        LogInViewContent(viewModel: .init(api: api))
    }
}

private struct LogInViewContent: View {
    @Environment(\.dismiss) private var dismiss

    @State var viewModel: LogInView.ViewModel

    @FocusState private var focusedField: Field?

    private enum Field {
        case email
        case password
    }

    var body: some View {
        NavigationStack {
            content()
                .background {
                    Color.BG_2.ignoresSafeArea()
                }
                .foregroundStyle(.FG_1)
                .navigationBarTitle("Sign in")
                .toolbar {
                    ToolbarItem(placement: .navigation) {
                        Button {
                            dismiss()
                        } label: {
                            Image(systemName: "xmark")
                        }
                    }
                }
                .alert($viewModel.alertMessage)
                .onReceive(viewModel.dismiss) {
                    dismiss()
                }
        }
    }

    @ViewBuilder private func content() -> some View {
        ScrollView {
            VStack(spacing: 26) {
                textFields()

                logInButton()

                forgotPassword()

                createNewAccount()
            }
            .padding()
        }
    }

    @ViewBuilder private func textFields() -> some View {
        VStack(spacing: 10) {
            Group {
                TextField("Email", text: $viewModel.email)
                    .textContentType(.emailAddress)
                    .keyboardType(.emailAddress)
                    .focused($focusedField, equals: .email)
                    .submitLabel(.next)
                    .onSubmit {
                        focusedField = .password
                    }

                SecureField("Password", text: $viewModel.password)
                    .textContentType(.password)
                    .focused($focusedField, equals: .password)
                    .submitLabel(.go)
                    .onSubmit {
                        submit()
                    }
            }
            .textInputAutocapitalization(.never)
            .autocorrectionDisabled()
            .textFieldStyle(.devPlace)
            .disabled(viewModel.isLoading)
        }
    }

    @ViewBuilder private func logInButton() -> some View {
        ZStack {
            ProgressView()
                .opacity(viewModel.isLoading ? 1 : 0)

            Button {
                submit()
            } label: {
                Text("Sign in")
                    .frame(maxWidth: .infinity)
            }
            .buttonStyle(.accentGradient)
            .bold()
            .disabled(!viewModel.canSubmit)
            .opacity(viewModel.isLoading ? 0 : 1)
        }
    }

    private func submit() {
        focusedField = nil
        viewModel.logIn()
    }

    @ViewBuilder private func forgotPassword() -> some View {
        Link(
            destination: URL(string: "https://devplace.net/auth/forgot-password")!
        ) {
            Text("Forgot your password?")
        }
        .foregroundStyle(Color.accentColor)
    }

    @ViewBuilder private func createNewAccount() -> some View {
        let text = Text("Don't have an account?")
            .fontWeight(.medium)
            .foregroundStyle(.FG_2)

        let link = NavigationLink {
            SignUpView()
        } label: {
            Text("Create new account").bold()
        }
        .foregroundStyle(Color.accentColor)

        ViewThatFits {
            HStack(spacing: 8) {
                text
                link
            }

            VStack(spacing: 2) {
                text
                link
            }
        }
    }
}

#Preview("mock") {
    LogInView()
        .environment(\.api, .mock)
}

#Preview("prod") {
    LogInView()
}
25.8ms