Guest User

Untitled

a guest
Jun 21st, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.36 KB | None | 0 0
  1. public class MainActivity extends AppCompatActivity
  2. private MutableLiveData<List<Photo>> mLivePhotos;
  3. //some code...
  4.  
  5. @Override
  6. protected void onCreate(Bundle savedInstanceState) {
  7. mLivePhotos = loadData();
  8. mLivePhotos.observe(this, photos -> {
  9. Log.d(TAG, "onChanged!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
  10. mProgressBar.setVisibility(View.GONE);
  11. mPhotos = photos;
  12. if (mIsInitialCall) {
  13. initiateAdapter();
  14. mIsInitialCall = false;
  15. } else {
  16. mAdapter.updateList(mPhotos.subList(mPageNumber, mPageNumber + 10));
  17. }
  18. });
  19.  
  20. mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
  21. @Override
  22. public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
  23. int lastPosition =
  24. mLayoutManager.findLastCompletelyVisibleItemPosition();
  25. Log.d(TAG, "onScrolled - lastPosition: " + lastPosition);
  26. if (lastPosition == mLayoutManager.getItemCount() - 1) {
  27. Log.d(TAG, "onScrolled - End of list?");
  28. loadData();
  29. }
  30. }
  31. });
  32. }
  33.  
  34. private MutableLiveData<List<Photo>> loadData() {
  35. Log.d(TAG, "loadData");
  36. if (mArticleViewModel == null) return null;
  37. mPageNumber += 10;
  38. mProgressBar.setVisibility(View.VISIBLE);
  39. return mArticleViewModel.loadPhotos();
  40. }
  41.  
  42. public class ArticleViewModel extends ViewModel {
  43. private MutableLiveData<List<Photo>> photos;
  44. private ArticleRepository articleRepository;
  45.  
  46. public MutableLiveData<List<Photo>> loadPhotos() {
  47. Log.d(TAG, "getArticleList");
  48. //TODO; add Dagger 2
  49. articleRepository = new ArticleRepository();
  50. photos = articleRepository.getPhotos();
  51. return photos;
  52. }
  53.  
  54. public class ArticleRepository {
  55.  
  56. public MutableLiveData<List<Photo>> getPhotos() {
  57. final MutableLiveData<List<Photo>> result = new MutableLiveData<>();
  58. Log.d(TAG, "getResults");
  59. ApiService.getService().getPhotos().enqueue(new Callback<List<Photo>>() {
  60. @Override
  61. public void onResponse(Call<List<Photo>> call, Response<List<Photo>> response) {
  62. Log.d(TAG, "onResponse");
  63. if (response.isSuccessful()) {
  64. Log.d(TAG, "isSuccessful");
  65. result.postValue(response.body());
  66. }
  67. }
  68.  
  69. @Override
  70. public void onFailure(Call<List<Photo>> call, Throwable t) {
  71. Log.d(TAG, "onFailure: " + t.getMessage() + "n" + t.getStackTrace());
  72. }
  73. });
  74. return result;
  75. }
Add Comment
Please, Sign In to add comment