Skip to content

Commit

Permalink
Make it easier to distinguish between selection types
Browse files Browse the repository at this point in the history
  • Loading branch information
Tunous committed Feb 3, 2019
1 parent 968a2fa commit 5ee5f9e
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 15 deletions.
16 changes: 13 additions & 3 deletions app/src/main/java/com/gh4a/activities/IssueEditActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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();
Expand Down
17 changes: 13 additions & 4 deletions app/src/main/java/com/gh4a/activities/IssueListActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,14 @@
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;

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);
}
Expand Down Expand Up @@ -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();
}

Expand Down
51 changes: 43 additions & 8 deletions app/src/main/java/com/gh4a/dialogs/MilestoneDialog.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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,
Expand All @@ -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);
}
Expand All @@ -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
}
}
}

0 comments on commit 5ee5f9e

Please sign in to comment.