Integrated the GetUserUseCase use case into the ContentView view for the BeReal app target.

This commit is contained in:
Javier Cicchelli 2022-12-13 00:03:26 +01:00
parent 07ffd2bf80
commit c1c25c356d

View File

@ -12,6 +12,7 @@ import Login
import KeychainStorage
import Profile
import SwiftUI
import UseCases
struct ContentView: View {
@ -24,6 +25,10 @@ struct ContentView: View {
@State private var user: User?
@State private var showSheet: SheetView?
// MARK: Properties
private let getUser: GetUserUseCase = .init()
// MARK: Body
var body: some View {
@ -36,12 +41,6 @@ struct ContentView: View {
showSheet = .profile
}
}
.onAppear {
shouldShowLogin()
}
.onChange(of: account) { _ in
shouldShowLogin()
}
.sheet(item: $showSheet) { sheet in
switch sheet {
case .login:
@ -56,6 +55,9 @@ struct ContentView: View {
}
}
}
.task(id: account) {
await loadUserOrLogin()
}
}
}
@ -63,10 +65,17 @@ struct ContentView: View {
// MARK: - Helpers
private extension ContentView {
func shouldShowLogin() {
showSheet = account == nil
? .login
: nil
func loadUserOrLogin() async {
guard let account else {
showSheet = .login
return
}
showSheet = nil
user = try? await getUser(
username: account.username,
password: account.password
)
}
}