ChunxiaZhang / Toaster

0 stars 0 forks source link

Simplify view elements usage through ButterKnife #25

Open ChunxiaZhang opened 9 years ago

ChunxiaZhang commented 9 years ago

In IssueFragment.java use ButterKnife framework. That can simply finding view elements in layout, and reducing code when implement listeners. To use ButterKnife, in gradle, need to add: compile 'com.jakewharton:butterknife:7.0.1'

  1. In onViewCreated(), add: ButterKnife.bind(this, headerView);
  2. Delete: private TextView stateText;

    private TextView titleText;

    private TextView bodyText;

    private TextView authorText;

    private TextView createdDateText;

    private ImageView creatorAvatar;

    private ViewGroup commitsView;

    private TextView assigneeText;

    private ImageView assigneeAvatar;

    private TextView labelsArea;

    private View milestoneArea;

    private View milestoneProgressArea;

    private TextView milestoneText;

  3. add: @Bind(R.id.tv_state) protected TextView stateText; @Bind(R.id.tv_issue_title) protected TextView titleText; @Bind(R.id.tv_issue_body) protected TextView bodyText; @Bind(R.id.tv_issue_author) protected TextView authorText; @Bind(R.id.tv_issue_creation_date) protected TextView createdDateText; @Bind(R.id.iv_avatar) protected ImageView creatorAvatar; @Bind(R.id.ll_issue_commits) protected ViewGroup commitsView; @Bind(R.id.tv_assignee_name) protected TextView assigneeText; @Bind(R.id.iv_assignee_avatar) protected ImageView assigneeAvatar; @Bind(R.id.tv_labels) protected TextView labelsArea; @Bind(R.id.ll_milestone) protected View milestoneArea; @Bind(R.id.v_closed) protected View milestoneProgressArea; @Bind(R.id.tv_milestone) protected TextView milestoneText;

4.delete: stateText = (TextView) headerView.findViewById(R.id.tv_state); titleText = (TextView) headerView.findViewById(R.id.tv_issue_title); authorText = (TextView) headerView.findViewById(R.id.tv_issue_author); createdDateText = (TextView) headerView .findViewById(R.id.tv_issue_creation_date); creatorAvatar = (ImageView) headerView.findViewById(R.id.iv_avatar); commitsView = (ViewGroup) headerView.findViewById(R.id.ll_issue_commits); assigneeText = (TextView) headerView.findViewById(R.id.tv_assignee_name); assigneeAvatar = (ImageView) headerView .findViewById(R.id.iv_assignee_avatar); labelsArea = (TextView) headerView.findViewById(R.id.tv_labels); milestoneArea = headerView.findViewById(R.id.ll_milestone); milestoneText = (TextView) headerView.findViewById(R.id.tv_milestone); milestoneProgressArea = headerView.findViewById(R.id.v_closed); bodyText = (TextView) headerView.findViewById(R.id.tv_issue_body); bodyText.setMovementMethod(SelectableLinkMovementMethod.getInstance());

    commitsView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            if (IssueUtils.isPullRequest(issue))
                openPullRequestCommits();
        }
    });

    stateText.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            if (issue != null)
                stateTask.confirm(IssueState.open.equals(issue.state));
        }
    });

    milestoneArea.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            if (issue != null && isCollaborator)
                milestoneTask.prompt(issue.milestone);
        }
    });

    headerView.findViewById(R.id.ll_assignee).setOnClickListener(
            new OnClickListener() {

                @Override
                public void onClick(View v) {
                    if (issue != null && isCollaborator)
                        assigneeTask.prompt(issue.assignee);
                }
            });

    labelsArea.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            if (issue != null && isCollaborator)
                labelsTask.prompt(issue.labels);
        }
    });
  1. add: @OnClick(R.id.ll_issue_commits) public void onClickCommitsView() { if (IssueUtils.isPullRequest(issue)) openPullRequestCommits(); } @OnClick(R.id.tv_state) public void onClickState() { if (issue != null) stateTask.confirm(IssueState.open.equals(issue.state)); } @OnClick(R.id.ll_milestone) public void onClickMilstoneArea(){ if (issue != null && isCollaborator) milestoneTask.prompt(issue.milestone); } @OnClick(R.id.ll_assignee) public void onClickHeaderView() { if (issue != null && isCollaborator) assigneeTask.prompt(issue.assignee); } @OnClick(R.id.tv_labels) public void onClickLabelsArea() { if (issue != null && isCollaborator) labelsTask.prompt(issue.labels); }
ChunxiaZhang commented 9 years ago

This solution can use at other layout. Here I just implemented in IssueFragment as an example.