Issue
I'm trying to send params from my ViewModel to my ViewRepository but I don't understan how can I send some params.
For example this is my observer in my fragment:
apoyaLoginViewModel.getPostLoginApoya(tokenApoya, usuario, password).observe(getActivity(), new Observer<PostLoginApoya>() {
@Override
public void onChanged(PostLoginApoya postLoginApoya) {
loginApoyaModel = postLoginApoya;
}
});
I'm sending some params in this line:
getPostLoginApoya(tokenApoya, usuario, password)
And this is my ViewModel:
public class ApoyaLoginViewModel extends AndroidViewModel {
private ApoyaLoginViewRepositori apoyaLoginViewRepositori;
private LiveData<PostLoginApoya> postLoginApoya;
public ApoyaLoginViewModel(Application aplication){
super(aplication);
apoyaLoginViewRepositori = new ApoyaLoginViewRepositori();
postLoginApoya = apoyaLoginViewRepositori.loginApoyaUser;
}
public LiveData<PostLoginApoya> getPostLoginApoya(String tokenApoya, String usuario, String password){return postLoginApoya;}
}
And this is a fragment of my ViewRepository:
ApoyaLoginViewRepositori(){
seccion15ServerClient = Seccion15ServerClient.getInstance();
seccionApiService = seccion15ServerClient.getSeccionApiService();
loginApoyaUser = getLoginUser();
}
public MutableLiveData<PostLoginApoya> getLoginUser(String tokenApoya, String usuario, String password){
if(loginApoyaUser == null){
loginApoyaUser = new MutableLiveData<>();
}
But I'm getting an error in this line:
loginApoyaUser = getLoginUser();
This is because my method getLoginUser has 3 parameters but my constructor no. maybe this is not the correct way to send information between ViewModel and ViewRepository.
How can I send this params to my constructor in my ViewRepository
Solution
You don't have to pass any argument in getPostLoginApoya
, create a separate method for that: loginApoyaUser(token, usuario, password)
. and call this method whenever you want to login the user, you will automatically receive an event with the logged in user.
fragment:
viewModel.getPostLoginApoya().observe(getActivity(), new Observer<PostLoginApoya>() {
@Override
public void onChanged(PostLoginApoya postLoginApoya) {
// do something with your user here
}
});
//you have to call this method somewhere, when you click on a button for example.
viewModel.loginApoyaUser(token, usuario, password);
ViewModel:
public class ApoyaLoginViewModel extends AndroidViewModel {
private ApoyaLoginViewRepositori apoyaLoginViewRepositori;
private LiveData<PostLoginApoya> postLoginApoya;
public ApoyaLoginViewModel(@NonNull Application application) {
super(application);
apoyaLoginViewRepositori = new ApoyaLoginViewRepositori();
postLoginApoya = apoyaLoginViewRepositori.getPostLoginApoya();
}
public LiveData<PostLoginApoya> getPostLoginApoya(){
return postLoginApoya;
}
public void loginApoyaUser(String tokenApoya, String usuario, String password) {
apoyaLoginViewRepositori.loginApoyaUser(tokenApoya, usuario, password);
}
}
Repo:
public class ApoyaLoginViewRepositori {
private MutableLiveData<PostLoginApoya> postLoginApoyaLiveData;
private PostLoginApoya postLoginApoya;
public ApoyaLoginViewRepositori() {
postLoginApoyaLiveData = new MutableLiveData<>();
}
public LiveData<PostLoginApoya> getPostLoginApoya() {
return postLoginApoyaLiveData;
}
public void loginApoyaUser(String tokenApoya, String usuario, String password) {
postLoginApoya = //login user here
//notify observers data has been changed
postLoginApoyaLiveData.postValue(postLoginApoya);
}
}
Answered By - Abdo21
Answer Checked By - David Marino (JavaFixing Volunteer)