Search code examples
androidandroid-livedataandroid-architecture-componentsandroid-viewmodelandroid-mvvm

ViewModel/Repository displaying blank


I'm following the Github Browser app tutorial to build my project without using databinding. The display is just blank only showing the navigation view. I cannot figure out where I am going wrong. I tried to debug and all its showing is that result is null. No errors.

ViewModel.java:

public class ListViewModel extends ViewModel {
    private final LiveData<Resource<List<Something>>> repositories;
    private int langID;
    private int categoryID;
    private MutableLiveData<List<Something>> networkInfoObservable = new MutableLiveData<>();
    private static String TAG = "SomethingViewModel";

    @SuppressWarnings("unchecked")
    @Inject
    public ListViewModel(ListRepository listRepository) {
        repositories = Transformations.switchMap(networkInfoObservable, someList ->
                listRepository.loadList(2, 1)); // hard-coding values to test)

    }


    @VisibleForTesting
    public void setListArgs(int langID, int categoryID) {
        this.langID = langID;
        this.categoryID = categoryID;
    }

    @VisibleForTesting
    public LiveData<Resource<List<Something>>> getList() {
        return repositories;
    }

}

Repository.java:

@Singleton
public class ListRepository {
    private final SomeDao someDao;
    private final SomeService someService;
    private final AppExecutors appExecutors;
    private static String TAG = "LIST_REPOSITORY";

    @Inject
    ListRepository(AppExecutors appExecutors, SomeDao someDao, SomeService someService) {
        this.someDao = someDao;
        this.someService = someService;
        this.appExecutors = appExecutors;
    }

    public LiveData<Resource<List<Something>>> loadList(int langId, int categoryID) {
        return new NetworkBoundResource<List<Something>, ListWrapper<Something>>(appExecutors) {
            @Override
            protected void saveCallResult(@NonNull ListWrapper<Something> item) {
                for (Something someItem : item.someItems){
                    someItem.lang_id = langId;
                    someItem.cat_id = categoryID;
                    someDao.insertList(someItem);
                }
            }

            @Override
            protected boolean shouldFetch(@Nullable List<Something> data) {
                return data == null;
            }

            @NonNull
            @Override
            protected LiveData<List<Something>> loadFromDb() {
                return someDao.getList(langId, categoryID);
            }

            @NonNull
            @Override
            protected LiveData<ApiResponse<ListWrapper<Something>>> createCall() {
                Log.d(TAG, "Test");
                return someService.getList(langId, categoryID);
            }
        }.asLiveData();
    }
}

DAO.java:

@Dao
public interface SomeDao {
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    public void insertList(Something someList);

    @Query("SELECT * FROM list WHERE langId=:langId AND catId=:catId")
    public LiveData<List<Something>> getList(int langId, int catId);
}

APIService.java:

public interface SomeService {

        @POST("/getCategoryItem")
        LiveData<ApiResponse<ListWrapper<Something>>> getList(@Query("langId") Integer langId,
                                                                                     @Query("catId") Integer category);

}

BaseFragment:

    public abstract class BaseFragment<B extends ViewBinding> extends Fragment implements Injectable {
    
    //variables
private ListViewModel listViewModel;
    
    
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            binding = onCreateBinding(inflater, container, savedInstanceState);
    
            context = this.getActivity().getApplicationContext();
    
            adapter = new ListViewAdapter();
    
            return binding.getRoot();
        }
    
    
        @Override
        public void onActivityCreated(@Nullable Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
    
            listViewModel = new ViewModelProvider(this, viewModelFactory).get(ListViewModel.class);
            listViewModel.setListArgs(langId, categoryID);
            observeListViewModel();
        }
    
        private void observeListViewModel() {
            listViewModel.getList().observe(getViewLifecycleOwner(), result -> {
                if (result != null) {
    
                    adapter.setList(result.data);
                    adapter.setOnItemClickListener(this::itemClicked);
                }
            });
        }
    
    
    }

Solution

  • I did a little research and figured that I had to implement an inner static class to update the arguments:

    public class ListViewModel extends ViewModel {
        private final LiveData<Resource<List<Something>>> repositories;
        private final MutableLiveData<ListArgs> listArgs;
        private static String TAG = "SomethingViewModel";
    
        @SuppressWarnings("unchecked")
        @Inject
        public ListViewModel(ListRepository listRepository) {
           this.listArgs = new MutableLiveData<>();
            repositories = Transformations.switchMap(listArgs, listArg ->
                    listRepository.loadList(listArg.langID, listArg.categoryID)); 
    
        }
    
    
         @VisibleForTesting
        public void setListArgs(int langID, int categoryID) {
            ListArgs update = new ListArgs(langID, categoryID);
            if (Objects.equals(listArgs.getValue(), update)) {
                return;
            }
            listArgs.setValue(update);
        }
    
        @VisibleForTesting
        public LiveData<Resource<List<Something>>> getList() {
            return repositories;
        }
    
        static class ListArgs {
            final int langID;
            final int categoryID;
    
            ListArgs(int langID, int categoryID) {
                this.langID = langID;
                this.categoryID = categoryID;
            }
        }
    
    
    
    }