Skip to content

Commit a5201e5

Browse files
committed
Updated versions, code cleanings
1 parent 3250bdf commit a5201e5

8 files changed

Lines changed: 65 additions & 61 deletions

File tree

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: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,19 @@ import android.content.Context
44
import android.os.Handler
55
import android.os.Looper
66
import android.text.Html
7-
import android.text.Spanned
87
import android.util.TypedValue
98
import java.io.BufferedReader
109
import java.io.InputStreamReader
1110
import java.util.concurrent.Executors
1211

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

1822
/**
@@ -22,7 +26,7 @@ object Consts {
2226
* @param dp Dip value
2327
* @return Converted to px value
2428
*/
25-
fun dpToPx(context: Context, dp: Int): Int =
29+
fun dpToPx(context: Context, dp: Int) =
2630
TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
2731
dp.toFloat(), context.resources.displayMetrics).toInt()
2832

@@ -49,7 +53,7 @@ fun extractLines(source: String) = listOf(*source.split("\n").toTypedArray())
4953
* @return Spanned HTML string
5054
*/
5155
@Suppress("deprecation")
52-
fun html(content: String): Spanned =
56+
fun html(content: String) =
5357
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N)
5458
Html.fromHtml(content, Html.FROM_HTML_MODE_LEGACY)
5559
else
@@ -80,15 +84,14 @@ object Thread {
8084
* @param body Operation body
8185
* @param delayMs Delay in m
8286
*/
83-
fun delayed(delayMs: Long = Consts.DELAY, body: () -> Unit) =
84-
Handler().postDelayed(body, delayMs)
87+
fun delayed(delayMs: Long = Const.DefaultDelay, body: () -> Unit) {
88+
Handler().postDelayed(body, delayMs)
89+
}
8590

8691
// - Extensions for block manipulations
8792

8893
fun (() -> Unit).ui(isUi: Boolean = true) {
89-
if (isUi) ui {
90-
this()
91-
} else this()
94+
if (isUi) ui(this) else this()
9295
}
9396
}
9497

@@ -113,15 +116,12 @@ object Files {
113116
var content = ""
114117

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

118121
BufferedReader(InputStreamReader(input, "UTF-8")).useLines {
119-
it.forEach { line ->
120-
content += line
121-
}
122+
content += it.reduce { acc, line -> acc + line }
122123
}
123124
}
124-
125125
return content
126126
}
127127
}

example/build.gradle

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ apply plugin: 'com.android.application'
22
apply plugin: 'kotlin-android'
33

44
android {
5-
compileSdkVersion 24
6-
buildToolsVersion "24.0.2"
5+
compileSdkVersion 25
6+
buildToolsVersion '25.0.3'
77

88
defaultConfig {
9-
applicationId "io.github.kbiakov.codeviewexample"
9+
applicationId 'io.github.kbiakov.codeviewexample'
1010
minSdkVersion 15
11-
targetSdkVersion 24
11+
targetSdkVersion 25
1212
versionCode 1
13-
versionName "1.0"
13+
versionName '1.0'
1414
}
1515
buildTypes {
1616
release {
@@ -24,7 +24,7 @@ android {
2424
}
2525

2626
dependencies {
27-
compile 'com.android.support:appcompat-v7:24.2.0'
27+
compile 'com.android.support:appcompat-v7:25.3.1'
2828
compile project(path: ':codeview')
2929
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
3030
}

example/src/main/java/io/github/kbiakov/codeviewexample/ListingsActivity.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
2424

2525
final CodeView codeView = (CodeView) findViewById(R.id.code_view);
2626

27-
/**
27+
/*
2828
* 1: set code content
2929
*/
3030

@@ -34,7 +34,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
3434
// specify language for code listing
3535
codeView.setCode(getString(R.string.listing_py), "py");
3636

37-
/**
37+
/*
3838
* 2: working with options
3939
*/
4040

@@ -66,7 +66,7 @@ public void onCodeLineClicked(int n, @NotNull String line) {
6666
}
6767
}));
6868

69-
/**
69+
/*
7070
* 3: color themes
7171
*/
7272

@@ -79,7 +79,7 @@ public void onCodeLineClicked(int n, @NotNull String line) {
7979

8080
codeView.getOptions().setTheme(myTheme);
8181

82-
/**
82+
/*
8383
* 4: custom adapter with footer views
8484
*/
8585

@@ -94,7 +94,7 @@ public void onCodeLineClicked(int n, @NotNull String line) {
9494
}
9595
});
9696

97-
/**
97+
/*
9898
* 5: diff adapter with footer views
9999
*/
100100

@@ -107,8 +107,7 @@ public void onCodeLineClicked(int n, @NotNull String line) {
107107
diffsAdapter.addFooterEntity(16, new DiffModel(getString(R.string.py_addition_16), true));
108108
diffsAdapter.addFooterEntity(11, new DiffModel(getString(R.string.py_deletion_11), false));
109109

110-
111-
/**
110+
/*
112111
* 6: shortcut adapter with footer views
113112
*/
114113

example/src/main/res/layout/activity_listings.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<RelativeLayout
33
xmlns:android="http://schemas.android.com/apk/res/android"
4+
xmlns:app="http://schemas.android.com/apk/res-auto"
45
xmlns:tools="http://schemas.android.com/tools"
56
android:layout_width="match_parent"
67
android:layout_height="match_parent"
@@ -10,6 +11,7 @@
1011
<io.github.kbiakov.codeview.CodeView
1112
android:id="@+id/code_view"
1213
android:layout_width="match_parent"
13-
android:layout_height="wrap_content"/>
14+
android:layout_height="wrap_content"
15+
app:animateOnStart="true"/>
1416

1517
</RelativeLayout>
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#Wed Aug 17 15:40:39 MSK 2016
1+
#Sat Aug 26 20:57:29 MSK 2017
22
distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip

0 commit comments

Comments
 (0)