Skip to content

Commit

Permalink
Update the example app structure to make it easy to add new language …
Browse files Browse the repository at this point in the history
…or theme
  • Loading branch information
AmrDeveloper committed Feb 6, 2022
1 parent 69494d4 commit 05d75bf
Show file tree
Hide file tree
Showing 8 changed files with 262 additions and 146 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,22 @@

import com.amrdeveloper.codeview.Code;
import com.amrdeveloper.codeview.CodeView;
import com.amrdeveloper.codeview.Keyword;
import com.amrdeveloper.codeviewlibrary.syntax.Theme;
import com.amrdeveloper.codeviewlibrary.syntax.Language;
import com.amrdeveloper.codeviewlibrary.syntax.SyntaxManager;
import com.amrdeveloper.codeviewlibrary.syntax.ThemeName;
import com.amrdeveloper.codeviewlibrary.syntax.LanguageName;
import com.amrdeveloper.codeviewlibrary.syntax.LanguageManager;
import com.google.android.material.bottomsheet.BottomSheetDialog;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class MainActivity extends AppCompatActivity {

private CodeView codeView;
private SyntaxManager syntaxManager;
private LanguageManager languageManager;

private Language currentLanguage = Language.JAVA;
private Theme currentTheme = Theme.MONOKAI;
private LanguageName currentLanguage = LanguageName.JAVA;
private ThemeName currentTheme = ThemeName.MONOKAI;

private final boolean useModernAutoCompleteAdapter = true;

Expand Down Expand Up @@ -64,17 +60,9 @@ private void configCodeView() {
codeView.setTabLength(4);
codeView.setEnableAutoIndentation(true);

Set<Character> indentationStart = new HashSet<>();
indentationStart.add('{');
codeView.setIndentationStarts(indentationStart);

Set<Character> indentationEnds = new HashSet<>();
indentationEnds.add('}');
codeView.setIndentationEnds(indentationEnds);

// Setup the language and theme with SyntaxManager helper class
syntaxManager = new SyntaxManager(this, codeView);
syntaxManager.applyTheme(currentLanguage,currentTheme);
languageManager = new LanguageManager(this, codeView);
languageManager.applyTheme(currentLanguage, currentTheme);

// Setup auto pair complete
final Map<Character, Character> pairCompleteMap = new HashMap<>();
Expand All @@ -87,45 +75,41 @@ private void configCodeView() {
codeView.setPairCompleteMap(pairCompleteMap);
codeView.enablePairComplete(true);

// Setup the auto complete for the current language
// Setup the auto complete and auto indenting for the current language
configLanguageAutoComplete();
configLanguageAutoIndentation();
}

private void configLanguageAutoComplete() {
final String[] languageKeywords;
switch (currentLanguage) {
case JAVA:
languageKeywords = getResources().getStringArray(R.array.java_keywords);
break;
case PYTHON:
languageKeywords = getResources().getStringArray(R.array.python_keywords);
break;
default:
languageKeywords = getResources().getStringArray(R.array.go_keywords);
break;
}

if (useModernAutoCompleteAdapter) {
final List<Code> codeList = new ArrayList<>();
for (String keyword : languageKeywords) {
codeList.add(new Keyword(keyword, keyword));
}
// Here you can add snippets to the codeList
// Load the code list (keywords and snippets) for the current language
List<Code> codeList = languageManager.getLanguageCodeList(currentLanguage);

// Use CodeViewAdapter or custom one
CustomCodeViewAdapter adapter = new CustomCodeViewAdapter(this, codeList);

// Add the odeViewAdapter to the CodeView
codeView.setAdapter(adapter);
} else {
String[] languageKeywords = languageManager.getLanguageKeywords(currentLanguage);

// Custom list item xml layout
final int layoutId = R.layout.list_item_suggestion;

// TextView id to put suggestion on it
final int viewId = R.id.suggestItemTextView;
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, layoutId, viewId, languageKeywords);

// Add Custom Adapter to the CodeView
// Add the ArrayAdapter to the CodeView
codeView.setAdapter(adapter);
}
}

private void configLanguageAutoIndentation() {
codeView.setIndentationStarts(languageManager.getLanguageIndentationStarts(currentLanguage));
codeView.setIndentationEnds(languageManager.getLanguageIndentationEnds(currentLanguage));
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
Expand All @@ -146,26 +130,27 @@ public boolean onOptionsItemSelected(@NonNull MenuItem item) {
}

private void changeTheEditorLanguage(int languageId) {
final Language oldLanguage = currentLanguage;
if (languageId == R.id.language_java) currentLanguage = Language.JAVA;
else if (languageId == R.id.language_python) currentLanguage = Language.PYTHON;
else if(languageId == R.id.language_go) currentLanguage = Language.GO_LANG;
final LanguageName oldLanguage = currentLanguage;
if (languageId == R.id.language_java) currentLanguage = LanguageName.JAVA;
else if (languageId == R.id.language_python) currentLanguage = LanguageName.PYTHON;
else if(languageId == R.id.language_go) currentLanguage = LanguageName.GO_LANG;

if (currentLanguage != oldLanguage) {
syntaxManager.applyTheme(currentLanguage, currentTheme);
languageManager.applyTheme(currentLanguage, currentTheme);
configLanguageAutoComplete();
configLanguageAutoIndentation();
}
}

private void changeTheEditorTheme(int themeId) {
final Theme oldTheme = currentTheme;
if (themeId == R.id.theme_monokia) currentTheme = Theme.MONOKAI;
else if (themeId == R.id.theme_noctics) currentTheme = Theme.NOCTIS_WHITE;
else if(themeId == R.id.theme_five_color) currentTheme = Theme.FIVE_COLOR;
else if(themeId == R.id.theme_orange_box) currentTheme = Theme.ORANGE_BOX;
final ThemeName oldTheme = currentTheme;
if (themeId == R.id.theme_monokia) currentTheme = ThemeName.MONOKAI;
else if (themeId == R.id.theme_noctics) currentTheme = ThemeName.NOCTIS_WHITE;
else if(themeId == R.id.theme_five_color) currentTheme = ThemeName.FIVE_COLOR;
else if(themeId == R.id.theme_orange_box) currentTheme = ThemeName.ORANGE_BOX;

if (currentTheme != oldTheme) {
syntaxManager.applyTheme(currentLanguage, currentTheme);
languageManager.applyTheme(currentLanguage, currentTheme);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@

import android.content.Context;

import com.amrdeveloper.codeview.Code;
import com.amrdeveloper.codeview.CodeView;
import com.amrdeveloper.codeview.Keyword;
import com.amrdeveloper.codeviewlibrary.R;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;

public class GoSyntaxUtils {
public class GoLanguage {

//Language Keywords
private static final Pattern PATTERN_KEYWORDS = Pattern.compile("\\b(break|default|func|interface|case|defer|" +
Expand Down Expand Up @@ -129,4 +135,28 @@ public static void applyOrangeBoxTheme(Context context, CodeView codeView) {
codeView.reHighlightSyntax();
}

public static String[] getKeywords(Context context) {
return context.getResources().getStringArray(R.array.go_keywords);
}

public static List<Code> getCodeList(Context context) {
List<Code> codeList = new ArrayList<>();
String[] keywords = getKeywords(context);
for (String keyword : keywords) {
codeList.add(new Keyword(keyword));
}
return codeList;
}

public static Set<Character> getIndentationStarts() {
Set<Character> characterSet = new HashSet<>();
characterSet.add('{');
return characterSet;
}

public static Set<Character> getIndentationEnds() {
Set<Character> characterSet = new HashSet<>();
characterSet.add('}');
return characterSet;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@

import android.content.Context;

import com.amrdeveloper.codeview.Code;
import com.amrdeveloper.codeview.CodeView;
import com.amrdeveloper.codeview.Keyword;
import com.amrdeveloper.codeviewlibrary.R;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;

public class JavaSyntaxUtils {
public class JavaLanguage {

//Language Keywords
private static final Pattern PATTERN_KEYWORDS = Pattern.compile("\\b(abstract|boolean|break|byte|case|catch" +
Expand Down Expand Up @@ -139,4 +145,28 @@ public static void applyOrangeBoxTheme(Context context, CodeView codeView) {
codeView.reHighlightSyntax();
}

public static String[] getKeywords(Context context) {
return context.getResources().getStringArray(R.array.java_keywords);
}

public static List<Code> getCodeList(Context context) {
List<Code> codeList = new ArrayList<>();
String[] keywords = getKeywords(context);
for (String keyword : keywords) {
codeList.add(new Keyword(keyword));
}
return codeList;
}

public static Set<Character> getIndentationStarts() {
Set<Character> characterSet = new HashSet<>();
characterSet.add('{');
return characterSet;
}

public static Set<Character> getIndentationEnds() {
Set<Character> characterSet = new HashSet<>();
characterSet.add('}');
return characterSet;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package com.amrdeveloper.codeviewlibrary.syntax;

import android.content.Context;

import com.amrdeveloper.codeview.Code;
import com.amrdeveloper.codeview.CodeView;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class LanguageManager {

private final Context context;
private final CodeView codeView;

public LanguageManager(Context context, CodeView codeView) {
this.context = context;
this.codeView = codeView;
}

public void applyTheme(LanguageName language, ThemeName theme) {
switch (theme) {
case MONOKAI:
applyMonokaiTheme(language);
break;
case NOCTIS_WHITE:
applyNoctisWhiteTheme(language);
break;
case FIVE_COLOR:
applyFiveColorsDarkTheme(language);
break;
case ORANGE_BOX:
applyOrangeBoxTheme(language);
break;
}
}

public String[] getLanguageKeywords(LanguageName language) {
switch (language) {
case JAVA: return JavaLanguage.getKeywords(context);
case PYTHON: return PythonLanguage.getKeywords(context);
case GO_LANG: return GoLanguage.getKeywords(context);
default: return new String[]{};
}
}

public List<Code> getLanguageCodeList(LanguageName language) {
switch (language) {
case JAVA: return JavaLanguage.getCodeList(context);
case PYTHON: return PythonLanguage.getCodeList(context);
case GO_LANG: return GoLanguage.getCodeList(context);
default: return new ArrayList<>();
}
}

public Set<Character> getLanguageIndentationStarts(LanguageName language) {
switch (language) {
case JAVA: return JavaLanguage.getIndentationStarts();
case PYTHON: return PythonLanguage.getIndentationStarts();
case GO_LANG: return GoLanguage.getIndentationStarts();
default: return new HashSet<>();
}
}

public Set<Character> getLanguageIndentationEnds(LanguageName language) {
switch (language) {
case JAVA: return JavaLanguage.getIndentationEnds();
case PYTHON: return PythonLanguage.getIndentationEnds();
case GO_LANG: return GoLanguage.getIndentationEnds();
default: return new HashSet<>();
}
}

private void applyMonokaiTheme(LanguageName language) {
switch (language) {
case JAVA:
JavaLanguage.applyMonokaiTheme(context, codeView);
break;
case PYTHON:
PythonLanguage.applyMonokaiTheme(context, codeView);
break;
case GO_LANG:
GoLanguage.applyMonokaiTheme(context, codeView);
break;
}
}

private void applyNoctisWhiteTheme(LanguageName language) {
switch (language) {
case JAVA:
JavaLanguage.applyNoctisWhiteTheme(context, codeView);
break;
case PYTHON:
PythonLanguage.applyNoctisWhiteTheme(context, codeView);
break;
case GO_LANG:
GoLanguage.applyNoctisWhiteTheme(context, codeView);
break;
}
}

private void applyFiveColorsDarkTheme(LanguageName language) {
switch (language) {
case JAVA:
JavaLanguage.applyFiveColorsDarkTheme(context, codeView);
break;
case PYTHON:
PythonLanguage.applyFiveColorsDarkTheme(context, codeView);
break;
case GO_LANG:
GoLanguage.applyFiveColorsDarkTheme(context, codeView);
break;
}
}

private void applyOrangeBoxTheme(LanguageName language) {
switch (language) {
case JAVA:
JavaLanguage.applyOrangeBoxTheme(context, codeView);
break;
case PYTHON:
PythonLanguage.applyOrangeBoxTheme(context, codeView);
break;
case GO_LANG:
GoLanguage.applyOrangeBoxTheme(context, codeView);
break;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* List of examples languages for CodeView
*/
public enum Language {
public enum LanguageName {
JAVA,
PYTHON,
GO_LANG
Expand Down
Loading

0 comments on commit 05d75bf

Please sign in to comment.