Skip to content

Commit 18c6e19

Browse files
committed
Move theme activity to fragment
1 parent 485ec18 commit 18c6e19

File tree

10 files changed

+147
-51
lines changed

10 files changed

+147
-51
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@
125125
<activity
126126
android:name="jackpal.androidterm.TermPreferencesActivity"
127127
android:label="@string/preferences" />
128-
<activity android:name="com.duy.editor.EditorThemeActivity" />
128+
<activity android:name="com.duy.editor.theme.ThemeActivity" />
129129
</application>
130130

131131
</manifest>

app/src/main/java/com/duy/editor/CodeEditorActivity.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import com.duy.common.DLog;
4242
import com.duy.common.purchase.InAppPurchaseHelper;
4343
import com.duy.common.purchase.Premium;
44+
import com.duy.editor.theme.ThemeActivity;
4445
import com.duy.ide.editor.SimpleEditorActivity;
4546
import com.duy.ide.filemanager.SaveListener;
4647
import com.jecelyin.common.utils.UIUtils;
@@ -168,7 +169,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
168169
break;
169170

170171
case R.id.action_editor_color_scheme:
171-
startActivity(new Intent(this, EditorThemeActivity.class));
172+
startActivity(new Intent(this, ThemeActivity.class));
172173
break;
173174
}
174175
return super.onOptionsItemSelected(item);

editor-view/src/main/java/com/duy/ide/editor/theme/ThemeAdapter.java renamed to app/src/main/java/com/duy/editor/theme/EditorThemeAdapter.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.duy.ide.editor.theme;
1+
package com.duy.editor.theme;
22

33
import android.content.Context;
44
import android.core.text.SpannableStringBuilder;
@@ -11,7 +11,8 @@
1111
import android.view.ViewGroup;
1212
import android.widget.TextView;
1313

14-
import com.duy.ide.editor.editor.R;
14+
import com.duy.ccppcompiler.R;
15+
import com.duy.ide.editor.theme.ThemeLoader;
1516
import com.duy.ide.editor.theme.model.EditorTheme;
1617
import com.jecelyin.editor.v2.editor.Highlighter;
1718
import com.jecelyin.editor.v2.highlight.Buffer;
@@ -24,14 +25,13 @@
2425
import java.util.Comparator;
2526
import java.util.HashMap;
2627

