@@ -41,6 +41,15 @@ class CodeView : RelativeLayout {
4141 private val vShadowBottomLine: View
4242 private val vShadowBottomContent: View
4343
44+ internal var colorTheme: ColorThemeData
45+ set(colorTheme) {
46+ field = colorTheme
47+ }
48+
49+ init {
50+ colorTheme = ColorTheme .SOLARIZED_LIGHT .with ()
51+ }
52+
4453 /* *
4554 * Core view to draw code by lines.
4655 */
@@ -57,8 +66,9 @@ class CodeView : RelativeLayout {
5766 */
5867 private var state: ViewState
5968 set(newState) {
60- if (newState == ViewState .PRESENTED )
69+ if (newState == ViewState .PRESENTED ) {
6170 hidePlaceholder()
71+ }
6272 field = newState
6373 }
6474
@@ -86,6 +96,8 @@ class CodeView : RelativeLayout {
8696 /* *
8797 * Code view state to control build flow.
8898 */
99+ // todo: remove states and tasks
100+ // todo: or sort task positions (first we need to set adapter, then other actions and call highlight task on finish)
89101 enum class ViewState {
90102 BUILD ,
91103 PREPARE ,
@@ -100,19 +112,20 @@ class CodeView : RelativeLayout {
100112 * @return Result of state check
101113 */
102114 fun isBuilding () = state == ViewState .BUILD
115+
103116 fun isPrepared () = state == ViewState .PREPARE
104117 fun isPresented () = state == ViewState .PRESENTED
105118
106119 /* *
107120 * Accessor/mutator to reduce frequently used actions.
108121 */
109- var adapter: AbstractCodeAdapter <* >
122+ var adapter: AbstractCodeAdapter <* >?
110123 get() {
111124 return rvCodeContent.adapter as AbstractCodeAdapter <* >
112125 }
113126 set(adapter) {
127+ rvCodeContent.adapter = adapter
114128 delayed { // prevent UI overhead & initialization inconsistency
115- rvCodeContent.adapter = adapter
116129 state = ViewState .PRESENTED
117130 }
118131 }
@@ -157,13 +170,15 @@ class CodeView : RelativeLayout {
157170 */
158171
159172 // default color theme provided by enum
160- fun setColorTheme (colorTheme : ColorTheme ) = addTask {
161- adapter.colorTheme = colorTheme.with ()
173+ fun colorTheme (colorTheme : ColorTheme ): CodeView {
174+ this .colorTheme = colorTheme.with ()
175+ return this
162176 }
163177
164178 // custom color theme provided by user
165- fun setColorTheme (colorTheme : ColorThemeData ) = addTask {
166- adapter.colorTheme = colorTheme
179+ fun colorTheme (colorTheme : ColorThemeData ): CodeView {
180+ this .colorTheme = colorTheme
181+ return this
167182 }
168183
169184 /* *
@@ -172,19 +187,19 @@ class CodeView : RelativeLayout {
172187 *
173188 * @param language Language to highlight
174189 */
175- fun highlightCode (language : String ) = addTask {
176- adapter.highlightCode(language ) {
177- refreshAnimated( )
190+ fun highlight (language : String? = null) {
191+ if (adapter == null ) {
192+ throw IllegalStateException ( " Please set adapter or use codeContent() before highlight() " )
178193 }
179- }
180194
181- /* *
182- * Highlight code with trying to classify by code snippet.
183- * It shows not highlighted code & then when classified refreshes view.
184- */
185- fun highlightCode () = addTask {
186- adapter.highlightCode {
187- refreshAnimated()
195+ adapter?.highlight(language) {
196+ animate().setDuration(Utils .DELAY * 2 )// * 2 to wait notifyDataSetChanged
197+ .alpha(.1f )
198+
199+ delayed {
200+ animate().alpha(1f )
201+ adapter?.notifyDataSetChanged()
202+ }
188203 }
189204 }
190205
@@ -194,15 +209,24 @@ class CodeView : RelativeLayout {
194209 *
195210 * @param listener Code line click listener
196211 */
197- fun setCodeListener (listener : OnCodeLineClickListener ) = addTask {
198- adapter.codeListener = listener
212+ fun codeListener (listener : OnCodeLineClickListener ): CodeView {
213+ if (adapter == null ) {
214+ throw IllegalStateException (" Please set adapter or use codeContent() before highlight()" )
215+ }
216+
217+ adapter?.codeListener = listener
218+ return this
199219 }
200220
201221 /* *
202222 * Remove code listener.
203223 */
204224 fun removeCodeListener () = addTask {
205- adapter.codeListener = null
225+ if (adapter == null ) {
226+ throw IllegalStateException (" Please set adapter or use codeContent() before highlight()" )
227+ }
228+
229+ adapter?.codeListener = null
206230 }
207231
208232 /* *
@@ -222,7 +246,7 @@ class CodeView : RelativeLayout {
222246 *
223247 * @param content Code content
224248 */
225- fun setCodeContent (content : String ) {
249+ fun codeContent (content : String ): CodeView {
226250 when (state) {
227251 ViewState .BUILD ->
228252 build(content)
@@ -233,6 +257,8 @@ class CodeView : RelativeLayout {
233257 ViewState .PRESENTED ->
234258 update(content)
235259 }
260+
261+ return this
236262 }
237263
238264 /* *
@@ -249,8 +275,9 @@ class CodeView : RelativeLayout {
249275 measurePlaceholder(linesCount)
250276 state = ViewState .PREPARE
251277
278+ rvCodeContent.adapter = CodeWithNotesAdapter (context, content, colorTheme)
279+
252280 delayed {
253- rvCodeContent.adapter = CodeWithNotesAdapter (context, content)
254281 processBuildTasks()
255282 setupShadows()
256283 hidePlaceholder()
@@ -266,7 +293,7 @@ class CodeView : RelativeLayout {
266293 private fun update (content : String ) {
267294 state = ViewState .PREPARE
268295 measurePlaceholder(extractLines(content).size)
269- adapter.updateCodeContent(content)
296+ adapter? .updateCodeContent(content)
270297 hidePlaceholder()
271298 state = ViewState .PRESENTED
272299 }
@@ -277,7 +304,7 @@ class CodeView : RelativeLayout {
277304 * Border shadows will shown if presented full code listing.
278305 * It helps user to see what part of content are scrolled & hidden.
279306 */
280- private fun setupShadows () = setShadowsVisible(! adapter.isFullShowing)
307+ private fun setupShadows () = setShadowsVisible(! adapter? .isFullShowing!! )
281308
282309 /* *
283310 * Placeholder fills space at start and stretched to marked up view size
@@ -287,15 +314,11 @@ class CodeView : RelativeLayout {
287314 */
288315 private fun measurePlaceholder (linesCount : Int ) {
289316 val lineHeight = dpToPx(context, 24 )
290- val topPadding = dpToPx(context, 8 )
291-
292- // double padding (top & bottom), one is enough for single line view
293- val padding = (if (linesCount > 1 ) 2 else 1 ) * topPadding
317+ val verticalPadding = dpToPx(context, 8 )
294318
295- val height = linesCount * lineHeight + padding
319+ val height = linesCount * lineHeight + verticalPadding
296320
297- vPlaceholder.layoutParams = LayoutParams (
298- LayoutParams .MATCH_PARENT , height)
321+ vPlaceholder.layoutParams = LayoutParams (LayoutParams .MATCH_PARENT , height)
299322
300323 vPlaceholder.alpha = 1f
301324 }
@@ -306,14 +329,6 @@ class CodeView : RelativeLayout {
306329 .setDuration(350 )
307330 .alpha(0f )
308331 .didAnimated { vPlaceholder.alpha = 0f }
309-
310- private fun refreshAnimated () = animate()
311- .setDuration(150 )
312- .alpha(.2f )
313- .didAnimated {
314- adapter.notifyDataSetChanged()
315- animate().alpha(1f )
316- }
317332}
318333
319334/* *
0 commit comments