Skip to content

Commit 3531978

Browse files
authored
Merge pull request #23 from Softwee/feature/api.revision
Feature/api.revision
2 parents 1f18253 + 30138e9 commit 3531978

File tree

6 files changed

+170
-127
lines changed

6 files changed

+170
-127
lines changed

README.md

Lines changed: 60 additions & 45 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,87 @@ 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 which library has.
42+
43+
Add view to your layout & bind as usual:
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
```
48-
49-
Use chaining syntax when build view:
5050
```java
5151
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));
5652
```
5753

58-
And perform actions sequentially when view built:
54+
So now you can set code using implicit form:
5955
```java
60-
codeView.setCodeContent(getString(R.string.listing_java));
61-
codeView.highlightCode("java");
56+
// auto language recognition
57+
codeView.setCode(getString(R.string.listing_js));
6258
```
6359

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:
60+
Or explicit (see available extensions below):
6861
```java
69-
codeView.highlightCode();
62+
// will work faster!
63+
codeView.setCode(getString(R.string.listing_py), "py");
7064
```
71-
or eplixit (see available extensions below):
65+
66+
## Customization
67+
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.
68+
69+
### Initialization
70+
You can initialize view with options:
7271
```java
73-
codeView.highlightCode("js"); // it will work fast!
72+
codeView.setOptions(Options.Default.get(this)
73+
.withLanguage("python")
74+
.withCode(R.string.listing_py)
75+
.withTheme(ColorTheme.MONOKAI));
7476
```
7577

76-
Use default color theme:
78+
Or using adapter (see <b>Adapter</b> or example for more details):
7779
```java
78-
codeView.setColorTheme(ColorTheme.SOLARIZED_LIGHT);
80+
final CustomAdapter myAdapter = new CustomAdapter(this, getString(R.string.listing_md));
81+
codeView.setAdapter(myAdapter);
7982
```
80-
or extend default:
83+
84+
<b>Note:</b> Each <b>CodeView</b> has adapter and each adapter has options. When calling ```setOptions(...)``` or ```setAdapter(...)``` current adapter "flushed" with current options. If you want to save the state and just update options saving adapter or set adapter saving options you must call ```updateOptions(...)``` or ```updateAdapter(...)``` accordingly.
85+
86+
### Options
87+
Options helps to easily set necessary params, such as code & language, color theme, shortcut params (max lines, note), code line click listener. Some params are unnecessary.
88+
89+
When view initialized (options or adapter are set) you can manipulate options in various ways:
8190
```java
82-
int myColor = ContextCompat.getColor(this, R.color.my_color);
83-
codeView.setColorTheme(ColorTheme.MONOKAI.withBgContent(myColor));
91+
codeView.getOptions()
92+
.withCode(R.string.listing_java)
93+
.withLanguage("java")
94+
.withTheme(ColorTheme.MONOKAI);
8495
```
85-
or provide your own (don't forget to open PR with this stuff!)
96+
97+
### Color theme
98+
There are some default themes (see full list below):
8699
```java
87-
codeView.setColorTheme(new ColorThemeData(new SyntaxColors(...), ...));
100+
codeView.getOptions().setTheme(ColorTheme.SOLARIZED_LIGHT);
88101
```
89102

90-
Handle user clicks on code lines:
103+
But you can build your own from existing one:
91104
```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-
});
105+
ColorThemeData myTheme = ColorTheme.SOLARIZED_LIGHT.theme()
106+
.withBgContent(android.R.color.black)
107+
.withNoteColor(android.R.color.white);
108+
109+
codeView.getOptions().setTheme(myTheme);
98110
```
99111

100-
Enable shadows to hide scrolled content:
112+
Or create your own from scratch (don't forget to open PR with this stuff!):
101113
```java
102-
codeView.setShadowsEnabled(true);
114+
ColorThemeData customTheme = new ColorThemeData(new SyntaxColors(...), ...);
115+
codeView.getOptions().setTheme(customTheme);
103116
```
104117

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

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

171186
## List of available themes
172-
1. Default (simple light theme)
173-
2. Solarized Light
174-
3. Monokai
187+
1. Default (simple light theme).
188+
2. Solarized Light.
189+
3. Monokai.
175190

176191
## Contribute
177192
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.
193+
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>
194+
3. Various adapters also welcome.
180195

181196
## Author
182197
### [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)