27-
public class ThemeAdapter extends RecyclerView.Adapter<ThemeAdapter.ViewHolder> implements FastScrollRecyclerView.SectionedAdapter {
28+
public class EditorThemeAdapter extends RecyclerView.Adapter<EditorThemeAdapter.ViewHolder> implements FastScrollRecyclerView.SectionedAdapter {
2829
private final ArrayList<EditorTheme> mEditorThemes;
2930
private Context mContext;
3031
private OnThemeSelectListener onThemeSelectListener;
3132

32-
public ThemeAdapter(Context context) {
33+
public EditorThemeAdapter(Context context) {
3334
mContext = context;
34-
ThemeLoader.init(context);
3535
mEditorThemes = ThemeLoader.getAll(context);
3636
Collections.sort(mEditorThemes, new Comparator<EditorTheme>() {
3737
@Override
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.duy.editor.theme;
2+
3+
import android.os.Bundle;
4+
import android.support.annotation.NonNull;
5+
import android.support.annotation.Nullable;
6+
import android.support.v4.app.Fragment;
7+
import android.support.v7.widget.DividerItemDecoration;
8+
import android.support.v7.widget.LinearLayoutManager;
9+
import android.support.v7.widget.RecyclerView;
10+
import android.view.LayoutInflater;
11+
import android.view.View;
12+
import android.view.ViewGroup;
13+
14+
import com.duy.ccppcompiler.R;
15+
import com.duy.ide.editor.theme.model.EditorTheme;
16+
import com.jecelyin.editor.v2.Preferences;
17+
18+
public class EditorThemeFragment extends Fragment {
19+
private RecyclerView mRecyclerView;
20+
private EditorThemeAdapter mEditorThemeAdapter;
21+
private Preferences mPreferences;
22+
23+
public static EditorThemeFragment newInstance() {
24+
25+
Bundle args = new Bundle();
26+
27+
EditorThemeFragment fragment = new EditorThemeFragment();
28+
fragment.setArguments(args);
29+
return fragment;
30+
}
31+
32+
@Nullable
33+
@Override
34+
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
35+
return inflater.inflate(R.layout.fragment_editor_theme, container, false);
36+
}
37+
38+
@Override
39+
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
40+
super.onViewCreated(view, savedInstanceState);
41+
mPreferences = Preferences.getInstance(getContext());
42+
mRecyclerView = view.findViewById(R.id.recyclerView);
43+
mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
44+
mRecyclerView.addItemDecoration(new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL));
45+
mEditorThemeAdapter = new EditorThemeAdapter(getContext());
46+
mEditorThemeAdapter.setOnThemeSelectListener((EditorThemeAdapter.OnThemeSelectListener) getActivity());
47+
mRecyclerView.setAdapter(mEditorThemeAdapter);
48+
mRecyclerView.scrollToPosition(findThemeIndex(mPreferences.getEditorTheme()));
49+
50+
}
51+
52+
private int findThemeIndex(EditorTheme editorTheme) {
53+
int position = mEditorThemeAdapter.getPosition(editorTheme);
54+
if (position < 0) {
55+
return 0;
56+
}
57+
return position;
58+
}
59+
60+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.duy.editor.theme;
2+
3+
import android.support.v4.app.Fragment;
4+
5+
public class TerminalThemeFragment extends Fragment {
6+
}
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,27 @@
1-
package com.duy.editor;
1+
package com.duy.editor.theme;
22

33
import android.content.SharedPreferences;
44
import android.os.Bundle;
55
import android.support.annotation.Nullable;
6-
import android.support.design.widget.Snackbar;
7-
import android.support.v7.widget.DividerItemDecoration;
8-
import android.support.v7.widget.LinearLayoutManager;
9-
import android.support.v7.widget.RecyclerView;
6+
import android.support.design.widget.TabLayout;
7+
import android.support.v4.app.Fragment;
8+
import android.support.v4.app.FragmentPagerAdapter;
9+
import android.support.v4.view.ViewPager;
1010
import android.support.v7.widget.Toolbar;
1111
import android.view.View;
1212
import android.widget.AdapterView;
1313
import android.widget.Spinner;
14+
import android.widget.Toast;
1415

1516
import com.duy.ccppcompiler.R;
1617
import com.duy.ccppcompiler.ui.dialogs.PremiumDialog;
1718
import com.duy.common.purchase.InAppPurchaseHelper;
1819
import com.duy.common.purchase.Premium;
19-
import com.duy.ide.editor.theme.ThemeAdapter;
2020
import com.duy.ide.editor.theme.model.EditorTheme;
2121
import com.jecelyin.editor.v2.Preferences;
2222
import com.jecelyin.editor.v2.ThemeSupportActivity;
2323

24-
public class EditorThemeActivity extends ThemeSupportActivity implements ThemeAdapter.OnThemeSelectListener {
25-
private RecyclerView mRecyclerView;
26-
private ThemeAdapter mThemeAdapter;
27-
private Spinner mSpinner;
24+
public class ThemeActivity extends ThemeSupportActivity implements EditorThemeAdapter.OnThemeSelectListener {
2825
private Preferences mPreferences;
2926
private InAppPurchaseHelper mInAppPurchaseHelper;
3027

@@ -35,20 +32,14 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
3532
setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
3633
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
3734
setTitle("");
38-
mPreferences = Preferences.getInstance(this);
3935

40-
mRecyclerView = findViewById(R.id.recyclerView);
41-
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
42-
mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
43-
mThemeAdapter = new ThemeAdapter(this);
44-
mThemeAdapter.setOnThemeSelectListener(this);
45-
mRecyclerView.setAdapter(mThemeAdapter);
46-
mRecyclerView.scrollToPosition(findThemeIndex(mPreferences.getEditorTheme()));
36+
mPreferences = Preferences.getInstance(this);
37+
mInAppPurchaseHelper = new InAppPurchaseHelper(this);
4738

4839
boolean useLightTheme = mPreferences.isUseLightTheme();
49-
mSpinner = findViewById(R.id.spinner_themes);
50-
mSpinner.setSelection(useLightTheme ? 0 : 1);
51-
mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
40+
Spinner spinner = findViewById(R.id.spinner_themes);
41+
spinner.setSelection(useLightTheme ? 0 : 1);
42+
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
5243
@Override
5344
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
5445
useLightTheme(position == 0);
@@ -59,19 +50,41 @@ public void onNothingSelected(AdapterView<?> parent) {
5950

6051
}
6152
});
62-
63-
mInAppPurchaseHelper = new InAppPurchaseHelper(this);
6453
mPreferences.registerOnSharedPreferenceChangeListener(this);
65-
}
6654

67-
private int findThemeIndex(EditorTheme editorTheme) {
68-
int position = mThemeAdapter.getPosition(editorTheme);
69-
if (position < 0) {
70-
return 0;
71-
}
72-
return position;
73-
}
55+
ViewPager viewPager = findViewById(R.id.view_pager);
56+
viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
57+
@Override
58+
public Fragment getItem(int position) {
59+
switch (position) {
60+
case 0:
61+
return new EditorThemeFragment();
62+
case 1:
63+
return new TerminalThemeFragment();
64+
}
65+
return null;
66+
}
7467

68+
@Nullable
69+
@Override
70+
public CharSequence getPageTitle(int position) {
71+
switch (position) {
72+
case 0:
73+
return getString(R.string.editor);
74+
case 1:
75+
return getString(R.string.terminal);
76+
}
77+
return "";
78+
}
79+
80+
@Override
81+
public int getCount() {
82+
return 2;
83+
}
84+
});
85+
TabLayout tabLayout = findViewById(R.id.tab_layout);
86+
tabLayout.setupWithViewPager(viewPager);
87+
}
7588

