Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MainActivity extends AppCompatActivity
- private MutableLiveData<List<Photo>> mLivePhotos;
- //some code...
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- mLivePhotos = loadData();
- mLivePhotos.observe(this, photos -> {
- Log.d(TAG, "onChanged!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
- mProgressBar.setVisibility(View.GONE);
- mPhotos = photos;
- if (mIsInitialCall) {
- initiateAdapter();
- mIsInitialCall = false;
- } else {
- mAdapter.updateList(mPhotos.subList(mPageNumber, mPageNumber + 10));
- }
- });
- mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
- @Override
- public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
- int lastPosition =
- mLayoutManager.findLastCompletelyVisibleItemPosition();
- Log.d(TAG, "onScrolled - lastPosition: " + lastPosition);
- if (lastPosition == mLayoutManager.getItemCount() - 1) {
- Log.d(TAG, "onScrolled - End of list?");
- loadData();
- }
- }
- });
- }
- private MutableLiveData<List<Photo>> loadData() {
- Log.d(TAG, "loadData");
- if (mArticleViewModel == null) return null;
- mPageNumber += 10;
- mProgressBar.setVisibility(View.VISIBLE);
- return mArticleViewModel.loadPhotos();
- }
- public class ArticleViewModel extends ViewModel {
- private MutableLiveData<List<Photo>> photos;
- private ArticleRepository articleRepository;
- public MutableLiveData<List<Photo>> loadPhotos() {
- Log.d(TAG, "getArticleList");
- //TODO; add Dagger 2
- articleRepository = new ArticleRepository();
- photos = articleRepository.getPhotos();
- return photos;
- }
- public class ArticleRepository {
- public MutableLiveData<List<Photo>> getPhotos() {
- final MutableLiveData<List<Photo>> result = new MutableLiveData<>();
- Log.d(TAG, "getResults");
- ApiService.getService().getPhotos().enqueue(new Callback<List<Photo>>() {
- @Override
- public void onResponse(Call<List<Photo>> call, Response<List<Photo>> response) {
- Log.d(TAG, "onResponse");
- if (response.isSuccessful()) {
- Log.d(TAG, "isSuccessful");
- result.postValue(response.body());
- }
- }
- @Override
- public void onFailure(Call<List<Photo>> call, Throwable t) {
- Log.d(TAG, "onFailure: " + t.getMessage() + "n" + t.getStackTrace());
- }
- });
- return result;
- }
Add Comment
Please, Sign In to add comment