Skip to content
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

feat: Invoke DetermineAuthMethodUseCase - WPB-15920 #2532

Open
wants to merge 23 commits into
base: develop
Choose a base branch
from

Conversation

samwyndham
Copy link
Contributor

Issue

This PR invokes DetermineAuthMethodUseCase. When this is merged DetermineAuthMethodView should be largely complete, excluding the route navigation of loginViaSSO & onPremLogin.

Testing

Run unit tests

Checklist

  • Title contains a reference JIRA issue number like [WPB-XXX].
  • Description is filled and free of optional paragraphs.
  • Adds/updates automated tests.

# Conflicts:
#	WireAuthentication/Sources/WireAuthenticationAPI/Use cases/DetermineAuthMethodUseCase.swift
@@ -61,7 +61,6 @@ package struct DetermineAuthMethodView: View {
.padding(.trailing)

VStack(alignment: .leading, spacing: 8) {
// TODO: [WPB-16045] Set error on `LabeledTextField` when supported.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No longer needed

@@ -70,21 +69,14 @@ package struct DetermineAuthMethodView: View {
)
.lineLimit(nil)
.fixedSize(horizontal: false, vertical: true)

if let errorMessage = viewModel.errorMessage {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No longer needed

// TODO: [WPB-15920] Handle on-prem login
do {
// TODO: Fix concurrency issue
let method = try await determineAuthMethod.invoke(emailOrSSOCode: emailOrSSOCode)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@KaterinaWire I haven't yet managed to fix this concurrency warning. One solution is to do:

            let task = Task.detached { [determineAuthMethod, emailOrSSOCode] in
                try await determineAuthMethod.invoke(emailOrSSOCode: emailOrSSOCode)
            }
            let method = try await task.value

However the typed error becomes untyped with this which is a little annoying.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@samwyndham I marked invoke(emailOrSSOCode:) as @mainactor, maybe as a temp solution. We still have same warnings in DetermineAuthMethodUseCase and need to find a proper solution

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there are a few ways to solve this that we should explore, but if we mark the use case as main actor, then we must ensure appropriate concurrency inside the use case implementation.

@samwyndham if you don't want to lose the error type you could catch inside the task and return a typed error in the result. This adds a bit of boilerplate, so it could be factored out in a utility extension so the API looks something like:

try await Task.detached<Success, Failure> {
  ...
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we instead declare DetermineAuthMethodUseCaseProtocol as Sendable and everything else which is needed for the compiler to not produce Sendable related warnings?
Like AuthenticationAPI etc.

@KaterinaWire KaterinaWire marked this pull request as ready for review February 13, 2025 16:55
Copy link
Contributor

github-actions bot commented Feb 13, 2025

Test Results

24 tests   24 ✅  7s ⏱️
 3 suites   0 💤
 1 files     0 ❌

Results for commit 50558b1.

♻️ This comment has been updated with latest results.

@datadog-wireapp
Copy link

datadog-wireapp bot commented Feb 14, 2025

Datadog Report

Branch report: feat/invoke-auth-method-use-case-WPB-15920
Commit report: 51f8d68
Test service: wire-ios-mono

✅ 0 Failed, 19 Passed, 0 Skipped, 7.58s Total Time

@@ -20,11 +20,12 @@ import Foundation

public protocol DetermineAuthMethodUseCaseProtocol {

@MainActor
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thought: I thought about this in the past whether use cases should be invoked on the main thread and internally they manage their own concurrency, or if we should instead defer that thought to the caller (i.e a view model which is marked as MainActor). Perhaps it's a topic for the architecture forum.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my opinion the @MainActor requirement here on the protocol should be removed.
Since invoke is declared async anyhow, it's up to the implementation to decide, whether it should run on the main actor or not.

@@ -117,28 +119,56 @@ package struct DetermineAuthMethodView: View {

}

// MARK: - Private helpers

private func titleForAlert(_ alert: DetermineAuthMethodViewModel.Alert) -> Text {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

praise: nice way to keep the view code a more readable.

// TODO: [WPB-15920] Handle on-prem login
do {
// TODO: Fix concurrency issue
let method = try await determineAuthMethod.invoke(emailOrSSOCode: emailOrSSOCode)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there are a few ways to solve this that we should explore, but if we mark the use case as main actor, then we must ensure appropriate concurrency inside the use case implementation.

@samwyndham if you don't want to lose the error type you could catch inside the task and return a typed error in the result. This adds a bit of boilerplate, so it could be factored out in a utility extension so the API looks something like:

try await Task.detached<Success, Failure> {
  ...
}

@@ -20,11 +20,12 @@ import Foundation

public protocol DetermineAuthMethodUseCaseProtocol {

@MainActor
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my opinion the @MainActor requirement here on the protocol should be removed.
Since invoke is declared async anyhow, it's up to the implementation to decide, whether it should run on the main actor or not.

@@ -97,6 +89,16 @@ package struct DetermineAuthMethodView: View {
.disabled(viewModel.isNextButtonEnabled || viewModel.isLoading)
}.padding()
}
.alert(item: $viewModel.alert) { alert in
Alert(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// TODO: [WPB-15920] Handle on-prem login
do {
// TODO: Fix concurrency issue
let method = try await determineAuthMethod.invoke(emailOrSSOCode: emailOrSSOCode)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we instead declare DetermineAuthMethodUseCaseProtocol as Sendable and everything else which is needed for the compiler to not produce Sendable related warnings?
Like AuthenticationAPI etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants