22
33import android .annotation .SuppressLint ;
44import android .content .Context ;
5+ import android .os .AsyncTask ;
56import android .os .Bundle ;
67import android .support .annotation .NonNull ;
78import android .support .annotation .Nullable ;
1314import android .view .LayoutInflater ;
1415import android .view .View ;
1516import android .view .ViewGroup ;
17+ import android .widget .ProgressBar ;
1618import android .widget .TextView ;
1719
1820import com .duy .ccppcompiler .R ;
@@ -36,7 +38,7 @@ public class EditorThemeFragment extends Fragment {
3638 private RecyclerView mRecyclerView ;
3739 private EditorThemeAdapter mEditorThemeAdapter ;
3840 private Preferences mPreferences ;
39-
41+ private ProgressBar mProgressBar ;
4042
4143 @ Nullable
4244 @ Override
@@ -48,16 +50,19 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c
4850 public void onViewCreated (@ NonNull View view , @ Nullable Bundle savedInstanceState ) {
4951 super .onViewCreated (view , savedInstanceState );
5052 mPreferences = Preferences .getInstance (getContext ());
53+ mProgressBar = view .findViewById (R .id .progress_bar );
5154 mRecyclerView = view .findViewById (R .id .recyclerView );
5255 mRecyclerView .setLayoutManager (new LinearLayoutManager (getContext ()));
5356 mRecyclerView .addItemDecoration (new DividerItemDecoration (getContext (), DividerItemDecoration .VERTICAL ));
54- mEditorThemeAdapter = new EditorThemeAdapter (getContext ());
55- mEditorThemeAdapter .setOnThemeSelectListener ((EditorThemeAdapter .OnThemeSelectListener ) getActivity ());
56- mRecyclerView .setAdapter (mEditorThemeAdapter );
57- mRecyclerView .scrollToPosition (findThemeIndex (mPreferences .getEditorTheme ()));
57+ loadData ();
5858
5959 }
6060
61+
62+ private void loadData () {
63+ new LoadThemeTask (getContext ()).execute ();
64+ }
65+
6166 private int findThemeIndex (EditorTheme editorTheme ) {
6267 int position = mEditorThemeAdapter .getPosition (editorTheme );
6368 if (position < 0 ) {
@@ -72,7 +77,7 @@ public static class EditorThemeAdapter extends RecyclerView.Adapter<EditorThemeA
7277 private OnThemeSelectListener onThemeSelectListener ;
7378 private Mode mLanguage = Catalog .getModeByName ("C++" );
7479
75- public EditorThemeAdapter (Context context ) {
80+ EditorThemeAdapter (Context context ) {
7681 mContext = context ;
7782 mEditorThemes = ThemeLoader .getAll (context );
7883 Collections .sort (mEditorThemes , new Comparator <EditorTheme >() {
@@ -83,7 +88,7 @@ public int compare(EditorTheme o1, EditorTheme o2) {
8388 });
8489 }
8590
86- public int getPosition (EditorTheme editorTheme ) {
91+ int getPosition (EditorTheme editorTheme ) {
8792 return mEditorThemes .indexOf (editorTheme );
8893 }
8994
@@ -199,4 +204,39 @@ public ViewHolder(View itemView) {
199204 }
200205 }
201206 }
207+
208+ private class LoadThemeTask extends AsyncTask <Void , Void , Void > {
209+ private Context context ;
210+
211+ LoadThemeTask (Context context ) {
212+ this .context = context ;
213+ }
214+
215+ public Context getContext () {
216+ return context ;
217+ }
218+
219+ @ Override
220+ protected void onPreExecute () {
221+ super .onPreExecute ();
222+ mProgressBar .setVisibility (View .VISIBLE );
223+ mProgressBar .setIndeterminate (true );
224+ }
225+
226+ @ Override
227+ protected Void doInBackground (Void ... voids ) {
228+ ThemeLoader .init (getContext ());
229+ return null ;
230+ }
231+
232+ @ Override
233+ protected void onPostExecute (Void aVoid ) {
234+ super .onPostExecute (aVoid );
235+ mEditorThemeAdapter = new EditorThemeAdapter (getContext ());
236+ mEditorThemeAdapter .setOnThemeSelectListener ((EditorThemeAdapter .OnThemeSelectListener ) getActivity ());
237+ mRecyclerView .setAdapter (mEditorThemeAdapter );
238+ mRecyclerView .scrollToPosition (findThemeIndex (mPreferences .getEditorTheme ()));
239+ mProgressBar .setVisibility (View .GONE );
240+ }
241+ }
202242}
0 commit comments