Jossy Paul
1 min readAug 28, 2021

--

Thanks Serge.

1. You are right. We can achieve similar behaviour by creating fields of delegated classes inside the viewModel. But we would have to access everything through those fields. And then if we combine some data in separate classes inside viewModel then those new data will have to be accessed directly from viewModel. But if we are using kotlin delegates we will be accessing everything directly from the viewModel. I feel this would look more cleaner while we access these liveDatas or StatesFlow from the view.

2. If we need to inject CoroutineScope to each delegate, we would need to add our CoroutineScope to the dependency graph manually. I think something like this would do the trick:

@Module

@InstallIn(ViewModelComponent::class)

object ViewModelModule {

@ViewModelScoped

@Provides

fun providesCoroutineScope(): CoroutineScope {

return CoroutineScope(SupervisorJob() + Dispatchers.Main)

}

}

And then update the delegate constructor:

class AddNotesDelegateImpl @Inject constructor(private val coroutineScope: CoroutineScope, private val repository: NotesRepository) :

AddNotesDelegate

I know this is some extra work we are doing, coz we google is already providing viewModelScope. Let me know your take on this.

--

--

Jossy Paul
Jossy Paul

Responses (1)