Skip to content

Commit

Permalink
Fixed broken code highlighter for space trimming, code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
kbiakov committed Aug 27, 2017
1 parent e8db973 commit 7f47e38
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 31 deletions.
37 changes: 17 additions & 20 deletions codeview/src/main/java/io/github/kbiakov/codeview/CodeView.kt
Expand Up @@ -20,18 +20,29 @@ import io.github.kbiakov.codeview.adapters.Options
*/
class CodeView(context: Context, attrs: AttributeSet) : RelativeLayout(context, attrs) {

private val vShadowRight: View
private val vShadowBottomLine: View
private val vShadowBottomContent: View

private val vCodeList: RecyclerView
private val vShadows: List<View>

/**
* Primary constructor.
*/
init {
inflate(context, R.layout.layout_code_view, this)
checkInitialAnimation(attrs)

// TODO: add shadow color customization
vShadows = listOf(
R.id.v_shadow_right,
R.id.v_shadow_bottom_line,
R.id.v_shadow_bottom_content
).map(this::findViewById)

vCodeList = findViewById(R.id.rv_code_content) as RecyclerView
vCodeList.layoutManager = LinearLayoutManager(context)
vCodeList.isNestedScrollingEnabled = true
}

private fun checkInitialAnimation(attrs: AttributeSet) {
if (visibility == VISIBLE && isAnimateOnStart(context, attrs)) {
alpha = Const.Alpha.Invisible

Expand All @@ -41,15 +52,6 @@ class CodeView(context: Context, attrs: AttributeSet) : RelativeLayout(context,
} else {
alpha = Const.Alpha.Initial
}

// TODO: add shadow color customization
vShadowRight = findViewById(R.id.v_shadow_right)
vShadowBottomLine = findViewById(R.id.v_shadow_bottom_line)
vShadowBottomContent = findViewById(R.id.v_shadow_bottom_content)

vCodeList = findViewById(R.id.rv_code_content) as RecyclerView
vCodeList.layoutManager = LinearLayoutManager(context)
vCodeList.isNestedScrollingEnabled = true
}

/**
Expand All @@ -58,13 +60,11 @@ class CodeView(context: Context, attrs: AttributeSet) : RelativeLayout(context,
*/
private fun highlight() {
getAdapter()?.highlight {

animate()
.setDuration(Const.DefaultDelay * 2)
.alpha(.1f)

delayed {
animate().alpha(1f)
animate().alpha(Const.Alpha.Visible)
getAdapter()?.notifyDataSetChanged()
}
}
Expand All @@ -78,10 +78,7 @@ class CodeView(context: Context, attrs: AttributeSet) : RelativeLayout(context,
*/
private fun setupShadows(isShadows: Boolean) {
val visibility = if (isShadows) VISIBLE else GONE

vShadowRight.visibility = visibility
vShadowBottomLine.visibility = visibility
vShadowBottomContent.visibility = visibility
vShadows.forEach { it.visibility = visibility }
}

// - Initialization
Expand Down
27 changes: 19 additions & 8 deletions codeview/src/main/java/io/github/kbiakov/codeview/Utils.kt
Expand Up @@ -4,12 +4,15 @@ import android.content.Context
import android.os.Handler
import android.os.Looper
import android.text.Html
import android.text.SpannableString
import android.text.Spanned
import android.text.TextUtils
import android.util.Log
import android.util.TypedValue
import java.io.BufferedReader
import java.io.InputStreamReader
import java.util.*
import java.util.concurrent.Executors
import java.util.stream.IntStream

object Const {
val DefaultDelay = 250L
Expand Down Expand Up @@ -54,7 +57,7 @@ fun extractLines(source: String) = listOf(*source.split("\n").toTypedArray())
* @param idx Index to slice
* @return Pair of lists with head and tail
*/
fun <T> List<T>.slice(idx: Int) = Pair(this.subList(0, idx), this.subList(idx, this.lastIndex))
fun <T> List<T>.slice(idx: Int) = Pair(subList(0, idx), subList(idx, lastIndex))

/**
* Get HTML from string.
Expand All @@ -63,13 +66,21 @@ fun <T> List<T>.slice(idx: Int) = Pair(this.subList(0, idx), this.subList(idx, t
* @return Spanned HTML string
*/
@Suppress("deprecation")
fun html(content: String) =
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N)
Html.fromHtml(content.htmlSafe(), Html.FROM_HTML_MODE_LEGACY)
else
Html.fromHtml(content.htmlSafe())
fun html(content: String): Spanned {
val spanned = if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N)
Html.fromHtml(content, Html.FROM_HTML_MODE_LEGACY)
else
Html.fromHtml(content)
val spaces = content.startSpacesForTaggedString()
return SpannableString(TextUtils.concat(spaces, spanned))
}

fun String.htmlSafe() = replace(" ", "&nbsp;")
private fun String.startSpacesForTaggedString(): String {
val startIdx = indexOf('>') + 1
val escaped = substring(startIdx)
val count = escaped.indexOf(escaped.trim())
return " ".repeat(count)
}

object Thread {
/**
Expand Down
Expand Up @@ -146,7 +146,6 @@ abstract class AbstractCodeAdapter<T> : RecyclerView.Adapter<AbstractCodeAdapter
private fun highlighting(language: String, onReady: () -> Unit) {
// TODO: highlight by 10 lines
val code = CodeHighlighter.highlight(language, options.code, options.theme)

updateContent(code, onReady)
}

Expand Down
Expand Up @@ -261,7 +261,7 @@ fun String.withFontParams(color: String?): String {

// put tag on the borders (end & start of line, ..., end of tag)
do { // until closing tag is reached
val part = substring(idx..newIdx - 1).inFontTag(color).plus("\n")
val part = substring(idx .. newIdx - 1).inFontTag(color).plus("\n")
parametrizedString.append(part)

idx = newIdx + 1
Expand Down
2 changes: 1 addition & 1 deletion codeview/src/main/res/values/dimens.xml
Expand Up @@ -3,4 +3,4 @@
<dimen name="line_num_width">32dp</dimen>
<dimen name="line_height">24dp</dimen>
<dimen name="line_text_size">12sp</dimen>
</resources>
</resources>

0 comments on commit 7f47e38

Please sign in to comment.