@@ -13,6 +13,7 @@ import android.view.ViewPropertyAnimator
13
13
import android.widget.RelativeLayout
14
14
import io.github.kbiakov.codeview.highlight.ColorTheme
15
15
import io.github.kbiakov.codeview.highlight.ColorThemeData
16
+ import io.github.kbiakov.codeview.highlight.color
16
17
import java.util.*
17
18
18
19
/* *
@@ -135,6 +136,8 @@ class CodeView : RelativeLayout {
135
136
/* *
136
137
* Specify color theme: syntax colors (need to highlighting) & related to
137
138
* code view (numeration color & background, content backgrounds).
139
+ *
140
+ * @param colorTheme Default or custom color theme
138
141
*/
139
142
140
143
// default color theme provided by enum
@@ -150,6 +153,8 @@ class CodeView : RelativeLayout {
150
153
/* *
151
154
* Highlight code by defined programming language.
152
155
* It holds the placeholder on the view until code is highlighted.
156
+ *
157
+ * @param language Language to highlight
153
158
*/
154
159
fun highlightCode (language : String ) = addTask {
155
160
adapter.highlightCode(language)
@@ -166,15 +171,19 @@ class CodeView : RelativeLayout {
166
171
}
167
172
168
173
/* *
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
171
178
*/
172
179
fun setCodeListener (listener : OnCodeLineClickListener ) = addTask {
173
180
adapter.codeListener = listener
174
181
}
175
182
176
183
/* *
177
184
* Control shadows visibility to provide more sensitive UI.
185
+ *
186
+ * @param isVisible Shadows visibility
178
187
*/
179
188
fun setShadowsVisible (isVisible : Boolean = true) = addTask {
180
189
val visibility = if (isVisible) View .VISIBLE else GONE
@@ -185,17 +194,19 @@ class CodeView : RelativeLayout {
185
194
186
195
/* *
187
196
* Update code content if view was built or, finally, build code view.
197
+ *
198
+ * @param content Code content
188
199
*/
189
200
fun setCodeContent (content : String ) {
190
201
when (state) {
191
202
ViewState .BUILD ->
192
203
build(content)
193
204
ViewState .PREPARE ->
194
205
Thread .delayed {
195
- adapter.updateCodeContent (content)
206
+ update (content)
196
207
}
197
208
ViewState .PRESENTED ->
198
- adapter.updateCodeContent (content)
209
+ update (content)
199
210
}
200
211
}
201
212
@@ -222,6 +233,19 @@ class CodeView : RelativeLayout {
222
233
}
223
234
}
224
235
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
+
225
249
// - Setup actions
226
250
227
251
/* *
@@ -232,15 +256,22 @@ class CodeView : RelativeLayout {
232
256
233
257
/* *
234
258
* 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
236
262
*/
237
263
private fun measurePlaceholder (linesCount : Int ) {
238
264
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
241
271
242
272
vPlaceholder.layoutParams = RelativeLayout .LayoutParams (
243
273
RelativeLayout .LayoutParams .MATCH_PARENT , height)
274
+ vPlaceholder.visibility = View .VISIBLE
244
275
}
245
276
246
277
// - Animations
@@ -270,11 +301,15 @@ interface OnCodeLineClickListener {
270
301
271
302
/* *
272
303
* Extension for delayed block call.
304
+ *
305
+ * @param body Operation body
273
306
*/
274
307
fun Thread.delayed (body : () -> Unit ) = Handler ().postDelayed(body, 150 )
275
308
276
309
/* *
277
310
* More readable form for animation listener (hi, iOS & Cocoa Touch!).
311
+ *
312
+ * @param handler Handler body
278
313
*/
279
314
fun ViewPropertyAnimator.didAnimated (handler : () -> Unit ) =
280
315
setListener(object : AnimatorListenerAdapter () {
0 commit comments