<commit_before>package com.github.crazyatom.subscaleimagedrawview.drawtools;

import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.graphics.PointF;
import android.support.annotation.NonNull;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AlertDialog;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;

import com.jaredrummler.materialspinner.MaterialSpinner;

import java.util.ArrayList;

import com.github.crazyatom.subscaleimagedrawview.R;
import com.github.crazyatom.subscaleimagedrawview.drawviews.BaseDrawView;
import com.github.crazyatom.subscaleimagedrawview.drawviews.DrawViewDimension;
import com.github.crazyatom.subscaleimagedrawview.drawviews.DrawViewReferenceDimension;
import com.github.crazyatom.subscaleimagedrawview.util.DrawViewFactory;
import com.github.crazyatom.subscaleimagedrawview.util.DrawViewSetting;
import com.github.crazyatom.subscaleimagedrawview.util.Utillity;
import com.github.crazyatom.subscaleimagedrawview.views.ImageDrawView;

/**
 * Created by crazy on 2017-07-11.
 */

public class DrawToolDimension extends BaseDrawTool {

    private boolean isBegin = true;
    private PointF begin;
    private PointF end;
    private Snackbar snackbar;
    private boolean isReferenceDrawMode = false;

    final private String SAVE_DIMENSION_KEY = "save_dimension";

    public DrawToolDimension(@NonNull ImageDrawView imageDrawView) {
        super(imageDrawView);
        showSnackbar();
    }

    @Override
    protected void touchBegin(int x, int y) {
        if (this.isBegin == true) {
            this.begin = getCorrectPos(x, y);
            this.end = this.begin;
            previewDrawView = createDrawView();
            ((DrawViewDimension) previewDrawView).setCurrentState(DrawViewDimension.state.PREVIEW_BEGIN);
            imageDrawView.addDrawView(previewDrawView);
        } else {
            this.end = getCorrectPos(x, y);
            previewDrawView.setPosition(1, this.end);
            previewDrawView.invalidate();
        }
    }

    @Override
    protected void touchMove(int x, int y) {
        if (this.isBegin == true) {
            this.begin = getCorrectPos(x, y);
            this.end = this.begin;
            previewDrawView.setPosition(0, this.begin);
            previewDrawView.setPosition(1, this.end);
            previewDrawView.invalidate();
        } else {
            this.end = getCorrectPos(x, y);
            previewDrawView.setPosition(1, this.end);
            previewDrawView.invalidate();
        }
    }

    @Override
    protected void touchEnd(int x, int y) {
        if (this.isBegin == true) {
            this.isBegin = false;
            ((DrawViewDimension) previewDrawView).setCurrentState(DrawViewDimension.state.PREVIEW_END);
        } else {
            this.isBegin = true;
            ((DrawViewDimension) previewDrawView).setCurrentState(DrawViewDimension.state.COMPLETE);
            this.end = getCorrectPos(x, y);
            imageDrawView.removeDrawView(previewDrawView);
            injectAnnotation();
            checkContinueTool();
        }
    }

    @Override
    public void enter() {

    }

    @Override
    public void exit() {
        if (snackbar != null && snackbar.isShown() == true) {
            snackbar.dismiss();
        }

        if (previewDrawView != null) {
            imageDrawView.removeDrawView(previewDrawView);
        }
    }

    @Override
    protected BaseDrawView createDrawView() {
        BaseDrawView drawView = DrawViewFactory.getInstance().create(imageDrawView,
                (isReferenceDrawMode == true) ? BaseDrawView.DrawViewType.DIMENSION_REF : BaseDrawView.DrawViewType.DIMENSION);
        drawView.addPosition(this.begin);
        drawView.addPosition(this.end);

        return drawView;
    }

    /**
     * annotation을 view에 추가
     */
    private void injectAnnotation() {
        if (this.begin != this.end) {
            DrawViewDimension drawView = (DrawViewDimension) createDrawView();
            drawView.setCurrentState(DrawViewDimension.state.COMPLETE);
            imageDrawView.addDrawView(drawView);

            // 기준 치수일 경우 설정 다이얼로그 팝업
            if (isReferenceDrawMode == true) {
                showSetupDialog((DrawViewReferenceDimension) drawView);
            }
        }
    }

