Skip to content

Instantly share code, notes, and snippets.

@joecks
Created August 21, 2017 17:05
Show Gist options
  • Save joecks/a0da0874165f69961a222e6c1e0ecc55 to your computer and use it in GitHub Desktop.
Save joecks/a0da0874165f69961a222e6c1e0ecc55 to your computer and use it in GitHub Desktop.
// Definition of the provided dependencies of the MainActivity
class MainActivityDependencies(private val activity: FragmentActivity) : Dependencies {
override fun taskRepository(): TasksRepository {
return TasksRepository.getInstance(TasksRemoteDataSource.instance, TasksLocalDataSource.getInstance(activity))
}
fun mainViewModel(): MainViewModel = ViewModelProviders.of(activity).get(MainViewModel::class.java)
fun tasksActions(): TasksActions = mainViewModel()
fun addEditActions(): AddEditActions = mainViewModel()
}
// TasksFragment when creating the view model,
// method will be called when the ViewModelFactory needs a ViewModel in the BaseReactFragment
override fun createViewModel() : TasksViewModel {
val dependencies = (activity as DependencyProvider<MainActivityDependencies>).dependencies
return TasksViewModel(dependencies.taskRepository(), dependencies.tasksActions())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment