Hi there !
At first thank you for providing this repository and your related blog posts. I enjoyed reading them and they helped me a lot.
But I have a question about reacting to @State
changes in View
s in this architecture.
For example I have the following case in my application:
I have a view which displays a list of tasks for a specific date interval. It has your dependency injection container injected:
@Environment(\.injected) private var injected: DIContainer
@State private var dateInterval: DateInterval = DateInterval(start: Date().startOfWeek ?? Date(), end: Date().endOfWeek ?? Date())
Initially the data is fetched with a method that looks like this:
func fetch() {
injected.interactors.myInteractor
.fetchData(range: self.dateInterval)
.store(in: cancelBag)
}
The view has a child which accepts a binding to the dateInterval
and is able to change it:
WeeksTaskGraph(dateInterval: self.$dateInterval, tasks: tasks)
Now I need to refetch data when the binding changes. So basically I would need to run the fetch
method again when the dateInterval
changes. I tried to create an ObservableObject
ViewModel class for the view, for encapsulating this kind of functionality. It looks roughly like this:
class DateIntervalModel: ObservableObject {
private let cancelBag = CancelBag()
@Environment(\.injected) private var injected: DIContainer
@Published var dateInterval: DateInterval = DateInterval(start: Date().startOfWeek ?? Date(), end: Date().endOfWeek ?? Date()) {
didSet {
print("Date interval changed")
injected.interactors.myInteractor.fetchData(range: self.dateInterval)
.store(in: cancelBag)
}
}
}
But as seen this class would then need access to the @EnvironmentObject
which I could not find a solution how to achieve that - since it is not part of the view hierarchy.
Do you maybe have an approach or a suggestion how this can be achieved with the clean architecture ?
Any help is appreciated ! If this is the wrong place for asking this kind of questions feel free to close the issue or tell me so.