@@ -13,6 +13,7 @@ import android.view.ViewPropertyAnimator
1313import android.widget.RelativeLayout
1414import io.github.kbiakov.codeview.highlight.ColorTheme
1515import io.github.kbiakov.codeview.highlight.ColorThemeData
16+ import io.github.kbiakov.codeview.highlight.color
1617import 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 */
274307fun 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 */
279314fun ViewPropertyAnimator.didAnimated (handler : () -> Unit ) =
280315 setListener(object : AnimatorListenerAdapter () {
0 commit comments