This is my fragment class:
public class MyFragment extends DaggerFragment {
@Inject MyViewModelFactory factory;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle) {
String uid = "123"; //How to pass this String??
MyViewModel viewModel = ViewModelProviders.of(this, factory).get(MyViewModel.class);
LiveData<User> liveData = viewModel.getUserLiveData();
}
}
Now this is MyViewModel
class:
public class MyViewModel extends ViewModel {
private MutableLiveData<User> liveData;
@Inject
MyViewModel(MyRepository repository) {
userLiveData = repository.addUserToLiveData();
//Here I need the value of that String "123"
}
LiveData<User> getUserLiveData() {
return liveData;
}
}
And this MyRepository
class:
@Singleton
class MyRepository {
private Retrofit retrofit;
@Inject
MyRepository(Retrofit retrofit) {
//Or here I need the value of that String "123"
}
MutableLiveData<User> addUserToLiveData() {
//Make api call using retrofit
}
}
Please also take a look at my AppModule
class:
@Module
public class AppModule {
@Singleton
@Provides
static Retrofit provideRetrofit(){
return new Retrofit.Builder()... //Initialize retrofit
}
}
And at MyViewModelModule
class:
@Module
abstract class MyViewModelModule {
@Binds
abstract ViewModelProvider.Factory bindMyViewModelFactory(MyViewModelFactory factory);
@Binds
@IntoMap
@ViewModelKey(MyViewModel.class)
abstract ViewModel provideMyViewModel(MyViewModel viewModel);
}
What I have tried is to add the string in the ViewModel/repository constructor but without any luck. Please help me pass that uid
to the ViewModel or repository class. Thanks!