Advertisement
vmeansdev

MVP

Sep 23rd, 2019
537
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.99 KB | None | 0 0
  1. interface View {
  2.     void displayPhotos(List<Photo> photos);
  3.     void displayError();
  4. }
  5.  
  6. interface Presenter {
  7.     void onViewCreated();
  8.     void loadPhotosAtPage(Integer page);
  9.     void onPhotosLoaded(List<Photo> photos);
  10.     void onLoadFailure();
  11.     void onViewDestroyed();
  12. }
  13.  
  14. interface Model {
  15.     void loadPhotos(Integer page);
  16. }
  17.  
  18.  
  19. class PhotosPresenter implements Presenter {
  20.  
  21.     private Model model;
  22.     private WeakReference<View> view;
  23.  
  24.     private Integer currentPage = 1;
  25.  
  26.     PhotosPresenter(View view) {
  27.         this.view = new WeakReference(view);
  28.         this.model = new UnsplashService(this);
  29.     }
  30.  
  31.     void onViewCreated() {
  32.         model.loadPhotos(currentPage);
  33.     }
  34.  
  35.     void loadPhotosAtPage(Integer page) {
  36.         model.loadPhotos(page);
  37.     }
  38.  
  39.     void onPhotosLoaded(List<Photo> photos) {
  40.         if (viewExists()) {
  41.             view.get().displayPhotos(photos);
  42.         }
  43.     }
  44.  
  45.     void onLoadFailure() {
  46.         if (viewExists()) {
  47.             view.get().displayError();
  48.         }
  49.     }
  50.  
  51.     void onViewDestroyed() {
  52.         view = null;
  53.     }
  54.  
  55.     private boolean viewExists() {
  56.         return view != null && view.get() != null;
  57.     }
  58.  
  59. }
  60.  
  61.  
  62. class WPFRagment extends Fragment implements View {
  63.  
  64.     private PhotoAdapter adapter;
  65.     private RecyclerView recyclerView;
  66.     private Presenter presenter;
  67.  
  68.     ...
  69.  
  70.     @Override
  71.     void onCreateView(...) {
  72.         ...
  73.         presenter = new PhotosPresenter(this);
  74.         // configure recyclerView and adapter here
  75.         ...
  76.         presenter.onViewCreated();
  77.     }
  78.  
  79.     @Override
  80.     void onDestroy() { // или как там фрагменты убиваются
  81.         presenter.onViewDestroyed();
  82.     }
  83.  
  84.     void displayPhotos(List<Photo> photos) {
  85.         adapter.addPhotos(photos);
  86.         adapter.notifyDatasetChanged();
  87.     }
  88.  
  89.     void displayError() {
  90.         // handle error UI here
  91.     }
  92.  
  93. }
  94.  
  95. class UnsplashService implements Model {
  96.  
  97.     private Presenter presenter;
  98.     private PhotosEndPoint photosEndPoint;
  99.  
  100.     UnsplashService(Presenter presenter) {
  101.         this.presenter = presenter;
  102.         ...
  103.     }
  104.  
  105.     void loadPhotos(Integer page) {
  106.         Call<List<Photo>> call = photosEndPoint.getPhotos(USER_ID, page, ORIENTATION);
  107.         call.enqueue(new Callback<List<Photo>>() {
  108.  
  109.             @Override
  110.             public void onResponse(Call<List<Photo>> call, Response<List<Photo>> response) {
  111.                 if (response.isSuccessful()) {
  112.                     presenter.onPhotosLoaded(response.body()); // или как там получать данные из ответа
  113.                 } else {
  114.                     // something went wrong
  115.                     presenter.onLoadFailure();
  116.                 }
  117.             }
  118.  
  119.             @Override
  120.             public void onFailure(Call<List<Photo>> call, Throwable t) {
  121.                 // handle failure here
  122.                 presenter.onLoadFailure();
  123.             }
  124.  
  125.         });
  126.     }
  127.  
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement