I'm trying to build an app to fetch list of feed from server and display in Recyclerview. I am trying out basic implementation of LiveData like this.
I have set up an observer in my Fragment as follows:
viewModel = ViewModelProviders.of(getActivity()).get(SellViewModel.class);
viewModel.getSellItemList(19).observe(this, new Observer<List<LambdaSellRequestClass>>() {
#Override
public void onChanged(#Nullable List<LambdaSellRequestClass> sellItems) {
adapter.setSellEntities(sellItems);
}
});
My SellViewModel clas like this:
public class SellViewModel extends AndroidViewModel {
private SellRepository repository;
private MutableLiveData<List<LambdaSellRequestClass>> sellItems;
public SellViewModel(#NonNull Application application) {
super(application);
repository = new SellRepository(application);
try {
if (sellItems == null) {
sellItems = new MutableLiveData<>();
sellItems.postValue(repository.getSellItemList(user_id));
}
}catch (Exception e) {
Log.d("SELLFRAGMENT", "Error: " + e.getLocalizedMessage());
}
}
public MutableLiveData<List<LambdaSellRequestClass>> getSellItemList(int userId) throws ExecutionException, InterruptedException {
return sellItems;
}
}
My SellRepository like this:
public class SellRepository {
public SellRepository(Application application) {
}
public List<LambdaSellRequestClass> getSellItemList(int userId) throws ExecutionException, InterruptedException {
return new SellRepository.GetSellItemListAsync(SellRepository.this).execute(userId).get();
}
private static class GetSellItemListAsync extends AsyncTask<Integer, Void, List<LambdaSellRequestClass>> {
List<LambdaSellRequestClass> list = new ArrayList<>();
public GetSellItemListAsync() {
}
#Override
protected List<LambdaSellRequestClass> doInBackground(Integer... integers) {
final int userID = integers[0];
list =
lambdaFunctionsCalls.getSellItemByUser_lambda(requestClass).getSellItems();
return list;
}
}
My problem is when I add new sell items to database its not update mobile app.
Related
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
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);
}
}
I checked this article but observe the response changes in MainActivity.
Here is my code for LoginRepo
public MutableLiveData<LoginResponseModel> checkLogin(LoginRequestModel loginRequestModel) {
final MutableLiveData<LoginResponseModel> data = new MutableLiveData<>();
Map<String, String> params = new HashMap<>();
params.put("email", loginRequestModel.getEmail());
params.put("password", loginRequestModel.getPassword());
apiService.checkLogin(params)
.enqueue(new Callback<LoginResponseModel>() {
#Override
public void onResponse(Call<LoginResponseModel> call, Response<LoginResponseModel> response) {
if (response.isSuccessful()) {
data.setValue(response.body());
Log.i("Response ", response.body().getMessage());
}
}
#Override
public void onFailure(Call<LoginResponseModel> call, Throwable t) {
data.setValue(null);
}
});
return data;
}
Here is my Code LoginViewModel
public class LoginViewModel extends ViewModel {
public MutableLiveData<String> emailAddress = new MutableLiveData<>();
public MutableLiveData<String> password = new MutableLiveData<>();
Map<String, String> params = new HashMap<>();
LoginRepo loginRepo;
private MutableLiveData<LoginResponseModel> loginResponseModelMutableLiveData;
public LiveData<LoginResponseModel> getUser() {
if (loginResponseModelMutableLiveData == null) {
loginResponseModelMutableLiveData = new MutableLiveData<>();
loginRepo = LoginRepo.getInstance();
}
return loginResponseModelMutableLiveData;
}
//This method is using Retrofit to get the JSON data from URL
private void checkLogin(LoginRequestModel loginRequestModel) {
loginResponseModelMutableLiveData = loginRepo.checkLogin(loginRequestModel);
}
public void onLoginClick(View view) {
LoginRequestModel loginRequestModel = new LoginRequestModel();
loginRequestModel.setEmail(emailAddress.getValue());
loginRequestModel.setPassword(password.getValue());
params.put("email", loginRequestModel.getEmail());
params.put("password", loginRequestModel.getPassword());
checkLogin(loginRequestModel);
}
}
Here is my code for LoginActivity
private LoginViewModel loginViewModel;
private ActivityMainBinding binding;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
loginViewModel = ViewModelProviders.of(this).get(LoginViewModel.class);
binding = DataBindingUtil.setContentView(LoginActivity.this, R.layout.activity_main);
binding.setLifecycleOwner(this);
binding.setLoginViewModel(loginViewModel);
loginViewModel.getUser().observe(this, new Observer<LoginResponseModel>() {
#Override
public void onChanged(#Nullable LoginResponseModel loginUser) {
if (loginUser != null) {
binding.lblEmailAnswer.setText(loginUser.getUser().getId());
Toast.makeText(getApplicationContext(), loginUser.getUser().getId(), Toast.LENGTH_SHORT).show();
}
}
});
}
onLoginClick method used in LoginViewModel is using LiveData.
The Response coming from api is okay. But onchange() it is not shown, how to use LiveData using MVVM pattern in simple Login Example. Please help!
Here is what i have tried using your classes just altering retrofit to background thread to wait 5 seconds and then setting the data (you need to confirm the response being successful as you don't change the data if it's failing and hence if the loginResponseModel is null then it will enter the onChanged Method but it won't do anything as you don't have a condition if it is equals to null) here is what i did
in Main Activity -> onCreate() i just created the viewmodel and observed on the mutableLiveData
myViewModel.onLoginClick(null);
myViewModel.simpleModelMutableLiveData.observe(this, new Observer<String>() {
#Override
public void onChanged(#Nullable String s) {
if(s==null)
Log.v("testinggg","test - onChanged --- Null " );
else
Log.v("testinggg","test - onChanged --- s -> "+s );
}
});
Then here is the ViewModel -> in which you will have the MutableLiveData itself named simpleModelMutableLiveData
MutableLiveData<String> simpleModelMutableLiveData;
public LiveData<String> getUser() {
if (simpleModelMutableLiveData == null) {
simpleModelMutableLiveData = new MutableLiveData<>();
}
return simpleModelMutableLiveData;
}
// this method will return Object of MutableLiveData<String> and let the simpleModelMutableLiveData be the returned object
private void checkLogin(String placeholder) {
simpleModelMutableLiveData = MyRepo.checkLogin(placeholder);
}
public void onLoginClick(View view) {
checkLogin("test");
}
and at last the Repo method in which i will return the MutableLiveData and let the simpleModelMutableLiveData to be the return and initiate a background thread using runnable that will wait 5 seconds before it sets the value using a handler (in your case you will need to set the value of the data after enqueue inside the Overridden Methods onResponse and onFailure)
as follows
public static MutableLiveData<String> checkLogin(String test) {
final MutableLiveData<String> data = new MutableLiveData<>();
Runnable r = new Runnable() {
public void run() {
runYourBackgroundTaskHere(data);
}
};
new Thread(r).start();
return data;
}
private static void runYourBackgroundTaskHere(final MutableLiveData<String> data) {
try {
Thread.sleep(5000);
// Handler handler = new Handler();
new Handler(Looper.getMainLooper()).post(new Runnable() {
#Override
public void run() {
// things to do on the main thread
/* Here i set the data to sss and then null and when
you check the logcat and type the keyword used for
logging which is "testinggg"
you will find it show sss and then null which means
it has entered the onChanged and showed you the log */
data.setValue("sss");
data.setValue(null);
}
});
} catch (InterruptedException e) {
e.printStackTrace();
}
}
I'm trying to transform a response from server to my databinding object... I didn't understand too much how i make that with the Transformations of livedata...
I think i'm i need to change few things, but i didnt find what i need to change...
:(
When i call loadSellers(), it dont enter on switchMap function
Can someone help me?
public class SellersViewModel extends BaseViewModel {
private EzGasRepository repository;
private MutableLiveData<List<Seller>> sellers;
#Inject
public SellersViewModel(EzGasRepository repository) {
this.repository = repository;
}
public LiveData<List<Seller>> fetchAllSellers() {
if (sellers == null) {
sellers = new MutableLiveData<>();
loadSellers();
}
return sellers;
}
private LiveData<List<Seller>> loadSellers() {
return Transformations.switchMap(repository.fetchAllSellers(), input -> {
List<Seller> sellerList = new ArrayList<>();
for (SellerResponse sellerResponse: input) {
Seller seller = new Seller();
seller.setSellerName(sellerResponse.getName());
seller.setRating(sellerResponse.getRating());
seller.setProductValue(sellerResponse.getProduct().getValue());
seller.setSellerAddress(sellerResponse.getAddress().getFormattedAddress());
sellerList.add(seller);
}
return sellers;
});
}
}
This is my repository
public LiveData<List<SellerResponse>> fetchAllSellers() {
MutableLiveData<List<SellerResponse>> data = new MutableLiveData<>();
ezGasApi.fetchAllSellers().enqueue(new Callback<List<SellerResponse>>() {
#Override
public void onResponse(Call<List<SellerResponse>> call, Response<List<SellerResponse>> response) {
if(response.isSuccessful()) {
data.setValue(response.body());
} else {
//data.setValue(response.errorBody());
}
}
#Override
public void onFailure(Call<List<SellerResponse>> call, Throwable t) {
//data.setValue(t.getMessage());
Timber.e(t);
}
});
return data;
}
This is my View
viewModel.fetchAllSellers().observe(this, response -> {
});
I have a singleton to handle the registration and elimination of an entity Profilo ( a Profile).
This entity is set by passing an identifier and gathering information on the server in an async way.
My problem is that when I have to return my instance of profilo if it's not still loaded it will return null.
public class AccountHandler {
private static AccountHandler istanza = null;
Context context;
private boolean logged;
private Profilo profilo;
private AccountHandler(Context context) {
this.context = context;
//initialization
//setting logged properly
assignField(this.getName());
}
}
public static AccountHandler getAccountHandler(Context context) {
if (istanza == null) {
synchronized (AccountHandler.class) {
if (istanza == null) {
istanza = new AccountHandler(context);
}
}
}
return istanza;
}
public void setAccount(String nickname, String accessingCode) {
logged = true;
assignField(nickname);
}
//other methods
private void assignField(String nickname) {
ProfiloClient profiloClient = new ProfiloClient();
profiloClient.addParam(Profilo.FIELDS[0], nickname);
profiloClient.get(new JsonHttpResponseHandler() {
#Override
public void onSuccess(int statusCode,
Header[] headers,
JSONArray response) {
JSONObject objson = null;
try {
objson = (JSONObject) response.getJSONObject(0);
} catch (JSONException e) {
e.printStackTrace();
}
AccountHandler accountHandler = AccountHandler.getAccountHandler(context);
// Profilo is created with a JSONObject
// **setProfilo is called in async**
**accountHandler.setProfilo(new Profilo(objson));**
}
});
}
private void setProfilo(Profilo profilo) {
this.profilo = profilo;
}
public Profilo getProfilo() {
if( logged && profilo == null)
//How can I wait that profilo is loaded by the JsonHttpResponseHandler before to return it
return this.profilo;
}
}
Instead of calling getProfilo you could use a callback mechanism in which the AccountHandler class notifies the caller when the profile has been loaded. e.g.
public void setAccount(String nickname, String accessingCode, MyCallback cb) {
assignField(nickname, cb);
}
private void assignField(String nickname, MyCallback cb) {
....
accountHandler.setProfilo(new Profilo(objson));
cb.onSuccess(this.profilo);
}
Create an inner Interface MyCallback (rename it) in your AccountHandler class
public class AccountHandler {
public interface MyCallback {
void onSuccess(Profilo profile);
}
}
Now whenever you call setAccount you will pass the callback and get notified when the profile is available e.g.
accountHandler.setAccount("Test", "Test", new AccountHandler.MyCallback() {
void onSuccess(Profilio profile) {
// do something with the profile
}
}
I added, as #Murat K. suggested, an interface to my Class that will provide a method to be call with the object when it is ready to be used.
public class AccountHandler {
public interface Callback {
void profiloReady(Profilo profilo);
}
}
This method is called in getProfilo in a Handler that makes recursive calls to getProfilo until profilo is ready to be used, then it call the callback method which class is passed as argument of getProfilo.
public void getProfilo(final Callback Callback) {
if( logged && (profilo == null || !profilo.isReady() ) {
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
getProfilo(Callback);
}
}, 500);
}else
Callback.profiloReady(profilo);
}
Example of getProfilo call
public class ProfiloCall implements AccountHandler.MyCallback {
#Override
public void profiloReady(Profilo profilo) {
//Use profilo as needed
//EXECUTED ONLY WHEN PROFILO IS READY
}
public void callerMethod() {
//useful code
accountHandler.getProfilo(this);
//other useful code
}
}
Problem Description
I'm trying to write test for simple class which is using Observable.
Test must be written for function buildUseCaseObservable which should first try to get data from network and if not succeed try to get from local database.
In the buildUseCaseObservable I'm using operator first which should filter data and return true if data is not null and empty.
If in the case of rest.getData() is called and data returned is not null I assume that first should return true and in that case data.getData() should not be called.
But in my case it seems that while testing function first is not called and both functions rest.getData() and data.getData() are always called.
Question
What I'm doing wrong and how I can correct test?
DataInteractor.java
#PerActivity
public class DataInteractor extends Interactor {
private RestService rest;
private DataService data;
#Inject
DataInteractor(RestService rest, DataService data) {
this.rest = rest;
this.data = data;
}
#Override
protected Observable buildUseCaseObservable() {
return Observable.concat(
rest.getData(),
data.getData())
.first(data -> data != null && !data.isEmpty());
}
}
DataService.java
public interface DataService {
Observable<List<IData>> getData();
}
RestService.java
public interface RestService {
#GET("data")
Observable<List<IData>> getData();
}
DataInteractorTest.java
public class DataInteractorTest {
private DataInteractor interactor;
#Mock private RestService mockedRest;
#Mock private DataService mockedData;
#Before
public void setup() {
MockitoAnnotations.initMocks(this);
this.interactor = new DataInteractor(mockedRest, mockedData);
}
#Test
public void firstDownloadDataFromNetwork() {
when(mockedRest.getData()).thenReturn(Observable.create(new Observable.OnSubscribe<List<IData>>() {
#Override
public void call(Subscriber<? super List<IData>> subscriber)
List<IData> data = new ArrayList<IData>() {{
add(new Data());
}};
subscriber.onNext(data);
subscriber.onCompleted();
}
}));
this.interactor.buildUseCaseObservable()
verify(this.mockedData, times(0)).getData();
}
}
Solution
Fortunately I found solution and right way of testing Rx stuff.
I found a nice article with very helpful class called RxAssertions with a small modification of class my tests start passing.
public class DataInteractorTest {
private DataInteractor interactor;
#Mock private RestService mockedRest;
#Mock private DataService mockedData;
#Before
public void setup() {
MockitoAnnotations.initMocks(this);
this.interactor = new DataInteractor(mockedRest, mockedData);
}
#Test
#SuppressWarnings("unchecked")
public void downloadDataFromNetwork_ignoreDataFromDatabase() {
when(mockedRest.getData()).thenReturn(this.getMockedData(4));
when(mockedData.getData()).thenReturn(this.getMockedData(8));
RxAssertions.subscribeAssertingThat(this.interactor.buildUseCaseObservable())
.completesSuccessfully()
.hasSize(4);
}
/**
* Helper function which return mocked data
*/
private Observable getMockedData(final int size) {
return Observable.create(new Observable.OnSubscribe<List<IData>>() {
#Override
public void call(Subscriber<? super List<IData>> subscriber) {
List<IData> data = new ArrayList<>();
for (int i = 0; i < size; ++i) {
data.add(new Data());
}
subscriber.onNext(data);
subscriber.onCompleted();
}
});
}
}
RxAsserations
public class RxAssertions {
public static <T> ObservableAssertions<T> subscribeAssertingThat(Observable<List<T>> observable) {
return new ObservableAssertions<>(observable);
}
public static class ObservableAssertions<T> {
private List<T> mResult;
private Throwable mError;
private boolean mCompleted;
public ObservableAssertions(Observable<List<T>> observable) {
mCompleted = false;
mResult = new ArrayList<>();
observable.subscribeOn(Schedulers.immediate())
.subscribe(new Observer<List<T>>() {
#Override
public void onCompleted() {
mCompleted = true;
}
#Override
public void onError(Throwable error) {
mError = error;
}
#Override
public void onNext(List<T> list) {
mResult.addAll(list);
}
});
}
public ObservableAssertions<T> completesSuccessfully() {
if (!mCompleted || mError != null) {
if (mError != null) mError.printStackTrace();
throw new AssertionFailedError("Observable has not completed successfully - cause: "
+ (mError != null ? mError : "onComplete not called"));
}
return this;
}
public ObservableAssertions<T> fails() {
if (mError == null) {
throw new AssertionFailedError("Observable has not failed");
}
return this;
}
public ObservableAssertions<T> failsWithError(Throwable throwable) {
fails();
if (!throwable.equals(mError)) {
throw new AssertionFailedError("Observable has failed with a different error," +
" expected is " + throwable + " but thrown was " + mError);
}
return this;
}
public ObservableAssertions<T> hasSize(int numItemsExpected) {
if (numItemsExpected != mResult.size()) {
throw new AssertionFailedError("Observable has emitted " + mResult.size()
+ " items but expected was " + numItemsExpected);
}
return this;
}
#SafeVarargs
public final ObservableAssertions<T> emits(T... itemsExpected) {
completesSuccessfully();
assertEmittedEquals(itemsExpected);
return this;
}
#SuppressWarnings("unchecked")
public ObservableAssertions<T> emits(Collection<T> itemsExpected) {
completesSuccessfully();
assertEmittedEquals((T[]) itemsExpected.toArray());
return this;
}
public ObservableAssertions<T> emitsNothing() {
completesSuccessfully();
if (mResult.size() > 0) {
throw new AssertionFailedError("Observable has emitted " + mResult.size() + " items");
}
return this;
}
private void assertEmittedEquals(T[] itemsExpected) {
hasSize(itemsExpected.length);
for (int i = 0; i < itemsExpected.length; i++) {
T expected = itemsExpected[i];
T actual = mResult.get(i);
if (!expected.equals(actual)) {
throw new AssertionFailedError("Emitted item in position " + i + " does not match," +
" expected " + expected + " actual " + actual);
}
}
}
}
}