Skip to content

Commit 485ec18

Browse files
committed
Clean preference
1 parent 9faf13b commit 485ec18

File tree

5 files changed

+48
-57
lines changed

5 files changed

+48
-57
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
4646
mRecyclerView.scrollToPosition(findThemeIndex(mPreferences.getEditorTheme()));
4747

4848
boolean useLightTheme = mPreferences.isUseLightTheme();
49-
5049
mSpinner = findViewById(R.id.spinner_themes);
5150
mSpinner.setSelection(useLightTheme ? 0 : 1);
5251
mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@@ -76,7 +75,7 @@ private int findThemeIndex(EditorTheme editorTheme) {
7675

7776
private void useLightTheme(boolean useLightTheme) {
7877
if (mPreferences.isUseLightTheme() != useLightTheme) {
79-
mPreferences.setTheme(useLightTheme ? 0 : 1);
78+
mPreferences.setAppTheme(useLightTheme ? 0 : 1);
8079
recreate();
8180
}
8281
}

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

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import android.support.annotation.IntDef;
2424
import android.support.annotation.IntRange;
2525
import android.support.annotation.NonNull;
26-
import android.support.annotation.StyleableRes;
26+
import android.support.annotation.StyleRes;
2727
import android.text.TextUtils;
2828

2929
import com.duy.ide.editor.editor.R;
@@ -45,40 +45,37 @@
4545
public class Preferences implements SharedPreferences.OnSharedPreferenceChangeListener {
4646
public static final String KEY_FONT_SIZE = "pref_font_size";
4747
public static final String KEY_CURSOR_WIDTH = "pref_cursor_width";
48-
public static final String KEY_TOUCH_TO_ADJUST_TEXT_SIZE = "pref_touch_to_adjust_text_size";
4948
public static final String KEY_WORD_WRAP = "pref_word_wrap";
5049
public static final String KEY_SHOW_LINE_NUMBER = "pref_show_linenumber";
5150
public static final String KEY_SHOW_WHITESPACE = "pref_show_whitespace";
5251
public static final String KEY_AUTO_INDENT = "pref_auto_indent";
53-
public static final String KEY_INSERT_SPACE_FOR_TAB = "pref_insert_space_for_tab";
5452
public static final String KEY_TAB_SIZE = "pref_tab_size";
5553
public static final String KEY_SYMBOL = "pref_symbol";
5654
public static final String KEY_AUTO_CAPITALIZE = "pref_auto_capitalize";
57-
public static final String KEY_HIGHLIGHT_FILE_SIZE_LIMIT = "pref_highlight_file_size_limit";
58-
public static final String KEY_REMEMBER_LAST_OPENED_FILES = "pref_remember_last_opened_files";
5955
public static final String KEY_SCREEN_ORIENTATION = "pref_screen_orientation";
6056
public static final String KEY_KEEP_SCREEN_ON = "pref_keep_screen_on";
61-
public static final String KEY_TOOLBAR_ICONS = "pref_toolbar_icons";
62-
public static final String KEY_PREF_AUTO_CHECK_UPDATES = "pref_auto_check_updates";
63-
public static final String KEY_LAST_OPEN_PATH = "last_open_path";
6457
public static final String KEY_READ_ONLY = "readonly_mode";
65-
public static final String KEY_SHOW_HIDDEN_FILES = "show_hidden_files";
66-
public static final String KEY_FILE_SORT_TYPE = "show_file_sort";
67-
public static final String KEY_FULL_SCREEN = "fullscreen_mode";
68-
public static final String KEY_LAST_TAB = "last_tab";
69-
7058
public static final int DEF_MIN_FONT_SIZE = 9;
7159
public static final int DEF_MAX_FONT_SIZE = 32;
72-
7360
public static final int SCREEN_ORIENTATION_AUTO = 0;
7461
public static final int SCREEN_ORIENTATION_LANDSCAPE = 1;
7562
public static final int SCREEN_ORIENTATION_PORTRAIT = 2;
7663
public static final String VALUE_SYMBOL = TextUtils.join("\n", new String[]{"{", "}", "<", ">"
7764
, ",", ";", "'", "\"", "(", ")", "/", "\\", "%", "[", "]", "|", "#", "=", "$", ":"
7865
, "&", "?", "!", "@", "^", "+", "*", "-", "_", "`", "\\t", "\\n"});
7966

80-
public static final int[] THEMES = new int[]{R.style.LightTheme, R.style.DarkTheme};
81-
67+
private static final int[] THEMES = new int[]{R.style.LightTheme, R.style.DarkTheme};
68+
private static final String KEY_TOUCH_TO_ADJUST_TEXT_SIZE = "pref_touch_to_adjust_text_size";
69+
private static final String KEY_INSERT_SPACE_FOR_TAB = "pref_insert_space_for_tab";
70+
private static final String KEY_HIGHLIGHT_FILE_SIZE_LIMIT = "pref_highlight_file_size_limit";
71+
private static final String KEY_REMEMBER_LAST_OPENED_FILES = "pref_remember_last_opened_files";
72+
private static final String KEY_TOOLBAR_ICONS = "pref_toolbar_icons";
73+
private static final String KEY_PREF_AUTO_CHECK_UPDATES = "pref_auto_check_updates";
74+
private static final String KEY_LAST_OPEN_PATH = "last_open_path";
75+
private static final String KEY_SHOW_HIDDEN_FILES = "show_hidden_files";
76+
private static final String KEY_FILE_SORT_TYPE = "show_file_sort";
77+
private static final String KEY_FULL_SCREEN = "fullscreen_mode";
78+
private static final String KEY_LAST_TAB = "last_tab";
8279
private static final Object mContent = new Object();
8380
private static Preferences instance;
8481

@@ -212,16 +209,6 @@ public boolean isShowWhiteSpace() {
212209
return (boolean) map.get(KEY_SHOW_WHITESPACE);
213210
}
214211

215-
public int getTheme() {
216-
return getInt(context.getString(R.string.pref_app_theme), 0);
217-
}
218-
219-
/**
220-
* theme index of {@link #THEMES}
221-
*/
222-
public void setTheme(int theme) {
223-
preferences.edit().putInt(context.getString(R.string.pref_app_theme), theme).apply();
224-
}
225212

226213
public int getHighlightSizeLimit() {
227214
return 1024 * (int) map.get(KEY_HIGHLIGHT_FILE_SIZE_LIMIT);
@@ -332,7 +319,7 @@ public boolean isAutoCapitalize() {
332319

333320

334321
public boolean isOpenLastFiles() {
335-
return (boolean) map.get(KEY_REMEMBER_LAST_OPENED_FILES);
322+
return getBoolean(context.getString(R.string.pref_remember_last_opened_files), true);
336323
}
337324

338325
public int getTabSize() {
@@ -408,7 +395,7 @@ public void setEditorTheme(String fileName) {
408395
preferences.edit().putString(context.getString(R.string.pref_theme_editor_theme), fileName).apply();
409396
}
410397

411-
@StyleableRes
398+
@StyleRes
412399
public int getAppTheme() {
413400
int index = getInt(context.getString(R.string.pref_app_theme), 0);
414401
return THEMES[index];
@@ -423,7 +410,7 @@ public void setTerminalTheme(int index) {
423410
}
424411

425412
public boolean isUseLightTheme() {
426-
return getTheme() == 0;
413+
return getAppTheme() == THEMES[0];
427414
}
428415

429416
@IntDef({SCREEN_ORIENTATION_AUTO, SCREEN_ORIENTATION_LANDSCAPE, SCREEN_ORIENTATION_PORTRAIT})

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,7 @@ protected void attachKeyboardListeners() {
7474
@Override
7575
protected void onCreate(@Nullable Bundle savedInstanceState) {
7676
super.onCreate(savedInstanceState);
77-
int theme = Preferences.getInstance(this).getTheme();
78-
if (theme != 0) {
79-
setTheme(Preferences.THEMES[theme]);
80-
}
77+
setTheme(Preferences.getInstance(this).getAppTheme());
8178
setFullScreenMode(isFullScreenMode());
8279
}
8380

editor-view/src/main/res/values/do_not_translate.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,19 @@
55
<string name="pref_app_theme" translatable="false">pref_current_theme</string>
66
<string name="pref_terminal_theme" translatable="false">pref_terminal_theme</string>
77
<string name="pref_theme_editor_theme" translatable="false">pref_key_editor_theme</string>
8+
<string name="pref_tab_size" translatable="false">pref_tab_size</string>
9+
<string name="pref_insert_space_for_tab" translatable="false">pref_insert_space_for_tab</string>
10+
<string name="pref_auto_indent" translatable="false">pref_auto_indent</string>
11+
<string name="pref_show_whitespace" translatable="false">pref_show_whitespace</string>
12+
<string name="pref_show_linenumber" translatable="false">pref_show_linenumber</string>
13+
<string name="pref_word_wrap" translatable="false">pref_word_wrap</string>
14+
<string name="pref_touch_to_adjust_text_size" translatable="false">pref_touch_to_adjust_text_size</string>
15+
<string name="pref_cursor_width" translatable="false">pref_cursor_width</string>
16+
<string name="pref_font_size" translatable="false">pref_font_size</string>
17+
<string name="pref_auto_capitalize" translatable="false">pref_auto_capitalize</string>
18+
<string name="pref_symbol" translatable="false">pref_symbol</string>
19+
<string name="pref_highlight_file_size_limit" translatable="false">pref_highlight_file_size_limit</string>
20+
<string name="pref_remember_last_opened_files" translatable="false">pref_remember_last_opened_files</string>
21+
<string name="pref_keep_screen_on" translatable="false">pref_keep_screen_on</string>
22+
<string name="pref_screen_orientation" translatable="false">pref_screen_orientation</string>
823
</resources>

editor-view/src/main/res/xml/preference_editor.xml

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,54 +2,54 @@
22
<PreferenceCategory android:title="@string/editor">
33

44
<com.jecelyin.editor.v2.preference.FontSizePreference
5-
android:key="pref_font_size"
5+
android:key="@string/pref_font_size"
66
android:summary="@string/default_value"
77
android:title="@string/font_size" />
88

99
<com.jecelyin.editor.v2.preference.CursorWidthPreference
10-
android:key="pref_cursor_width"
10+
android:key="@string/pref_cursor_width"
1111
android:summary="@string/default_value"
1212
android:title="@string/cursor_width" />
1313

1414
<CheckBoxPreference
15-
android:key="pref_touch_to_adjust_text_size"
15+
android:key="@string/pref_touch_to_adjust_text_size"
1616
android:title="@string/touch_to_adjust_text_size" />
1717

1818
<CheckBoxPreference
19-
android:key="pref_word_wrap"
19+
android:key="@string/pref_word_wrap"
2020
android:title="@string/word_wrap_in_editor" />
2121

2222
<CheckBoxPreference
23-
android:key="pref_show_linenumber"
23+
android:key="@string/pref_show_linenumber"
2424
android:title="@string/show_linenumber" />
2525

2626
<CheckBoxPreference
27-
android:key="pref_show_whitespace"
27+
android:key="@string/pref_show_whitespace"
2828
android:title="@string/show_whitespace" />
2929

3030
<CheckBoxPreference
31-
android:key="pref_auto_indent"
31+
android:key="@string/pref_auto_indent"
3232
android:title="@string/auto_indent" />
3333

3434
<CheckBoxPreference
35-
android:key="pref_insert_space_for_tab"
35+
android:key="@string/pref_insert_space_for_tab"
3636
android:title="@string/insert_space_for_tab" />
3737

3838
<EditTextPreference
3939
android:inputType="number"
40-
android:key="pref_tab_size"
40+
android:key="@string/pref_tab_size"
4141
android:maxLength="1"
4242
android:singleLine="true"
4343
android:summary="4"
4444
android:title="@string/tab_size" />
4545

4646
<CheckBoxPreference
47-
android:key="pref_auto_capitalize"
47+
android:key="@string/pref_auto_capitalize"
4848
android:title="@string/auto_capitalize" />
4949

5050

5151
<EditTextPreference
52-
android:key="pref_symbol"
52+
android:key="@string/pref_symbol"
5353
android:maxLines="7"
5454
android:negativeButtonText="@string/reset"
5555
android:scrollbars="vertical"
@@ -64,16 +64,9 @@
6464
android:key="@string/pref_key_fullscreen"
6565
android:title="@string/fullscreen_mode" />
6666

67-
<ListPreference
68-
android:defaultValue="0"
69-
android:entries="@array/theme_titles"
70-
android:entryValues="@array/theme_values"
71-
android:key="pref_current_theme"
72-
android:title="@string/change_theme" />
73-
7467
<EditTextPreference
7568
android:inputType="number"
76-
android:key="pref_highlight_file_size_limit"
69+
android:key="@string/pref_highlight_file_size_limit"
7770
android:singleLine="true"
7871
android:summary="500 KB"
7972
android:title="@string/highlight_the_file_size_limit" />
@@ -87,18 +80,18 @@
8780
android:title="@string/auto_save" />
8881

8982
<CheckBoxPreference
90-
android:key="pref_remember_last_opened_files"
83+
android:key="@string/pref_remember_last_opened_files"
9184
android:summary="@string/remember_last_opened_files_summary"
9285
android:title="@string/remember_last_opened_files" />
9386

9487
<com.jecelyin.editor.v2.preference.JecListPreference
9588
android:entries="@array/screen_orientation"
9689
android:entryValues="@array/screen_orientation_values"
97-
android:key="pref_screen_orientation"
90+
android:key="@string/pref_screen_orientation"
9891
android:title="@string/screen_orientation" />
9992

10093
<CheckBoxPreference
101-
android:key="pref_keep_screen_on"
94+
android:key="@string/pref_keep_screen_on"
10295
android:title="@string/keep_screen_on" />
10396

10497

0 commit comments

Comments
 (0)