@@ -3,17 +3,18 @@ package io.github.kbiakov.codeview
33import android.animation.Animator
44import android.animation.AnimatorListenerAdapter
55import android.content.Context
6- import android.os.Handler
76import android.support.v7.widget.LinearLayoutManager
87import android.support.v7.widget.RecyclerView
98import android.util.AttributeSet
109import android.view.LayoutInflater
1110import android.view.View
1211import android.view.ViewPropertyAnimator
1312import android.widget.RelativeLayout
13+ import io.github.kbiakov.codeview.adapters.AbstractCodeAdapter
14+ import io.github.kbiakov.codeview.Thread.delayed
15+ import io.github.kbiakov.codeview.adapters.CodeWithNotesAdapter
1416import io.github.kbiakov.codeview.highlight.ColorTheme
1517import io.github.kbiakov.codeview.highlight.ColorThemeData
16- import io.github.kbiakov.codeview.highlight.color
1718import java.util.*
1819
1920/* *
@@ -55,7 +56,15 @@ class CodeView : RelativeLayout {
5556 * (and awaiting for build) or view was built & code is presented.
5657 */
5758 private var state: ViewState
59+ set(newState) {
60+ if (newState == ViewState .PRESENTED )
61+ hidePlaceholder()
62+ field = newState
63+ }
5864
65+ /* *
66+ * Default constructor.
67+ */
5968 constructor (context: Context , attrs: AttributeSet ) : super (context, attrs) {
6069 val inflater = context.getSystemService(Context .LAYOUT_INFLATER_SERVICE ) as LayoutInflater
6170 inflater.inflate(R .layout.layout_code_view, this , true )
@@ -69,13 +78,13 @@ class CodeView : RelativeLayout {
6978 rvCodeContent.layoutManager = LinearLayoutManager (context)
7079 rvCodeContent.isNestedScrollingEnabled = true
7180
72- tasks = LinkedList ()
73-
7481 state = ViewState .BUILD
82+
83+ tasks = LinkedList ()
7584 }
7685
7786 /* *
78- * Code view states .
87+ * Code view state to control build flow .
7988 */
8089 enum class ViewState {
8190 BUILD ,
@@ -84,22 +93,28 @@ class CodeView : RelativeLayout {
8493 }
8594
8695 /* *
87- * Public getter for accessing view state.
88- * It may be useful if code view state is unknown.
89- * If code view was built it is not safe to use operations chaining.
96+ * Public getters for checking view state.
97+ * May be useful when code view state is unknown.
98+ * If view was built it is unsafe to use operations chaining.
99+ *
100+ * @return Result of state check
90101 */
91- fun getState () = state
102+ fun isBuilding () = state == ViewState .BUILD
103+ fun isPrepared () = state == ViewState .PREPARE
104+ fun isPresented () = state == ViewState .PRESENTED
92105
93106 /* *
94107 * Accessor/mutator to reduce frequently used actions.
95108 */
96- var adapter: CodeContentAdapter
109+ var adapter: AbstractCodeAdapter < * >
97110 get() {
98- return rvCodeContent.adapter as CodeContentAdapter
111+ return rvCodeContent.adapter as AbstractCodeAdapter < * >
99112 }
100113 set(adapter) {
101- rvCodeContent.adapter = adapter
102- state = ViewState .PRESENTED
114+ delayed { // prevent UI overhead & initialization inconsistency
115+ rvCodeContent.adapter = adapter
116+ state = ViewState .PRESENTED
117+ }
103118 }
104119
105120 // - Build processor
@@ -116,7 +131,7 @@ class CodeView : RelativeLayout {
116131 ViewState .BUILD ->
117132 tasks.add(task)
118133 ViewState .PREPARE ->
119- Thread . delayed(task)
134+ delayed(body = task)
120135 ViewState .PRESENTED ->
121136 task()
122137 }
@@ -180,6 +195,13 @@ class CodeView : RelativeLayout {
180195 adapter.codeListener = listener
181196 }
182197
198+ /* *
199+ * Remove code listener.
200+ */
201+ fun removeCodeListener () = addTask {
202+ adapter.codeListener = null
203+ }
204+
183205 /* *
184206 * Control shadows visibility to provide more sensitive UI.
185207 *
@@ -202,7 +224,7 @@ class CodeView : RelativeLayout {
202224 ViewState .BUILD ->
203225 build(content)
204226 ViewState .PREPARE ->
205- Thread . delayed {
227+ delayed {
206228 update(content)
207229 }
208230 ViewState .PRESENTED ->
@@ -224,8 +246,8 @@ class CodeView : RelativeLayout {
224246 measurePlaceholder(linesCount)
225247 state = ViewState .PREPARE
226248
227- Thread . delayed {
228- rvCodeContent.adapter = CodeContentAdapter (context, content)
249+ delayed {
250+ rvCodeContent.adapter = CodeWithNotesAdapter (context, content)
229251 processBuildTasks()
230252 setupShadows()
231253 hidePlaceholder()
@@ -264,13 +286,13 @@ class CodeView : RelativeLayout {
264286 val lineHeight = dpToPx(context, 24 )
265287 val topPadding = dpToPx(context, 8 )
266288
267- // double padding (top & bottom) for big view , one is enough for small
289+ // double padding (top & bottom), one is enough for single line view
268290 val padding = (if (linesCount > 1 ) 2 else 1 ) * topPadding
269291
270292 val height = linesCount * lineHeight + padding
271293
272- vPlaceholder.layoutParams = RelativeLayout . LayoutParams (
273- RelativeLayout . LayoutParams .MATCH_PARENT , height)
294+ vPlaceholder.layoutParams = LayoutParams (
295+ LayoutParams .MATCH_PARENT , height)
274296 vPlaceholder.visibility = View .VISIBLE
275297 }
276298
@@ -296,16 +318,9 @@ class CodeView : RelativeLayout {
296318 * Provides listener to code line clicks.
297319 */
298320interface OnCodeLineClickListener {
299- fun onCodeLineClicked (n : Int )
321+ fun onCodeLineClicked (n : Int , line : String )
300322}
301323
302- /* *
303- * Extension for delayed block call.
304- *
305- * @param body Operation body
306- */
307- fun Thread.delayed (body : () -> Unit ) = Handler ().postDelayed(body, 150 )
308-
309324/* *
310325 * More readable form for animation listener (hi, iOS & Cocoa Touch!).
311326 *
0 commit comments