Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class PreorderPresenter implements PreorderContract.Presenter {
- @Nullable
- private PreorderContract.View preorderView;
- @NonNull
- private final MenuLoaderInteractor menuLoaderInteractor;
- @NonNull
- private final MenuAvailabilityInteractor menuAvailabilityInteractor;
- private int currentCategoryPosition;
- @Inject
- PreorderPresenter(@NonNull MenuLoaderInteractor menuLoaderInteractor, @NonNull MenuAvailabilityInteractor menuAvailabilityInteractor) {
- this.menuLoaderInteractor = menuLoaderInteractor;
- this.menuAvailabilityInteractor = menuAvailabilityInteractor;
- }
- @Override
- public void takeView(PreorderContract.View view) {
- preorderView = view;
- if (preorderView != null) {
- preorderView.enableProgressBar(true);
- }
- getCategories();
- }
- private void getCategories() {
- menuLoaderInteractor.loadCategories(new MenuLoaderInteractor.CategoriesDownloadListener() {
- @Override
- public void onCategoriesDownloadedSuccess(List<Category> categories) {
- if (preorderView != null) {
- preorderView.enableProgressBar(false);
- if (categories != null) {
- currentCategoryPosition = findSelectedCategory(categories);
- preorderView.fillCategoryList(categories, currentCategoryPosition);
- preorderView.fillProductList(categories.get(currentCategoryPosition).getProducts(),
- categories.get(currentCategoryPosition).getTitle());
- changePreOrderOnOffButton();
- }
- }
- }
- @Override
- public void onDownloadedFailure(int errorCode) {
- if (preorderView != null) {
- preorderView.enableProgressBar(false);
- handleErrorsFromServer(errorCode);
- changePreOrderOnOffButton();
- }
- }
- @Override
- public void noInternet() {
- if (preorderView != null) {
- preorderView.enableProgressBar(false);
- preorderView.showError(R.string.no_internet);
- changePreOrderOnOffButton();
- }
- }
- });
- }
- private int findSelectedCategory(List<Category> categories) {
- String selectedId = menuLoaderInteractor.getSelectedCategoryId();
- int selectedPosition = 0;
- if (selectedId != null) {
- for (int i = 0; i < categories.size(); i++) {
- if (categories.get(i).getId().equals(selectedId)) {
- selectedPosition = i;
- break;
- }
- }
- }
- return selectedPosition;
- }
- private void handleErrorsFromServer(int errorCode) {
- switch (errorCode) {
- case ERROR_CODE_SERVER_ERROR:
- assert preorderView != null;
- preorderView.showError(R.string.server_error_msg);
- break;
- case ERROR_CODE_UNAUTHORIZED:
- case ERROR_CODE_FORBIDDEN:
- assert preorderView != null;
- preorderView.showError(R.string.error_msg_pos_info_incorrect);
- break;
- }
- }
- @Override
- public void dropView() {
- if (preorderView == null) {
- return;
- }
- preorderView.enableProgressBar(false);
- preorderView = null;
- }
- @Override
- public void onSwitchClickCategory(int position, boolean isChecked, OnUndoSwitchListener onUndoSwitchListener) {
- if (preorderView == null) {
- return;
- }
- preorderView.enableProgressBar(true);
- Category category = menuLoaderInteractor.getCachedCategories().get(position);
- menuAvailabilityInteractor.markCategoryAvailability(category.getId(), isChecked, new RemoteRepository.MarkAvailableListener() {
- @Override
- public void onMarkAvailable(boolean success, String errorMessage) {
- if (success) {
- category.setAvailable(isChecked);
- getCategories();
- } else {
- if (preorderView != null) {
- preorderView.enableProgressBar(false);
- preorderView.showToast(errorMessage);
- }
- onUndoSwitchListener.undoSwitch();
- }
- }
- @Override
- public void onMarkAvailable(boolean success, int errorMessageRes) {
- }
- });
- }
- @Override
- public void onSwitchClickProduct(int position, boolean isChecked, OnUndoSwitchListener onUndoSwitchListener) {
- if (preorderView == null) {
- return;
- }
- preorderView.enableProgressBar(true);
- CategoryProduct product = menuLoaderInteractor.getCachedCategories().get(currentCategoryPosition).getProducts().get(position);
- menuAvailabilityInteractor.markProductAvailability(currentCategoryPosition,
- menuLoaderInteractor.getCachedCategories().get(currentCategoryPosition).getId(),
- product.getId(), isChecked, new RemoteRepository.MarkAvailableListener() {
- @Override
- public void onMarkAvailable(boolean success, String errorMessage) {
- if (success) {
- product.setAvailable(isChecked);
- getCategories();
- } else {
- if (preorderView != null) {
- preorderView.enableProgressBar(false);
- preorderView.showToast(errorMessage);
- }
- onUndoSwitchListener.undoSwitch();
- }
- }
- @Override
- public void onMarkAvailable(boolean success, int errorMessageRes) {
- if (preorderView != null) {
- preorderView.enableProgressBar(false);
- }
- if (success) {
- product.setAvailable(isChecked);
- } else {
- preorderView.showToast(errorMessageRes);
- onUndoSwitchListener.undoSwitch();
- }
- }
- });
- }
- @Override
- public void onItemClickCategory(int position) {
- currentCategoryPosition = position;
- String selectedId = menuLoaderInteractor.getCachedCategories().get(position).getId();
- menuLoaderInteractor.saveSelectedCategory(selectedId);
- if (preorderView != null) {
- preorderView.fillProductList(menuLoaderInteractor.getCachedCategories().get(position).getProducts(),
- menuLoaderInteractor.getCachedCategories().get(position).getTitle());
- }
- }
- @Override
- public void onMenuOnOffClick() {
- if (preorderView == null) {
- return;
- }
- preorderView.enableProgressBar(true);
- boolean menuAvailable = menuAvailabilityInteractor.isMenuAvailable();
- menuAvailabilityInteractor.markAllCategoryAvailability(!menuAvailable, new RemoteRepository.MarkAvailableListener() {
- @Override
- public void onMarkAvailable(boolean success, String errorMessage) {
- if (success) {
- getCategories();
- } else {
- if (preorderView != null) {
- preorderView.enableProgressBar(false);
- }
- preorderView.showToast(errorMessage);
- }
- }
- @Override
- public void onMarkAvailable(boolean success, int errorMessageRes) {
- }
- });
- }
- private void changePreOrderOnOffButton() {
- if (preorderView == null) {
- return;
- }
- if (menuAvailabilityInteractor.isMenuAvailable()) {
- preorderView.onMenuChange(R.string.preorder_on, R.drawable.ic_preorder_on);
- } else {
- preorderView.onMenuChange(R.string.preorder_off, R.drawable.ic_preorder_off);
- }
- }
- public interface OnUndoSwitchListener {
- void undoSwitch();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement