Updated the LoginView and the ContentView views based on the changes to the GetuseUseCase use case.

This commit is contained in:
Javier Cicchelli 2022-12-18 16:12:56 +01:00
parent c25ca4cf0f
commit 9045853db9
3 changed files with 27 additions and 44 deletions

View File

@ -9,17 +9,12 @@
import Browse import Browse
import DataModels import DataModels
import Login import Login
import KeychainStorage
import Profile import Profile
import SwiftUI import SwiftUI
import UseCases import UseCases
struct ContentView: View { struct ContentView: View {
// MARK: Storages
@KeychainStorage(key: .KeychainStorage.account) private var account: Account?
// MARK: States // MARK: States
@State private var user: User? @State private var user: User?
@ -33,10 +28,6 @@ struct ContentView: View {
var body: some View { var body: some View {
Container(user: user) { Container(user: user) {
// TODO: create a new folder
} uploadFile: {
// TODO: upload a new file
} showProfile: {
showSheet = .profile showSheet = .profile
} login: { } login: {
showSheet = .login showSheet = .login
@ -45,17 +36,18 @@ struct ContentView: View {
switch sheet { switch sheet {
case .login: case .login:
LoginView { LoginView {
account = $0 user = $0
user = $1
} }
case .profile: case .profile:
ProfileView(user: user) { ProfileView(user: user) {
account = nil
user = nil user = nil
} }
} }
} }
.task(id: account) { .onChange(of: user) {
showSheet = $0 == nil ? .login : nil
}
.task {
await loadUserOrLogin() await loadUserOrLogin()
} }
} }
@ -70,8 +62,6 @@ private extension ContentView {
// MARK: Properties // MARK: Properties
let user: User? let user: User?
let createFolder: ActionClosure
let uploadFile: ActionClosure
let showProfile: ActionClosure let showProfile: ActionClosure
let login: ActionClosure let login: ActionClosure
@ -102,16 +92,11 @@ private extension ContentView {
private extension ContentView { private extension ContentView {
func loadUserOrLogin() async { func loadUserOrLogin() async {
guard let account else { do {
showSheet = .login user = try await getUser()
return } catch {
// TODO: Handle this error appropriately.
} }
showSheet = nil
user = try? await getUser(
username: account.username,
password: account.password
)
} }
} }

View File

@ -8,7 +8,7 @@
import Foundation import Foundation
public struct User { public struct User: Equatable {
// MARK: Properties // MARK: Properties
@ -30,7 +30,7 @@ public struct User {
// MARK: - Structs // MARK: - Structs
extension User { extension User {
public struct Profile { public struct Profile: Equatable {
// MARK: Properties // MARK: Properties
@ -49,7 +49,7 @@ extension User {
} }
public struct RootFolder { public struct RootFolder: Equatable {
// MARK: Properties // MARK: Properties

View File

@ -34,7 +34,7 @@ public struct LoginView: View {
.vertical, .vertical,
showsIndicators: false showsIndicators: false
) { ) {
LoginContainer(authenticated: authenticated) Container(authenticated)
.padding(.horizontal, 24) .padding(.horizontal, 24)
.padding(.top, containerTopPadding) .padding(.top, containerTopPadding)
} }
@ -52,7 +52,7 @@ public struct LoginView: View {
// MARK: - Views // MARK: - Views
fileprivate extension LoginView { fileprivate extension LoginView {
struct LoginContainer: View { struct Container: View {
// MARK: States // MARK: States
@ -60,12 +60,16 @@ fileprivate extension LoginView {
@State private var username: String = "" @State private var username: String = ""
@State private var password: String = "" @State private var password: String = ""
@State private var errorMessage: String? @State private var errorMessage: String?
// MARK: Properties // MARK: Properties
private var getUser: GetUserUseCase = .init()
let authenticated: AuthenticatedClosure private let authenticated: AuthenticatedClosure
private let getUser: GetUserUseCase = .init() init(_ authenticated: @escaping AuthenticatedClosure) {
self.authenticated = authenticated
}
// MARK: Body // MARK: Body
@ -122,7 +126,7 @@ fileprivate extension LoginView {
// MARK: - Helpers // MARK: - Helpers
private extension LoginView.LoginContainer { private extension LoginView.Container {
// MARK: Computed // MARK: Computed
@ -148,13 +152,7 @@ private extension LoginView.LoginContainer {
// Added some throttle (1 second) not to hide the loading indicator right away. // Added some throttle (1 second) not to hide the loading indicator right away.
try await Task.sleep(nanoseconds: .Constants.secondInNanoseconds) try await Task.sleep(nanoseconds: .Constants.secondInNanoseconds)
authenticated( authenticated(user)
.init(
username: username,
password: password
),
user
)
} catch APIClientError.authenticationFailed { } catch APIClientError.authenticationFailed {
errorMessage = "login.error.authentication_failed.text" errorMessage = "login.error.authentication_failed.text"
} catch { } catch {
@ -166,7 +164,7 @@ private extension LoginView.LoginContainer {
// MARK: - Type aliases // MARK: - Type aliases
public typealias AuthenticatedClosure = (Account, User) -> Void public typealias AuthenticatedClosure = (User) -> Void
// MARK: - UInt64+Constants // MARK: - UInt64+Constants
@ -180,8 +178,8 @@ private extension UInt64 {
struct LoginView_Previews: PreviewProvider { struct LoginView_Previews: PreviewProvider {
static var previews: some View { static var previews: some View {
LoginView { _, _ in LoginView { _ in
// closure for authenticated action. // authenticated closure.
} }
} }
} }