Skip to content

Commit

Permalink
Add manage milestones button
Browse files Browse the repository at this point in the history
  • Loading branch information
Tunous committed Feb 3, 2019
1 parent 5ee5f9e commit 198bb05
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 25 deletions.
5 changes: 2 additions & 3 deletions app/src/main/java/com/gh4a/activities/IssueEditActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -357,12 +357,11 @@ public void onMilestoneSelected(MilestoneDialog.MilestoneSelection milestoneSele
}

private void showMilestonesDialog() {
MilestoneDialog dialog = MilestoneDialog.newInstance(mRepoOwner, mRepoName, false);
boolean fromPullRequest = mEditIssue.pullRequest() != null;
MilestoneDialog dialog = MilestoneDialog.newInstance(mRepoOwner, mRepoName, fromPullRequest, false, true);
getSupportFragmentManager().beginTransaction()
.add(dialog, "dialog_milestone")
.commitAllowingStateLoss();

// TODO: Button to manage milestones
}

private void showAssigneesDialog() {
Expand Down
4 changes: 1 addition & 3 deletions app/src/main/java/com/gh4a/activities/IssueListActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,13 @@
import com.gh4a.ServiceFactory;
import com.gh4a.dialogs.MilestoneDialog;
import com.gh4a.fragment.IssueListFragment;
import com.gh4a.fragment.IssueMilestoneListFragment;
import com.gh4a.fragment.LoadingListFragmentBase;
import com.gh4a.utils.ApiHelpers;
import com.gh4a.utils.RxUtils;
import com.gh4a.utils.SingleFactory;
import com.gh4a.utils.UiUtils;
import com.meisolsson.githubsdk.model.Issue;
import com.meisolsson.githubsdk.model.Label;
import com.meisolsson.githubsdk.model.Milestone;
import com.meisolsson.githubsdk.model.User;
import com.meisolsson.githubsdk.service.issues.IssueAssigneeService;
import com.meisolsson.githubsdk.service.issues.IssueLabelService;
Expand Down Expand Up @@ -564,7 +562,7 @@ private void showLabelsDialog() {
}

private void showMilestonesDialog() {
MilestoneDialog dialog = MilestoneDialog.newInstance(mRepoOwner, mRepoName, true);
MilestoneDialog dialog = MilestoneDialog.newInstance(mRepoOwner, mRepoName, mIsPullRequest, true, false);
getSupportFragmentManager().beginTransaction()
.add(dialog, "dialog_milestone")
.commitAllowingStateLoss();
Expand Down
73 changes: 56 additions & 17 deletions app/src/main/java/com/gh4a/dialogs/BasePagerDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,22 @@
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;

import com.gh4a.BaseActivity;
import com.gh4a.R;

public abstract class BasePagerDialog extends DialogFragment implements View.OnClickListener {
private LinearLayout mButtonBar;
private FragmentAdapter mPagerAdapter;

@Nullable
@Override
Expand All @@ -24,23 +28,8 @@ public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
View view = inflater.inflate(R.layout.dialog_pager, container, false);

ViewPager pager = view.findViewById(R.id.dialog_pager);
pager.setAdapter(new FragmentPagerAdapter(getChildFragmentManager()) {
@Override
public Fragment getItem(int position) {
return makeFragment(position);
}

@Override
public CharSequence getPageTitle(int position) {
return getString(getTabTitleResIds()[position]);
}

@Override
public int getCount() {
int[] titleResIds = getTabTitleResIds();
return titleResIds != null ? titleResIds.length : 0;
}
});
mPagerAdapter = new FragmentAdapter(getChildFragmentManager());
pager.setAdapter(mPagerAdapter);

mButtonBar = view.findViewById(R.id.button_bar);

Expand All @@ -57,6 +46,15 @@ public void onClick(View v) {
}
}

protected void refreshPages() {
for (int position = 0; position < mPagerAdapter.getCount(); position++) {
Fragment fragment = mPagerAdapter.getExistingFragment(position);
if (fragment instanceof BaseActivity.RefreshableChild) {
((BaseActivity.RefreshableChild) fragment).onRefresh();
}
}
}

protected Button addButton(int textResId) {
Button button = (Button) getLayoutInflater()
.inflate(R.layout.dialog_button, mButtonBar, false);
Expand All @@ -74,4 +72,45 @@ protected Button addButton(int textResId) {
protected abstract int[] getTabTitleResIds();

protected abstract Fragment makeFragment(int position);

private class FragmentAdapter extends FragmentPagerAdapter {
private final SparseArray<Fragment> mFragments = new SparseArray<>();

FragmentAdapter(FragmentManager fm) {
super(fm);
}

@Override
public Fragment getItem(int position) {
return makeFragment(position);
}

@Override
public Object instantiateItem(ViewGroup container, int position) {
Fragment f = (Fragment) super.instantiateItem(container, position);
mFragments.put(position, f);
return f;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
super.destroyItem(container, position, object);
mFragments.remove(position);
}

Fragment getExistingFragment(int position) {
return mFragments.get(position);
}

@Override
public CharSequence getPageTitle(int position) {
return getString(getTabTitleResIds()[position]);
}

@Override
public int getCount() {
int[] titleResIds = getTabTitleResIds();
return titleResIds != null ? titleResIds.length : 0;
}
}
}
36 changes: 34 additions & 2 deletions app/src/main/java/com/gh4a/dialogs/MilestoneDialog.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.gh4a.dialogs;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
Expand All @@ -10,6 +12,7 @@
import android.widget.Button;

import com.gh4a.R;
import com.gh4a.activities.IssueMilestoneListActivity;
import com.gh4a.fragment.IssueMilestoneListFragment;
import com.meisolsson.githubsdk.model.Milestone;

Expand All @@ -18,26 +21,35 @@ public class MilestoneDialog extends BasePagerDialog
private static final String EXTRA_OWNER = "owner";
private static final String EXTRA_REPO = "repo";
private static final String EXTRA_SHOW_ANY_MILESTONE = "show_any_milestone";
private static final String EXTRA_SHOW_MANAGE_MILESTONES_BUTTON = "show_manage_milestones_button";
private static final String EXTRA_FROM_PULL_REQUEST = "from_pull_request";
private static final int[] TITLES = new int[]{
R.string.open, R.string.closed
};
private static final int REQUEST_MANAGE_MILESTONES = 3000;

public static MilestoneDialog newInstance(String repoOwner, String repoName,
boolean showAnyMilestoneButton) {
boolean fromPullRequest, boolean showAnyMilestoneButton,
boolean showManageMilestonesButton) {
MilestoneDialog dialog = new MilestoneDialog();
Bundle args = new Bundle();
args.putString(EXTRA_OWNER, repoOwner);
args.putString(EXTRA_REPO, repoName);
args.putBoolean(EXTRA_FROM_PULL_REQUEST, fromPullRequest);
args.putBoolean(EXTRA_SHOW_ANY_MILESTONE, showAnyMilestoneButton);
args.putBoolean(EXTRA_SHOW_MANAGE_MILESTONES_BUTTON, showManageMilestonesButton);
dialog.setArguments(args);
return dialog;
}

private String mRepoOwner;
private String mRepoName;
private boolean mFromPullRequest;
private boolean mShowAnyMilestoneButton;
private boolean mShowManageMilestonesButton;
private Button mNoMilestoneButton;
private Button mAnyMilestoneButton;
private Button mManageMilestonesButton;
private SelectionCallback mSelectionCallback;

@Override
Expand All @@ -46,7 +58,9 @@ public void onCreate(@Nullable Bundle savedInstanceState) {
Bundle args = getArguments();
mRepoOwner = args.getString(EXTRA_OWNER);
mRepoName = args.getString(EXTRA_REPO);
mFromPullRequest = args.getBoolean(EXTRA_FROM_PULL_REQUEST);
mShowAnyMilestoneButton = args.getBoolean(EXTRA_SHOW_ANY_MILESTONE);
mShowManageMilestonesButton = args.getBoolean(EXTRA_SHOW_MANAGE_MILESTONES_BUTTON);
}

@Override
Expand All @@ -68,6 +82,9 @@ public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
mAnyMilestoneButton = addButton(R.string.issue_filter_by_any_milestone);
}
mNoMilestoneButton = addButton(R.string.issue_filter_by_no_milestone);
if (mShowManageMilestonesButton) {
mManageMilestonesButton = addButton(R.string.issue_manage_milestones);
}
return view;
}

Expand All @@ -77,19 +94,34 @@ public void onClick(View v) {
onMilestoneSelected(MilestoneSelection.Type.NO_MILESTONE);
} else if (v == mAnyMilestoneButton) {
onMilestoneSelected(MilestoneSelection.Type.ANY_MILESTONE);
} else if (v == mManageMilestonesButton) {
Intent intent = IssueMilestoneListActivity.makeIntent(
getContext(), mRepoOwner, mRepoName, mFromPullRequest);
startActivityForResult(intent, REQUEST_MANAGE_MILESTONES);
} else {
super.onClick(v);
}
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_MANAGE_MILESTONES) {
if (resultCode == Activity.RESULT_OK) {
refreshPages();
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}

@Override
protected int[] getTabTitleResIds() {
return TITLES;
}

@Override
protected Fragment makeFragment(int position) {
return IssueMilestoneListFragment.newInstance(mRepoOwner, mRepoName, position == 1, false);
return IssueMilestoneListFragment.newInstance(mRepoOwner, mRepoName, position == 1, mFromPullRequest);
}

@Override
Expand Down

0 comments on commit 198bb05

Please sign in to comment.