Integrated the GetUserUseCase use case into the LoginView view.

This commit is contained in:
Javier Cicchelli 2022-12-12 00:55:37 +01:00
parent 27d2848a45
commit 332b8ab245

View File

@ -7,10 +7,6 @@
//
import APIService
import DataModels
import DependencyInjection
import Dependencies
import KeychainStorage
import SwiftUI
public struct LoginView: View {
@ -19,9 +15,15 @@ public struct LoginView: View {
@State private var containerTopPadding: CGFloat = 0
// MARK: Properties
private let success: LoginSuccessClosure
// MARK: Initialisers
public init() {}
public init(success: @escaping LoginSuccessClosure) {
self.success = success
}
// MARK: Body
@ -30,7 +32,7 @@ public struct LoginView: View {
.vertical,
showsIndicators: false
) {
LoginContainer()
LoginContainer(success: success)
.padding(.horizontal, 24)
.padding(.top, containerTopPadding)
}
@ -39,6 +41,7 @@ public struct LoginView: View {
.onPreferenceChange(ViewHeightPreferenceKey.self) { height in
containerTopPadding = height * 0.1
}
.interactiveDismissDisabled()
}
}
@ -47,21 +50,23 @@ public struct LoginView: View {
fileprivate extension LoginView {
struct LoginContainer: View {
// MARK: Dependencies
@Dependency(\.apiService) private var apiService
// MARK: Storages
@KeychainStorage(key: .KeychainStorage.account) private var account: Account?
// MARK: States
@State private var isAuthenticating: Bool = false
@State private var username: String = ""
@State private var password: String = ""
@State private var errorMessage: String?
// MARK: Properties
private let getUser: GetUserUseCase
// MARK: Initialisers
init(success: @escaping LoginSuccessClosure) {
self.getUser = .init(success: success)
}
// MARK: Body
@ -133,12 +138,7 @@ private extension LoginView.LoginContainer {
guard isAuthenticating else { return }
do {
_ = try await apiService.getUser(credentials: .init(
username: username,
password: password
))
account = .init(
try await getUser(
username: username,
password: password
)
@ -157,6 +157,6 @@ private extension LoginView.LoginContainer {
struct LoginView_Previews: PreviewProvider {
static var previews: some View {
LoginView()
LoginView { _, _ in }
}
}