Advertisement
Guest User

Untitled

a guest
Jan 24th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.56 KB | None | 0 0
  1. public class PreorderPresenter implements PreorderContract.Presenter {
  2.  
  3.     @Nullable
  4.     private PreorderContract.View preorderView;
  5.  
  6.     @NonNull
  7.     private final MenuLoaderInteractor menuLoaderInteractor;
  8.  
  9.     @NonNull
  10.     private final MenuAvailabilityInteractor menuAvailabilityInteractor;
  11.  
  12.     private int currentCategoryPosition;
  13.  
  14.     @Inject
  15.     PreorderPresenter(@NonNull MenuLoaderInteractor menuLoaderInteractor, @NonNull MenuAvailabilityInteractor menuAvailabilityInteractor) {
  16.         this.menuLoaderInteractor = menuLoaderInteractor;
  17.         this.menuAvailabilityInteractor = menuAvailabilityInteractor;
  18.     }
  19.  
  20.     @Override
  21.     public void takeView(PreorderContract.View view) {
  22.         preorderView = view;
  23.  
  24.         if (preorderView != null) {
  25.             preorderView.enableProgressBar(true);
  26.         }
  27.  
  28.         getCategories();
  29.  
  30.     }
  31.  
  32.     private void getCategories() {
  33.         menuLoaderInteractor.loadCategories(new MenuLoaderInteractor.CategoriesDownloadListener() {
  34.             @Override
  35.             public void onCategoriesDownloadedSuccess(List<Category> categories) {
  36.                 if (preorderView != null) {
  37.                     preorderView.enableProgressBar(false);
  38.  
  39.                     if (categories != null) {
  40.                         currentCategoryPosition = findSelectedCategory(categories);
  41.  
  42.                         preorderView.fillCategoryList(categories, currentCategoryPosition);
  43.                         preorderView.fillProductList(categories.get(currentCategoryPosition).getProducts(),
  44.                                 categories.get(currentCategoryPosition).getTitle());
  45.  
  46.                         changePreOrderOnOffButton();
  47.                     }
  48.  
  49.                 }
  50.             }
  51.  
  52.             @Override
  53.             public void onDownloadedFailure(int errorCode) {
  54.                 if (preorderView != null) {
  55.                     preorderView.enableProgressBar(false);
  56.                     handleErrorsFromServer(errorCode);
  57.                     changePreOrderOnOffButton();
  58.  
  59.                 }
  60.             }
  61.  
  62.             @Override
  63.             public void noInternet() {
  64.                 if (preorderView != null) {
  65.                     preorderView.enableProgressBar(false);
  66.                     preorderView.showError(R.string.no_internet);
  67.  
  68.                     changePreOrderOnOffButton();
  69.  
  70.                 }
  71.             }
  72.         });
  73.     }
  74.  
  75.     private int findSelectedCategory(List<Category> categories) {
  76.         String selectedId = menuLoaderInteractor.getSelectedCategoryId();
  77.         int selectedPosition = 0;
  78.  
  79.         if (selectedId != null) {
  80.             for (int i = 0; i < categories.size(); i++) {
  81.                 if (categories.get(i).getId().equals(selectedId)) {
  82.                     selectedPosition = i;
  83.                     break;
  84.                 }
  85.             }
  86.         }
  87.         return selectedPosition;
  88.     }
  89.  
  90.     private void handleErrorsFromServer(int errorCode) {
  91.         switch (errorCode) {
  92.             case ERROR_CODE_SERVER_ERROR:
  93.                 assert preorderView != null;
  94.                 preorderView.showError(R.string.server_error_msg);
  95.                 break;
  96.             case ERROR_CODE_UNAUTHORIZED:
  97.             case ERROR_CODE_FORBIDDEN:
  98.                 assert preorderView != null;
  99.                 preorderView.showError(R.string.error_msg_pos_info_incorrect);
  100.                 break;
  101.         }
  102.     }
  103.  
  104.  
  105.     @Override
  106.     public void dropView() {
  107.         if (preorderView == null) {
  108.             return;
  109.         }
  110.         preorderView.enableProgressBar(false);
  111.         preorderView = null;
  112.     }
  113.  
  114.     @Override
  115.     public void onSwitchClickCategory(int position, boolean isChecked, OnUndoSwitchListener onUndoSwitchListener) {
  116.         if (preorderView == null) {
  117.             return;
  118.         }
  119.         preorderView.enableProgressBar(true);
  120.  
  121.         Category category = menuLoaderInteractor.getCachedCategories().get(position);
  122.  
  123.         menuAvailabilityInteractor.markCategoryAvailability(category.getId(), isChecked, new RemoteRepository.MarkAvailableListener() {
  124.             @Override
  125.             public void onMarkAvailable(boolean success, String errorMessage) {
  126.  
  127.                 if (success) {
  128.                     category.setAvailable(isChecked);
  129.  
  130.                     getCategories();
  131.                 } else {
  132.                     if (preorderView != null) {
  133.                         preorderView.enableProgressBar(false);
  134.                         preorderView.showToast(errorMessage);
  135.                     }
  136.  
  137.                     onUndoSwitchListener.undoSwitch();
  138.                 }
  139.             }
  140.  
  141.             @Override
  142.             public void onMarkAvailable(boolean success, int errorMessageRes) {
  143.  
  144.             }
  145.         });
  146.  
  147.     }
  148.  
  149.     @Override
  150.     public void onSwitchClickProduct(int position, boolean isChecked, OnUndoSwitchListener onUndoSwitchListener) {
  151.         if (preorderView == null) {
  152.             return;
  153.         }
  154.         preorderView.enableProgressBar(true);
  155.  
  156.         CategoryProduct product = menuLoaderInteractor.getCachedCategories().get(currentCategoryPosition).getProducts().get(position);
  157.  
  158.         menuAvailabilityInteractor.markProductAvailability(currentCategoryPosition,
  159.                 menuLoaderInteractor.getCachedCategories().get(currentCategoryPosition).getId(),
  160.                 product.getId(), isChecked, new RemoteRepository.MarkAvailableListener() {
  161.                     @Override
  162.                     public void onMarkAvailable(boolean success, String errorMessage) {
  163.  
  164.                         if (success) {
  165.                             product.setAvailable(isChecked);
  166.  
  167.                             getCategories();
  168.                         } else {
  169.  
  170.                             if (preorderView != null) {
  171.                                 preorderView.enableProgressBar(false);
  172.                                 preorderView.showToast(errorMessage);
  173.                             }
  174.                             onUndoSwitchListener.undoSwitch();
  175.                         }
  176.                     }
  177.  
  178.                     @Override
  179.                     public void onMarkAvailable(boolean success, int errorMessageRes) {
  180.                         if (preorderView != null) {
  181.                             preorderView.enableProgressBar(false);
  182.                         }
  183.                         if (success) {
  184.                             product.setAvailable(isChecked);
  185.                         } else {
  186.                             preorderView.showToast(errorMessageRes);
  187.                             onUndoSwitchListener.undoSwitch();
  188.                         }
  189.                     }
  190.  
  191.         });
  192.     }
  193.  
  194.     @Override
  195.     public void onItemClickCategory(int position) {
  196.  
  197.         currentCategoryPosition = position;
  198.  
  199.         String selectedId = menuLoaderInteractor.getCachedCategories().get(position).getId();
  200.         menuLoaderInteractor.saveSelectedCategory(selectedId);
  201.         if (preorderView != null) {
  202.             preorderView.fillProductList(menuLoaderInteractor.getCachedCategories().get(position).getProducts(),
  203.                     menuLoaderInteractor.getCachedCategories().get(position).getTitle());
  204.         }
  205.     }
  206.  
  207.     @Override
  208.     public void onMenuOnOffClick() {
  209.         if (preorderView == null) {
  210.             return;
  211.         }
  212.  
  213.         preorderView.enableProgressBar(true);
  214.  
  215.         boolean menuAvailable = menuAvailabilityInteractor.isMenuAvailable();
  216.  
  217.         menuAvailabilityInteractor.markAllCategoryAvailability(!menuAvailable, new RemoteRepository.MarkAvailableListener() {
  218.             @Override
  219.             public void onMarkAvailable(boolean success, String errorMessage) {
  220.  
  221.                 if (success) {
  222.                     getCategories();
  223.  
  224.                 } else {
  225.                     if (preorderView != null) {
  226.                         preorderView.enableProgressBar(false);
  227.                     }
  228.                     preorderView.showToast(errorMessage);
  229.                 }
  230.             }
  231.  
  232.             @Override
  233.             public void onMarkAvailable(boolean success, int errorMessageRes) {
  234.  
  235.             }
  236.         });
  237.  
  238.  
  239.     }
  240.  
  241.     private void changePreOrderOnOffButton() {
  242.         if (preorderView == null) {
  243.             return;
  244.         }
  245.  
  246.         if (menuAvailabilityInteractor.isMenuAvailable()) {
  247.             preorderView.onMenuChange(R.string.preorder_on, R.drawable.ic_preorder_on);
  248.         } else {
  249.             preorderView.onMenuChange(R.string.preorder_off, R.drawable.ic_preorder_off);
  250.         }
  251.     }
  252.  
  253.     public interface OnUndoSwitchListener {
  254.         void undoSwitch();
  255.     }
  256. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement