PageRenderTime 44ms CodeModel.GetById 38ms app.highlight 5ms RepoModel.GetById 0ms app.codeStats 0ms

/app/src/main/java/com/ahmadrosid/inspections/fragment_view/presenter/report/ReportFragmentRightPresenter.java

https://gitlab.com/ahmadrosid/TrackinsAndroid
Java | 122 lines | 96 code | 18 blank | 8 comment | 16 complexity | afdacfd092c9f261a377c831390e81ed MD5 | raw file
  1package com.ahmadrosid.inspections.fragment_view.presenter.report;
  2
  3import android.content.Context;
  4import android.text.TextUtils;
  5
  6import com.ahmadrosid.inspections.core.Presenter;
  7import com.ahmadrosid.inspections.data.local.TrackinsCacheManager;
  8import com.ahmadrosid.inspections.fragment_view.fragment.report.ReportFragmentRight;
  9import com.ahmadrosid.inspections.fragment_view.view.report.FragmentLeftRightReportView;
 10import com.ahmadrosid.inspections.helper.Constants;
 11import com.ahmadrosid.inspections.helper.PickImageHelper;
 12import com.ahmadrosid.inspections.ui.FormInputImage;
 13import com.ahmadrosid.inspections.ui.dialog.DialogPickImage;
 14import com.ahmadrosid.inspections.ui.dialog.DialogShowMessages;
 15
 16import java.util.ArrayList;
 17import java.util.List;
 18import java.util.Map;
 19
 20import static com.ahmadrosid.inspections.helper.Constants.REQUEST_CAMERA;
 21import static com.ahmadrosid.inspections.helper.Constants.REQUEST_GALLERY;
 22
 23/**
 24 * Created by ocittwo on 10/21/16.
 25 *
 26 * @Author Ahmad Rosid
 27 * @Email ocittwo@gmail.com
 28 * @Github https://github.com/ar-android
 29 * @Web http://ahmadrosid.com
 30 */
 31public class ReportFragmentRightPresenter implements Presenter<FragmentLeftRightReportView> {
 32
 33    private TrackinsCacheManager cacheManager;
 34    private Context context;
 35    private ArrayList<FormInputImage> arrayForm;
 36    private FragmentLeftRightReportView view;
 37
 38    public ReportFragmentRightPresenter(ReportFragmentRight reportFragmentRight, ArrayList<FormInputImage> arrayForm) {
 39        this.context = reportFragmentRight.getContext();
 40        this.arrayForm = arrayForm;
 41    }
 42
 43
 44    @Override public void onAttachView(FragmentLeftRightReportView view) {
 45        this.view = view;
 46    }
 47
 48    @Override public void onDetachView() {
 49        view = null;
 50    }
 51
 52    public void setPickImage() {
 53        for (FormInputImage formInputImage : arrayForm) {
 54            formInputImage.getImageView().setOnClickListener(v -> pickImageFrom(formInputImage));
 55        }
 56    }
 57
 58    private void pickImageFrom(FormInputImage formInputImage) {
 59        view.setCurrentForm(formInputImage);
 60        DialogPickImage.getInstance(context)
 61                .setCallback(new DialogPickImage.ClickDialog() {
 62                    @Override public void clickCamera() {
 63                        view.pickImageFromCamera(REQUEST_CAMERA);
 64                    }
 65
 66                    @Override public void clickGallery() {
 67                        view.pickImageFromGallery(REQUEST_GALLERY);
 68                    }
 69                }).show();
 70    }
 71
 72    private boolean validate() {
 73        boolean isValidate = true;
 74        boolean isShowEmpty = false;
 75        for (FormInputImage formInputImage : arrayForm) {
 76            if (formInputImage.isRequired()) {
 77                if (TextUtils.isEmpty(formInputImage.getText())) {
 78                    isValidate = false;
 79                    if (!isShowEmpty) {
 80                        showEmptyField(formInputImage.getHint());
 81                        isShowEmpty = true;
 82                    }
 83                }
 84            }
 85        }
 86        return isValidate;
 87    }
 88
 89    private void showEmptyField(String s) {
 90        DialogShowMessages.getInstance(context).setMessage("Please input " + s).show();
 91    }
 92
 93    public void resumeFragment(ArrayList<FormInputImage> arrayForm) {
 94        cacheManager = new TrackinsCacheManager(context);
 95        Map<Object, List<String>> listMap = cacheManager.getCacheFragmentReportRight();
 96        if (listMap != null) {
 97            List<String> dataField = listMap.get(Constants.JSON_RIGHT_INPUT_FIELD);
 98            List<String> dataHint = listMap.get(Constants.JSON_RIGHT_TEXT_HINT);
 99            List<String> dataImage = listMap.get(Constants.JSON_RIGHT_IMAGE_PATH);
100            for (int i = 0; i < arrayForm.size(); i++) {
101                FormInputImage formInputImage = arrayForm.get(i);
102                if (dataField.get(i) != null) formInputImage.setText(dataField.get(i));
103                if (dataHint.get(i) != null) formInputImage.setHint(dataHint.get(i));
104                String pathImage = dataImage.get(i);
105                if (pathImage != null) {
106                    formInputImage.setImgPath(pathImage);
107                    new PickImageHelper(context).setImageFromPath(formInputImage.getImageView(), pathImage);
108                }
109            }
110        }
111    }
112
113    public boolean finish() {
114        return validate();
115    }
116
117    public void cache(ArrayList<FormInputImage> arrayForm) {
118        if (cacheManager == null)
119            cacheManager = new TrackinsCacheManager(context);
120        cacheManager.cacheFragmentReportRight(arrayForm);
121    }
122}