Skip to content

Commit baa7210

Browse files
committed
Support change editor theme by click button
1 parent 1de26e8 commit baa7210

File tree

10 files changed

+97
-61
lines changed

10 files changed

+97
-61
lines changed

app/src/main/java/com/duy/common/purchase/Premium.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public class Premium {
4747
* @param context - Android context
4848
*/
4949
public static boolean isPremiumUser(Context context) {
50-
return IS_PREMIUM || PremiumFileUtil.licenseCached(context);
50+
return IS_PREMIUM || PremiumFileUtil.licenseCached(context) || BuildConfig.DEBUG;
5151
}
5252

5353
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ protected void onDestroy() {
8989
@Override
9090
public void onThemeSelected(EditorTheme theme) {
9191
if (Premium.isPremiumUser(this)) {
92-
mPreferences.setEditorTheme(theme.getName());
92+
mPreferences.setEditorTheme(theme.getFileName());
9393
String text = getString(R.string.selected_theme, theme.getName());
9494
Snackbar.make(mRecyclerView, text, Snackbar.LENGTH_SHORT).show();
9595
} else {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,11 @@
2727
<string name="title_premium_version">Premium version</string>
2828
<string name="button_purchase">Purchase 1$</string>
2929
<string name="message_premium">This app is free and no ads, it open source so you can build easily.
30-
To support developer, you can buy premium version with 1$. With premium version, you can access some locked features: \n
30+
To support developer, you can buy premium version with 1$. Some locked features: \n
3131
1. Dark theme\n
3232
2. Editor color schemes\n
3333
3. Change editor fonts
34+
4. Terminal theme
3435
</string>
3536

3637
<string name="button_purchase_restore">Restore</string>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6788,7 +6788,7 @@ private void init() {
67886788

67896789
setLineNumber(1);
67906790
onTextSizeChanged();
6791-
setTheme(ThemeLoader.loadDefault(getContext()));
6791+
setTheme(preferences.getEditorTheme());
67926792

67936793
onSharedPreferenceChanged(null, Preferences.KEY_FONT_SIZE);
67946794
onSharedPreferenceChanged(null, Preferences.KEY_CURSOR_WIDTH);

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

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class ThemeAdapter extends RecyclerView.Adapter<ThemeAdapter.ViewHolder>
3333
public ThemeAdapter(Context context) {
3434
mContext = context;
3535
ThemeLoader.init(context);
36-
mEditorThemes = ThemeLoader.getAll();
36+
mEditorThemes = ThemeLoader.getAll(context);
3737
Collections.sort(mEditorThemes, new Comparator<EditorTheme>() {
3838
@Override
3939
public int compare(EditorTheme o1, EditorTheme o2) {
@@ -96,17 +96,6 @@ private String makeTitle(int position, EditorTheme editorTheme) {
9696
return builder.toString();
9797
}
9898

99-
private String refine(String name) {
100-
StringBuilder builder = new StringBuilder(name);
101-
builder.insert(9, " ");
102-
for (int i = 0; i < builder.length(); i++) {
103-
if (builder.charAt(i) == ' ' && i + 1 < builder.length()) {
104-
builder.setCharAt(i + 1, Character.toUpperCase(builder.charAt(i + 1)));
105-
}
106-
}
107-
return builder.toString();
108-
}
109-
11099
@Override
111100
public int getItemCount() {
112101
return mEditorThemes.size();

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

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22

33
import android.content.Context;
44
import android.content.res.AssetManager;
5-
import android.graphics.Color;
65

76
import com.duy.ide.editor.theme.model.EditorTheme;
8-
import com.jecelyin.common.utils.DLog;
97

108
import org.apache.commons.io.IOUtils;
119

@@ -14,13 +12,12 @@
1412
import java.io.StringReader;
1513
import java.util.ArrayList;
1614
import java.util.HashMap;
17-
import java.util.Map;
1815
import java.util.Properties;
1916

2017
public class ThemeLoader {
21-
private static final String TAG = "ThemeLoader";
22-
private static final HashMap<String, EditorTheme> CACHED = new HashMap<>();
2318
private static final String ASSET_PATH = "themes/vscode";
19+
private static final String DEFAULT_EDITOR_THEME_LIGHT = "absent-light.json.properties";
20+
private static final HashMap<String, EditorTheme> CACHED = new HashMap<>();
2421

2522
public static void init(Context context) {
2623
try {
@@ -33,64 +30,62 @@ public static void init(Context context) {
3330
}
3431
}
3532

36-
public static EditorTheme getTheme(Context context, String name) {
37-
loadTheme(context, name);
38-
return CACHED.get(name);
33+
public static EditorTheme getTheme(Context context, String fileName) {
34+
return loadTheme(context, fileName);
3935
}
4036

41-
public static ArrayList<EditorTheme> getAll() {
37+
public static ArrayList<EditorTheme> getAll(Context context) {
4238
ArrayList<EditorTheme> themes = new ArrayList<>();
43-
for (Map.Entry<String, EditorTheme> entry : CACHED.entrySet()) {
44-
themes.add(entry.getValue());
39+
try {
40+
String[] names = context.getAssets().list(ASSET_PATH);
41+
for (String name : names) {
42+
EditorTheme theme = loadTheme(context, name);
43+
themes.add(theme);
44+
}
45+
} catch (IOException e) {
46+
e.printStackTrace();
4547
}
4648
return themes;
4749
}
4850

4951
public static EditorTheme loadDefault(Context context) {
50-
AssetManager assets = context.getAssets();
51-
return loadFromAsset(assets, "themes/vscode/absent.json.properties");
52+
return loadTheme(context, DEFAULT_EDITOR_THEME_LIGHT);
5253
}
5354

54-
private static void loadTheme(Context context, String theme) {
55-
if (CACHED.get(theme) != null) {
56-
return;
55+
private static EditorTheme loadTheme(Context context, String fileName) {
56+
if (CACHED.get(fileName) != null) {
57+
return CACHED.get(fileName);
5758
}
58-
EditorTheme editorTheme = loadFromAsset(context.getAssets(), ASSET_PATH + "/" + theme);
59-
CACHED.put(theme, editorTheme);
59+
EditorTheme editorTheme = loadFromAsset(context.getAssets(), fileName);
60+
CACHED.put(fileName, editorTheme);
61+
return editorTheme;
6062
}
6163

62-
private static EditorTheme loadFromAsset(AssetManager assets, String propFile) {
64+
private static EditorTheme loadFromAsset(AssetManager assets, String fileName) {
6365
try {
64-
InputStream input = assets.open(propFile);
66+
InputStream input = assets.open(ASSET_PATH + "/" + fileName);
6567
String content = IOUtils.toString(input);
6668
input.close();
6769

6870
Properties properties = new Properties();
6971
properties.load(new StringReader(content));
7072

71-
return loadTheme(properties);
73+
EditorTheme editorTheme = loadTheme(properties);
74+
editorTheme.setFileName(fileName);
75+
return editorTheme;
7276
} catch (IOException e) {
7377
e.printStackTrace();
7478
}
7579

7680
return null;
7781
}
7882

83+
7984
private static EditorTheme loadTheme(Properties properties) {
8085
EditorTheme editorTheme = new EditorTheme();
8186
editorTheme.load(properties);
8287
return editorTheme;
8388
}
8489

85-
private static int getColor(Properties properties, ThemeAttr attr, int defaultValue) {
86-
try {
87-
String str = properties.getProperty(attr.getKey());
88-
return Color.parseColor(str);
89-
} catch (Exception e) {
90-
if (DLog.DEBUG) DLog.e(TAG, "getColor: ", e);
91-
return defaultValue;
92-
}
93-
}
94-
9590
}
9691

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ public class EditorTheme extends ColorScheme {
1515
private WhiteSpaceStyle whiteSpaceStyle = new WhiteSpaceStyle();
1616
private SyntaxStyle[] syntaxStyles;
1717

18+
/**
19+
* File name in assets
20+
*/
21+
private String fileName;
22+
1823
private String name;
1924

2025
public SyntaxStyle[] getSyntaxStyles() {
@@ -91,6 +96,14 @@ public void setName(String name) {
9196
this.name = name;
9297
}
9398

99+
public String getFileName() {
100+
return fileName;
101+
}
102+
103+
public void setFileName(String fileName) {
104+
this.fileName = fileName;
105+
}
106+
94107
public enum ThemeAttr {
95108
SCHEME_NAME("theme.name"),
96109
TYPE("theme.type"),

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

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,14 @@
2121
import android.os.Environment;
2222
import android.preference.PreferenceManager;
2323
import android.support.annotation.IntDef;
24+
import android.support.annotation.IntRange;
25+
import android.support.annotation.NonNull;
26+
import android.support.annotation.StyleableRes;
2427
import android.text.TextUtils;
2528

2629
import com.duy.ide.editor.editor.R;
30+
import com.duy.ide.editor.theme.ThemeLoader;
31+
import com.duy.ide.editor.theme.model.EditorTheme;
2732
import com.jecelyin.common.utils.DLog;
2833
import com.jecelyin.common.utils.StringUtils;
2934
import com.jecelyin.common.utils.SysUtils;
@@ -72,10 +77,7 @@ public class Preferences implements SharedPreferences.OnSharedPreferenceChangeLi
7277
, ",", ";", "'", "\"", "(", ")", "/", "\\", "%", "[", "]", "|", "#", "=", "$", ":"
7378
, "&", "?", "!", "@", "^", "+", "*", "-", "_", "`", "\\t", "\\n"});
7479

75-
public static final int[] THEMES = new int[]{
76-
R.style.LightTheme,
77-
R.style.DarkTheme
78-
};
80+
public static final int[] THEMES = new int[]{R.style.LightTheme, R.style.DarkTheme};
7981

8082
private static final Object mContent = new Object();
8183
private static Preferences instance;
@@ -211,14 +213,14 @@ public boolean isShowWhiteSpace() {
211213
}
212214

213215
public int getTheme() {
214-
return getInt(context.getString(R.string.pref_current_theme), 0);
216+
return getInt(context.getString(R.string.pref_app_theme), 0);
215217
}
216218

217219
/**
218220
* theme index of {@link #THEMES}
219221
*/
220222
public void setTheme(int theme) {
221-
preferences.edit().putInt(context.getString(R.string.pref_current_theme), theme).apply();
223+
preferences.edit().putInt(context.getString(R.string.pref_app_theme), theme).apply();
222224
}
223225

224226
public int getHighlightSizeLimit() {
@@ -246,6 +248,14 @@ private int getInt(String key, int def) {
246248
}
247249
}
248250

251+
private String getString(String key, String def) {
252+
try {
253+
return preferences.getString(key, def);
254+
} catch (ClassCastException e) {
255+
return def;
256+
}
257+
}
258+
249259
public boolean isKeepScreenOn() {
250260
return (boolean) map.get(KEY_KEEP_SCREEN_ON);
251261
}
@@ -381,12 +391,39 @@ public void setLastTab(int index) {
381391
map.put(KEY_LAST_TAB, index);
382392
}
383393

384-
public boolean isUseLightTheme() {
385-
return getTheme() == 0;
394+
/**
395+
* Theme and fonts
396+
*/
397+
@NonNull
398+
public EditorTheme getEditorTheme() {
399+
String fileName = getString(context.getString(R.string.pref_theme_editor_theme), "");
400+
EditorTheme theme = ThemeLoader.getTheme(context, fileName);
401+
if (theme != null) {
402+
return theme;
403+
}
404+
return ThemeLoader.loadDefault(context);
386405
}
387406

388-
public void setEditorTheme(String name) {
389-
preferences.edit().putString(context.getString(R.string.pref_theme_editor_theme), name).apply();
407+
public void setEditorTheme(String fileName) {
408+
preferences.edit().putString(context.getString(R.string.pref_theme_editor_theme), fileName).apply();
409+
}
410+
411+
@StyleableRes
412+
public int getAppTheme() {
413+
int index = getInt(context.getString(R.string.pref_app_theme), 0);
414+
return THEMES[index];
415+
}
416+
417+
public void setAppTheme(@IntRange(from = 0, to = 1) int index) {
418+
preferences.edit().putInt(context.getString(R.string.pref_app_theme), index).apply();
419+
}
420+
421+
public void setTerminalTheme(int index) {
422+
preferences.edit().putInt(context.getString(R.string.pref_terminal_theme), index).apply();
423+
}
424+
425+
public boolean isUseLightTheme() {
426+
return getTheme() == 0;
390427
}
391428

392429
@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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
126126
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
127127
if (key.equals(getString(R.string.pref_key_fullscreen))) {
128128
setFullScreenMode(isFullScreenMode());
129-
} else if (key.equals(getString(R.string.pref_current_theme))) {
129+
} else if (key.equals(getString(R.string.pref_app_theme))) {
130130
if (DLog.DEBUG) DLog.d(TAG, "onSharedPreferenceChanged: change theme");
131131
}
132132
}
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<resources>
3-
<string name="pref_key_fullscreen" translatable="false">fullscreen_mode</string>
3+
<string name="pref_key_fullscreen" translatable="false">pref_key_fullscreen</string>
44
<string name="pref_auto_save" translatable="false">pref_auto_save</string>
5-
<string name="pref_current_theme" translatable="false">pref_current_theme</string>
6-
<string name="pref_theme_editor_theme">pref_key_editor_theme</string>
5+
<string name="pref_app_theme" translatable="false">pref_current_theme</string>
6+
<string name="pref_terminal_theme" translatable="false">pref_terminal_theme</string>
7+
<string name="pref_theme_editor_theme" translatable="false">pref_key_editor_theme</string>
78
</resources>

0 commit comments

Comments
 (0)