-
Notifications
You must be signed in to change notification settings - Fork 1
fed-00006: Add bindings email #27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| [tools] | ||
| tuist = "4.140.0" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| import SwiftUI | ||
|
|
||
| struct AuthBindingsView: View { | ||
| let onEmailLinked: (String) -> Void | ||
|
|
||
| @State private var viewModel = AuthBindingsViewModel() | ||
|
|
||
| var body: some View { | ||
| ScrollView { | ||
| VStack(alignment: .leading, spacing: 16) { | ||
| titleLabel | ||
| subtitleLabel | ||
| emailBindingNavigationLink | ||
| AppleBindingButton(viewModel: viewModel) | ||
| GoogleBindingButton(viewModel: viewModel) | ||
| } | ||
| .padding() | ||
| } | ||
| .background(Color(uiColor: .systemGroupedBackground)) | ||
| .navigationTitle(String(localized: .authBindingsNavigationTitle)) | ||
| .navigationBarTitleDisplayMode(.inline) | ||
| .alert( | ||
| String(localized: .authBindingsProviderUnavailableTitle), | ||
| isPresented: $viewModel.showProviderUnavailableAlert, | ||
| ) { | ||
| Button(String(localized: .signUpErrorOk), role: .cancel) {} | ||
| } message: { | ||
| Text(.authBindingsProviderUnavailableMessage) | ||
| } | ||
| } | ||
|
|
||
| private var titleLabel: some View { | ||
| Text(.authBindingsTitle) | ||
| .font(.title2) | ||
| .fontWeight(.bold) | ||
| .frame(maxWidth: .infinity, alignment: .center) | ||
| .multilineTextAlignment(.center) | ||
| } | ||
|
|
||
| private var subtitleLabel: some View { | ||
| Text(.authBindingsSubtitle) | ||
| .font(.subheadline) | ||
| .foregroundStyle(.secondary) | ||
| .frame(maxWidth: .infinity, alignment: .center) | ||
| .multilineTextAlignment(.center) | ||
| } | ||
|
|
||
| private var emailBindingNavigationLink: some View { | ||
| NavigationLink { | ||
| EmailBindingView(onSuccess: onEmailLinked) | ||
| } label: { | ||
| EmailBindingButtonLabel() | ||
| } | ||
| .buttonStyle(.borderedProminent) | ||
| } | ||
| } | ||
|
|
||
| private struct EmailBindingButtonLabel: View { | ||
| var body: some View { | ||
| HStack { | ||
| Image(systemName: "envelope") | ||
| Text(.profileEditBindEmailButton) | ||
| } | ||
| .font(.headline) | ||
| .frame(maxWidth: .infinity) | ||
| .padding(.vertical, 12) | ||
| } | ||
| } | ||
|
|
||
| private struct AppleBindingButton: View { | ||
| let viewModel: AuthBindingsViewModel | ||
|
|
||
| var body: some View { | ||
| Button(action: { viewModel.tapAppleAuthorization() }) { | ||
| HStack { | ||
| Image(systemName: "apple.logo") | ||
| Text(.authBindingsAppleButton) | ||
| } | ||
| .font(.headline) | ||
| .frame(maxWidth: .infinity) | ||
| .padding(.vertical, 12) | ||
| } | ||
| .buttonStyle(.bordered) | ||
| } | ||
| } | ||
|
|
||
| private struct GoogleBindingButton: View { | ||
| let viewModel: AuthBindingsViewModel | ||
|
|
||
| var body: some View { | ||
| Button(action: { viewModel.tapGoogleAuthorization() }) { | ||
| HStack { | ||
| Image(systemName: "globe") | ||
| Text(.authBindingsGoogleButton) | ||
| } | ||
| .font(.headline) | ||
| .frame(maxWidth: .infinity) | ||
| .padding(.vertical, 12) | ||
| } | ||
| .buttonStyle(.bordered) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| import Foundation | ||
|
|
||
| @MainActor | ||
| @Observable | ||
| class AuthBindingsViewModel { | ||
| var showProviderUnavailableAlert: Bool = false | ||
|
|
||
| func tapAppleAuthorization() { | ||
| showProviderUnavailableAlert = true | ||
| } | ||
|
|
||
| func tapGoogleAuthorization() { | ||
| showProviderUnavailableAlert = true | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| struct ConfirmationCode { | ||
| let int: Int | ||
|
|
||
| init?(_ value: String) { | ||
| let digits = Self.sanitize(value) | ||
| guard digits.count == 8, let int = Int(digits) else { | ||
| return nil | ||
| } | ||
| self.int = int | ||
| } | ||
|
|
||
| static func sanitize(_ value: String) -> String { | ||
| String(value.filter(\.isNumber).prefix(8)) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| import Foundation | ||
|
|
||
| final class EmailAuthWorker { | ||
| private let networkClient: NetworkClient | ||
|
|
||
| init(networkClient: NetworkClient = .meetacy) { | ||
| self.networkClient = networkClient | ||
| } | ||
|
|
||
| func requestBindingCode( | ||
| authorization: Authorization, | ||
| email: String, | ||
| ) async throws { | ||
| do { | ||
| try await networkClient.emailLink( | ||
| authorization: authorization, | ||
| email: email, | ||
| ) | ||
| try Task.checkCancellation() | ||
| } catch { | ||
| try Task.checkCancellation() | ||
| throw error | ||
| } | ||
| } | ||
|
|
||
| func confirmBinding( | ||
| authorization: Authorization, | ||
| code: Int, | ||
| ) async throws { | ||
| do { | ||
| try await networkClient.emailConfirm( | ||
| authorization: authorization, | ||
| code: code, | ||
| ) | ||
| try Task.checkCancellation() | ||
| } catch { | ||
| try Task.checkCancellation() | ||
| throw error | ||
| } | ||
| } | ||
|
|
||
| func requestLoginCode(email: String) async throws { | ||
| do { | ||
| try await networkClient.authEmail(email: email) | ||
| try Task.checkCancellation() | ||
| } catch { | ||
| try Task.checkCancellation() | ||
| throw error | ||
| } | ||
| } | ||
|
|
||
| func login( | ||
| email: String, | ||
| code: Int, | ||
| ) async throws -> Authorization { | ||
| do { | ||
| let authorization = try await networkClient.authLogin( | ||
| email: email, | ||
| code: code, | ||
| ) | ||
| try Task.checkCancellation() | ||
| return authorization | ||
| } catch { | ||
| try Task.checkCancellation() | ||
| throw error | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,190 @@ | ||
| import SwiftUI | ||
|
|
||
| struct EmailBindingView: View { | ||
| let onSuccess: (String) -> Void | ||
|
|
||
| @State private var viewModel = EmailBindingViewModel() | ||
| @FocusState private var isCodeInputFocused: Bool | ||
|
|
||
| var body: some View { | ||
| ScrollView { | ||
| contentView | ||
| .padding() | ||
| } | ||
| .background(Color(uiColor: .systemGroupedBackground)) | ||
| .navigationTitle(String(localized: .profileEditBindEmailSheetTitle)) | ||
| .navigationBarTitleDisplayMode(.inline) | ||
| .onDisappear { | ||
| viewModel.cancelTasks() | ||
| } | ||
| } | ||
|
|
||
| private var contentView: some View { | ||
| VStack(alignment: .leading, spacing: 16) { | ||
| titleLabel | ||
| subtitleLabel | ||
| detailsLabel | ||
| emailTextField | ||
| changeEmailButton | ||
| sendCodeButton | ||
| resendTimerView | ||
| verificationCodeInputView | ||
| confirmCodeButton | ||
| statusMessageView | ||
| } | ||
| } | ||
|
|
||
| private var titleLabel: some View { | ||
| Text(.profileEditBindEmailTitle) | ||
| .font(.title3) | ||
| .fontWeight(.semibold) | ||
| .frame(maxWidth: .infinity, alignment: .center) | ||
| .multilineTextAlignment(.center) | ||
| } | ||
|
|
||
| private var subtitleLabel: some View { | ||
| Text(.profileEditBindEmailSubtitle) | ||
| .font(.subheadline) | ||
| .foregroundStyle(.secondary) | ||
| .frame(maxWidth: .infinity, alignment: .center) | ||
| .multilineTextAlignment(.center) | ||
| } | ||
|
|
||
| private var detailsLabel: some View { | ||
| Text(.emailBindingDetails) | ||
| .font(.footnote) | ||
| .foregroundStyle(.secondary) | ||
| .frame(maxWidth: .infinity, alignment: .center) | ||
| .multilineTextAlignment(.center) | ||
| } | ||
|
|
||
| private var emailTextField: some View { | ||
| TextField( | ||
| String(localized: .profileEditBindEmailEmailExample), | ||
| text: Binding( | ||
| get: { viewModel.email }, | ||
| set: { viewModel.email = $0 } | ||
| ) | ||
| ) | ||
| .keyboardType(.emailAddress) | ||
| .textInputAutocapitalization(.never) | ||
| .autocorrectionDisabled() | ||
| .disabled(viewModel.isEmailLocked) | ||
| .padding(.horizontal, 14) | ||
| .padding(.vertical, 14) | ||
| .background(Color(uiColor: .secondarySystemGroupedBackground)) | ||
| .clipShape(RoundedRectangle(cornerRadius: 12)) | ||
| } | ||
|
|
||
| @ViewBuilder | ||
| private var changeEmailButton: some View { | ||
| if viewModel.isEmailLocked { | ||
| Button { | ||
| viewModel.resetEmailRequest() | ||
| } label: { | ||
| Text(.emailChangeAddress) | ||
| } | ||
| .frame(maxWidth: .infinity, alignment: .trailing) | ||
| } | ||
| } | ||
|
|
||
| private var sendCodeButton: some View { | ||
| Button(action: { viewModel.requestCode() }) { | ||
| sendCodeButtonLabel | ||
| } | ||
| .buttonStyle(.borderedProminent) | ||
| .disabled(!viewModel.canRequestCode) | ||
| } | ||
|
|
||
| @ViewBuilder | ||
| private var sendCodeButtonLabel: some View { | ||
| if viewModel.isSendingCode { | ||
| ProgressView() | ||
| .frame(maxWidth: .infinity) | ||
| .padding(.vertical, 10) | ||
| } else { | ||
| Text(.profileEditBindEmailSendCode) | ||
| .frame(maxWidth: .infinity) | ||
| .padding(.vertical, 10) | ||
| } | ||
| } | ||
|
|
||
| @ViewBuilder | ||
| private var resendTimerView: some View { | ||
| if viewModel.remainingSeconds > 0 { | ||
| Text(.emailBindingResendAfter) | ||
| .font(.footnote) | ||
| .foregroundStyle(.secondary) | ||
| .frame(maxWidth: .infinity, alignment: .center) | ||
| .multilineTextAlignment(.center) | ||
| Text(verbatim: viewModel.formattedTimer) | ||
| .font(.headline.monospacedDigit()) | ||
| .frame(maxWidth: .infinity, alignment: .center) | ||
| } | ||
| } | ||
|
|
||
| private var verificationCodeInputView: some View { | ||
| VerificationCodeInputView( | ||
| code: Binding( | ||
| get: { viewModel.code }, | ||
| set: { viewModel.updateCode($0) } | ||
| ), | ||
| isFocused: $isCodeInputFocused, | ||
| isError: viewModel.status == .invalidCode, | ||
| ) | ||
| } | ||
|
|
||
| private var confirmCodeButton: some View { | ||
| Button(action: { | ||
| Task { | ||
| do { | ||
| let email = try await viewModel.confirmCode() | ||
| onSuccess(email) | ||
| } catch { | ||
| // The view model exposes the error through its status. | ||
| } | ||
| } | ||
| }) { | ||
| confirmCodeButtonLabel | ||
| } | ||
| .buttonStyle(.borderedProminent) | ||
| .disabled(!viewModel.canConfirm) | ||
| } | ||
|
|
||
| @ViewBuilder | ||
| private var confirmCodeButtonLabel: some View { | ||
| if viewModel.isConfirmingCode { | ||
| ProgressView() | ||
| .frame(maxWidth: .infinity) | ||
| .padding(.vertical, 10) | ||
| } else { | ||
| Text(.profileEditBindEmailConfirm) | ||
| .frame(maxWidth: .infinity) | ||
| .padding(.vertical, 10) | ||
| } | ||
| } | ||
|
|
||
| @ViewBuilder | ||
| private var statusMessageView: some View { | ||
| switch viewModel.status { | ||
| case .idle: | ||
| EmptyView() | ||
| case .success: | ||
| Text(.profileEditBindEmailSuccess) | ||
| .font(.footnote) | ||
| .foregroundStyle(.green) | ||
| .frame(maxWidth: .infinity, alignment: .center) | ||
| .multilineTextAlignment(.center) | ||
| case .invalidEmail: | ||
| InlineErrorView(.emailErrorInvalid) | ||
| case .alreadyUsed: | ||
| InlineErrorView(.emailBindingErrorUsed) | ||
| case .invalidCode: | ||
| InlineErrorView(.emailErrorInvalidCode) | ||
| case .unauthorized: | ||
| InlineErrorView(.emailErrorUnauthorized) | ||
| case .networkError: | ||
| InlineErrorView(.profileEditBindEmailError) | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
тут бы хорошо разбить весь боди на переменные, уж очень он длинный получился, больше 30 строк лучше не писать, читать код становится трудно