Skip to content

Commit 7d27591

Browse files
committed
Fixed shadow color
1 parent a25d487 commit 7d27591

File tree

8 files changed

+163
-159
lines changed

8 files changed

+163
-159
lines changed

codeview/src/main/java/io/github/kbiakov/codeview/CodeView.kt

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package io.github.kbiakov.codeview
22

33
import android.content.Context
4+
import android.graphics.drawable.Drawable
5+
import android.graphics.drawable.GradientDrawable
46
import android.support.v7.widget.LinearLayoutManager
57
import android.support.v7.widget.RecyclerView
68
import android.util.AttributeSet
@@ -10,6 +12,8 @@ import io.github.kbiakov.codeview.Thread.delayed
1012
import io.github.kbiakov.codeview.adapters.AbstractCodeAdapter
1113
import io.github.kbiakov.codeview.adapters.CodeWithNotesAdapter
1214
import 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
2125
class 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

codeview/src/main/java/io/github/kbiakov/codeview/highlight/CodeHighlighter.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,8 @@ enum class Font {
222222

223223
// - Helpers
224224

225+
const val transparent = "#00000000"
226+
225227
/**
226228
* @return Converted hex int to color by adding alpha-channel
227229
*/
@@ -271,7 +273,6 @@ fun String.withFontParams(color: String?): String {
271273
if (idx != indexOf("\n")) // if not replaced only once (for multiline tag coverage)
272274
parametrizedString.append(substring(idx).inFontTag(color))
273275
}
274-
275276
return parametrizedString.toString()
276277
}
277278

codeview/src/main/res/drawable/shadow_bottom_content.xml

Lines changed: 0 additions & 10 deletions
This file was deleted.

codeview/src/main/res/drawable/shadow_bottom_line.xml

Lines changed: 0 additions & 10 deletions
This file was deleted.

codeview/src/main/res/drawable/shadow_right.xml

Lines changed: 0 additions & 10 deletions
This file was deleted.

codeview/src/main/res/layout/layout_code_view.xml

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,31 +20,26 @@
2020
</io.github.kbiakov.codeview.views.BidirectionalScrollView>
2121

2222
<View
23-
android:id="@+id/v_shadow_right"
24-
android:layout_width="24dp"
23+
android:id="@+id/shadow_right_border"
24+
android:layout_width="@dimen/shadow_width"
2525
android:layout_height="match_parent"
26-
android:layout_alignParentRight="true"
27-
android:background="@drawable/shadow_right"
28-
android:visibility="gone"/>
26+
android:layout_alignParentRight="true"/>
2927

3028
<LinearLayout
3129
android:layout_width="match_parent"
32-
android:layout_height="16dp"
30+
android:layout_height="@dimen/shadow_height"
3331
android:layout_alignParentBottom="true"
34-
android:orientation="horizontal"
35-
android:visibility="gone">
32+
android:orientation="horizontal">
3633

3734
<View
38-
android:id="@+id/v_shadow_bottom_line"
39-
android:layout_width="24dp"
40-
android:layout_height="match_parent"
41-
android:background="@drawable/shadow_bottom_line"/>
35+
android:id="@+id/shadow_num_bottom"
36+
android:layout_width="@dimen/shadow_width"
37+
android:layout_height="match_parent"/>
4238

4339
<View
44-
android:id="@+id/v_shadow_bottom_content"
40+
android:id="@+id/shadow_content_bottom"
4541
android:layout_width="match_parent"
46-
android:layout_height="match_parent"
47-
android:background="@drawable/shadow_bottom_content"/>
42+
android:layout_height="match_parent"/>
4843

4944
</LinearLayout>
5045

codeview/src/main/res/values/dimens.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@
44
<dimen name="line_height">24dp</dimen>
55
<dimen name="line_border_height">4dp</dimen>
66
<dimen name="line_text_size">12sp</dimen>
7+
<dimen name="shadow_width">24dp</dimen>
8+
<dimen name="shadow_height">16dp</dimen>
79
</resources>

0 commit comments

Comments
 (0)