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 ;
11+ import android .preference .PreferenceManager ;
1012import android .support .v7 .app .AppCompatActivity ;
1113import android .util .Log ;
1214import android .view .LayoutInflater ;
@@ -58,6 +60,12 @@ public class LoadProjectFragment extends Fragment implements AbsListView.OnItemC
5860 private ArrayList <SavedProject > savedProjects ;
5961 private View selectedView = null ;
6062
63+ private SharedPreferences prefs ;
64+ private static final String PREF_SORT = "sortType" ;
65+ public static final int SORT_TYPE_MODIFIED = 0 ;
66+ public static final int SORT_TYPE_NAME = 1 ;
67+ public static final int SORT_TYPE_AUTHOR = 2 ;
68+
6169 private int selectedPosition = -1 ;
6270
6371 /**
@@ -70,6 +78,7 @@ public void onCreate(Bundle savedInstanceState) {
7078 mToolkit = (ToolkitApplication ) getActivity ().getApplicationContext ();
7179 activity = getActivity ();
7280 savedProjects = new ArrayList <>();
81+ prefs = PreferenceManager .getDefaultSharedPreferences (getActivity ());
7382
7483 String path = mToolkit .getSavedDir ();
7584 Log .d ("Files" , "Path: " + path );
@@ -105,13 +114,6 @@ public void onCreate(Bundle savedInstanceState) {
105114 }
106115 }
107116
108- Collections .sort (savedProjects , new Comparator <SavedProject >() {
109- public int compare (SavedProject f1 , SavedProject f2 ) {
110- return Long .valueOf (f1 .getFile ().lastModified ()).compareTo (f2 .getFile ().lastModified ());
111- }
112- });
113-
114- Collections .reverse (savedProjects );
115117 }
116118
117119 /**
@@ -131,7 +133,11 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
131133 public void onViewCreated (View view , Bundle savedInstanceState ) {
132134 mAdapter = new SavedProjectAdapter (getActivity (), savedProjects );
133135 mListView = (AbsListView ) view .findViewById (android .R .id .list );
136+ mAdapter .setSortType (prefs .getInt (PREF_SORT , SORT_TYPE_MODIFIED ));
137+ mAdapter .sort ();
134138 setAdapter (mAdapter );
139+
140+
135141 mListView .setOnItemClickListener (this );
136142 mListView .setOnItemLongClickListener (new AdapterView .OnItemLongClickListener () {
137143 @ Override
@@ -230,13 +236,7 @@ public void onResume() {
230236 }
231237 }
232238
233- Collections .sort (savedProjects , new Comparator <SavedProject >() {
234- public int compare (SavedProject f1 , SavedProject f2 ) {
235- return Long .valueOf (f1 .getFile ().lastModified ()).compareTo (f2 .getFile ().lastModified ());
236- }
237- });
238-
239- Collections .reverse (savedProjects );
239+ mAdapter .sort ();
240240 mAdapter .notifyDataSetChanged ();
241241 }
242242 super .onResume ();
@@ -291,8 +291,11 @@ public void changeColorScheme() {
291291 @ Override
292292 public void onPrepareOptionsMenu (Menu menu ) {
293293 super .onPrepareOptionsMenu (menu );
294+ menu .clear ();
294295 if (showTemplateSelectedMenu ) {
295296 activity .getMenuInflater ().inflate (R .menu .menu_project_selected , menu );
297+ } else {
298+ activity .getMenuInflater ().inflate (R .menu .menu_project , menu );
296299 }
297300 }
298301
@@ -306,22 +309,43 @@ public boolean onOptionsItemSelected(MenuItem item) {
306309 switch (id ) {
307310 case R .id .action_delete :
308311
309- final MaterialDialog dialog = new MaterialDialog .Builder (activity )
312+ final MaterialDialog dialogDelete = new MaterialDialog .Builder (activity )
310313 .title (R .string .dialog_delete_title )
311314 .content (R .string .dialog_delete_msg )
312315 .positiveText (R .string .dialog_yes )
313316 .negativeText (R .string .dialog_no )
314317 .build ();
315318
316- dialog .getActionButton (DialogAction .POSITIVE ).setOnClickListener (new View .OnClickListener () {
319+ dialogDelete .getActionButton (DialogAction .POSITIVE ).setOnClickListener (new View .OnClickListener () {
317320 @ Override
318321 public void onClick (View v ) {
319- dialog .dismiss ();
322+ dialogDelete .dismiss ();
320323 deleteItem (selectedPosition );
321324 restoreSelectedView ();
322325 }
323326 });
324- dialog .show ();
327+ dialogDelete .show ();
328+ break ;
329+ case R .id .action_sort :
330+
331+ final MaterialDialog dialogSort = new MaterialDialog .Builder (activity )
332+ .title (R .string .dialog_sort_title )
333+ .items (R .array .project_sort )
334+ .itemsCallbackSingleChoice (mAdapter .getSortType (), new MaterialDialog .ListCallbackSingleChoice () {
335+ @ Override
336+ public boolean onSelection (MaterialDialog dialog , View itemView , int which , CharSequence text ) {
337+ mAdapter .setSortType (which );
338+ SharedPreferences .Editor editor = prefs .edit ();
339+ editor .putInt (PREF_SORT , which );
340+ editor .commit ();
341+ mAdapter .sort ();
342+ mAdapter .notifyDataSetChanged ();
343+ return true ;
344+ }
345+ })
346+ .build ();
347+
348+ dialogSort .show ();
325349 break ;
326350 }
327351 return super .onOptionsItemSelected (item );
0 commit comments