@@ -35,7 +35,6 @@ import java.util.*
3535 */
3636class CodeView : RelativeLayout {
3737
38- private val vRoot: View
3938 private val vPlaceholder: View
4039 private val vShadowRight: View
4140 private val vShadowBottomLine: View
@@ -61,7 +60,6 @@ class CodeView : RelativeLayout {
6160 val inflater = context.getSystemService(Context .LAYOUT_INFLATER_SERVICE ) as LayoutInflater
6261 inflater.inflate(R .layout.layout_code_view, this , true )
6362
64- vRoot = findViewById(R .id.v_root)
6563 vPlaceholder = findViewById(R .id.v_placeholder)
6664 vShadowRight = findViewById(R .id.v_shadow_right)
6765 vShadowBottomLine = findViewById(R .id.v_shadow_bottom_line)
@@ -145,13 +143,11 @@ class CodeView : RelativeLayout {
145143 // default color theme provided by enum
146144 fun setColorTheme (colorTheme : ColorTheme ) = addTask {
147145 adapter.colorTheme = colorTheme.with ()
148- fillBackground(colorTheme.bgContent)
149146 }
150147
151148 // custom color theme provided by user
152149 fun setColorTheme (colorTheme : ColorThemeData ) = addTask {
153150 adapter.colorTheme = colorTheme
154- fillBackground(colorTheme.bgContent)
155151 }
156152
157153 /* *
@@ -207,10 +203,10 @@ class CodeView : RelativeLayout {
207203 build(content)
208204 ViewState .PREPARE ->
209205 Thread .delayed {
210- adapter.updateCodeContent (content)
206+ update (content)
211207 }
212208 ViewState .PRESENTED ->
213- adapter.updateCodeContent (content)
209+ update (content)
214210 }
215211 }
216212
@@ -231,13 +227,25 @@ class CodeView : RelativeLayout {
231227 Thread .delayed {
232228 rvCodeContent.adapter = CodeContentAdapter (context, content)
233229 processBuildTasks()
234- // fillBackground()
235230 setupShadows()
236231 hidePlaceholder()
237232 state = ViewState .PRESENTED
238233 }
239234 }
240235
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+
241249 // - Setup actions
242250
243251 /* *
@@ -246,27 +254,24 @@ class CodeView : RelativeLayout {
246254 */
247255 private fun setupShadows () = setShadowsVisible(! adapter.isFullShowing)
248256
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-
257257 /* *
258258 * Placeholder fills space at start and stretched to marked up view size
259- * (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.
260260 *
261261 * @param linesCount Count of lines to measure space for placeholder
262262 */
263263 private fun measurePlaceholder (linesCount : Int ) {
264264 val lineHeight = dpToPx(context, 24 )
265- val topMargin = dpToPx(context, 8 )
266- 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
267271
268272 vPlaceholder.layoutParams = RelativeLayout .LayoutParams (
269273 RelativeLayout .LayoutParams .MATCH_PARENT , height)
274+ vPlaceholder.visibility = View .VISIBLE
270275 }
271276
272277 // - Animations
0 commit comments