22
33import android .annotation .SuppressLint ;
44import android .content .Context ;
5+ import android .content .res .AssetManager ;
56import android .os .AsyncTask ;
67import android .os .Bundle ;
78import android .support .annotation .NonNull ;
1920
2021import com .duy .ide .editor .Highlighter ;
2122import com .duy .ide .editor .editor .R ;
23+ import com .duy .ide .editor .theme .model .Constants ;
2224import com .duy .ide .editor .theme .model .EditorTheme ;
2325import com .duy .ide .editor .view .IEditAreaView ;
2426import com .jecelyin .editor .v2 .Preferences ;
2527import com .jecelyin .editor .v2 .highlight .Buffer ;
2628import com .simplecityapps .recyclerview_fastscroll .views .FastScrollRecyclerView ;
2729
30+ import org .apache .commons .io .IOUtils ;
2831import org .gjt .sp .jedit .Catalog ;
2932import org .gjt .sp .jedit .Mode ;
3033
34+ import java .io .InputStream ;
3135import java .util .ArrayList ;
32- import java .util .Collections ;
33- import java .util .Comparator ;
36+ import java .util .Arrays ;
3437import java .util .HashMap ;
3538
3639public class EditorThemeFragment extends Fragment {
@@ -54,8 +57,12 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
5457 mRecyclerView = view .findViewById (R .id .recyclerView );
5558 mRecyclerView .setLayoutManager (new LinearLayoutManager (getContext ()));
5659 mRecyclerView .addItemDecoration (new DividerItemDecoration (getContext (), DividerItemDecoration .VERTICAL ));
57- loadData ();
5860
61+ mEditorThemeAdapter = new EditorThemeAdapter (getContext ());
62+ mEditorThemeAdapter .setOnThemeSelectListener ((EditorThemeAdapter .OnThemeSelectListener ) getActivity ());
63+ mRecyclerView .setAdapter (mEditorThemeAdapter );
64+
65+ loadData ();
5966 }
6067
6168 private void loadData () {
@@ -83,17 +90,36 @@ public static class EditorThemeAdapter extends RecyclerView.Adapter<EditorThemeA
8390 private final ArrayList <EditorTheme > mEditorThemes ;
8491 private Context mContext ;
8592 private OnThemeSelectListener onThemeSelectListener ;
86- private Mode mLanguage = Catalog .getModeByName ("C++" );
93+ private Mode mLanguage ;
94+ private String mSampleCode ;
8795
8896 EditorThemeAdapter (Context context ) {
8997 mContext = context ;
90- mEditorThemes = ThemeLoader .getAll (context );
91- Collections .sort (mEditorThemes , new Comparator <EditorTheme >() {
92- @ Override
93- public int compare (EditorTheme o1 , EditorTheme o2 ) {
94- return o1 .getName ().compareTo (o2 .getName ());
98+ mEditorThemes = new ArrayList <>();
99+ resolveLanguage ();
100+ }
101+
102+ private void resolveLanguage () {
103+ if (mSampleCode == null ) {
104+ try {
105+ String fileName = null ;
106+ if (mContext .getPackageName ().contains ("cpp" )) {
107+ fileName = "templates/cplusplus.template" ;
108+ mLanguage = Catalog .getModeByName ("C++" );
109+ } else if (mContext .getPackageName ().contains ("java" )) {
110+ fileName = "templates/java.template" ;
111+ mLanguage = Catalog .getModeByName ("Java" );
112+ } else if (mContext .getPackageName ().contains ("pascal" )) {
113+ fileName = "templates/pascal.template" ;
114+ mLanguage = Catalog .getModeByName ("Pascal" );
115+ }
116+ InputStream input = mContext .getAssets ().open (fileName );
117+ mSampleCode = IOUtils .toString (input );
118+ input .close ();
119+ } catch (Exception e ) {
120+ e .printStackTrace ();
95121 }
96- });
122+ }
97123 }
98124
99125 int getPosition (EditorTheme editorTheme ) {
@@ -150,39 +176,13 @@ public int getItemCount() {
150176 return mEditorThemes .size ();
151177 }
152178
179+
153180 private String getSampleData () {
154- return "// C++ Program to Find Largest Element of an Array\n " +
155- "\n " +
156- "// This program takes n number of element from user (where, n is specified by user) and stores data in an array. Then, this program displays the largest element of that array using loops.\n " +
157- "\n " +
158- "#include <iostream>\n " +
159- "\n " +
160- "using namespace std;\n " +
161- "\n " +
162- "int main() {\n " +
163- " int i, n;\n " +
164- " float arr[100];\n " +
165- "\n " +
166- " cout << \" Enter total number of elements(1 to 100): \" ;\n " +
167- " cin >> n;\n " +
168- " cout << endl;\n " +
169- "\n " +
170- " // Store number entered by the user\n " +
171- " for (i = 0; i < n; ++i) {\n " +
172- " cout << \" Enter Number \" << i + 1 << \" : \" ;\n " +
173- " cin >> arr[i];\n " +
174- " }\n " +
175- "\n " +
176- " // Loop to store largest number to arr[0]\n " +
177- " for (i = 1; i < n; ++i) {\n " +
178- " // Change < to > if you want to find the smallest element\n " +
179- " if (arr[0] < arr[i])\n " +
180- " arr[0] = arr[i];\n " +
181- " }\n " +
182- " cout << \" Largest element = \" << arr[0];\n " +
183- "\n " +
184- " return 0;\n " +
185- "}" ;
181+
182+ if (mSampleCode == null ) {
183+ mSampleCode = Constants .C_PLUS_PLUS_SAMPLE ;
184+ }
185+ return mSampleCode ;
186186 }
187187
188188 @ NonNull
@@ -195,6 +195,11 @@ public void setOnThemeSelectListener(OnThemeSelectListener onThemeSelectListener
195195 this .onThemeSelectListener = onThemeSelectListener ;
196196 }
197197
198+ public void addTheme (EditorTheme theme ) {
199+ mEditorThemes .add (theme );
200+ notifyItemInserted (mEditorThemes .size () - 1 );
201+ }
202+
198203 public interface OnThemeSelectListener {
199204 void onEditorThemeSelected (EditorTheme theme );
200205 }
@@ -204,7 +209,7 @@ static class ViewHolder extends RecyclerView.ViewHolder {
204209 IEditAreaView mEditorView ;
205210 TextView mTxtName ;
206211
207- public ViewHolder (View itemView ) {
212+ ViewHolder (View itemView ) {
208213 super (itemView );
209214 mEditorView = itemView .findViewById (R .id .editor_view );
210215 mTxtName = itemView .findViewById (R .id .txt_name );
@@ -213,8 +218,11 @@ public ViewHolder(View itemView) {
213218 }
214219 }
215220
216- private class LoadThemeTask extends AsyncTask <Void , Void , Void > {
221+ @ SuppressLint ("StaticFieldLeak" )
222+ private class LoadThemeTask extends AsyncTask <Void , EditorTheme , Void > {
223+ private static final String TAG = "LoadThemeTask" ;
217224 private Context context ;
225+ private AssetManager mAssetManager ;
218226
219227 LoadThemeTask (Context context ) {
220228 this .context = context ;
@@ -227,27 +235,49 @@ public Context getContext() {
227235 @ Override
228236 protected void onPreExecute () {
229237 super .onPreExecute ();
238+ mAssetManager = getContext ().getAssets ();
239+
230240 mProgressBar .setVisibility (View .VISIBLE );
231241 mProgressBar .setIndeterminate (true );
232242 }
233243
234244 @ Override
235245 protected Void doInBackground (Void ... voids ) {
236- ThemeLoader .init (getContext ());
246+ try {
247+ String [] list = mAssetManager .list (ThemeLoader .ASSET_PATH );
248+ Arrays .sort (list );
249+ for (String name : list ) {
250+ if (isCancelled ()) {
251+ return null ;
252+ }
253+ Thread .sleep (1 );
254+ EditorTheme theme = ThemeLoader .getTheme (context , name );
255+ publishProgress (theme );
256+ }
257+ } catch (Exception e ) {
258+ e .printStackTrace ();
259+ }
237260 return null ;
238261 }
239262
263+ @ Override
264+ protected void onProgressUpdate (EditorTheme ... themes ) {
265+ super .onProgressUpdate (themes );
266+ try {
267+ mEditorThemeAdapter .addTheme (themes [0 ]);
268+ } catch (Exception e ) {
269+ }
270+ }
271+
240272 @ Override
241273 protected void onPostExecute (Void aVoid ) {
242274 super .onPostExecute (aVoid );
243275 if (isCancelled ()) {
244276 return ;
245277 }
246- mEditorThemeAdapter = new EditorThemeAdapter (getContext ());
247- mEditorThemeAdapter .setOnThemeSelectListener ((EditorThemeAdapter .OnThemeSelectListener ) getActivity ());
248- mRecyclerView .setAdapter (mEditorThemeAdapter );
249278 mRecyclerView .scrollToPosition (findThemeIndex (mPreferences .getEditorTheme ()));
250279 mProgressBar .setVisibility (View .GONE );
280+
251281 }
252282 }
253283}
0 commit comments