Skip to content
Repos

WO_DevPlaceIOS

Swift 6.8 KB218 lines Opens in site

Directory Raw Download

import SwiftUI

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

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

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

    @State var viewModel: SignUpView.ViewModel

    @State private var isShowingSuccess = false

    @FocusState private var focusedField: Field?

    private let characterCounterHorizontalPadding = 10.0

    private enum Field {
        case username
        case email
        case password
        case confirmPassword
    }

    var body: some View {
        content()
            .contentShape(Rectangle())
            .onTapGesture {
                focusedField = nil
            }
            .background {
                Color.BG_2.ignoresSafeArea()
            }
            .foregroundStyle(.FG_1)
            .navigationBarTitle("Create account")
            .alert($viewModel.alertMessage)
            .alert("Account created", isPresented: $isShowingSuccess) {
                Button("OK") {
                    dismiss()
                }
            } message: {
                Text("Your account has been created. You can now sign in.")
            }
            .onReceive(viewModel.succeeded) {
                isShowingSuccess = true
            }
    }

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

                dateOfBirth()

                termsOfService()

                signUpButton()
            }
            .padding()
        }
    }

    @ViewBuilder private func textFields() -> some View {
        VStack(spacing: 10) {
            field {
                TextField("Username", text: $viewModel.username)
                    .textContentType(.username)
                    .focused($focusedField, equals: .username)
                    .submitLabel(.next)
                    .onSubmit {
                        focusedField = .email
                    }
            } counter: {
                CharacterCounterView(
                    text: viewModel.username,
                    minCount: SignUpView.ViewModel.usernameMinLength,
                    maxCount: SignUpView.ViewModel.usernameMaxLength,
                )
            }

            field {
                TextField("Email", text: $viewModel.email)
                    .textContentType(.emailAddress)
                    .keyboardType(.emailAddress)
                    .focused($focusedField, equals: .email)
                    .submitLabel(.next)
                    .onSubmit {
                        focusedField = .password
                    }
            } counter: {
                CharacterCounterView(
                    text: viewModel.email,
                    minCount: SignUpView.ViewModel.emailMinLength,
                    maxCount: SignUpView.ViewModel.emailMaxLength,
                )
            }

            field {
                SecureField("Password", text: $viewModel.password)
                    .textContentType(.newPassword)
                    .focused($focusedField, equals: .password)
                    .submitLabel(.next)
                    .onSubmit {
                        focusedField = .confirmPassword
                    }
            } counter: {
                CharacterCounterView(
                    text: viewModel.password,
                    minCount: SignUpView.ViewModel.passwordMinLength,
                    maxCount: SignUpView.ViewModel.passwordMaxLength,
                )
            }

            field {
                SecureField("Confirm password", text: $viewModel.confirmPassword)
                    .textContentType(.newPassword)
                    .focused($focusedField, equals: .confirmPassword)
                    .submitLabel(.go)
                    .onSubmit {
                        submit()
                    }
            } counter: {
                CharacterCounterView(
                    text: viewModel.confirmPassword,
                    minCount: SignUpView.ViewModel.passwordMinLength,
                    maxCount: SignUpView.ViewModel.passwordMaxLength,
                )
            }
        }
        .textInputAutocapitalization(.never)
        .autocorrectionDisabled()
        .textFieldStyle(.devPlace)
        .disabled(viewModel.isLoading)
    }

    @ViewBuilder private func field<Input: View, Counter: View>(
        @ViewBuilder input: () -> Input,
        @ViewBuilder counter: () -> Counter,
    ) -> some View {
        VStack(alignment: .leading, spacing: 4) {
            input()

            counter()
                .padding(.horizontal, characterCounterHorizontalPadding)
        }
    }

    @ViewBuilder private func dateOfBirth() -> some View {
        VStack(alignment: .leading, spacing: 8) {
            DatePicker(
                "Date of birth",
                selection: $viewModel.birthDate,
                in: ...Date.now,
                displayedComponents: .date,
            )
            .disabled(viewModel.isLoading)

            Text("Your date of birth is only used to confirm your age. It is not stored - only your age range is kept.")
                .font(.footnote)
                .foregroundStyle(.FG_2)
        }
        .frame(maxWidth: .infinity, alignment: .leading)
    }

    @ViewBuilder private func termsOfService() -> some View {
        VStack(alignment: .leading, spacing: 12) {
            Link(
                destination: URL(string: "https://devplace.net/docs/terms.html")!
            ) {
                Text("Read the Terms of Service")
            }
            .foregroundStyle(Color.accentColor)

            Toggle(isOn: $viewModel.tosAccepted) {
                Text("I accept the Terms of Service")
                    .foregroundStyle(.FG_2)
            }
            .tint(Color.accentColor)
            .disabled(viewModel.isLoading)
        }
        .frame(maxWidth: .infinity, alignment: .leading)
    }

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

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

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

#Preview("mock") {
    NavigationStack {
        SignUpView()
            .environment(\.api, .mock)
    }
}
55.0ms