|
15 | 15 | */ |
16 | 16 | package com.gh4a.fragment; |
17 | 17 |
|
18 | | -import org.eclipse.egit.github.core.Repository; |
19 | | -import org.eclipse.egit.github.core.client.PageIterator; |
20 | | -import org.eclipse.egit.github.core.service.StarService; |
21 | | - |
22 | 18 | import android.os.Bundle; |
23 | 19 | import android.support.v7.widget.RecyclerView; |
| 20 | +import android.view.Menu; |
| 21 | +import android.view.MenuInflater; |
| 22 | +import android.view.MenuItem; |
24 | 23 |
|
25 | 24 | import com.gh4a.Gh4Application; |
26 | 25 | import com.gh4a.R; |
27 | 26 | import com.gh4a.activities.RepositoryActivity; |
28 | 27 | import com.gh4a.adapter.RepositoryAdapter; |
29 | 28 | import com.gh4a.adapter.RootAdapter; |
30 | 29 |
|
| 30 | +import org.eclipse.egit.github.core.Repository; |
| 31 | +import org.eclipse.egit.github.core.client.PageIterator; |
| 32 | +import org.eclipse.egit.github.core.service.StarService; |
| 33 | + |
31 | 34 | import java.util.HashMap; |
32 | 35 |
|
33 | 36 | public class StarredRepositoryListFragment extends PagedDataBaseFragment<Repository> { |
34 | | - public static StarredRepositoryListFragment newInstance(String login, |
35 | | - String sortOrder, String sortDirection) { |
| 37 | + private static final String STATE_KEY_SORT_ORDER = "sort_order"; |
| 38 | + private static final String STATE_KEY_SORT_DIRECTION = "sort_direction"; |
| 39 | + |
| 40 | + public static StarredRepositoryListFragment newInstance(String login) { |
36 | 41 | StarredRepositoryListFragment f = new StarredRepositoryListFragment(); |
37 | 42 |
|
38 | 43 | Bundle args = new Bundle(); |
39 | 44 | args.putString("user", login); |
40 | | - args.putString("sort_order", sortOrder); |
41 | | - args.putString("sort_direction", sortDirection); |
42 | 45 | f.setArguments(args); |
43 | 46 |
|
44 | 47 | return f; |
45 | 48 | } |
46 | 49 |
|
47 | 50 | private String mLogin; |
48 | | - private String mSortOrder; |
49 | | - private String mSortDirection; |
| 51 | + private String mSortOrder = "created"; |
| 52 | + private String mSortDirection = "desc"; |
| 53 | + private RepositoryListContainerFragment.SortDrawerHelper mSortHelper; |
50 | 54 |
|
51 | 55 | @Override |
52 | 56 | public void onCreate(Bundle savedInstanceState) { |
53 | 57 | super.onCreate(savedInstanceState); |
54 | 58 | mLogin = getArguments().getString("user"); |
55 | | - mSortOrder = getArguments().getString("sort_order"); |
56 | | - mSortDirection = getArguments().getString("sort_direction"); |
| 59 | + |
| 60 | + mSortHelper = new RepositoryListContainerFragment.SortDrawerHelper(); |
| 61 | + |
| 62 | + if (savedInstanceState != null && savedInstanceState.containsKey(STATE_KEY_SORT_ORDER)) { |
| 63 | + mSortOrder = savedInstanceState.getString(STATE_KEY_SORT_ORDER); |
| 64 | + mSortDirection = savedInstanceState.getString(STATE_KEY_SORT_DIRECTION); |
| 65 | + } |
| 66 | + setHasOptionsMenu(true); |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public void onSaveInstanceState(Bundle outState) { |
| 71 | + super.onSaveInstanceState(outState); |
| 72 | + outState.putString(STATE_KEY_SORT_ORDER, mSortOrder); |
| 73 | + outState.putString(STATE_KEY_SORT_DIRECTION, mSortDirection); |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { |
| 78 | + super.onCreateOptionsMenu(menu, inflater); |
| 79 | + inflater.inflate(R.menu.repo_starred_list_menu, menu); |
| 80 | + mSortHelper.selectSortType(menu, mSortOrder, mSortDirection, true); |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public boolean onOptionsItemSelected(MenuItem item) { |
| 85 | + String[] sortOrderAndDirection = mSortHelper.handleSelectionAndGetSortOrder(item); |
| 86 | + if (sortOrderAndDirection == null) { |
| 87 | + return false; |
| 88 | + } |
| 89 | + mSortOrder = sortOrderAndDirection[0]; |
| 90 | + mSortDirection = sortOrderAndDirection[1]; |
| 91 | + item.setChecked(true); |
| 92 | + recreateIteratorAndRefresh(); |
| 93 | + return true; |
57 | 94 | } |
58 | 95 |
|
59 | 96 | @Override |
|
0 commit comments