Skip to content

Commit de94001

Browse files
committed
Extended docs, wrapped some placeholder logic
1 parent 1ef929d commit de94001

File tree

7 files changed

+65
-10
lines changed

7 files changed

+65
-10
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ class CodeContentAdapter : RecyclerView.Adapter<CodeContentAdapter.ViewHolder> {
7676
/**
7777
* Split code content by lines. If listing must not be shown full it shows
7878
* only necessary lines & rest are dropped (and stores in named variable).
79+
*
80+
* @param isShowFull Show full listing?
81+
* @param shortcutNote Note will shown below code for listing shortcut
7982
*/
8083
private fun initCodeContent(isShowFull: Boolean,
8184
shortcutNote: String = mContext.getString(R.string.show_all)) {

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

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import android.view.ViewPropertyAnimator
1313
import android.widget.RelativeLayout
1414
import io.github.kbiakov.codeview.highlight.ColorTheme
1515
import io.github.kbiakov.codeview.highlight.ColorThemeData
16+
import io.github.kbiakov.codeview.highlight.color
1617
import java.util.*
1718

1819
/**
@@ -34,6 +35,7 @@ import java.util.*
3435
*/
3536
class CodeView : RelativeLayout {
3637

38+
private val vRoot: View
3739
private val vPlaceholder: View
3840
private val vShadowRight: View
3941
private val vShadowBottomLine: View
@@ -59,6 +61,7 @@ class CodeView : RelativeLayout {
5961
val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
6062
inflater.inflate(R.layout.layout_code_view, this, true)
6163

64+
vRoot = findViewById(R.id.v_root)
6265
vPlaceholder = findViewById(R.id.v_placeholder)
6366
vShadowRight = findViewById(R.id.v_shadow_right)
6467
vShadowBottomLine = findViewById(R.id.v_shadow_bottom_line)
@@ -135,21 +138,27 @@ class CodeView : RelativeLayout {
135138
/**
136139
* Specify color theme: syntax colors (need to highlighting) & related to
137140
* code view (numeration color & background, content backgrounds).
141+
*
142+
* @param colorTheme Default or custom color theme
138143
*/
139144

140145
// default color theme provided by enum
141146
fun setColorTheme(colorTheme: ColorTheme) = addTask {
142147
adapter.colorTheme = colorTheme.with()
148+
fillBackground(colorTheme.bgContent)
143149
}
144150

145151
// custom color theme provided by user
146152
fun setColorTheme(colorTheme: ColorThemeData) = addTask {
147153
adapter.colorTheme = colorTheme
154+
fillBackground(colorTheme.bgContent)
148155
}
149156

150157
/**
151158
* Highlight code by defined programming language.
152159
* It holds the placeholder on the view until code is highlighted.
160+
*
161+
* @param language Language to highlight
153162
*/
154163
fun highlightCode(language: String) = addTask {
155164
adapter.highlightCode(language)
@@ -166,15 +175,19 @@ class CodeView : RelativeLayout {
166175
}
167176

168177
/**
169-
* Useful in some cases if you want to listen user line selection.
170-
* (May be you want to show alert when user click on code line, who knows?) ¯\_(ツ)_/¯
178+
* Useful in some cases if you want to listen user line clicks.
179+
* (May be you want to show alert, who knows?) ¯\_(ツ)_/¯
180+
*
181+
* @param listener Code line click listener
171182
*/
172183
fun setCodeListener(listener: OnCodeLineClickListener) = addTask {
173184
adapter.codeListener = listener
174185
}
175186

176187
/**
177188
* Control shadows visibility to provide more sensitive UI.
189+
*
190+
* @param isVisible Shadows visibility
178191
*/
179192
fun setShadowsVisible(isVisible: Boolean = true) = addTask {
180193
val visibility = if (isVisible) View.VISIBLE else GONE
@@ -185,6 +198,8 @@ class CodeView : RelativeLayout {
185198

186199
/**
187200
* Update code content if view was built or, finally, build code view.
201+
*
202+
* @param content Code content
188203
*/
189204
fun setCodeContent(content: String) {
190205
when (state) {
@@ -216,6 +231,7 @@ class CodeView : RelativeLayout {
216231
Thread.delayed {
217232
rvCodeContent.adapter = CodeContentAdapter(context, content)
218233
processBuildTasks()
234+
//fillBackground()
219235
setupShadows()
220236
hidePlaceholder()
221237
state = ViewState.PRESENTED
@@ -230,9 +246,19 @@ class CodeView : RelativeLayout {
230246
*/
231247
private fun setupShadows() = setShadowsVisible(!adapter.isFullShowing)
232248

249+
/**
250+
* Fill background to color accordingly to color theme.
251+
*
252+
* @color Background color
253+
*/
254+
private fun fillBackground(color: Int = adapter.colorTheme.bgContent) =
255+
vRoot.setBackgroundColor(color.color())
256+
233257
/**
234258
* Placeholder fills space at start and stretched to marked up view size
235259
* (by extracting code lines) because at this point it's not built yet.
260+
*
261+
* @param linesCount Count of lines to measure space for placeholder
236262
*/
237263
private fun measurePlaceholder(linesCount: Int) {
238264
val lineHeight = dpToPx(context, 24)
@@ -270,11 +296,15 @@ interface OnCodeLineClickListener {
270296

271297
/**
272298
* Extension for delayed block call.
299+
*
300+
* @param body Operation body
273301
*/
274302
fun Thread.delayed(body: () -> Unit) = Handler().postDelayed(body, 150)
275303

276304
/**
277305
* More readable form for animation listener (hi, iOS & Cocoa Touch!).
306+
*
307+
* @param handler Handler body
278308
*/
279309
fun ViewPropertyAnimator.didAnimated(handler: () -> Unit) =
280310
setListener(object : AnimatorListenerAdapter() {

codeview/src/main/java/io/github/kbiakov/codeview/classifier/CodeClassifier.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ object CodeClassifier {
4040
* At this point all files with code listings prepared to process
4141
* by classifier. This operation often is very expensive & should
4242
* be performed asynchronously when app starts.
43+
*
44+
* @param context Context
4345
*/
4446
fun train(context: Context) {
4547
Files.ls(context, TRAINING_SET_FOLDER).forEach { language ->
@@ -53,6 +55,9 @@ object CodeClassifier {
5355

5456
/**
5557
* Try to define what language is used in code snippet.
58+
*
59+
* @param snippet Code snippet
60+
* @return Code language
5661
*/
5762
fun classify(snippet: String): String {
5863
val feature = classifier.classify(spaceSplit(snippet))

codeview/src/main/java/io/github/kbiakov/codeview/highlight/CodeHighlighter.kt

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ object CodeHighlighter {
2525
* @param codeLanguage Programming language
2626
* @param rawSource Code source by one string
2727
* @param colorTheme Color theme (see below)
28-
* @return Highlighted code. String with necessary inserted color tags.
28+
* @return Highlighted code, string with necessary inserted color tags
2929
*/
3030
fun highlight(codeLanguage: String, rawSource: String, colorTheme: ColorThemeData): String {
3131
val source = rawSource.escapeLT()
@@ -46,6 +46,10 @@ object CodeHighlighter {
4646

4747
/**
4848
* Parse user input by extracting highlighted content.
49+
*
50+
* @param codeContent Code content
51+
* @param result Syntax unit
52+
* @return Parsed content to highlight
4953
*/
5054
private fun parseContent(codeContent: String, result: ParseResult): String {
5155
val length = result.offset + result.length
@@ -55,12 +59,19 @@ object CodeHighlighter {
5559

5660
/**
5761
* Color accessor from built color map for selected color theme.
62+
*
63+
* @param colorsMap Colors map built from color theme
64+
* @param result Syntax unit
65+
* @return Color for syntax unit
5866
*/
5967
private fun getColor(colorsMap: HashMap<String, String>, result: ParseResult) =
6068
colorsMap[result.styleKeys[0]] ?: colorsMap["pln"]
6169

6270
/**
6371
* Build fast accessor (as map) for selected color theme.
72+
*
73+
* @param colorTheme Color theme
74+
* @return Colors map built from color theme
6475
*/
6576
private fun buildColorsMap(colorTheme: ColorThemeData) =
6677
object : HashMap<String, String>() {
@@ -155,7 +166,7 @@ data class ColorThemeData(
155166
val noteColor: Int)
156167

157168
/**
158-
* Colors for highlighting code spots.
169+
* Colors for highlighting code units.
159170
*/
160171
data class SyntaxColors(
161172
val type: Int = 0x859900,

codeview/src/main/res/layout/item_code_line.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,9 @@
1717
android:id="@+id/tv_line_content"
1818
android:layout_width="match_parent"
1919
android:layout_height="match_parent"
20-
android:layout_centerVertical="true"
21-
android:layout_toRightOf="@+id/tv_line_num"
2220
android:layout_marginLeft="16dp"
2321
android:layout_marginRight="16dp"
22+
android:layout_toRightOf="@+id/tv_line_num"
2423
android:gravity="center_vertical"
2524
android:fontFamily="monospace"
2625
android:singleLine="true"

codeview/src/main/res/layout/layout_code_view.xml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
xmlns:android="http://schemas.android.com/apk/res/android"
44
xmlns:tools="http://schemas.android.com/tools"
55
android:layout_width="match_parent"
6-
android:layout_height="wrap_content">
6+
android:layout_height="wrap_content"
7+
android:id="@+id/v_root">
78

89
<io.github.kbiakov.codeview.BidirectionalScrollView
910
android:id="@+id/v_scroll"
@@ -13,7 +14,7 @@
1314
<android.support.v7.widget.RecyclerView
1415
android:id="@+id/rv_code_content"
1516
android:layout_width="match_parent"
16-
android:layout_height="match_parent"
17+
android:layout_height="wrap_content"
1718
tools:listitem="@layout/item_code_line"/>
1819

1920
</io.github.kbiakov.codeview.BidirectionalScrollView>

example/src/main/res/layout/activity_listings.xml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,13 @@
88

99
<io.github.kbiakov.codeview.CodeView
1010
android:id="@+id/code_view"
11-
android:layout_width="wrap_content"
12-
android:layout_height="wrap_content"/>
11+
android:layout_width="match_parent"
12+
android:layout_height="120dp"/>
13+
14+
<TextView
15+
android:layout_width="match_parent"
16+
android:layout_height="match_parent"
17+
android:layout_below="@+id/code_view"
18+
android:text="123123123123"/>
1319

1420
</RelativeLayout>

0 commit comments

Comments
 (0)