Description
Based on the documentation: IdentifiableType
: The identity
provided by the IdentifiableType
protocol must be an immutable identifier representing an instance of the model. For example, in case of a Car
model, you might want to use the car's plateNumber
as its identity.
I am getting a bit confused on how to provide an immutable identifier in the case of a Unidirectional Data Flow
.
Take as an example a Restaurant screen with some menu items:
struct RestaurantViewModel: Equatable {
enum State: Equatable {
case failed(Error)
case initialized
case loaded(Restaurant)
case loading
}
enum ViewModelType: IdentifiableType {
case failure(ErrorViewModel)
case hero(RestaurantHeroViewModel)
case item(RestaurantItemViewModel)
var identity: String {
return ????
}
}
let state: State
let viewModels: [ViewModelType]
init(with state: State) {
self.state = state
switch state {
// build array of ViewModelType depending on the state
}
}
}
struct ErrorViewModel: Equatable {
let description: String
}
struct RestaurantHeroViewModel: Equatable {
let name: LoadableTextViewModel
}
struct LoadableTextViewModel: Equatable {
enum State: Equatable {
case initialized
case loaded(NSAttributedString?)
case loading
}
let state: State
let text: NSAttributedString?
init(state: State) {
self.state = state
switch state {
// set text depending on the state
}
}
}
With this approach in mind, it is easy to make all the ViewModels conform to Equatable, but how can you give an immutable identifier to those as they are immutable ViewModels renewed at each state change?
I am a bit confused here as the example projects only handle a basic case with unique numbers on the model, whereas in my case there is no model involved except when the Restaurant is fetched successfully.
Thank you.