33import android .app .Activity ;
44import android .app .Fragment ;
55import android .content .Intent ;
6+ import android .content .SharedPreferences ;
67import android .content .res .ColorStateList ;
78import android .graphics .drawable .ColorDrawable ;
89import android .os .Build ;
910import android .os .Bundle ;
1011import android .support .v4 .content .ContextCompat ;
12+ import android .preference .PreferenceManager ;
1113import android .support .v7 .app .AppCompatActivity ;
1214import android .util .Log ;
1315import android .view .LayoutInflater ;
@@ -60,6 +62,14 @@ public class LoadProjectFragment extends Fragment implements AbsListView.OnItemC
6062 private ArrayList <SavedProject > savedProjects ;
6163 private View selectedView = null ;
6264
65+ private SharedPreferences prefs ;
66+ private static final String PREF_SORT = "sortType" ;
67+ public static final int SORT_TYPE_MODIFIED = 0 ;
68+ public static final int SORT_TYPE_NAME = 1 ;
69+ public static final int SORT_TYPE_AUTHOR = 2 ;
70+
71+ private int selectedPosition = -1 ;
72+
6373 /**
6474 * {@inheritDoc}
6575 */
@@ -70,6 +80,7 @@ public void onCreate(Bundle savedInstanceState) {
7080 mToolkit = (ToolkitApplication ) getActivity ().getApplicationContext ();
7181 activity = getActivity ();
7282 savedProjects = new ArrayList <>();
83+ prefs = PreferenceManager .getDefaultSharedPreferences (getActivity ());
7384
7485 String path = mToolkit .getSavedDir ();
7586 Log .d ("Files" , "Path: " + path );
@@ -99,13 +110,6 @@ public void onCreate(Bundle savedInstanceState) {
99110 }
100111 }
101112
102- Collections .sort (savedProjects , new Comparator <SavedProject >() {
103- public int compare (SavedProject f1 , SavedProject f2 ) {
104- return Long .valueOf (f1 .getFile ().lastModified ()).compareTo (f2 .getFile ().lastModified ());
105- }
106- });
107-
108- Collections .reverse (savedProjects );
109113 }
110114
111115 /**
@@ -124,7 +128,11 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
124128 public void onViewCreated (View view , Bundle savedInstanceState ) {
125129 mAdapter = new SavedProjectAdapter (getActivity (), savedProjects );
126130 mListView = (AbsListView ) view .findViewById (android .R .id .list );
131+ mAdapter .setSortType (prefs .getInt (PREF_SORT , SORT_TYPE_MODIFIED ));
132+ mAdapter .sort ();
127133 setAdapter (mAdapter );
134+
135+
128136 mListView .setOnItemClickListener (this );
129137 mListView .setOnItemLongClickListener (new AdapterView .OnItemLongClickListener () {
130138 @ Override
@@ -217,13 +225,7 @@ public void onResume() {
217225 }
218226 }
219227
220- Collections .sort (savedProjects , new Comparator <SavedProject >() {
221- public int compare (SavedProject f1 , SavedProject f2 ) {
222- return Long .valueOf (f1 .getFile ().lastModified ()).compareTo (f2 .getFile ().lastModified ());
223- }
224- });
225-
226- Collections .reverse (savedProjects );
228+ mAdapter .sort ();
227229 mAdapter .notifyDataSetChanged ();
228230 }
229231 super .onResume ();
@@ -276,10 +278,14 @@ private void changeColorScheme() {
276278 * {@inheritDoc}
277279 */
278280 @ Override
279- public void onCreateOptionsMenu (Menu menu , MenuInflater inflater ) {
280- super .onCreateOptionsMenu (menu , inflater );
281+
282+ public void onPrepareOptionsMenu (Menu menu ) {
283+ super .onPrepareOptionsMenu (menu );
284+ menu .clear ();
281285 if (showTemplateSelectedMenu ) {
282286 activity .getMenuInflater ().inflate (R .menu .menu_project_selected , menu );
287+ } else {
288+ activity .getMenuInflater ().inflate (R .menu .menu_project , menu );
283289 }
284290 }
285291
@@ -293,22 +299,43 @@ public boolean onOptionsItemSelected(MenuItem item) {
293299 switch (id ) {
294300 case R .id .action_delete :
295301
296- final MaterialDialog dialog = new MaterialDialog .Builder (activity )
302+ final MaterialDialog dialogDelete = new MaterialDialog .Builder (activity )
297303 .title (R .string .dialog_delete_title )
298304 .content (R .string .dialog_delete_msg )
299305 .positiveText (R .string .dialog_yes )
300306 .negativeText (R .string .dialog_no )
301307 .build ();
302308
303- dialog .getActionButton (DialogAction .POSITIVE ).setOnClickListener (new View .OnClickListener () {
309+ dialogDelete .getActionButton (DialogAction .POSITIVE ).setOnClickListener (new View .OnClickListener () {
304310 @ Override
305311 public void onClick (View v ) {
306- dialog .dismiss ();
307- deleteItem (mAdapter . getSelectedPosition () );
312+ dialogDelete .dismiss ();
313+ deleteItem (selectedPosition );
308314 restoreSelectedView ();
309315 }
310316 });
311- dialog .show ();
317+ dialogDelete .show ();
318+ break ;
319+ case R .id .action_sort :
320+
321+ final MaterialDialog dialogSort = new MaterialDialog .Builder (activity )
322+ .title (R .string .dialog_sort_title )
323+ .items (R .array .project_sort )
324+ .itemsCallbackSingleChoice (mAdapter .getSortType (), new MaterialDialog .ListCallbackSingleChoice () {
325+ @ Override
326+ public boolean onSelection (MaterialDialog dialog , View itemView , int which , CharSequence text ) {
327+ mAdapter .setSortType (which );
328+ SharedPreferences .Editor editor = prefs .edit ();
329+ editor .putInt (PREF_SORT , which );
330+ editor .commit ();
331+ mAdapter .sort ();
332+ mAdapter .notifyDataSetChanged ();
333+ return true ;
334+ }
335+ })
336+ .build ();
337+
338+ dialogSort .show ();
312339 break ;
313340 default : //do nothing
314341 break ;
0 commit comments