Skip to content

Commit ed6e0f2

Browse files
authored
Merge pull request #4 from Softwee/develop
Fixed view size bugs
2 parents 8240c3a + 302091e commit ed6e0f2

File tree

8 files changed

+75
-21
lines changed

8 files changed

+75
-21
lines changed

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

+3
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

+42-7
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
/**
@@ -135,6 +136,8 @@ class CodeView : RelativeLayout {
135136
/**
136137
* Specify color theme: syntax colors (need to highlighting) & related to
137138
* code view (numeration color & background, content backgrounds).
139+
*
140+
* @param colorTheme Default or custom color theme
138141
*/
139142

140143
// default color theme provided by enum
@@ -150,6 +153,8 @@ class CodeView : RelativeLayout {
150153
/**
151154
* Highlight code by defined programming language.
152155
* It holds the placeholder on the view until code is highlighted.
156+
*
157+
* @param language Language to highlight
153158
*/
154159
fun highlightCode(language: String) = addTask {
155160
adapter.highlightCode(language)
@@ -166,15 +171,19 @@ class CodeView : RelativeLayout {
166171
}
167172

168173
/**
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?) ¯\_(ツ)_/¯
174+
* Useful in some cases if you want to listen user line clicks.
175+
* (May be you want to show alert, who knows?) ¯\_(ツ)_/¯
176+
*
177+
* @param listener Code line click listener
171178
*/
172179
fun setCodeListener(listener: OnCodeLineClickListener) = addTask {
173180
adapter.codeListener = listener
174181
}
175182

176183
/**
177184
* Control shadows visibility to provide more sensitive UI.
185+
*
186+
* @param isVisible Shadows visibility
178187
*/
179188
fun setShadowsVisible(isVisible: Boolean = true) = addTask {
180189
val visibility = if (isVisible) View.VISIBLE else GONE
@@ -185,17 +194,19 @@ class CodeView : RelativeLayout {
185194

186195
/**
187196
* Update code content if view was built or, finally, build code view.
197+
*
198+
* @param content Code content
188199
*/
189200
fun setCodeContent(content: String) {
190201
when (state) {
191202
ViewState.BUILD ->
192203
build(content)
193204
ViewState.PREPARE ->
194205
Thread.delayed {
195-
adapter.updateCodeContent(content)
206+
update(content)
196207
}
197208
ViewState.PRESENTED ->
198-
adapter.updateCodeContent(content)
209+
update(content)
199210
}
200211
}
201212

@@ -222,6 +233,19 @@ class CodeView : RelativeLayout {
222233
}
223234
}
224235

236+
/**
237+
* Hot view updating.
238+
*
239+
* @param content Code content
240+
*/
241+
private fun update(content: String) {
242+
state = ViewState.PREPARE
243+
measurePlaceholder(extractLines(content).size)
244+
adapter.updateCodeContent(content)
245+
hidePlaceholder()
246+
state = ViewState.PRESENTED
247+
}
248+
225249
// - Setup actions
226250

227251
/**
@@ -232,15 +256,22 @@ class CodeView : RelativeLayout {
232256

233257
/**
234258
* Placeholder fills space at start and stretched to marked up view size
235-
* (by extracting code lines) because at this point it's not built yet.
259+
* (by code lines count) 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)
239-
val topMargin = dpToPx(context, 8)
240-
val height = linesCount * lineHeight + 2 * topMargin
265+
val topPadding = dpToPx(context, 8)
266+
267+
// double padding (top & bottom) for big view, one is enough for small
268+
val padding = (if (linesCount > 1) 2 else 1) * topPadding
269+
270+
val height = linesCount * lineHeight + padding
241271

242272
vPlaceholder.layoutParams = RelativeLayout.LayoutParams(
243273
RelativeLayout.LayoutParams.MATCH_PARENT, height)
274+
vPlaceholder.visibility = View.VISIBLE
244275
}
245276

246277
// - Animations
@@ -270,11 +301,15 @@ interface OnCodeLineClickListener {
270301

271302
/**
272303
* Extension for delayed block call.
304+
*
305+
* @param body Operation body
273306
*/
274307
fun Thread.delayed(body: () -> Unit) = Handler().postDelayed(body, 150)
275308

276309
/**
277310
* More readable form for animation listener (hi, iOS & Cocoa Touch!).
311+
*
312+
* @param handler Handler body
278313
*/
279314
fun ViewPropertyAnimator.didAnimated(handler: () -> Unit) =
280315
setListener(object : AnimatorListenerAdapter() {

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

+5
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

+13-2
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

+1-2
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

+8-8
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@
88
<io.github.kbiakov.codeview.BidirectionalScrollView
99
android:id="@+id/v_scroll"
1010
android:layout_width="match_parent"
11-
android:layout_height="wrap_content">
11+
android:layout_height="wrap_content"
12+
android:fillViewport="true">
1213

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>
@@ -24,27 +25,26 @@
2425
android:layout_height="match_parent"
2526
android:layout_alignParentRight="true"
2627
android:background="@drawable/shadow_right"
27-
android:visibility="invisible"/>
28+
android:visibility="gone"/>
2829

2930
<LinearLayout
3031
android:layout_width="match_parent"
3132
android:layout_height="16dp"
3233
android:layout_alignParentBottom="true"
33-
android:orientation="horizontal">
34+
android:orientation="horizontal"
35+
android:visibility="gone">
3436

3537
<View
3638
android:id="@+id/v_shadow_bottom_line"
3739
android:layout_width="24dp"
3840
android:layout_height="match_parent"
39-
android:background="@drawable/shadow_bottom_line"
40-
android:visibility="invisible"/>
41+
android:background="@drawable/shadow_bottom_line"/>
4142

4243
<View
4344
android:id="@+id/v_shadow_bottom_content"
4445
android:layout_width="match_parent"
4546
android:layout_height="match_parent"
46-
android:background="@drawable/shadow_bottom_content"
47-
android:visibility="invisible"/>
47+
android:background="@drawable/shadow_bottom_content"/>
4848

4949
</LinearLayout>
5050

example/src/main/java/io/github/kbiakov/codeviewexample/ListingsActivity.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,13 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
2222

2323
// use chaining to build view
2424
codeView.highlightCode("js")
25-
//.setColorTheme(ColorTheme.SOLARIZED_LIGHT.withBgContent(myColor))
25+
.setColorTheme(ColorTheme.SOLARIZED_LIGHT)
2626
.setCodeContent(getString(R.string.listing_js));
2727

2828
// do not use chaining for built view
2929
// (you can, but follow it should be performed sequentially)
3030
codeView.setCodeContent(getString(R.string.listing_java));
31+
codeView.setColorTheme(ColorTheme.DEFAULT);
3132
codeView.highlightCode("java");
3233
}
3334
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
<io.github.kbiakov.codeview.CodeView
1010
android:id="@+id/code_view"
11-
android:layout_width="wrap_content"
11+
android:layout_width="match_parent"
1212
android:layout_height="wrap_content"/>
1313

1414
</RelativeLayout>

0 commit comments

Comments
 (0)