    private void showSnackbar() {
        final SharedPreferences sharedPreferences = imageDrawView.getContext().getSharedPreferences("Show_message", Context.MODE_PRIVATE);
        boolean notSeeAgainSaveDimension = sharedPreferences.getBoolean(SAVE_DIMENSION_KEY, false);
        if (notSeeAgainSaveDimension == true) {
            return;
        }

        snackbar = Snackbar.make(imageDrawView, "치수 마크업은 페이지 또는 공유 모드 변경시 자동 삭제 됩니다", Snackbar.LENGTH_INDEFINITE);
        snackbar.setAction("다시 보지 않기", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putBoolean(SAVE_DIMENSION_KEY, true);
                editor.commit();
            }
        });
        snackbar.setAction("확인", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                snackbar.dismiss();
            }
        });
        snackbar.show();
    }

    /**
     * 기준 치수선 작성 모드인지 설정
     * @param referenceDrawMode
     */
    public void setReferenceDrawMode(boolean referenceDrawMode) {
        isReferenceDrawMode = referenceDrawMode;
    }

    /**
     * 기준 치수선 설정 다이얼로그
     * @param referenceDimension
     */
    private void showSetupDialog(final DrawViewReferenceDimension referenceDimension) {
        if (referenceDimension == null) {
            return;
        }
<commit_msg>치수선 작성시 snackbar show/dismessed에 대한 이벤트 처리<commit_after>package com.github.crazyatom.subscaleimagedrawview.drawtools;

import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.graphics.PointF;
import android.support.annotation.NonNull;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AlertDialog;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;

import com.jaredrummler.materialspinner.MaterialSpinner;

import java.util.ArrayList;

import com.github.crazyatom.subscaleimagedrawview.R;
import com.github.crazyatom.subscaleimagedrawview.drawviews.BaseDrawView;
import com.github.crazyatom.subscaleimagedrawview.drawviews.DrawViewDimension;
import com.github.crazyatom.subscaleimagedrawview.drawviews.DrawViewReferenceDimension;
import com.github.crazyatom.subscaleimagedrawview.util.DrawViewFactory;
import com.github.crazyatom.subscaleimagedrawview.util.DrawViewSetting;
import com.github.crazyatom.subscaleimagedrawview.util.Utillity;
import com.github.crazyatom.subscaleimagedrawview.views.ImageDrawView;

/**
 * Created by crazy on 2017-07-11.
 */

public class DrawToolDimension extends BaseDrawTool {

    private boolean isBegin = true;
    private PointF begin;
    private PointF end;
    private Snackbar snackbar;
    private boolean isReferenceDrawMode = false;

    final private String SAVE_DIMENSION_KEY = "save_dimension";

    public DrawToolDimension(@NonNull ImageDrawView imageDrawView) {
        super(imageDrawView);
        showSnackbar();
    }

    @Override
    protected void touchBegin(int x, int y) {
        if (this.isBegin == true) {
            this.begin = getCorrectPos(x, y);
            this.end = this.begin;
            previewDrawView = createDrawView();
            ((DrawViewDimension) previewDrawView).setCurrentState(DrawViewDimension.state.PREVIEW_BEGIN);
            imageDrawView.addDrawView(previewDrawView);
        } else {
            this.end = getCorrectPos(x, y);
            previewDrawView.setPosition(1, this.end);
            previewDrawView.invalidate();
        }
    }

    @Override
    protected void touchMove(int x, int y) {
        if (this.isBegin == true) {
            this.begin = getCorrectPos(x, y);
            this.end = this.begin;
            previewDrawView.setPosition(0, this.begin);
            previewDrawView.setPosition(1, this.end);
            previewDrawView.invalidate();
        } else {
            this.end = getCorrectPos(x, y);
            previewDrawView.setPosition(1, this.end);
            previewDrawView.invalidate();
        }
    }

    @Override
    protected void touchEnd(int x, int y) {
        if (this.isBegin == true) {
            this.isBegin = false;
            ((DrawViewDimension) previewDrawView).setCurrentState(DrawViewDimension.state.PREVIEW_END);
        } else {
            this.isBegin = true;
            ((DrawViewDimension) previewDrawView).setCurrentState(DrawViewDimension.state.COMPLETE);
            this.end = getCorrectPos(x, y);
            imageDrawView.removeDrawView(previewDrawView);
            injectAnnotation();
            checkContinueTool();
        }
    }

    @Override
    public void enter() {

    }

    @Override
    public void exit() {
        if (snackbar != null && snackbar.isShown() == true) {
            snackbar.dismiss();
        }

        if (previewDrawView != null) {
            imageDrawView.removeDrawView(previewDrawView);
        }
    }

    @Override
    protected BaseDrawView createDrawView() {
        BaseDrawView drawView = DrawViewFactory.getInstance().create(imageDrawView,
                (isReferenceDrawMode == true) ? BaseDrawView.DrawViewType.DIMENSION_REF : BaseDrawView.DrawViewType.DIMENSION);
        drawView.addPosition(this.begin);
        drawView.addPosition(this.end);

        return drawView;
    }

    /**
     * annotation을 view에 추가
     */
    private void injectAnnotation() {
        if (this.begin != this.end) {
            DrawViewDimension drawView = (DrawViewDimension) createDrawView();
            drawView.setCurrentState(DrawViewDimension.state.COMPLETE);
            imageDrawView.addDrawView(drawView);

            // 기준 치수일 경우 설정 다이얼로그 팝업
            if (isReferenceDrawMode == true) {
                showSetupDialog((DrawViewReferenceDimension) drawView);
            }
        }
    }

    private void showSnackbar() {
        final SharedPreferences sharedPreferences = imageDrawView.getContext().getSharedPreferences("Show_message", Context.MODE_PRIVATE);
        boolean notSeeAgainSaveDimension = sharedPreferences.getBoolean(SAVE_DIMENSION_KEY, false);
        if (notSeeAgainSaveDimension == true) {
            return;
        }

        this.snackbar = Snackbar.make(imageDrawView, "치수 마크업은 페이지 또는 공유 모드 변경시 자동 삭제 됩니다", Snackbar.LENGTH_INDEFINITE);
        this.snackbar.addCallback(new Snackbar.Callback() {
            @Override
            public void onShown(Snackbar sb) {
                super.onShown(sb);
                if (showSnackbarListener != null) {
                    showSnackbarListener.onShown(sb.getView().getMeasuredHeight());
                }
            }

            @Override
            public void onDismissed(Snackbar transientBottomBar, int event) {
                super.onDismissed(transientBottomBar, event);
                if (showSnackbarListener != null) {
                    showSnackbarListener.onDismissed();
                }
            }
        });
        this.snackbar.setAction("다시 보지 않기", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putBoolean(SAVE_DIMENSION_KEY, true);
                editor.commit();
            }
        });
        this.snackbar.setAction("확인", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                 snackbar.dismiss();
            }
        });
        this.snackbar.show();
    }

    /**
     * 기준 치수선 작성 모드인지 설정
     * @param referenceDrawMode
     */
    public void setReferenceDrawMode(boolean referenceDrawMode) {
        isReferenceDrawMode = referenceDrawMode;
    }

    /**
     * 기준 치수선 설정 다이얼로그
     * @param referenceDimension
     */
    private void showSetupDialog(final DrawViewReferenceDimension referenceDimension) {
        if (referenceDimension == null) {
            return;
        }

{'fraction_non_alphanumeric': 0.09316572519691305, 'fraction_numerical': 0.00206858143050362, 'mean_word_length': 2.552854720180893, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5049331', 'n_tokens_mistral': 3693, 'n_tokens_neox': 3754, 'n_words': 641}