Updated the LoginView and the ContentView views based on the changes to the GetuseUseCase use case.
This commit is contained in:
parent
c25ca4cf0f
commit
9045853db9
@ -9,17 +9,12 @@
|
||||
import Browse
|
||||
import DataModels
|
||||
import Login
|
||||
import KeychainStorage
|
||||
import Profile
|
||||
import SwiftUI
|
||||
import UseCases
|
||||
|
||||
struct ContentView: View {
|
||||
|
||||
// MARK: Storages
|
||||
|
||||
@KeychainStorage(key: .KeychainStorage.account) private var account: Account?
|
||||
|
||||
// MARK: States
|
||||
|
||||
@State private var user: User?
|
||||
@ -33,10 +28,6 @@ struct ContentView: View {
|
||||
|
||||
var body: some View {
|
||||
Container(user: user) {
|
||||
// TODO: create a new folder
|
||||
} uploadFile: {
|
||||
// TODO: upload a new file
|
||||
} showProfile: {
|
||||
showSheet = .profile
|
||||
} login: {
|
||||
showSheet = .login
|
||||
@ -45,17 +36,18 @@ struct ContentView: View {
|
||||
switch sheet {
|
||||
case .login:
|
||||
LoginView {
|
||||
account = $0
|
||||
user = $1
|
||||
user = $0
|
||||
}
|
||||
case .profile:
|
||||
ProfileView(user: user) {
|
||||
account = nil
|
||||
user = nil
|
||||
}
|
||||
}
|
||||
}
|
||||
.task(id: account) {
|
||||
.onChange(of: user) {
|
||||
showSheet = $0 == nil ? .login : nil
|
||||
}
|
||||
.task {
|
||||
await loadUserOrLogin()
|
||||
}
|
||||
}
|
||||
@ -70,8 +62,6 @@ private extension ContentView {
|
||||
// MARK: Properties
|
||||
|
||||
let user: User?
|
||||
let createFolder: ActionClosure
|
||||
let uploadFile: ActionClosure
|
||||
let showProfile: ActionClosure
|
||||
let login: ActionClosure
|
||||
|
||||
@ -102,16 +92,11 @@ private extension ContentView {
|
||||
|
||||
private extension ContentView {
|
||||
func loadUserOrLogin() async {
|
||||
guard let account else {
|
||||
showSheet = .login
|
||||
return
|
||||
do {
|
||||
user = try await getUser()
|
||||
} catch {
|
||||
// TODO: Handle this error appropriately.
|
||||
}
|
||||
|
||||
showSheet = nil
|
||||
user = try? await getUser(
|
||||
username: account.username,
|
||||
password: account.password
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -8,7 +8,7 @@
|
||||
|
||||
import Foundation
|
||||
|
||||
public struct User {
|
||||
public struct User: Equatable {
|
||||
|
||||
// MARK: Properties
|
||||
|
||||
@ -30,7 +30,7 @@ public struct User {
|
||||
// MARK: - Structs
|
||||
|
||||
extension User {
|
||||
public struct Profile {
|
||||
public struct Profile: Equatable {
|
||||
|
||||
// MARK: Properties
|
||||
|
||||
@ -49,7 +49,7 @@ extension User {
|
||||
|
||||
}
|
||||
|
||||
public struct RootFolder {
|
||||
public struct RootFolder: Equatable {
|
||||
|
||||
// MARK: Properties
|
||||
|
||||
|
@ -34,7 +34,7 @@ public struct LoginView: View {
|
||||
.vertical,
|
||||
showsIndicators: false
|
||||
) {
|
||||
LoginContainer(authenticated: authenticated)
|
||||
Container(authenticated)
|
||||
.padding(.horizontal, 24)
|
||||
.padding(.top, containerTopPadding)
|
||||
}
|
||||
@ -52,7 +52,7 @@ public struct LoginView: View {
|
||||
// MARK: - Views
|
||||
|
||||
fileprivate extension LoginView {
|
||||
struct LoginContainer: View {
|
||||
struct Container: View {
|
||||
|
||||
// MARK: States
|
||||
|
||||
@ -63,9 +63,13 @@ fileprivate extension LoginView {
|
||||
|
||||
// MARK: Properties
|
||||
|
||||
let authenticated: AuthenticatedClosure
|
||||
private var getUser: GetUserUseCase = .init()
|
||||
|
||||
private let getUser: GetUserUseCase = .init()
|
||||
private let authenticated: AuthenticatedClosure
|
||||
|
||||
init(_ authenticated: @escaping AuthenticatedClosure) {
|
||||
self.authenticated = authenticated
|
||||
}
|
||||
|
||||
// MARK: Body
|
||||
|
||||
@ -122,7 +126,7 @@ fileprivate extension LoginView {
|
||||
|
||||
// MARK: - Helpers
|
||||
|
||||
private extension LoginView.LoginContainer {
|
||||
private extension LoginView.Container {
|
||||
|
||||
// MARK: Computed
|
||||
|
||||
@ -148,13 +152,7 @@ private extension LoginView.LoginContainer {
|
||||
// Added some throttle (1 second) not to hide the loading indicator right away.
|
||||
try await Task.sleep(nanoseconds: .Constants.secondInNanoseconds)
|
||||
|
||||
authenticated(
|
||||
.init(
|
||||
username: username,
|
||||
password: password
|
||||
),
|
||||
user
|
||||
)
|
||||
authenticated(user)
|
||||
} catch APIClientError.authenticationFailed {
|
||||
errorMessage = "login.error.authentication_failed.text"
|
||||
} catch {
|
||||
@ -166,7 +164,7 @@ private extension LoginView.LoginContainer {
|
||||
|
||||
// MARK: - Type aliases
|
||||
|
||||
public typealias AuthenticatedClosure = (Account, User) -> Void
|
||||
public typealias AuthenticatedClosure = (User) -> Void
|
||||
|
||||
// MARK: - UInt64+Constants
|
||||
|
||||
@ -180,8 +178,8 @@ private extension UInt64 {
|
||||
|
||||
struct LoginView_Previews: PreviewProvider {
|
||||
static var previews: some View {
|
||||
LoginView { _, _ in
|
||||
// closure for authenticated action.
|
||||
LoginView { _ in
|
||||
// authenticated closure.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user