Skip to content

Commit 979a57e

Browse files
committed
load theme using task
1 parent 61f9d03 commit 979a57e

File tree

2 files changed

+71
-16
lines changed

2 files changed

+71
-16
lines changed

app/src/main/java/com/duy/ccppcompiler/ide/editor/theme/EditorThemeFragment.java

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import android.annotation.SuppressLint;
44
import android.content.Context;
5+
import android.os.AsyncTask;
56
import android.os.Bundle;
67
import android.support.annotation.NonNull;
78
import android.support.annotation.Nullable;
@@ -13,6 +14,7 @@
1314
import android.view.LayoutInflater;
1415
import android.view.View;
1516
import android.view.ViewGroup;
17+
import android.widget.ProgressBar;
1618
import android.widget.TextView;
1719

1820
import 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
}
Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,27 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<com.simplecityapps.recyclerview_fastscroll.views.FastScrollRecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
3-
xmlns:app="http://schemas.android.com/apk/res-auto"
4-
android:id="@+id/recyclerView"
2+
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
53
android:layout_width="match_parent"
6-
android:layout_height="match_parent"
7-
app:fastScrollPopupBgColor="?android:windowBackground"
8-
app:fastScrollPopupTextColor="?android:textColorPrimary"
9-
app:fastScrollPopupTextSize="24sp"
10-
app:fastScrollThumbColor="?colorAccent">
4+
android:layout_height="match_parent">
115

12-
</com.simplecityapps.recyclerview_fastscroll.views.FastScrollRecyclerView>
6+
<com.simplecityapps.recyclerview_fastscroll.views.FastScrollRecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
7+
xmlns:app="http://schemas.android.com/apk/res-auto"
8+
android:id="@+id/recyclerView"
9+
android:layout_width="match_parent"
10+
android:layout_height="match_parent"
11+
app:fastScrollPopupBgColor="?android:windowBackground"
12+
app:fastScrollPopupTextColor="?android:textColorPrimary"
13+
app:fastScrollPopupTextSize="24sp"
14+
app:fastScrollThumbColor="?colorAccent">
15+
16+
</com.simplecityapps.recyclerview_fastscroll.views.FastScrollRecyclerView>
17+
18+
<android.support.v4.widget.ContentLoadingProgressBar
19+
android:id="@+id/progress_bar"
20+
style="@style/Widget.AppCompat.ProgressBar"
21+
android:layout_width="wrap_content"
22+
android:layout_height="wrap_content"
23+
android:layout_gravity="center"
24+
android:indeterminate="true"
25+
android:visibility="gone" />
26+
27+
</FrameLayout>

0 commit comments

Comments
 (0)