Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Providers #58

Open
ohitsdaniel opened this issue Mar 9, 2021 · 0 comments
Open

Add Providers #58

ohitsdaniel opened this issue Mar 9, 2021 · 0 comments
Labels
enhancement New feature or request

Comments

@ohitsdaniel
Copy link
Contributor

Idea

Add providers for dependencies / ViewModels. Inspired by Flutter's providers, let's add a Provider view that initialises a dependency when needed and keeps its reference. Also inspired by TCA's WithViewStore, that moves all observable object observation out of the observing view and instead wraps the observation in WithViewStore, updating it's content whenever the store emits a change.

Problem description

Currently, ComposableNavigator is very TCA focused and assumes that people "hand-down" their view models into the NavigationTree. Flutter solves this by wrapping Widgets in ProviderWidgets that take care of initialising the Widget's dependencies and handing it down the WidgetTree through the context.

Considered solutions

  • Provider exposing the initialised dependency via the environment
    -> Would need all dependencies to be ObservableObjects, potentially crashing the app when an EnvironmentObject is not initialised.
  • Explicitly passing the dependency into a build content closure ✅
struct DetailScreen: Screen {
  let presentationStyle: ScreenPresentationStyle = .push
  
  struct Builder<ViewModel: DetailViewModel & ObservableObject>: NavigationTree {
     let viewModel: () -> ViewModel
     
     var builder: some PathBuilder {
        Screen(
          DetailScreen.self,
          content: { 
            Provider(
              observing: viewModel,
              content: { viewModel in 
                // DetailView.init(viewModel: DetailViewModel), no ObservableObject needed here.
                DetailView(viewModel: viewModel) 
              }
          }
        ) 
     }
  }
}
@ohitsdaniel ohitsdaniel added the enhancement New feature or request label Mar 9, 2021
@ohitsdaniel ohitsdaniel mentioned this issue Mar 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant