diff --git a/BeReal/UI/Views/ContentView.swift b/BeReal/UI/Views/ContentView.swift index aa429f0..94b4f2a 100644 --- a/BeReal/UI/Views/ContentView.swift +++ b/BeReal/UI/Views/ContentView.swift @@ -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 + ) } }