Skip to content

Commit b51b450

Browse files
committed
Improve theme preview
1 parent e543565 commit b51b450

29 files changed

+130
-48
lines changed

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
import com.duy.common.purchase.InAppPurchaseHelper;
4343
import com.duy.common.purchase.Premium;
4444
import com.duy.ide.editor.SimpleEditorActivity;
45-
import com.duy.ide.editor.theme.EditorThemeActivity;
4645
import com.duy.ide.filemanager.SaveListener;
4746
import com.jecelyin.common.utils.UIUtils;
4847
import com.jecelyin.editor.v2.editor.Document;
@@ -70,7 +69,6 @@ public class CodeEditorActivity extends SimpleEditorActivity {
7069
@Override
7170
protected void onCreate(Bundle savedInstanceState) {
7271
super.onCreate(savedInstanceState);
73-
startActivity(new Intent(this, EditorThemeActivity.class));
7472
mPremiumHelper = new InAppPurchaseHelper(this);
7573

7674
final View toggleView = findViewById(R.id.btn_toggle_panel);

editor-view/src/main/java/android/core/widget/BaseEditorView.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6771,6 +6771,10 @@ public CharSequence getIterableTextForAccessibility() {
67716771
}
67726772

67736773
private void init() {
6774+
if(isInEditMode()){
6775+
return;
6776+
}
6777+
67746778
setImeOptions(EditorInfo.IME_ACTION_DONE | EditorInfo.IME_FLAG_NO_EXTRACT_UI);
67756779

67766780
preferences = layoutContext.preferences = Preferences.getInstance(getContext());

editor-view/src/main/java/com/duy/ide/editor/theme/EditorThemeActivity.java

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
import android.content.SharedPreferences;
44
import android.os.Bundle;
55
import android.support.annotation.Nullable;
6+
import android.support.v7.widget.DividerItemDecoration;
67
import android.support.v7.widget.LinearLayoutManager;
78
import android.support.v7.widget.RecyclerView;
8-
import android.support.v7.widget.SwitchCompat;
99
import android.support.v7.widget.Toolbar;
10-
import android.widget.CompoundButton;
10+
import android.view.View;
11+
import android.widget.AdapterView;
12+
import android.widget.Spinner;
1113

1214
import com.duy.ide.editor.editor.R;
1315
import com.jecelyin.editor.v2.Preferences;
@@ -17,19 +19,13 @@ public class EditorThemeActivity extends ThemeSupportActivity {
1719
private static final String EXTRA_USE_LIGHT_THEME = "EXTRA_USE_LIGHT_THEME";
1820
private RecyclerView mRecyclerView;
1921
private ThemeAdapter mThemeAdapter;
20-
private SwitchCompat mSwitchCompat;
22+
private Spinner mSpinner;
23+
private Preferences mPreferences;
2124

2225
@Override
2326
protected void onCreate(@Nullable Bundle savedInstanceState) {
2427
super.onCreate(savedInstanceState);
2528

26-
boolean useLightTheme = getIntent().getBooleanExtra(EXTRA_USE_LIGHT_THEME, true);
27-
if (useLightTheme){
28-
setTheme(R.style.LightTheme);
29-
}else {
30-
setTheme(R.style.DarkTheme);
31-
}
32-
3329
setContentView(R.layout.activity_editor_theme);
3430
setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
3531
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
@@ -39,23 +35,34 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
3935

4036
mRecyclerView = findViewById(R.id.recyclerView);
4137
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
38+
mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
4239
mThemeAdapter = new ThemeAdapter(this);
40+
4341
mRecyclerView.setAdapter(mThemeAdapter);
44-
mSwitchCompat = findViewById(R.id.switch_theme);
45-
mSwitchCompat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
42+
mPreferences = Preferences.getInstance(this);
43+
44+
boolean useLightTheme = mPreferences.isUseLightTheme();
45+
46+
mSpinner = findViewById(R.id.spinner_themes);
47+
mSpinner.setSelection(useLightTheme ? 0 : 1);
48+
mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
4649
@Override
47-
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
48-
useLightTheme(isChecked);
50+
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
51+
useLightTheme(position == 0);
52+
}
53+
54+
@Override
55+
public void onNothingSelected(AdapterView<?> parent) {
56+
4957
}
5058
});
5159

52-
Preferences.getInstance(this).registerOnSharedPreferenceChangeListener(this);
60+
mPreferences.registerOnSharedPreferenceChangeListener(this);
5361
}
5462

5563
private void useLightTheme(boolean useLightTheme) {
56-
boolean current = getIntent().getBooleanExtra(EXTRA_USE_LIGHT_THEME, true);
57-
if (current != useLightTheme) {
58-
getIntent().putExtra(EXTRA_USE_LIGHT_THEME, useLightTheme);
64+
if (mPreferences.isUseLightTheme() != useLightTheme) {
65+
mPreferences.setTheme(useLightTheme ? 0 : 1);
5966
recreate();
6067
}
6168
}

editor-view/src/main/java/com/duy/ide/editor/theme/ThemeAdapter.java

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import com.duy.ide.editor.theme.model.EditorTheme;
1616
import com.jecelyin.editor.v2.editor.Highlighter;
1717
import com.jecelyin.editor.v2.highlight.Buffer;
18+
import com.simplecityapps.recyclerview_fastscroll.views.FastScrollRecyclerView;
1819

1920
import org.gjt.sp.jedit.Catalog;
2021

@@ -23,7 +24,7 @@
2324
import java.util.Comparator;
2425
import java.util.HashMap;
2526

26-
public class ThemeAdapter extends RecyclerView.Adapter<ThemeAdapter.ViewHolder> {
27+
public class ThemeAdapter extends RecyclerView.Adapter<ThemeAdapter.ViewHolder> implements FastScrollRecyclerView.SectionedAdapter {
2728
private final ArrayList<EditorTheme> mEditorThemes;
2829
private Context mContext;
2930

@@ -51,7 +52,8 @@ public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
5152
public void onBindViewHolder(@NonNull final ViewHolder holder, int position) {
5253
final EditorTheme editorTheme = mEditorThemes.get(position);
5354

54-
holder.mTxtName.setText(editorTheme.getName());
55+
String title = makeTitle(position, editorTheme);
56+
holder.mTxtName.setText(title);
5557
EditAreaView editorView = holder.mEditorView;
5658

5759
Buffer buffer = new Buffer();
@@ -72,6 +74,29 @@ public void onBindViewHolder(@NonNull final ViewHolder holder, int position) {
7274
highlighter.highlight(buffer, editorTheme, colorsMap, editorView.getText(), 0, lineCount - 1);
7375
}
7476

77+
private String makeTitle(int position, EditorTheme editorTheme) {
78+
StringBuilder builder = new StringBuilder(String.valueOf(position + 1));
79+
builder.append(". ");
80+
builder.append(editorTheme.getName());
81+
for (int i = 0; i < builder.length(); i++) {
82+
if (builder.charAt(i) == ' ' && i + 1 < builder.length()) {
83+
builder.setCharAt(i + 1, Character.toUpperCase(builder.charAt(i + 1)));
84+
}
85+
}
86+
return builder.toString();
87+
}
88+
89+
private String refine(String name) {
90+
StringBuilder builder = new StringBuilder(name);
91+
builder.insert(9, " ");
92+
for (int i = 0; i < builder.length(); i++) {
93+
if (builder.charAt(i) == ' ' && i + 1 < builder.length()) {
94+
builder.setCharAt(i + 1, Character.toUpperCase(builder.charAt(i + 1)));
95+
}
96+
}
97+
return builder.toString();
98+
}
99+
75100

76101
@Override
77102
public int getItemCount() {
@@ -113,6 +138,12 @@ private CharSequence getSampleData() {
113138
"}";
114139
}
115140

141+
@NonNull
142+
@Override
143+
public String getSectionName(int position) {
144+
return mEditorThemes.get(position).getName();
145+
}
146+
116147
static class ViewHolder extends RecyclerView.ViewHolder {
117148
EditAreaView mEditorView;
118149
TextView mTxtName;

editor-view/src/main/java/com/jecelyin/editor/v2/Preferences.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,6 @@ public int getTheme() {
216216

217217
/**
218218
* theme index of {@link #THEMES}
219-
*
220219
*/
221220
public void setTheme(int theme) {
222221
preferences.edit().putInt(context.getString(R.string.pref_current_theme), theme).apply();
@@ -382,6 +381,10 @@ public void setLastTab(int index) {
382381
map.put(KEY_LAST_TAB, index);
383382
}
384383

384+
public boolean isUseLightTheme() {
385+
return getTheme() == 0;
386+
}
387+
385388
@IntDef({SCREEN_ORIENTATION_AUTO, SCREEN_ORIENTATION_LANDSCAPE, SCREEN_ORIENTATION_PORTRAIT})
386389
public @interface ScreenOrientation {
387390
}

editor-view/src/main/java/com/jecelyin/editor/v2/ThemeSupportActivity.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import android.support.annotation.CallSuper;
2323
import android.support.annotation.Nullable;
2424
import android.support.v7.app.AppCompatActivity;
25+
import android.support.v7.widget.Toolbar;
2526
import android.view.MenuItem;
2627
import android.view.View;
2728
import android.view.ViewGroup;
@@ -80,6 +81,11 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
8081
setFullScreenMode(isFullScreenMode());
8182
}
8283

84+
protected void setupToolbar(Toolbar toolbar) {
85+
setSupportActionBar(toolbar);
86+
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
87+
}
88+
8389
protected boolean isFullScreenMode() {
8490
return Preferences.getInstance(this).isFullScreenMode();
8591
}
Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
34
android:layout_width="match_parent"
45
android:layout_height="match_parent"
56
android:orientation="vertical">
@@ -17,24 +18,34 @@
1718
app:popupTheme="?attr/actionBarPopupTheme"
1819
app:theme="?attr/actionBarTheme">
1920

20-
<android.support.v7.widget.SwitchCompat
21-
android:id="@+id/switch_theme"
21+
<FrameLayout
2222
android:layout_width="match_parent"
23-
android:layout_height="match_parent"
24-
android:paddingLeft="16dp"
25-
android:paddingRight="16dp"
26-
android:text="@string/light_theme"
27-
android:textAppearance="@style/TextAppearance.AppCompat.Title"
28-
android:textOff="@string/dark_theme"
29-
android:textOn="@string/light_theme" />
23+
android:layout_height="match_parent">
24+
25+
<android.support.v7.widget.AppCompatSpinner
26+
android:id="@+id/spinner_themes"
27+
android:layout_width="wrap_content"
28+
android:layout_height="match_parent"
29+
android:layout_gravity="end"
30+
android:entries="@array/theme_titles"
31+
android:paddingLeft="16dp"
32+
android:paddingRight="16dp"
33+
android:textAppearance="@style/TextAppearance.AppCompat.Title" />
34+
35+
</FrameLayout>
3036
</android.support.v7.widget.Toolbar>
3137

3238
</android.support.design.widget.AppBarLayout>
3339

34-
<android.support.v7.widget.RecyclerView
40+
<com.simplecityapps.recyclerview_fastscroll.views.FastScrollRecyclerView
3541
android:id="@+id/recyclerView"
3642
android:layout_width="match_parent"
37-
android:layout_height="match_parent">
43+
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>
3850

39-
</android.support.v7.widget.RecyclerView>
4051
</LinearLayout>
Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,40 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
3-
xmlns:app="http://schemas.android.com/apk/res-auto"
2+
3+
4+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
45
android:layout_width="match_parent"
56
android:layout_height="wrap_content"
6-
app:cardUseCompatPadding="true">
7+
android:layout_marginTop="4dp"
8+
android:orientation="vertical">
79

8-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
10+
<LinearLayout
911
android:layout_width="match_parent"
1012
android:layout_height="wrap_content"
11-
android:layout_marginTop="4dp"
12-
android:orientation="vertical">
13+
android:paddingLeft="16dp"
14+
android:paddingRight="16dp">
1315

1416
<android.support.v7.widget.AppCompatTextView
1517
android:id="@+id/txt_name"
16-
android:layout_width="match_parent"
18+
android:layout_width="0dp"
1719
android:layout_height="wrap_content"
20+
android:layout_weight="1"
21+
android:padding="4dp"
1822
android:textAppearance="@style/TextAppearance.AppCompat.Title" />
1923

20-
<android.core.widget.EditAreaView
21-
android:id="@+id/editor_view"
22-
android:layout_width="match_parent"
23-
android:layout_height="wrap_content">
24+
<android.support.v7.widget.AppCompatButton
25+
android:id="@+id/btn_select"
26+
android:layout_width="wrap_content"
27+
android:layout_height="wrap_content"
28+
android:src="@drawable/baseline_check_24"
29+
android:text="Select" />
2430

25-
</android.core.widget.EditAreaView>
2631
</LinearLayout>
27-
</android.support.v7.widget.CardView>
32+
33+
<android.core.widget.EditAreaView
34+
android:id="@+id/editor_view"
35+
android:layout_width="match_parent"
36+
android:layout_height="wrap_content">
37+
38+
</android.core.widget.EditAreaView>
39+
</LinearLayout>
2840

176 Bytes
Loading
194 Bytes
Loading

0 commit comments

Comments
 (0)