Created
September 13, 2022 23:35
-
-
Save benlmyers/32cc162a8927b9956035e3ebaa1322d1 to your computer and use it in GitHub Desktop.
A simple protocol that defines a ViewModel for MVVM architecture. Requires a `Global` environment object used for dependency injection.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
/** | |
A protocol for MVVM-architecture. | |
View models manage most (but not all) of a view's logic. When handling large amounts of logic related to a particular view or views, it's useful to have a view model to manage the logic. | |
View models act as the bridge between models and views in MVVM architecture. | |
# Example | |
View models, when declared in a view's class scope, must be declared as a property in one of two ways. | |
__1. Instantiating First__ | |
When first instantiating a view model within a view's scope, the `@StateObject` wrapper should be used. | |
@StateObject var viewModel = SomeViewModel() | |
__2. Passing Second__ | |
When passing a view model from an instantiated view to another view, the `@ObservedObject` wrapped should be used. | |
@ObservedObject var viewModel: SomeViewModel | |
# Troubleshooting | |
If views fail to refresh when updating properties in view models, use the following checks: | |
1. Ensuring the property is declared within the view model using the `@Published` wrapper | |
2. Changing `@ObservedObject` to `@StateObject` when defining a view model property within a view | |
*/ | |
protocol ViewModel: ObservableObject { | |
// MARK: - Properties | |
/// The global instance | |
var global: Global? { get set } | |
} | |
extension ViewModel { | |
// MARK: - Methods | |
/** | |
Prepares the view model with the global instance. | |
- parameter global: The global instance, taken from a `View` | |
*/ | |
func prepare(with global: Global) { | |
self.global = global | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment