Skip to content

Commit

Permalink
chore: sign-in and register screens social sign in improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
saeedbashir committed Nov 18, 2024
1 parent 2463666 commit 6ab9590
Show file tree
Hide file tree
Showing 38 changed files with 503 additions and 261 deletions.
24 changes: 15 additions & 9 deletions Authorization/Authorization/Presentation/Login/SignInView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,20 @@ public struct SignInView: View {
.foregroundColor(Theme.Colors.textPrimary)
.padding(.bottom, 20)
.accessibilityIdentifier("welcome_back_text")

if viewModel.socialAuthEnabled {
SocialAuthView(
viewModel: .init(
config: viewModel.config,
lastUsedOption: viewModel.storage.lastUsedSocialAuth
) { result in
Task { await viewModel.login(with: result) }
}
)
.padding(.top, 22)
.padding(.bottom, 16)
}

Text(AuthLocalization.SignIn.emailOrUsername)
.font(Theme.Fonts.labelLarge)
.foregroundColor(Theme.Colors.textPrimary)
Expand Down Expand Up @@ -156,15 +170,6 @@ public struct SignInView: View {
.accessibilityIdentifier("signin_button")
}
}
if viewModel.socialAuthEnabled {
SocialAuthView(
viewModel: .init(
config: viewModel.config
) { result in
Task { await viewModel.login(with: result) }
}
)
}
agreements
Spacer()
}
Expand Down Expand Up @@ -254,6 +259,7 @@ struct SignInView_Previews: PreviewProvider {
config: ConfigMock(),
analytics: AuthorizationAnalyticsMock(),
validator: Validator(),
storage: CoreStorageMock(),
sourceScreen: .default
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,23 @@ public class SignInViewModel: ObservableObject {
private let interactor: AuthInteractorProtocol
private let analytics: AuthorizationAnalytics
private let validator: Validator
let storage: CoreStorage

public init(
interactor: AuthInteractorProtocol,
router: AuthorizationRouter,
config: ConfigProtocol,
analytics: AuthorizationAnalytics,
validator: Validator,
storage: CoreStorage,
sourceScreen: LogistrationSourceScreen
) {
self.interactor = interactor
self.router = router
self.config = config
self.analytics = analytics
self.validator = validator
self.storage = storage
self.sourceScreen = sourceScreen
}

Expand All @@ -81,7 +84,7 @@ public class SignInViewModel: ObservableObject {
let user = try await interactor.login(username: username, password: password)
analytics.identify(id: "\(user.id)", username: user.username, email: user.email)
analytics.userLogin(method: .password)
router.showMainOrWhatsNewScreen(sourceScreen: sourceScreen)
router.showMainOrWhatsNewScreen(sourceScreen: sourceScreen, authMethod: nil)
NotificationCenter.default.post(name: .userAuthorized, object: nil)
} catch let error {
failure(error)
Expand Down Expand Up @@ -113,7 +116,14 @@ public class SignInViewModel: ObservableObject {
let user = try await interactor.login(externalToken: externalToken, backend: backend)
analytics.identify(id: "\(user.id)", username: user.username, email: user.email)
analytics.userLogin(method: authMethod)
router.showMainOrWhatsNewScreen(sourceScreen: sourceScreen)
var socialAuthMethod: String? = nil
if case AuthMethod.socailAuth(let method) = authMethod {
socialAuthMethod = method.rawValue
}
router.showMainOrWhatsNewScreen(
sourceScreen: sourceScreen,
authMethod: socialAuthMethod
)
NotificationCenter.default.post(name: .userAuthorized, object: nil)
} catch let error {
failure(error, authMethod: authMethod)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,21 @@ public struct SignUpView: View {

let requiredFields = viewModel.requiredFields
let optionalFields = viewModel.optionalFields

if viewModel.socialAuthEnabled,
!requiredFields.isEmpty {
SocialAuthView(
authType: .register,
viewModel: .init(
config: viewModel.config,
lastUsedOption: viewModel.storage.lastUsedSocialAuth
) { result in
Task { await viewModel.register(with: result) }
}
)
.padding(.top, 22)
.padding(.bottom, -2)
}

FieldsView(
fields: requiredFields,
Expand Down Expand Up @@ -148,18 +163,6 @@ public struct SignUpView: View {
.frame(maxWidth: .infinity)
.accessibilityLabel("signup_button")
}
if viewModel.socialAuthEnabled,
!requiredFields.isEmpty {
SocialAuthView(
authType: .register,
viewModel: .init(
config: viewModel.config
) { result in
Task { await viewModel.register(with: result) }
}
)
.padding(.bottom, 30)
}
Spacer()
}
.padding(.horizontal, 24)
Expand Down Expand Up @@ -213,6 +216,7 @@ struct SignUpView_Previews: PreviewProvider {
config: ConfigMock(),
cssInjector: CSSInjectorMock(),
validator: Validator(),
storage: CoreStorageMock(),
sourceScreen: .default
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public class SignUpViewModel: ObservableObject {
private let analytics: AuthorizationAnalytics
private let validator: Validator
var authMethod: AuthMethod = .password
let storage: CoreStorage

public init(
interactor: AuthInteractorProtocol,
Expand All @@ -63,6 +64,7 @@ public class SignUpViewModel: ObservableObject {
config: ConfigProtocol,
cssInjector: CSSInjector,
validator: Validator,
storage: CoreStorage,
sourceScreen: LogistrationSourceScreen
) {
self.interactor = interactor
Expand All @@ -71,6 +73,7 @@ public class SignUpViewModel: ObservableObject {
self.config = config
self.cssInjector = cssInjector
self.validator = validator
self.storage = storage
self.sourceScreen = sourceScreen
}

Expand Down Expand Up @@ -135,7 +138,14 @@ public class SignUpViewModel: ObservableObject {
analytics.identify(id: "\(user.id)", username: user.username, email: user.email)
analytics.registrationSuccess(method: authMetod.analyticsValue)
isShowProgress = false
router.showMainOrWhatsNewScreen(sourceScreen: sourceScreen)
var socialAuthMethod: String? = nil
if case AuthMethod.socailAuth(let method) = authMethod {
socialAuthMethod = method.rawValue
}
router.showMainOrWhatsNewScreen(
sourceScreen: sourceScreen,
authMethod: socialAuthMethod
)
NotificationCenter.default.post(name: .userAuthorized, object: nil)
} catch let error {
isShowProgress = false
Expand Down Expand Up @@ -192,7 +202,15 @@ public class SignUpViewModel: ObservableObject {
analytics.identify(id: "\(user.id)", username: user.username, email: user.email)
analytics.userLogin(method: authMethod)
isShowProgress = false
router.showMainOrWhatsNewScreen(sourceScreen: sourceScreen)
var socialAuthMethod: String? = nil
if case AuthMethod.socailAuth(let method) = authMethod {
socialAuthMethod = method.rawValue
}
router.showMainOrWhatsNewScreen(
sourceScreen: sourceScreen,
authMethod: socialAuthMethod
)
setUsedAuthMethodOption(authMethod: authMethod)
NotificationCenter.default.post(
name: .userAuthorized,
object: [
Expand All @@ -208,9 +226,20 @@ public class SignUpViewModel: ObservableObject {
isShowProgress = false
self.authMethod = authMethod
await registerUser(authMetod: authMethod)
setUsedAuthMethodOption(authMethod: authMethod)
}
}

private func setUsedAuthMethodOption(authMethod: AuthMethod) {
switch authMethod {
case .socailAuth(let auth):
UserDefaults.standard.set(auth.rawValue, forKey: "lastSocialAuthUsedOption")
UserDefaults.standard.synchronize()
default:
break
}
}

private func update(fullName: String?, email: String?) {
fields.first(where: { $0.field.type == .email })?.text = email ?? ""
fields.first(where: { $0.field.name == "name" })?.text = fullName ?? ""
Expand Down
Loading

0 comments on commit 6ab9590

Please sign in to comment.