Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
ext.kotlin_version = '1.0.3'
ext.kotlin_version = '1.1.2-5'
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.2'
classpath 'com.android.tools.build:gradle:2.3.3'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

// NOTE: Do not place your application dependencies here; they belong
Expand Down
8 changes: 4 additions & 4 deletions codeview/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ apply plugin: 'kotlin-android'

android {
compileSdkVersion 25
buildToolsVersion "25.0.1"
buildToolsVersion '25.0.3'

defaultConfig {
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName "1.0"
versionName '1.0'
}
buildTypes {
release {
Expand All @@ -25,8 +25,8 @@ android {
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"

compile 'com.android.support:appcompat-v7:25.0.1'
compile 'com.android.support:recyclerview-v7:25.0.1'
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support:recyclerview-v7:25.3.1'
}
repositories {
mavenCentral()
Expand Down
49 changes: 26 additions & 23 deletions codeview/src/main/java/io/github/kbiakov/codeview/CodeView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,17 @@ class CodeView(context: Context, attrs: AttributeSet) : RelativeLayout(context,
* Primary constructor.
*/
init {
val isAnimateOnStart = visibility == VISIBLE && { ctx: Context, ats: AttributeSet ->
val a = ctx.theme.obtainStyledAttributes(ats, R.styleable.CodeView, 0, 0)

try {
a.getBoolean(R.styleable.CodeView_animateOnStart, true)
} finally {
a.recycle()
}
}(context, attrs)

alpha = if (isAnimateOnStart) 0f else Consts.ALPHA

inflate(context, R.layout.layout_code_view, this)

if (isAnimateOnStart)
if (visibility == VISIBLE && isAnimateOnStart(context, attrs)) {
alpha = Const.Alpha.Invisible

animate()
.setDuration(Consts.DELAY * 5)
.alpha(Consts.ALPHA)
.setDuration(Const.DefaultDelay * 5)
.alpha(Const.Alpha.Initial)
} else {
alpha = Const.Alpha.Initial
}

// TODO: add shadow color customization
vShadowRight = findViewById(R.id.v_shadow_right)
Expand All @@ -67,7 +60,7 @@ class CodeView(context: Context, attrs: AttributeSet) : RelativeLayout(context,
getAdapter()?.highlight {

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

delayed {
Expand Down Expand Up @@ -121,7 +114,7 @@ class CodeView(context: Context, attrs: AttributeSet) : RelativeLayout(context,
/**
* View options accessor.
*/
fun getOptions(): Options? = getAdapter()?.options
fun getOptions() = getAdapter()?.options
fun getOptionsOrDefault() = getOptions() ?: Options(context)

/**
Expand All @@ -130,10 +123,8 @@ class CodeView(context: Context, attrs: AttributeSet) : RelativeLayout(context,
* @param options Options
*/
fun updateOptions(options: Options) {
if (getAdapter() == null)
setOptions(options)
else
getAdapter()!!.options = options
getAdapter() ?: setOptions(options)
getAdapter()?.options = options
}

// - Adapter
Expand Down Expand Up @@ -169,7 +160,7 @@ class CodeView(context: Context, attrs: AttributeSet) : RelativeLayout(context,
*/
fun setCode(code: String) {
getAdapter() ?: prepare()
getAdapter()!!.updateCode(code)
getAdapter()?.updateCode(code)
}

/**
Expand All @@ -185,7 +176,19 @@ class CodeView(context: Context, attrs: AttributeSet) : RelativeLayout(context,
fun setCode(code: String, language: String) {
val options = getOptionsOrDefault()
updateOptions(options.withLanguage(language))
getAdapter()!!.updateCode(code)
getAdapter()?.updateCode(code)
}

companion object {

private fun isAnimateOnStart(context: Context, attr: AttributeSet): Boolean {
context.theme.obtainStyledAttributes(attr, R.styleable.CodeView, 0, 0).apply {
val flag = getBoolean(R.styleable.CodeView_animateOnStart, false)
recycle()
return@isAnimateOnStart flag
}
return false
}
}
}

Expand Down
46 changes: 29 additions & 17 deletions codeview/src/main/java/io/github/kbiakov/codeview/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,17 @@ import android.text.Spanned
import android.util.TypedValue
import java.io.BufferedReader
import java.io.InputStreamReader
import java.util.*
import java.util.concurrent.Executors

object Consts {
val ALPHA = 0.7F
val DELAY = 250L
object Const {
val DefaultDelay = 250L

object Alpha {
val Initial = 0.7f
val Invisible = 0f
val Visible = 1f
}
}

/**
Expand All @@ -22,7 +28,7 @@ object Consts {
* @param dp Dip value
* @return Converted to px value
*/
fun dpToPx(context: Context, dp: Int): Int =
fun dpToPx(context: Context, dp: Int) =
TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
dp.toFloat(), context.resources.displayMetrics).toInt()

Expand All @@ -42,18 +48,28 @@ fun spaceSplit(source: String) = source.split("\\s".toRegex())
*/
fun extractLines(source: String) = listOf(*source.split("\n").toTypedArray())

/**
* Slice list by index.
*
* @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))

/**
* Get HTML from string.
*
* @param content Source
* @return Spanned HTML string
*/
@Suppress("deprecation")
fun html(content: String): Spanned =
fun html(content: String) =
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N)
Html.fromHtml(content, Html.FROM_HTML_MODE_LEGACY)
Html.fromHtml(content.htmlSafe(), Html.FROM_HTML_MODE_LEGACY)
else
Html.fromHtml(content)
Html.fromHtml(content.htmlSafe())

fun String.htmlSafe() = replace(" ", "&nbsp;")

object Thread {
/**
Expand All @@ -80,15 +96,14 @@ object Thread {
* @param body Operation body
* @param delayMs Delay in m
*/
fun delayed(delayMs: Long = Consts.DELAY, body: () -> Unit) =
Handler().postDelayed(body, delayMs)
fun delayed(delayMs: Long = Const.DefaultDelay, body: () -> Unit) {
Handler().postDelayed(body, delayMs)
}

// - Extensions for block manipulations

fun (() -> Unit).ui(isUi: Boolean = true) {
if (isUi) ui {
this()
} else this()
if (isUi) ui(this) else this()
}
}

Expand All @@ -113,15 +128,12 @@ object Files {
var content = ""

ls(context, path).forEach { filename ->
val input = context.assets.open(path + '/' + filename)
val input = context.assets.open("$path/$filename")

BufferedReader(InputStreamReader(input, "UTF-8")).useLines {
it.forEach { line ->
content += line
}
content += it.reduce { acc, line -> acc + line }
}
}

return content
}
}
Loading