ok so If I am getting correctly then Account will have code for validation, network call, caching, routing, etc...
// State (shared in view hierarchy), part of your model
class Account: ObservableObject {
@Published var isLogged: Bool = false
func signIn(email: String, password: String) async throws {
// VALIDATION CODE ****1
// NETWORK CALL (we will have shared network object) ****2
// PERSISTENT (we will have shared persistent object) ****3
// ROUTE TO NEXT SCREEN (we will have global navigator or screen router) ****4
}
}
What we are achieving by giving so much responsibility to Account model.
Instead can't we just go with simple with SignInViewModel, also Validation, Persistency and Routing will not be tight together in Account.
See what I am thinking, and let's justify which one is better.
class SignInViewModel {
func signIn(email: String, password: String) async throws {
// NETWORK CALL (we will have shared network object) ****2
}
}
// View
struct SignInView: View {
@EnvironmentObject private var account: Account
@State private var email: String
@State private var password: String
var body: some View { ... }
// Call from button
func signIn() {
// WE CAN CALL VALIDATION UTILITY FROM HERE ****1
Task {
do {
try await signInViewModel.signIn(email: email, password: password)
// WE CAN CALL PERSISTENT UTILITY FROM HERE ****3
// THEN ROUTER CAN ROUTE TO NEXT VIEW ****4
} catch {
// Change error state
}
}
}
}
Topic:
UI Frameworks
SubTopic:
SwiftUI
Tags: