From 5ee5f9ed82e47990dbcdf35af6d25210f51de39d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Rutkowski?= Date: Sun, 3 Feb 2019 13:47:07 +0100 Subject: [PATCH] Make it easier to distinguish between selection types --- .../gh4a/activities/IssueEditActivity.java | 16 ++++-- .../gh4a/activities/IssueListActivity.java | 17 +++++-- .../com/gh4a/dialogs/MilestoneDialog.java | 51 ++++++++++++++++--- 3 files changed, 69 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/com/gh4a/activities/IssueEditActivity.java b/app/src/main/java/com/gh4a/activities/IssueEditActivity.java index dff97c9bd..bdf539b6a 100644 --- a/app/src/main/java/com/gh4a/activities/IssueEditActivity.java +++ b/app/src/main/java/com/gh4a/activities/IssueEditActivity.java @@ -42,7 +42,6 @@ import com.gh4a.R; import com.gh4a.ServiceFactory; import com.gh4a.dialogs.MilestoneDialog; -import com.gh4a.fragment.IssueMilestoneListFragment; import com.gh4a.utils.ApiHelpers; import com.gh4a.utils.AvatarHandler; import com.gh4a.utils.Optional; @@ -74,7 +73,7 @@ public class IssueEditActivity extends BasePagerActivity implements AppBarLayout.OnOffsetChangedListener, View.OnClickListener, - View.OnFocusChangeListener, IssueMilestoneListFragment.SelectionCallback { + View.OnFocusChangeListener, MilestoneDialog.SelectionCallback { public static Intent makeCreateIntent(Context context, String repoOwner, String repoName) { // can't reuse makeEditIntent here, because even a null extra counts for hasExtra() return new Intent(context, IssueEditActivity.class) @@ -339,7 +338,18 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) { } @Override - public void onMilestoneSelected(@Nullable Milestone milestone) { + public void onMilestoneSelected(MilestoneDialog.MilestoneSelection milestoneSelection) { + Milestone milestone; + switch (milestoneSelection.type) { + case NO_MILESTONE: + milestone = null; + break; + case MILESTONE: + milestone = milestoneSelection.milestone; + break; + default: + throw new IllegalArgumentException("Unsupported milestone selection type " + milestoneSelection.type); + } mEditIssue = mEditIssue.toBuilder() .milestone(milestone) .build(); diff --git a/app/src/main/java/com/gh4a/activities/IssueListActivity.java b/app/src/main/java/com/gh4a/activities/IssueListActivity.java index eb60749f2..8ad6a2df0 100644 --- a/app/src/main/java/com/gh4a/activities/IssueListActivity.java +++ b/app/src/main/java/com/gh4a/activities/IssueListActivity.java @@ -52,7 +52,6 @@ import com.meisolsson.githubsdk.model.User; import com.meisolsson.githubsdk.service.issues.IssueAssigneeService; import com.meisolsson.githubsdk.service.issues.IssueLabelService; -import com.meisolsson.githubsdk.service.issues.IssueMilestoneService; import java.util.List; import java.util.Locale; @@ -60,7 +59,7 @@ public class IssueListActivity extends BaseFragmentPagerActivity implements View.OnClickListener, LoadingListFragmentBase.OnRecyclerViewCreatedListener, SearchView.OnCloseListener, SearchView.OnQueryTextListener, - MenuItem.OnActionExpandListener, IssueMilestoneListFragment.SelectionCallback { + MenuItem.OnActionExpandListener, MilestoneDialog.SelectionCallback { public static Intent makeIntent(Context context, String repoOwner, String repoName) { return makeIntent(context, repoOwner, repoName, false); } @@ -468,8 +467,18 @@ protected Intent navigateUp() { } @Override - public void onMilestoneSelected(@Nullable Milestone milestone) { - mSelectedMilestone = milestone != null ? milestone.title() : null; + public void onMilestoneSelected(MilestoneDialog.MilestoneSelection milestoneSelection) { + switch (milestoneSelection.type) { + case NO_MILESTONE: + mSelectedMilestone = ""; + break; + case ANY_MILESTONE: + mSelectedMilestone = null; + break; + case MILESTONE: + mSelectedMilestone = milestoneSelection.milestone.title(); + break; + } onFilterUpdated(); } diff --git a/app/src/main/java/com/gh4a/dialogs/MilestoneDialog.java b/app/src/main/java/com/gh4a/dialogs/MilestoneDialog.java index 4817f617a..f7ccc0c0f 100644 --- a/app/src/main/java/com/gh4a/dialogs/MilestoneDialog.java +++ b/app/src/main/java/com/gh4a/dialogs/MilestoneDialog.java @@ -1,9 +1,9 @@ package com.gh4a.dialogs; +import android.content.Context; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; -import android.support.v4.app.FragmentActivity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -38,6 +38,7 @@ public static MilestoneDialog newInstance(String repoOwner, String repoName, private boolean mShowAnyMilestoneButton; private Button mNoMilestoneButton; private Button mAnyMilestoneButton; + private SelectionCallback mSelectionCallback; @Override public void onCreate(@Nullable Bundle savedInstanceState) { @@ -48,6 +49,16 @@ public void onCreate(@Nullable Bundle savedInstanceState) { mShowAnyMilestoneButton = args.getBoolean(EXTRA_SHOW_ANY_MILESTONE); } + @Override + public void onAttach(Context context) { + super.onAttach(context); + if (!(context instanceof SelectionCallback)) { + throw new IllegalStateException("Parent of " + MilestoneDialog.class.getSimpleName() + + " must implement " + SelectionCallback.class.getSimpleName()); + } + mSelectionCallback = (SelectionCallback) context; + } + @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @@ -63,9 +74,9 @@ public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Override public void onClick(View v) { if (v == mNoMilestoneButton) { - onMilestoneSelected(Milestone.builder().title("").build()); + onMilestoneSelected(MilestoneSelection.Type.NO_MILESTONE); } else if (v == mAnyMilestoneButton) { - onMilestoneSelected(null); + onMilestoneSelected(MilestoneSelection.Type.ANY_MILESTONE); } else { super.onClick(v); } @@ -83,11 +94,35 @@ protected Fragment makeFragment(int position) { @Override public void onMilestoneSelected(@Nullable Milestone milestone) { - FragmentActivity activity = getActivity(); - if (activity instanceof IssueMilestoneListFragment.SelectionCallback) { - ((IssueMilestoneListFragment.SelectionCallback) activity) - .onMilestoneSelected(milestone); - } + onMilestoneSelected(MilestoneSelection.Type.MILESTONE, milestone); + } + + private void onMilestoneSelected(MilestoneSelection.Type type) { + onMilestoneSelected(type, null); + } + + private void onMilestoneSelected(MilestoneSelection.Type type, Milestone milestone) { + mSelectionCallback.onMilestoneSelected(new MilestoneSelection(type, milestone)); dismiss(); } + + public interface SelectionCallback { + void onMilestoneSelected(MilestoneSelection milestoneSelection); + } + + public static class MilestoneSelection { + public final Type type; + public final Milestone milestone; + + MilestoneSelection(Type type, Milestone milestone) { + this.type = type; + this.milestone = milestone; + } + + public enum Type { + NO_MILESTONE, + ANY_MILESTONE, + MILESTONE + } + } }