7689
private void useLightTheme(boolean useLightTheme) {
7790
if (mPreferences.isUseLightTheme() != useLightTheme) {
@@ -96,10 +109,11 @@ public void onThemeSelected(EditorTheme theme) {
96109
if (Premium.isPremiumUser(this)) {
97110
mPreferences.setEditorTheme(theme.getFileName());
98111
String text = getString(R.string.selected_theme, theme.getName());
99-
Snackbar.make(mRecyclerView, text, Snackbar.LENGTH_SHORT).show();
112+
Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
100113
} else {
101114
PremiumDialog premiumDialog = new PremiumDialog(this, mInAppPurchaseHelper);
102115
premiumDialog.show();
103116
}
104117
}
118+
105119
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?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"
5+
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">
11+
12+
</com.simplecityapps.recyclerview_fastscroll.views.FastScrollRecyclerView>

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,5 @@
3636

3737
<string name="button_purchase_restore">Restore</string>
3838
<string name="selected_theme">Selected %s. Restart app to apply theme.</string>
39+
<string name="terminal">Terminal</string>
3940
</resources>

editor-view/src/main/res/layout/activity_editor_theme.xml

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
2+
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
33
xmlns:app="http://schemas.android.com/apk/res-auto"
44
android:layout_width="match_parent"
55
android:layout_height="match_parent"
66
android:orientation="vertical">
77

8-
<android.support.design.widget.AppBarLayout xmlns:android="http://schemas.android.com/apk/res/android"
9-
xmlns:app="http://schemas.android.com/apk/res-auto"
8+
<android.support.design.widget.AppBarLayout
109
android:id="@+id/appbar"
1110
android:layout_width="match_parent"
1211
android:layout_height="wrap_content">
@@ -18,6 +17,13 @@
1817
app:popupTheme="?attr/actionBarPopupTheme"
1918
app:theme="?attr/actionBarTheme">
2019

20+
<android.support.design.widget.TabLayout
21+
android:id="@+id/tab_layout"
22+
android:layout_width="match_parent"
23+
android:layout_height="wrap_content">
24+
25+
</android.support.design.widget.TabLayout>
26+
2127
<FrameLayout
2228
android:layout_width="match_parent"
2329
android:layout_height="match_parent">
@@ -37,15 +43,11 @@
3743

3844
</android.support.design.widget.AppBarLayout>
3945

40-
<com.simplecityapps.recyclerview_fastscroll.views.FastScrollRecyclerView
41-
android:id="@+id/recyclerView"
46+
<android.support.v4.view.ViewPager
47+
android:id="@+id/view_pager"
4248
android:layout_width="match_parent"
4349
android:layout_height="match_parent"
44-
app:fastScrollPopupBgColor="?android:windowBackground"
45-
app:fastScrollPopupTextColor="?android:textColorPrimary"
46-
app:fastScrollPopupTextSize="24sp"
47-
app:fastScrollThumbColor="?colorAccent">
48-
49-
</com.simplecityapps.recyclerview_fastscroll.views.FastScrollRecyclerView>
50+
app:layout_behavior="@string/appbar_scrolling_view_behavior">
5051

51-
</LinearLayout>
52+
</android.support.v4.view.ViewPager>
53+
</android.support.design.widget.CoordinatorLayout>

0 commit comments

Comments
 (0)