Skip to content

Commit 40f7891

Browse files
authored
Merge pull request #30 from Softwee/feature/bugfixes
Fixed tabs/spaces trimming, version updates & code cleanup
2 parents 3250bdf + 0c46245 commit 40f7891

File tree

9 files changed

+118
-113
lines changed

9 files changed

+118
-113
lines changed

build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
// Top-level build file where you can add configuration options common to all sub-projects/modules.
22

33
buildscript {
4-
ext.kotlin_version = '1.0.3'
4+
ext.kotlin_version = '1.1.2-5'
55
repositories {
66
jcenter()
77
}
88
dependencies {
9-
classpath 'com.android.tools.build:gradle:2.2.2'
9+
classpath 'com.android.tools.build:gradle:2.3.3'
1010
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
1111

1212
// NOTE: Do not place your application dependencies here; they belong

codeview/build.gradle

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ apply plugin: 'kotlin-android'
33

44
android {
55
compileSdkVersion 25
6-
buildToolsVersion "25.0.1"
6+
buildToolsVersion '25.0.3'
77

88
defaultConfig {
99
minSdkVersion 15
1010
targetSdkVersion 25
1111
versionCode 1
12-
versionName "1.0"
12+
versionName '1.0'
1313
}
1414
buildTypes {
1515
release {
@@ -25,8 +25,8 @@ android {
2525
dependencies {
2626
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
2727

28-
compile 'com.android.support:appcompat-v7:25.0.1'
29-
compile 'com.android.support:recyclerview-v7:25.0.1'
28+
compile 'com.android.support:appcompat-v7:25.3.1'
29+
compile 'com.android.support:recyclerview-v7:25.3.1'
3030
}
3131
repositories {
3232
mavenCentral()

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

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -30,24 +30,17 @@ class CodeView(context: Context, attrs: AttributeSet) : RelativeLayout(context,
3030
* Primary constructor.
3131
*/
3232
init {
33-
val isAnimateOnStart = visibility == VISIBLE && { ctx: Context, ats: AttributeSet ->
34-
val a = ctx.theme.obtainStyledAttributes(ats, R.styleable.CodeView, 0, 0)
35-
36-
try {
37-
a.getBoolean(R.styleable.CodeView_animateOnStart, true)
38-
} finally {
39-
a.recycle()
40-
}
41-
}(context, attrs)
42-
43-
alpha = if (isAnimateOnStart) 0f else Consts.ALPHA
44-
4533
inflate(context, R.layout.layout_code_view, this)
4634

47-
if (isAnimateOnStart)
35+
if (visibility == VISIBLE && isAnimateOnStart(context, attrs)) {
36+
alpha = Const.Alpha.Invisible
37+
4838
animate()
49-
.setDuration(Consts.DELAY * 5)
50-
.alpha(Consts.ALPHA)
39+
.setDuration(Const.DefaultDelay * 5)
40+
.alpha(Const.Alpha.Initial)
41+
} else {
42+
alpha = Const.Alpha.Initial
43+
}
5144

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

6962
animate()
70-
.setDuration(Consts.DELAY * 2)
63+
.setDuration(Const.DefaultDelay * 2)
7164
.alpha(.1f)
7265

7366
delayed {
@@ -121,7 +114,7 @@ class CodeView(context: Context, attrs: AttributeSet) : RelativeLayout(context,
121114
/**
122115
* View options accessor.
123116
*/
124-
fun getOptions(): Options? = getAdapter()?.options
117+
fun getOptions() = getAdapter()?.options
125118
fun getOptionsOrDefault() = getOptions() ?: Options(context)
126119

127120
/**
@@ -130,10 +123,8 @@ class CodeView(context: Context, attrs: AttributeSet) : RelativeLayout(context,
130123
* @param options Options
131124
*/
132125
fun updateOptions(options: Options) {
133-
if (getAdapter() == null)
134-
setOptions(options)
135-
else
136-
getAdapter()!!.options = options
126+
getAdapter() ?: setOptions(options)
127+
getAdapter()?.options = options
137128
}
138129

139130
// - Adapter
@@ -169,7 +160,7 @@ class CodeView(context: Context, attrs: AttributeSet) : RelativeLayout(context,
169160
*/
170161
fun setCode(code: String) {
171162
getAdapter() ?: prepare()
172-
getAdapter()!!.updateCode(code)
163+
getAdapter()?.updateCode(code)
173164
}
174165

175166
/**
@@ -185,7 +176,19 @@ class CodeView(context: Context, attrs: AttributeSet) : RelativeLayout(context,
185176
fun setCode(code: String, language: String) {
186177
val options = getOptionsOrDefault()
187178
updateOptions(options.withLanguage(language))
188-
getAdapter()!!.updateCode(code)
179+
getAdapter()?.updateCode(code)
180+
}
181+
182+
companion object {
183+
184+
private fun isAnimateOnStart(context: Context, attr: AttributeSet): Boolean {
185+
context.theme.obtainStyledAttributes(attr, R.styleable.CodeView, 0, 0).apply {
186+
val flag = getBoolean(R.styleable.CodeView_animateOnStart, false)
187+
recycle()
188+
return@isAnimateOnStart flag
189+
}
190+
return false
191+
}
189192
}
190193
}
191194

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

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,17 @@ import android.text.Spanned
88
import android.util.TypedValue
99
import java.io.BufferedReader
1010
import java.io.InputStreamReader
11+
import java.util.*
1112
import java.util.concurrent.Executors
1213

13-
object Consts {
14-
val ALPHA = 0.7F
15-
val DELAY = 250L
14+
object Const {
15+
val DefaultDelay = 250L
16+
17+
object Alpha {
18+
val Initial = 0.7f
19+
val Invisible = 0f
20+
val Visible = 1f
21+
}
1622
}
1723

1824
/**
@@ -22,7 +28,7 @@ object Consts {
2228
* @param dp Dip value
2329
* @return Converted to px value
2430
*/
25-
fun dpToPx(context: Context, dp: Int): Int =
31+
fun dpToPx(context: Context, dp: Int) =
2632
TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
2733
dp.toFloat(), context.resources.displayMetrics).toInt()
2834

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

51+
/**
52+
* Slice list by index.
53+
*
54+
* @param idx Index to slice
55+
* @return Pair of lists with head and tail
56+
*/
57+
fun <T> List<T>.slice(idx: Int) = Pair(this.subList(0, idx), this.subList(idx, this.lastIndex))
58+
4559
/**
4660
* Get HTML from string.
4761
*
4862
* @param content Source
4963
* @return Spanned HTML string
5064
*/
5165
@Suppress("deprecation")
52-
fun html(content: String): Spanned =
66+
fun html(content: String) =
5367
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N)
54-
Html.fromHtml(content, Html.FROM_HTML_MODE_LEGACY)
68+
Html.fromHtml(content.htmlSafe(), Html.FROM_HTML_MODE_LEGACY)
5569
else
56-
Html.fromHtml(content)
70+
Html.fromHtml(content.htmlSafe())
71+
72+
fun String.htmlSafe() = replace(" ", "&nbsp;")
5773

5874
object Thread {
5975
/**
@@ -80,15 +96,14 @@ object Thread {
8096
* @param body Operation body
8197
* @param delayMs Delay in m
8298
*/
83-
fun delayed(delayMs: Long = Consts.DELAY, body: () -> Unit) =
84-
Handler().postDelayed(body, delayMs)
99+
fun delayed(delayMs: Long = Const.DefaultDelay, body: () -> Unit) {
100+
Handler().postDelayed(body, delayMs)
101+
}
85102

86103
// - Extensions for block manipulations
87104

88105
fun (() -> Unit).ui(isUi: Boolean = true) {
89-
if (isUi) ui {
90-
this()
91-
} else this()
106+
if (isUi) ui(this) else this()
92107
}
93108
}
94109

@@ -113,15 +128,12 @@ object Files {
113128
var content = ""
114129

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

118133
BufferedReader(InputStreamReader(input, "UTF-8")).useLines {
119-
it.forEach { line ->
120-
content += line
121-
}
134+
content += it.reduce { acc, line -> acc + line }
122135
}
123136
}
124-
125137
return content
126138
}
127139
}

0 commit comments

Comments
 (0)