diff --git a/.travis.yml b/.travis.yml
index badb566..f5cfa4d 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -10,8 +10,8 @@ android:
- tools # to get the new `repository-11.xml`
- tools # to install latest Android SDK tools
- platform-tools
- - build-tools-25.0.3
- - android-25
+ - build-tools-28.0.3
+ - android-28
- extra-android-m2repository # Design Support library
- android-21
- sys-img-armeabi-v7a-android-21 # system image (emulator)
@@ -24,7 +24,7 @@ env:
before_script:
# Automatically accept all SDK licences
- - echo yes | android update sdk --no-ui --all --filter build-tools-24.0.2,android-24,extra-android-m2repository
+ - echo yes | android update sdk --no-ui --all --filter build-tools-28.0.3,android-28,extra-android-m2repository
# Emulator Management: create, start & wait
- echo no | android create avd --force -n test -t android-21 --abi armeabi-v7a
- emulator -avd test -no-skin -no-audio -no-window &
diff --git a/README.md b/README.md
index 877a15c..23995c1 100644
--- a/README.md
+++ b/README.md
@@ -1,8 +1,8 @@
# CodeView (Android)
[](https://android-arsenal.com/details/1/4216)
-[](https://jitpack.io/#kbiakov/CodeView-android)
-[](https://travis-ci.org/kbiakov/CodeView-android)
+[](https://jitpack.io/#kbiakov/CodeView-Android)
+[](https://travis-ci.org/kbiakov/CodeView-Android)
CodeView helps to show code content with syntax highlighting in native way.
@@ -29,7 +29,7 @@ allprojects {
Add the dependency:
```groovy
-compile 'com.github.kbiakov:CodeView-android:1.3.1'
+compile 'com.github.kbiakov:CodeView-android:1.3.2'
```
## Usage
diff --git a/build.gradle b/build.gradle
index 1f9ae80..37ef2a4 100644
--- a/build.gradle
+++ b/build.gradle
@@ -1,22 +1,26 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
- ext.kotlin_version = '1.1.2-5'
+ ext.kotlinVersion = '1.3.11'
+ ext.minSdk = 15
+ ext.compileSdk = 28
+ ext.buildTools = '28.0.3'
+ ext.supportLibrary = '28.0.0'
+
repositories {
jcenter()
+ google()
}
dependencies {
- 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
- // in the individual module build.gradle files
+ classpath 'com.android.tools.build:gradle:3.3.0'
+ classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
}
}
allprojects {
repositories {
jcenter()
+ google()
}
}
diff --git a/codeview/build.gradle b/codeview/build.gradle
index ad2d870..c460859 100644
--- a/codeview/build.gradle
+++ b/codeview/build.gradle
@@ -2,14 +2,14 @@ apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
android {
- compileSdkVersion 25
- buildToolsVersion '25.0.3'
+ compileSdkVersion compileSdk
+ buildToolsVersion buildTools
defaultConfig {
- minSdkVersion 15
- targetSdkVersion 25
+ minSdkVersion minSdk
+ targetSdkVersion compileSdk
versionCode 1
- versionName '1.3.0'
+ versionName '1.3.2'
}
buildTypes {
release {
@@ -23,8 +23,7 @@ android {
}
dependencies {
- compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
-
- compile 'com.android.support:appcompat-v7:25.3.1'
- compile 'com.android.support:recyclerview-v7:25.3.1'
+ implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"
+ implementation "com.android.support:appcompat-v7:$supportLibrary"
+ implementation "com.android.support:recyclerview-v7:$supportLibrary"
}
diff --git a/codeview/src/main/java/io/github/kbiakov/codeview/CodeView.kt b/codeview/src/main/java/io/github/kbiakov/codeview/CodeView.kt
index 0d3d4e2..f8f6d0c 100644
--- a/codeview/src/main/java/io/github/kbiakov/codeview/CodeView.kt
+++ b/codeview/src/main/java/io/github/kbiakov/codeview/CodeView.kt
@@ -22,7 +22,11 @@ import io.github.kbiakov.codeview.highlight.color
*
* @author Kirill Biakov
*/
-class CodeView(context: Context, attrs: AttributeSet) : RelativeLayout(context, attrs) {
+class CodeView @JvmOverloads constructor(
+ context: Context,
+ attrs: AttributeSet? = null,
+ defStyleAttr: Int = 0
+) : RelativeLayout(context, attrs, defStyleAttr) {
private val vCodeList: RecyclerView
private val vShadows: Map
@@ -32,17 +36,20 @@ class CodeView(context: Context, attrs: AttributeSet) : RelativeLayout(context,
*/
init {
inflate(context, R.layout.layout_code_view, this)
- checkStartAnimation(attrs)
+ attrs?.let(::checkStartAnimation)
- vCodeList = findViewById(R.id.rv_code_content) as RecyclerView
- vCodeList.layoutManager = LinearLayoutManager(context)
- vCodeList.isNestedScrollingEnabled = true
+ vCodeList = findViewById(R.id.rv_code_content).apply {
+ layoutManager = LinearLayoutManager(context)
+ isNestedScrollingEnabled = true
+ }
vShadows = mapOf(
ShadowPosition.RightBorder to R.id.shadow_right_border,
ShadowPosition.NumBottom to R.id.shadow_num_bottom,
ShadowPosition.ContentBottom to R.id.shadow_content_bottom
- ).mapValues { findViewById(it.value) }
+ ).mapValues {
+ findViewById(it.value)
+ }
}
private fun checkStartAnimation(attrs: AttributeSet) {
@@ -52,8 +59,9 @@ class CodeView(context: Context, attrs: AttributeSet) : RelativeLayout(context,
animate()
.setDuration(Const.DefaultDelay * 5)
.alpha(Const.Alpha.Initial)
- } else
+ } else {
alpha = Const.Alpha.Initial
+ }
}
private fun AbstractCodeAdapter<*>.checkHighlightAnimation(action: () -> Unit) {
@@ -65,7 +73,9 @@ class CodeView(context: Context, attrs: AttributeSet) : RelativeLayout(context,
animate().alpha(Const.Alpha.Visible)
action()
}
- } else action()
+ } else {
+ action()
+ }
}
/**
@@ -75,7 +85,7 @@ class CodeView(context: Context, attrs: AttributeSet) : RelativeLayout(context,
private fun highlight() {
getAdapter()?.apply {
highlight {
- checkHighlightAnimation(this::notifyDataSetChanged)
+ checkHighlightAnimation(::notifyDataSetChanged)
}
}
}
@@ -222,9 +232,9 @@ class CodeView(context: Context, attrs: AttributeSet) : RelativeLayout(context,
RightBorder -> GradientDrawable.Orientation.LEFT_RIGHT to theme.bgContent
NumBottom -> GradientDrawable.Orientation.TOP_BOTTOM to theme.bgNum
ContentBottom -> GradientDrawable.Orientation.TOP_BOTTOM to theme.bgContent
- }.let {
- val colors = arrayOf(android.R.color.transparent, it.second)
- GradientDrawable(it.first, colors.map(Int::color).toIntArray())
+ }.let { (orientation, color) ->
+ val colors = arrayOf(android.R.color.transparent, color)
+ GradientDrawable(orientation, colors.map(Int::color).toIntArray())
}
}
}
diff --git a/codeview/src/main/java/io/github/kbiakov/codeview/adapters/AbstractCodeAdapter.kt b/codeview/src/main/java/io/github/kbiakov/codeview/adapters/AbstractCodeAdapter.kt
index 93cdd08..68ac317 100644
--- a/codeview/src/main/java/io/github/kbiakov/codeview/adapters/AbstractCodeAdapter.kt
+++ b/codeview/src/main/java/io/github/kbiakov/codeview/adapters/AbstractCodeAdapter.kt
@@ -62,9 +62,9 @@ abstract class AbstractCodeAdapter : RecyclerView.Adapter
+ lines = linesToShow + options.shortcutNote.toUpperCase()
+ droppedLines = dropped
}
}
}
@@ -178,15 +178,15 @@ abstract class AbstractCodeAdapter : RecyclerView.Adapter : RecyclerView.Adapter : RecyclerView.Adapter
- val footerView = createFooter(context, entity, idx == 0)
- holder.llLineFooter.addView(footerView)
+ forEachIndexed { idx, entity ->
+ addView(createFooter(context, entity, idx == 0))
+ }
}
}
}
companion object {
private const val MaxShortcutLines = 6
-
- private fun Pair, List>.linesToShow() = first
- private fun Pair, List>.droppedLines() = second
}
// - View holder types
@@ -266,7 +264,7 @@ abstract class AbstractCodeAdapter : RecyclerView.Adapter
@@ -336,114 +334,53 @@ data class Options(
internal var isHighlighted: Boolean = false
- fun withCode(code: String): Options {
- this.code = code
- return this
- }
-
- fun withCode(codeResId: Int): Options {
- this.code = context.getString(codeResId)
- return this
- }
-
- fun setCode(codeResId: Int) {
- withCode(codeResId)
- }
-
- fun withLanguage(language: String): Options {
- this.language = language
- return this
- }
-
- fun withTheme(theme: ColorThemeData): Options {
- this.theme = theme
- return this
- }
-
- fun withTheme(theme: ColorTheme): Options {
- this.theme = theme.theme()
- return this
- }
+ fun withCode(code: String) = apply { this.code = code }
+ fun withCode(codeResId: Int) = apply { code = context.getString(codeResId) }
+ fun setCode(codeResId: Int) { withCode(codeResId) }
+ fun withLanguage(language: String) = apply { this.language = language }
- fun setTheme(theme: ColorTheme) {
- withTheme(theme)
- }
+ fun withTheme(theme: ColorThemeData) = apply { this.theme = theme }
+ fun withTheme(theme: ColorTheme) = apply { this.theme = theme.theme() }
+ fun setTheme(theme: ColorTheme) { withTheme(theme) }
- fun withFont(font: Font): Options {
- this.font = FontCache.get(context).getTypeface(context, font)
- return this
- }
+ fun withFont(font: Font) = apply { this.font = font.get() }
+ fun withFont(font: Typeface) = font saveAndThen { apply { this.font = font } }
+ fun withFont(fontPath: String) = apply { this.font = fontPath.get() }
+ fun setFont(fontPath: String) { withFont(fontPath) }
+ fun setFont(font: Font) { withFont(font) }
+ fun withFormat(format: Format) = apply { this.format = format }
- fun withFont(font: Typeface): Options {
- FontCache.get(context).saveTypeface(font)
- this.font = font
- return this
- }
+ fun animateOnHighlight() = apply { animateOnHighlight = true }
+ fun disableHighlightAnimation() = apply { animateOnHighlight = false }
+ fun withShadows() = apply { shadows = true }
+ fun withoutShadows() = apply { shadows = false }
- fun withFont(fontPath: String): Options {
- this.font = FontCache.get(context).getTypeface(context, fontPath)
- return this
- }
+ fun addCodeLineClickListener(listener: OnCodeLineClickListener) = apply { lineClickListener = listener }
+ fun removeCodeLineClickListener() = apply { lineClickListener = null }
- fun setFont(fontPath: String) {
- withFont(fontPath)
- }
-
- fun setFont(font: Font) {
- withFont(font)
- }
-
- fun withFormat(format: Format): Options {
- this.format = format
- return this
- }
-
- fun animateOnHighlight(): Options {
- this.animateOnHighlight = true
- return this
- }
-
- fun disableHighlightAnimation(): Options {
- this.animateOnHighlight = false
- return this
- }
-
- fun withShadows(): Options {
- this.shadows = true
- return this
- }
-
- fun withoutShadows(): Options {
- this.shadows = false
- return this
- }
-
- fun shortcut(maxLines: Int, shortcutNote: String): Options {
+ fun shortcut(maxLines: Int, shortcutNote: String) = apply {
this.shortcut = true
this.maxLines = maxLines
this.shortcutNote = shortcutNote
- return this
- }
-
- fun addCodeLineClickListener(listener: OnCodeLineClickListener): Options {
- this.lineClickListener = listener
- return this
- }
-
- fun removeCodeLineClickListener(): Options {
- this.lineClickListener = null
- return this
}
companion object Default {
fun get(context: Context) = Options(context)
}
+
+ // - Font helpers
+
+ private val fontCache = FontCache.get(context)
+ private fun Font.get() = fontCache.getTypeface(context, this)
+ private fun String.get() = fontCache.getTypeface(context, this)
+ private infix fun Typeface.saveAndThen(body: () -> T): T = fontCache.saveTypeface(this).let { body() }
}
-data class Format(val scaleFactor: Float = 1f,
- val lineHeight: Int = (LineHeight * scaleFactor).toInt(),
- val borderHeight: Int = (BorderHeight * scaleFactor).toInt(),
- val fontSize: Float = FontSize.toFloat()) {
+data class Format(
+ val scaleFactor: Float = 1f,
+ val lineHeight: Int = (LineHeight * scaleFactor).toInt(),
+ val borderHeight: Int = (BorderHeight * scaleFactor).toInt(),
+ val fontSize: Float = FontSize.toFloat()) {
companion object Default {
private const val LineHeight = 18
diff --git a/codeview/src/main/java/io/github/kbiakov/codeview/highlight/CodeHighlighter.kt b/codeview/src/main/java/io/github/kbiakov/codeview/highlight/CodeHighlighter.kt
index d64684f..581172c 100644
--- a/codeview/src/main/java/io/github/kbiakov/codeview/highlight/CodeHighlighter.kt
+++ b/codeview/src/main/java/io/github/kbiakov/codeview/highlight/CodeHighlighter.kt
@@ -237,20 +237,21 @@ infix fun String.applyFontParams(color: String?): String {
var idx = 0
var newIdx = indexOf("\n")
- if (newIdx.notFound()) // covers expected tag coverage (within only one line)
+ if (newIdx.notFound()) { // covers expected tag coverage (within only one line)
parametrizedString += inFontTag(color)
- else { // may contain multiple lines with line breaks
+ } else { // may contain multiple lines with line breaks
// put tag on the borders (end & start of line, ..., end of tag)
do { // until closing tag is reached
- parametrizedString += (substring(idx .. newIdx - 1) inFontTag color) + "\n"
+ parametrizedString += "${substring(idx until newIdx) inFontTag color}\n"
idx = newIdx + 1
newIdx = indexOf("\n", idx)
} while (newIdx.isFound())
- if (idx != indexOf("\n")) // if not replaced only once (for multiline tag coverage)
+ if (idx != indexOf("\n")) { // if not replaced only once (for multiline tag coverage)
parametrizedString += substring(idx) inFontTag color
+ }
}
return parametrizedString
}
diff --git a/codeview/src/main/java/io/github/kbiakov/codeview/views/BidirectionalScrollView.kt b/codeview/src/main/java/io/github/kbiakov/codeview/views/BidirectionalScrollView.kt
index 5c90791..ad5c0b1 100644
--- a/codeview/src/main/java/io/github/kbiakov/codeview/views/BidirectionalScrollView.kt
+++ b/codeview/src/main/java/io/github/kbiakov/codeview/views/BidirectionalScrollView.kt
@@ -39,15 +39,19 @@ class BidirectionalScrollView : HorizontalScrollView {
scroll(event)
val movedOnDistance = dpToPx(context, 2)
- if (deltaX > movedOnDistance || deltaY > movedOnDistance)
+ if (deltaX > movedOnDistance || deltaY > movedOnDistance) {
isMoved = true
+ }
}
MotionEvent.ACTION_UP -> {
- if (!isMoved)
+ if (!isMoved) {
return super.dispatchTouchEvent(event)
+ }
+ isMoved = false
+ }
+ MotionEvent.ACTION_CANCEL -> {
isMoved = false
}
- MotionEvent.ACTION_CANCEL -> isMoved = false
}
return true
}
@@ -64,17 +68,18 @@ class BidirectionalScrollView : HorizontalScrollView {
}
override fun measureChild(child: View, parentWidthMeasureSpec: Int, parentHeightMeasureSpec: Int) {
- val measureSpecZero = makeMeasureSpec(0)
- child.measure(measureSpecZero, measureSpecZero)
+ val zeroMeasureSpec = makeMeasureSpec(0)
+ child.measure(zeroMeasureSpec, zeroMeasureSpec)
}
- override fun measureChildWithMargins(child: View,
- parentWidthMeasureSpec: Int, widthUsed: Int,
- parentHeightMeasureSpec: Int, heightUsed: Int) {
- val params = child.layoutParams as MarginLayoutParams
- val childWidthMeasureSpec = makeMeasureSpec(params.leftMargin + params.rightMargin)
- val childHeightMeasureSpec = makeMeasureSpec(params.topMargin + params.bottomMargin)
- child.measure(childWidthMeasureSpec, childHeightMeasureSpec)
+ override fun measureChildWithMargins(
+ child: View,
+ parentWidthMeasureSpec: Int, widthUsed: Int,
+ parentHeightMeasureSpec: Int, heightUsed: Int
+ ) = with(child.layoutParams as MarginLayoutParams) {
+ val widthMeasureSpec = makeMeasureSpec(leftMargin + rightMargin, MeasureSpec.UNSPECIFIED)
+ val heightMeasureSpec = makeMeasureSpec(topMargin + bottomMargin, MeasureSpec.UNSPECIFIED)
+ child.measure(widthMeasureSpec, heightMeasureSpec)
}
private fun makeMeasureSpec(size: Int) = makeMeasureSpec(size, MeasureSpec.UNSPECIFIED)
diff --git a/example/build.gradle b/example/build.gradle
index 7ad5510..6ab7624 100644
--- a/example/build.gradle
+++ b/example/build.gradle
@@ -2,13 +2,13 @@ apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
android {
- compileSdkVersion 25
- buildToolsVersion '25.0.3'
+ compileSdkVersion compileSdk
+ buildToolsVersion buildTools
defaultConfig {
applicationId 'io.github.kbiakov.codeviewexample'
- minSdkVersion 15
- targetSdkVersion 25
+ minSdkVersion minSdk
+ targetSdkVersion compileSdk
versionCode 1
versionName '1.0'
}
@@ -24,9 +24,11 @@ android {
}
dependencies {
- compile 'com.android.support:appcompat-v7:25.3.1'
- compile project(path: ':codeview')
- compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
+ implementation project(':codeview')
+
+ implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"
+ implementation "com.android.support:appcompat-v7:$supportLibrary"
+ implementation "com.android.support:recyclerview-v7:$supportLibrary"
}
repositories {
mavenCentral()
diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties
index c1b0c03..9279f53 100644
--- a/gradle/wrapper/gradle-wrapper.properties
+++ b/gradle/wrapper/gradle-wrapper.properties
@@ -1,6 +1,6 @@
-#Sat Aug 26 20:57:29 MSK 2017
+#Thu Jan 17 15:59:44 MSK 2019
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
-distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
+distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.3-all.zip