0

I'm working in an App using the architecture MVVM, and im a little confused about how its the best way when you need to update or insert data from an event like when you click a button.

I have this in the suscribe Observers to keep track on changes, and this work well when i open the fragment after add Products to and order:

     shopViewModel.getOrderWithProducts().observe(getViewLifecycleOwner(), new Observer<Resource<OrderWithProducts>>() {
        @Override
        public void onChanged(Resource<OrderWithProducts> pedidoResource) {
            if (pedidoResource != null) {
                Log.d(TAG, "onChanged: status: " + pedidoResource.status);
                switch (pedidoResource.status) {
                    case LOADING: {
                        adapterPedido.displayLoading();
                        break;
                    }
                    case ERROR: {
                        adapterPedido.hideLoading();
                        if(pedidoResource.data != null) adapterPedido.setList(pedidoResource.data);
                        break;
                    }
                    case SUCCESS: {
                        if (pedidoResource.data != null) {
                            adapterPedido.hideLoading();
                            adapterPedido.setList(pedidoResource.data);
                        break;
                    }
                }
            }

        }
    });
}

And to update the order i do this:

 public void updateOrder() {
    pedidoRepository.setOrderConfirm(OrderWithProducts.getValue().data.getPedido().getId());
    pedidoConProductos.getValue().data.getPedido().setConfirmado(true);
}

But i think it has to be a better way, because the Fragment doesnt automatically update the data from the Fragment. After updating this the Adapter should change.

LizardStd
  • 41
  • 8

1 Answers1

1

Try

     shopViewModel.getOrderWithProducts().observe(getViewLifecycleOwner(), new Observer<List<OrderWithProducts>>() {
        @Override
        public void onChanged(List<OrderWithProducts> data) {
            adapterPedido.setList(data);
        }
    });

     shopViewModel.isLoading().observe(getViewLifecycleOwner(), new Observer<Boolean>() {
        @Override
        public void onChanged(Boolean isLoading) {
            if(isLoading) {
                adapterPedido.displayLoading();
            } else {
                adapterPedido.hideLoading();
            }
        }
    });

And

 public void updateOrder() {
    pedidoRepository.setOrderConfirm(OrderWithProducts.getValue().data.getPedido().getId());
    // pedidoConProductos.getValue().data.getPedido().setConfirmado(true);
}

If the original LiveData provided from getOrderWithProducts() comes from a Room DAO as LiveData<List<T>>, then it will auto-update accordingly.

EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
  • I make the changes and the update works well in the database but the UI isnt change the info. The original Livedata comes from this: final LiveData> repositorySource = pedidoRepository.searchOrderWithProducts(); – LizardStd Sep 11 '20 at 12:30
  • Well it sounds like your Repository implementation is wrong, then. – EpicPandaForce Sep 11 '20 at 12:52
  • I have this when i update. AppExecutors.getInstance().diskIO().execute(new Runnable() { @Override public void run() { orderDao.updateConfir(idOrder); } }); And in the DAO i have this @Query("UPDATE order SET confirm= 1 WHERE id = :idOrder") void updateConfirm(int idOrder); – LizardStd Sep 11 '20 at 14:21
  • Okay, but I need to know about your query :p – EpicPandaForce Sep 11 '20 at 16:51
  • Sorry, is hard to explain in a commentary, when i click the button the method "updateOrder" runs and later in the database the order is updated. If i reload the fragment the object order with products change. But the UI doesnt react to the change in the database directly after the event. – LizardStd Sep 11 '20 at 18:34
  • @EpicPandaForce need your help here --> https://stackoverflow.com/questions/64352752/user-login-using-viewmodel-and-retrofit – android dev Oct 14 '20 at 12:35