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