Skip to content

Commit 07b4aac

Browse files
committed
Refactored & updated README
1 parent 1f18253 commit 07b4aac

6 files changed

Lines changed: 166 additions & 130 deletions

File tree

README.md

Lines changed: 56 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@
44
[![Release](https://jitpack.io/v/softwee/codeview-android.svg)](https://jitpack.io/#softwee/codeview-android)
55
[![Build Status](https://travis-ci.org/Softwee/codeview-android.svg?branch=master)](https://travis-ci.org/Softwee/codeview-android)
66

7-
CodeView helps to show code content with syntax highlighting in native way.
7+
<b>CodeView</b> helps to show code content with syntax highlighting in native way.
88

99
## Description
10-
CodeView contains 3 core parts to implement necessary logic:<br>
10+
<b>CodeView</b> contains 3 core parts to implement necessary logic:<br>
1111

12-
1. <b>CodeClassifier</b> is trying to define what language presented in code snippet. It built upon [Naive Bayes classifier](https://github.com/ptnplanet/Java-Naive-Bayes-Classifier). There is no need to work with this class directly & you must just follow instructions below. (Experimental module, may not work properly!)<br>
12+
1. <b>CodeView</b> & related abstract adapter to provide options & customization (see below).<br>
1313

1414
2. For highlighting it uses <b>CodeHighlighter</b>, just highlights your code & returns formatted content. It based on [Google Prettify](https://github.com/google/code-prettify) and their Java implementation & [fork](https://github.com/google/code-prettify).<br>
1515

16-
3. <b>CodeView</b> & related abstract adapter to provide customization (see below).<br>
16+
3. <b>CodeClassifier</b> is trying to define what language presented in code snippet. It built using [Naive Bayes classifier](https://en.wikipedia.org/wiki/Naive_Bayes_classifier) upon found open-source [implementation](https://github.com/ptnplanet/Java-Naive-Bayes-Classifier), which I rewrote in Kotlin. There is no need to work with this class directly & you must just follow instructions below. (Experimental module, may not work properly!)<br>
1717

1818
## Download
1919
Add it in your root ```build.gradle``` at the end of repositories:
@@ -28,7 +28,7 @@ allprojects {
2828

2929
Add the dependency:
3030
```groovy
31-
compile 'com.github.softwee:codeview-android:1.1.2'
31+
compile 'com.github.softwee:codeview-android:1.2.0'
3232
```
3333

3434
## Usage
@@ -38,72 +38,80 @@ If you want to use code classifier to auto language recognizing just add to your
3838
CodeProcessor.init(this);
3939
```
4040

41-
Add view for your layout:
41+
Having done ones on app start you can classify language for different snippets more faster, because algorithm needs time for training on sets for presented listings of languages library have.
42+
43+
Add view to your layout:
4244
```xml
4345
<io.github.kbiakov.codeview.CodeView
4446
android:id="@+id/code_view"
4547
android:layout_width="wrap_content"
4648
android:layout_height="wrap_content"/>
4749
```
4850

49-
Use chaining syntax when build view:
51+
So now you can set code:
5052
```java
53+
// bind view
5154
CodeView codeView = (CodeView) findViewById(R.id.code_view);
52-
53-
codeView.highlightCode("js")
54-
.setColorTheme(ColorTheme.SOLARIZED_LIGHT.withBgContent(myColor))
55-
.setCodeContent(getString(R.string.listing_js));
55+
// auto language recognition
56+
codeView.setCode(getString(R.string.listing_js));
57+
// explicit form (will work faster!), see available extensions below
58+
codeView.setCode(getString(R.string.listing_py), "py");
5659
```
5760

58-
And perform actions sequentially when view built:
59-
```java
60-
codeView.setCodeContent(getString(R.string.listing_java));
61-
codeView.highlightCode("java");
62-
```
61+
## Customization
62+
When you call ```setCode(...)``` view will prepared with default params if view was not initialized before. So if you want some customization, it can be done using options and/or adapter.
6363

64-
You can use both forms for build & built view, but note: ```setCodeContent(String)``` is final step when you build your view, otherwise not. If you firstly highlight and then set code content, code will not be highlighted if view was not built yet. Instructions above helps you to avoid errors. View has state to handle this behavior.
65-
66-
## Customizing
67-
Use implicit form to code highlighting:
68-
```java
69-
codeView.highlightCode();
70-
```
71-
or eplixit (see available extensions below):
64+
### Initialization
65+
You can initialize view with options:
7266
```java
73-
codeView.highlightCode("js"); // it will work fast!
67+
codeView.setOptions(Options.Default.get(this)
68+
.withLanguage("python")
69+
.withCode(R.string.listing_py)
70+
.withTheme(ColorTheme.MONOKAI));
7471
```
7572

76-
Use default color theme:
73+
Or using adapter (see <b>Adapter customization<b/> or example for more details):
7774
```java
78-
codeView.setColorTheme(ColorTheme.SOLARIZED_LIGHT);
75+
final CustomAdapter myAdapter = new CustomAdapter(this, getString(R.string.listing_md));
76+
codeView.setAdapter(myAdapter);
7977
```
80-
or extend default:
78+
79+
### Options
80+
<b>Options</b> helps to easily set necessary params, such as code & language, color theme, shortcut params & line click listener. Some params are unnecessary.
81+
82+
When view initialized (options or adapter set) you can manipulate options in various ways:
8183
```java
82-
int myColor = ContextCompat.getColor(this, R.color.my_color);
83-
codeView.setColorTheme(ColorTheme.MONOKAI.withBgContent(myColor));
84+
codeView.getOptions()
85+
.withCode(R.string.listing_java)
86+
.withLanguage("java")
87+
.withTheme(ColorTheme.MONOKAI);
8488
```
85-
or provide your own (don't forget to open PR with this stuff!)
89+
90+
### Color themes
91+
There are some default themes (see full list below):
8692
```java
87-
codeView.setColorTheme(new ColorThemeData(new SyntaxColors(...), ...));
93+
codeView.getOptions().setTheme(ColorTheme.SOLARIZED_LIGHT);
8894
```
8995

90-
Handle user clicks on code lines:
96+
But you can build your own from existing:
9197
```java
92-
codeView.setCodeListener(new OnCodeLineClickListener() {
93-
@Override
94-
public void onCodeLineClicked(int n, @NotNull String line) {
95-
Log.i("ListingsActivity", "On " + (n + 1) + " line clicked");
96-
}
97-
});
98+
ColorThemeData myTheme = ColorTheme.SOLARIZED_LIGHT.theme()
99+
.withBgContent(android.R.color.black)
100+
.withNoteColor(android.R.color.white);
101+
102+
codeView.getOptions().setTheme(myTheme);
98103
```
99104

100-
Enable shadows to hide scrolled content:
105+
Or create your own from scratch (don't forget to open PR with this stuff!):
101106
```java
102-
codeView.setShadowsEnabled(true);
107+
ColorThemeData customTheme = new ColorThemeData(new SyntaxColors(...), ...);
108+
codeView.getOptions().setTheme(customTheme);
103109
```
104110

105-
## Adapter customization
106-
Sometimes you may want to add some content under line. You can create your own implementation as follows:
111+
### Adapter
112+
Sometimes you may want to take code lines under your control, and that's why you need <b>Adapter</b>.
113+
114+
You can create your own implementation as follows:
107115

108116
1. Create your model to store data, for example some ```MyModel``` class.<br>
109117
2. Extend ```AbstractCodeAdapter<MyModel>``` typed by your model class.<br>
@@ -169,14 +177,14 @@ C/C++/Objective-C (```"c"```, ```"cc"```, ```"cpp"```, ```"cxx"```, ```"cyc"```,
169177
Didn't found yours? Please, open issue to show your interest & I'll try to add this language in next releases.
170178

171179
## List of available themes
172-
1. Default (simple light theme)
173-
2. Solarized Light
174-
3. Monokai
180+
1. Default (simple light theme).
181+
2. Solarized Light.
182+
3. Monokai.
175183

176184
## Contribute
177185
1. You can add your theme (see [ColorTheme](https://github.com/Softwee/codeview-android/blob/master/codeview/src/main/java/io/github/kbiakov/codeview/highlight/CodeHighlighter.kt) class). Try to add some classic color themes or create your own if it looks cool. You can find many of them in different open-source text editors.<br>
178-
2. If you are strong in a regex add missed language as shown [here](https://github.com/Softwee/codeview-android/blob/master/codeview/src/main/java/io/github/kbiakov/codeview/highlight/prettify/lang/LangScala.java). You can find existing regex for some language in different sources of js-libraries, etc, which plays the same role.<br>
179-
3. Various adapters also welcome, customization is unlimited.
186+
2. If you are strong in regex, add missed language as shown [here](https://github.com/Softwee/codeview-android/blob/master/codeview/src/main/java/io/github/kbiakov/codeview/highlight/prettify/lang/LangScala.java). You can find existing regex for some language in different sources of libraries, which plays the same role.<br>
187+
3. Various adapters also welcome.
180188

181189
## Author
182190
### [Kirill Biakov](https://github.com/kbiakov)

codeview/src/main/java/io/github/kbiakov/codeview/CodeView.kt

Lines changed: 43 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ class CodeView(context: Context, attrs: AttributeSet) : RelativeLayout(context,
4545
inflate(context, R.layout.layout_code_view, this)
4646

4747
if (isAnimateOnStart)
48-
animate().setDuration(Consts.DELAY * 5)
48+
animate()
49+
.setDuration(Consts.DELAY * 5)
4950
.alpha(Consts.ALPHA)
5051

5152
// TODO: add shadow color customization
@@ -65,18 +66,19 @@ class CodeView(context: Context, attrs: AttributeSet) : RelativeLayout(context,
6566
private fun highlight() {
6667
getAdapter()?.highlight {
6768

68-
animate().setDuration(Consts.DELAY * 2)
69+
animate()
70+
.setDuration(Consts.DELAY * 2)
6971
.alpha(.1f)
7072

7173
delayed {
7274
animate().alpha(1f)
73-
vCodeList.adapter?.notifyDataSetChanged()
75+
getAdapter()?.notifyDataSetChanged()
7476
}
7577
}
7678
}
7779

7880
/**
79-
* Border shadows will shown if presented full code listing.
81+
* Border shadows will shown if full listing presented.
8082
* It helps to see what part of code is scrolled & hidden.
8183
*
8284
* @param isShadows Is shadows needed
@@ -89,63 +91,70 @@ class CodeView(context: Context, attrs: AttributeSet) : RelativeLayout(context,
8991
vShadowBottomContent.visibility = visibility
9092
}
9193

94+
// - Initialization
95+
9296
/**
9397
* Prepare view with default adapter & options.
9498
*/
9599
private fun prepare() = setAdapter(CodeWithNotesAdapter(context))
96100

97101
/**
98-
* View options accessor.
102+
* Initialize with options.
103+
*
104+
* @param options Options
99105
*/
100-
fun getOptions(): Options? = getAdapter()?.options
106+
fun setOptions(options: Options) = setAdapter(CodeWithNotesAdapter(context, options))
101107

102108
/**
103-
* Initialize with options.
109+
* Initialize with adapter.
104110
*
105-
* @param options Options
111+
* @param adapter Adapter
112+
*/
113+
fun setAdapter(adapter: AbstractCodeAdapter<*>) {
114+
vCodeList.adapter = adapter
115+
setupShadows(adapter.options.shadows)
116+
highlight()
117+
}
118+
119+
// - Options
120+
121+
/**
122+
* View options accessor.
106123
*/
107-
fun setOptions(options: Options) = setOptions(options, false)
124+
fun getOptions(): Options? = getAdapter()?.options
125+
fun getOptionsOrDefault() = getOptions() ?: Options(context)
108126

109127
/**
110-
* Set options & initialize if needed.
128+
* Update options or initialize if needed.
111129
*
112130
* @param options Options
113-
* @param isSaveAdapter Save adapter?
114131
*/
115-
fun setOptions(options: Options, isSaveAdapter: Boolean = true) {
116-
setAdapter(if (isSaveAdapter)
117-
getAdapter() ?: CodeWithNotesAdapter(context, options)
132+
fun updateOptions(options: Options) {
133+
if (getAdapter() == null)
134+
setOptions(options)
118135
else
119-
CodeWithNotesAdapter(context, options))
136+
getAdapter()!!.options = options
120137
}
121138

139+
// - Adapter
140+
122141
/**
123142
* Code adapter accessor.
124143
*/
125144
fun getAdapter() = vCodeList.adapter as? AbstractCodeAdapter<*>
126145

127146
/**
128-
* Initialize with adapter.
147+
* Update adapter or initialize if needed.
129148
*
130149
* @param adapter Adapter
131150
*/
132-
fun setAdapter(adapter: AbstractCodeAdapter<*>) = setAdapter(adapter, false)
133-
134-
/**
135-
* Set adapter & initialize if needed.
136-
*
137-
* @param adapter Adapter
138-
* @param isSaveOptions Save options?
139-
*/
140-
fun setAdapter(adapter: AbstractCodeAdapter<*>, isSaveOptions: Boolean = true) {
141-
if (isSaveOptions)
142-
adapter.options = getOptions() ?: Options(context)
143-
144-
vCodeList.adapter = adapter
145-
setupShadows(adapter.options.shadows)
146-
highlight()
151+
fun updateAdapter(adapter: AbstractCodeAdapter<*>) {
152+
adapter.options = getOptionsOrDefault()
153+
setAdapter(adapter)
147154
}
148155

156+
// - Set code
157+
149158
/**
150159
* Set code content.
151160
*
@@ -174,12 +183,8 @@ class CodeView(context: Context, attrs: AttributeSet) : RelativeLayout(context,
174183
* @param language Programming language
175184
*/
176185
fun setCode(code: String, language: String) {
177-
val options = if (getAdapter() == null)
178-
Options(context)
179-
else
180-
getAdapter()!!.options
181-
182-
setOptions(options.withLanguage(language))
186+
val options = getOptionsOrDefault()
187+
updateOptions(options.withLanguage(language))
183188
getAdapter()!!.updateCode(code)
184189
}
185190
}
@@ -188,5 +193,5 @@ class CodeView(context: Context, attrs: AttributeSet) : RelativeLayout(context,
188193
* Provide listener to code line clicks.
189194
*/
190195
interface OnCodeLineClickListener {
191-
fun onLineClicked(n: Int, line: String)
196+
fun onCodeLineClicked(n: Int, line: String)
192197
}

codeview/src/main/java/io/github/kbiakov/codeview/adapters/AbstractCodeAdapter.kt

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ abstract class AbstractCodeAdapter<T> : RecyclerView.Adapter<AbstractCodeAdapter
117117
* Mapper from entity to footer view.
118118
*
119119
* @param context Context
120-
* @param entity Entity to init view
120+
* @param entity Entity to setOptions view
121121
* @param isFirst Is first footer view
122122
* @return Footer view
123123
*/
@@ -193,7 +193,7 @@ abstract class AbstractCodeAdapter<T> : RecyclerView.Adapter<AbstractCodeAdapter
193193

194194
options.lineClickListener?.let {
195195
holder.itemView.setOnClickListener {
196-
options.lineClickListener?.onLineClicked(position, codeLine)
196+
options.lineClickListener?.onCodeLineClicked(position, codeLine)
197197
}
198198
}
199199

@@ -314,19 +314,32 @@ data class Options(
314314
return this
315315
}
316316

317+
fun withCode(codeResId: Int): Options {
318+
this.code = context.getString(codeResId)
319+
return this
320+
}
321+
322+
fun setCode(codeResId: Int) {
323+
withCode(codeResId)
324+
}
325+
317326
fun withLanguage(language: String): Options {
318327
this.language = language
319328
return this
320329
}
321330

331+
fun withTheme(theme: ColorThemeData): Options {
332+
this.theme = theme
333+
return this
334+
}
335+
322336
fun withTheme(theme: ColorTheme): Options {
323337
this.theme = theme.theme()
324338
return this
325339
}
326340

327-
fun withTheme(theme: ColorThemeData): Options {
328-
this.theme = theme
329-
return this
341+
fun setTheme(theme: ColorTheme) {
342+
withTheme(theme)
330343
}
331344

332345
fun withShadows(): Options {
@@ -346,7 +359,7 @@ data class Options(
346359
return this
347360
}
348361

349-
fun addLineClickListener(listener: OnCodeLineClickListener): Options {
362+
fun addCodeLineClickListener(listener: OnCodeLineClickListener): Options {
350363
this.lineClickListener = listener
351364
return this
352365
}

0 commit comments

Comments
 (0)