11package io.github.kbiakov.codeview
22
33import android.content.Context
4+ import android.graphics.drawable.Drawable
5+ import android.graphics.drawable.GradientDrawable
46import android.support.v7.widget.LinearLayoutManager
57import android.support.v7.widget.RecyclerView
68import android.util.AttributeSet
@@ -10,6 +12,8 @@ import io.github.kbiakov.codeview.Thread.delayed
1012import io.github.kbiakov.codeview.adapters.AbstractCodeAdapter
1113import io.github.kbiakov.codeview.adapters.CodeWithNotesAdapter
1214import io.github.kbiakov.codeview.adapters.Options
15+ import io.github.kbiakov.codeview.highlight.ColorThemeData
16+ import io.github.kbiakov.codeview.highlight.color
1317
1418/* *
1519 * @class CodeView
@@ -21,7 +25,7 @@ import io.github.kbiakov.codeview.adapters.Options
2125class CodeView (context : Context , attrs : AttributeSet ) : RelativeLayout(context, attrs) {
2226
2327 private val vCodeList: RecyclerView
24- private val vShadows: List < View >
28+ private val vShadows: Map < ShadowPosition , View >
2529
2630 /* *
2731 * Primary constructor.
@@ -30,16 +34,15 @@ class CodeView(context: Context, attrs: AttributeSet) : RelativeLayout(context,
3034 inflate(context, R .layout.layout_code_view, this )
3135 checkStartAnimation(attrs)
3236
33- // TODO: add shadow color customization
34- vShadows = listOf (
35- R .id.v_shadow_right,
36- R .id.v_shadow_bottom_line,
37- R .id.v_shadow_bottom_content
38- ).map(this ::findViewById)
39-
4037 vCodeList = findViewById(R .id.rv_code_content) as RecyclerView
4138 vCodeList.layoutManager = LinearLayoutManager (context)
4239 vCodeList.isNestedScrollingEnabled = true
40+
41+ vShadows = mapOf (
42+ ShadowPosition .RightBorder to R .id.shadow_right_border,
43+ ShadowPosition .NumBottom to R .id.shadow_num_bottom,
44+ ShadowPosition .ContentBottom to R .id.shadow_content_bottom
45+ ).mapValues { findViewById(it.value) }
4346 }
4447
4548 private fun checkStartAnimation (attrs : AttributeSet ) {
@@ -81,11 +84,15 @@ class CodeView(context: Context, attrs: AttributeSet) : RelativeLayout(context,
8184 * Border shadows will shown if full listing presented.
8285 * It helps to see what part of code is scrolled & hidden.
8386 *
84- * @param isShadows Is shadows needed
87+ * @param isVisible Is shadows visible
8588 */
86- private fun setupShadows (isShadows : Boolean ) {
87- val visibility = if (isShadows) VISIBLE else GONE
88- vShadows.forEach { it.visibility = visibility }
89+ fun setupShadows (isVisible : Boolean ) {
90+ val visibility = if (isVisible) VISIBLE else GONE
91+ val theme = getOptionsOrDefault().theme
92+ vShadows.forEach { (pos, view) ->
93+ view.visibility = visibility
94+ view.setSafeBackground(pos.createShadow(theme))
95+ }
8996 }
9097
9198 // - Initialization
@@ -109,7 +116,6 @@ class CodeView(context: Context, attrs: AttributeSet) : RelativeLayout(context,
109116 */
110117 fun setAdapter (adapter : AbstractCodeAdapter <* >) {
111118 vCodeList.adapter = adapter
112- setupShadows(adapter.options.shadows)
113119 highlight()
114120 }
115121
@@ -129,12 +135,12 @@ class CodeView(context: Context, attrs: AttributeSet) : RelativeLayout(context,
129135 fun updateOptions (options : Options ) {
130136 getAdapter() ? : setOptions(options)
131137 getAdapter()?.options = options
138+ setupShadows(options.shadows)
132139 }
133140
134- fun updateOptions () : Options {
141+ fun updateOptions (body : Options .() -> Unit ) {
135142 val options = getOptions() ? : getOptionsOrDefault()
136- updateOptions(options)
137- return options
143+ updateOptions(options.apply (body))
138144 }
139145
140146 // - Adapter
@@ -199,6 +205,27 @@ class CodeView(context: Context, attrs: AttributeSet) : RelativeLayout(context,
199205 }
200206 return false
201207 }
208+
209+ private fun View.setSafeBackground (newBackground : Drawable ) {
210+ if (android.os.Build .VERSION .SDK_INT >= android.os.Build .VERSION_CODES .JELLY_BEAN ) {
211+ background = newBackground
212+ }
213+ }
214+ }
215+
216+ private enum class ShadowPosition {
217+ RightBorder ,
218+ NumBottom ,
219+ ContentBottom ;
220+
221+ fun createShadow (theme : ColorThemeData ) = when (this ) {
222+ RightBorder -> GradientDrawable .Orientation .LEFT_RIGHT to theme.bgContent
223+ NumBottom -> GradientDrawable .Orientation .TOP_BOTTOM to theme.bgNum
224+ ContentBottom -> GradientDrawable .Orientation .TOP_BOTTOM to theme.bgContent
225+ }.let {
226+ val colors = arrayOf(android.R .color.transparent, it.second)
227+ GradientDrawable (it.first, colors.map(Int ::color).toIntArray())
228+ }
202229 }
203230}
204231
0 commit comments