Skip to content

Commit 6126776

Browse files
committed
Update versions, code cleanup
1 parent 088f985 commit 6126776

File tree

8 files changed

+95
-145
lines changed

8 files changed

+95
-145
lines changed

build.gradle

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
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.1.2-5'
4+
ext.kotlin_version = '1.2.21'
5+
ext.minSdk = 15
6+
ext.compileSdk = 27
7+
ext.buildTools = '27.0.3'
8+
ext.supportLibrary = '27.1.0'
9+
510
repositories {
611
jcenter()
12+
google()
713
}
814
dependencies {
9-
classpath 'com.android.tools.build:gradle:2.3.3'
15+
classpath 'com.android.tools.build:gradle:3.0.1'
1016
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
11-
12-
// NOTE: Do not place your application dependencies here; they belong
13-
// in the individual module build.gradle files
1417
}
1518
}
1619

1720
allprojects {
1821
repositories {
1922
jcenter()
23+
google()
2024
}
2125
}
2226

codeview/build.gradle

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

44
android {
5-
compileSdkVersion 25
6-
buildToolsVersion '25.0.3'
5+
compileSdkVersion compileSdk
6+
buildToolsVersion buildTools
77

88
defaultConfig {
9-
minSdkVersion 15
10-
targetSdkVersion 25
9+
minSdkVersion minSdk
10+
targetSdkVersion compileSdk
1111
versionCode 1
1212
versionName '1.3.0'
1313
}
@@ -24,7 +24,6 @@ android {
2424

2525
dependencies {
2626
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
27-
28-
compile 'com.android.support:appcompat-v7:25.3.1'
29-
compile 'com.android.support:recyclerview-v7:25.3.1'
27+
compile "com.android.support:appcompat-v7:$supportLibrary"
28+
compile "com.android.support:recyclerview-v7:$supportLibrary"
3029
}

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,17 @@ class CodeView(context: Context, attrs: AttributeSet) : RelativeLayout(context,
3434
inflate(context, R.layout.layout_code_view, this)
3535
checkStartAnimation(attrs)
3636

37-
vCodeList = findViewById(R.id.rv_code_content) as RecyclerView
37+
vCodeList = findViewById<RecyclerView>(R.id.rv_code_content)
3838
vCodeList.layoutManager = LinearLayoutManager(context)
3939
vCodeList.isNestedScrollingEnabled = true
4040

4141
vShadows = mapOf(
4242
ShadowPosition.RightBorder to R.id.shadow_right_border,
4343
ShadowPosition.NumBottom to R.id.shadow_num_bottom,
4444
ShadowPosition.ContentBottom to R.id.shadow_content_bottom
45-
).mapValues { findViewById(it.value) }
45+
).mapValues {
46+
findViewById<View>(it.value)
47+
}
4648
}
4749

4850
private fun checkStartAnimation(attrs: AttributeSet) {
@@ -52,8 +54,9 @@ class CodeView(context: Context, attrs: AttributeSet) : RelativeLayout(context,
5254
animate()
5355
.setDuration(Const.DefaultDelay * 5)
5456
.alpha(Const.Alpha.Initial)
55-
} else
57+
} else {
5658
alpha = Const.Alpha.Initial
59+
}
5760
}
5861

5962
private fun AbstractCodeAdapter<*>.checkHighlightAnimation(action: () -> Unit) {
@@ -65,7 +68,9 @@ class CodeView(context: Context, attrs: AttributeSet) : RelativeLayout(context,
6568
animate().alpha(Const.Alpha.Visible)
6669
action()
6770
}
68-
} else action()
71+
} else {
72+
action()
73+
}
6974
}
7075

7176
/**
@@ -75,7 +80,7 @@ class CodeView(context: Context, attrs: AttributeSet) : RelativeLayout(context,
7580
private fun highlight() {
7681
getAdapter()?.apply {
7782
highlight {
78-
checkHighlightAnimation(this::notifyDataSetChanged)
83+
checkHighlightAnimation(::notifyDataSetChanged)
7984
}
8085
}
8186
}

codeview/src/main/java/io/github/kbiakov/codeview/adapters/AbstractCodeAdapter.kt

Lines changed: 51 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@ abstract class AbstractCodeAdapter<T> : RecyclerView.Adapter<AbstractCodeAdapter
6262
extractLines(options.code).apply {
6363
if (!options.shortcut || size <= options.maxLines) // limit is not reached, show full
6464
lines = this
65-
else slice(options.maxLines).apply {
66-
lines = linesToShow() + options.shortcutNote.toUpperCase()
67-
droppedLines = droppedLines()
65+
else slice(options.maxLines).let { (linesToShow, dropped) ->
66+
lines = linesToShow + options.shortcutNote.toUpperCase()
67+
droppedLines = dropped
6868
}
6969
}
7070
}
@@ -178,15 +178,15 @@ abstract class AbstractCodeAdapter<T> : RecyclerView.Adapter<AbstractCodeAdapter
178178
tvLineContent.typeface = options.font
179179

180180
val isLine = viewType == ViewHolderType.Line.viewType
181+
181182
options.format.apply {
182183
val height = if (isLine) lineHeight else borderHeight
183184
lineView.layoutParams.height = dpToPx(context, height)
184185
}
185-
return if (isLine) {
186-
val holder = LineViewHolder(lineView)
187-
holder.setIsRecyclable(false)
188-
holder
189-
} else BorderViewHolder(lineView)
186+
return if (isLine)
187+
LineViewHolder(lineView).apply { setIsRecyclable(false) }
188+
else
189+
BorderViewHolder(lineView)
190190
}
191191

192192
override fun onBindViewHolder(holder: ViewHolder, pos: Int) {
@@ -207,9 +207,9 @@ abstract class AbstractCodeAdapter<T> : RecyclerView.Adapter<AbstractCodeAdapter
207207
// - Helpers (for view holder)
208208

209209
private fun bindClickListener(pos: Int, holder: ViewHolder) {
210-
options.lineClickListener?.let {
211-
holder.itemView.setOnClickListener {
212-
options.lineClickListener?.onCodeLineClicked(pos, lines[pos])
210+
holder.itemView.setOnClickListener {
211+
options.lineClickListener?.apply {
212+
onCodeLineClicked(pos, lines[pos])
213213
}
214214
}
215215
}
@@ -238,23 +238,21 @@ abstract class AbstractCodeAdapter<T> : RecyclerView.Adapter<AbstractCodeAdapter
238238
private fun displayFooter(pos: Int, holder: ViewHolder) {
239239
val entityList = footerEntities[pos]
240240

241-
holder.llLineFooter.removeAllViews()
241+
holder.llLineFooter.apply {
242+
removeAllViews()
242243

243-
entityList?.let {
244-
holder.llLineFooter.visibility = if (it.isNotEmpty()) View.VISIBLE else View.GONE
244+
entityList?.apply {
245+
visibility = if (isNotEmpty()) View.VISIBLE else View.GONE
245246

246-
it.forEachIndexed { idx, entity ->
247-
val footerView = createFooter(context, entity, idx == 0)
248-
holder.llLineFooter.addView(footerView)
247+
forEachIndexed { idx, entity ->
248+
addView(createFooter(context, entity, idx == 0))
249+
}
249250
}
250251
}
251252
}
252253

253254
companion object {
254255
private const val MaxShortcutLines = 6
255-
256-
private fun Pair<List<String>, List<String>>.linesToShow() = first
257-
private fun Pair<List<String>, List<String>>.droppedLines() = second
258256
}
259257

260258
// - View holder types
@@ -266,7 +264,7 @@ abstract class AbstractCodeAdapter<T> : RecyclerView.Adapter<AbstractCodeAdapter
266264
const val LineStartIdx = 1
267265
const val BordersCount = 2
268266

269-
fun Int.lineEndIdx() = this - BordersCount
267+
private fun Int.lineEndIdx() = this - BordersCount
270268

271269
fun get(pos: Int, n: Int) = when (pos) {
272270
in LineStartIdx .. n.lineEndIdx() ->
@@ -336,114 +334,53 @@ data class Options(
336334

337335
internal var isHighlighted: Boolean = false
338336

339-
fun withCode(code: String): Options {
340-
this.code = code
341-
return this
342-
}
343-
344-
fun withCode(codeResId: Int): Options {
345-
this.code = context.getString(codeResId)
346-
return this
347-
}
348-
349-
fun setCode(codeResId: Int) {
350-
withCode(codeResId)
351-
}
352-
353-
fun withLanguage(language: String): Options {
354-
this.language = language
355-
return this
356-
}
357-
358-
fun withTheme(theme: ColorThemeData): Options {
359-
this.theme = theme
360-
return this
361-
}
362-
363-
fun withTheme(theme: ColorTheme): Options {
364-
this.theme = theme.theme()
365-
return this
366-
}
337+
fun withCode(code: String) = apply { this.code = code }
338+
fun withCode(codeResId: Int) = apply { code = context.getString(codeResId) }
339+
fun setCode(codeResId: Int) { withCode(codeResId) }
340+
fun withLanguage(language: String) = apply { this.language = language }
367341

368-
fun setTheme(theme: ColorTheme) {
369-
withTheme(theme)
370-
}
342+
fun withTheme(theme: ColorThemeData) = apply { this.theme = theme }
343+
fun withTheme(theme: ColorTheme) = apply { this.theme = theme.theme() }
344+
fun setTheme(theme: ColorTheme) { withTheme(theme) }
371345

372-
fun withFont(font: Font): Options {
373-
this.font = FontCache.get(context).getTypeface(context, font)
374-
return this
375-
}
346+
fun withFont(font: Font) = apply { this.font = font.get() }
347+
fun withFont(font: Typeface) = font saveAndThen { apply { this.font = font } }
348+
fun withFont(fontPath: String) = apply { this.font = fontPath.get() }
349+
fun setFont(fontPath: String) { withFont(fontPath) }
350+
fun setFont(font: Font) { withFont(font) }
351+
fun withFormat(format: Format) = apply { this.format = format }
376352

377-
fun withFont(font: Typeface): Options {
378-
FontCache.get(context).saveTypeface(font)
379-
this.font = font
380-
return this
381-
}
353+
fun animateOnHighlight() = apply { animateOnHighlight = true }
354+
fun disableHighlightAnimation() = apply { animateOnHighlight = false }
355+
fun withShadows() = apply { shadows = true }
356+
fun withoutShadows() = apply { shadows = false }
382357

383-
fun withFont(fontPath: String): Options {
384-
this.font = FontCache.get(context).getTypeface(context, fontPath)
385-
return this
386-
}
358+
fun addCodeLineClickListener(listener: OnCodeLineClickListener) = apply { lineClickListener = listener }
359+
fun removeCodeLineClickListener() = apply { lineClickListener = null }
387360

388-
fun setFont(fontPath: String) {
389-
withFont(fontPath)
390-
}
391-
392-
fun setFont(font: Font) {
393-
withFont(font)
394-
}
395-
396-
fun withFormat(format: Format): Options {
397-
this.format = format
398-
return this
399-
}
400-
401-
fun animateOnHighlight(): Options {
402-
this.animateOnHighlight = true
403-
return this
404-
}
405-
406-
fun disableHighlightAnimation(): Options {
407-
this.animateOnHighlight = false
408-
return this
409-
}
410-
411-
fun withShadows(): Options {
412-
this.shadows = true
413-
return this
414-
}
415-
416-
fun withoutShadows(): Options {
417-
this.shadows = false
418-
return this
419-
}
420-
421-
fun shortcut(maxLines: Int, shortcutNote: String): Options {
361+
fun shortcut(maxLines: Int, shortcutNote: String) = apply {
422362
this.shortcut = true
423363
this.maxLines = maxLines
424364
this.shortcutNote = shortcutNote
425-
return this
426-
}
427-
428-
fun addCodeLineClickListener(listener: OnCodeLineClickListener): Options {
429-
this.lineClickListener = listener
430-
return this
431-
}
432-
433-
fun removeCodeLineClickListener(): Options {
434-
this.lineClickListener = null
435-
return this
436365
}
437366

438367
companion object Default {
439368
fun get(context: Context) = Options(context)
440369
}
370+
371+
// - Font helpers
372+
373+
private val fontCache = FontCache.get(context)
374+
private fun Font.get() = fontCache.getTypeface(context, this)
375+
private fun String.get() = fontCache.getTypeface(context, this)
376+
private infix fun <T> Typeface.saveAndThen(body: () -> T): T = fontCache.saveTypeface(this).let { body() }
441377
}
442378

443-
data class Format(val scaleFactor: Float = 1f,
444-
val lineHeight: Int = (LineHeight * scaleFactor).toInt(),
445-
val borderHeight: Int = (BorderHeight * scaleFactor).toInt(),
446-
val fontSize: Float = FontSize.toFloat()) {
379+
data class Format(
380+
val scaleFactor: Float = 1f,
381+
val lineHeight: Int = (LineHeight * scaleFactor).toInt(),
382+
val borderHeight: Int = (BorderHeight * scaleFactor).toInt(),
383+
val fontSize: Float = FontSize.toFloat()) {
447384

448385
companion object Default {
449386
private const val LineHeight = 18

codeview/src/main/java/io/github/kbiakov/codeview/highlight/CodeHighlighter.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -237,20 +237,21 @@ infix fun String.applyFontParams(color: String?): String {
237237
var idx = 0
238238
var newIdx = indexOf("\n")
239239

240-
if (newIdx.notFound()) // covers expected tag coverage (within only one line)
240+
if (newIdx.notFound()) { // covers expected tag coverage (within only one line)
241241
parametrizedString += inFontTag(color)
242-
else { // may contain multiple lines with line breaks
242+
} else { // may contain multiple lines with line breaks
243243

244244
// put tag on the borders (end & start of line, ..., end of tag)
245245
do { // until closing tag is reached
246-
parametrizedString += (substring(idx .. newIdx - 1) inFontTag color) + "\n"
246+
parametrizedString += "${substring(idx until newIdx) inFontTag color}\n"
247247

248248
idx = newIdx + 1
249249
newIdx = indexOf("\n", idx)
250250
} while (newIdx.isFound())
251251

252-
if (idx != indexOf("\n")) // if not replaced only once (for multiline tag coverage)
252+
if (idx != indexOf("\n")) { // if not replaced only once (for multiline tag coverage)
253253
parametrizedString += substring(idx) inFontTag color
254+
}
254255
}
255256
return parametrizedString
256257
}

codeview/src/main/java/io/github/kbiakov/codeview/views/BidirectionalScrollView.kt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,19 @@ class BidirectionalScrollView : HorizontalScrollView {
3939
scroll(event)
4040

4141
val movedOnDistance = dpToPx(context, 2)
42-
if (deltaX > movedOnDistance || deltaY > movedOnDistance)
42+
if (deltaX > movedOnDistance || deltaY > movedOnDistance) {
4343
isMoved = true
44+
}
4445
}
4546
MotionEvent.ACTION_UP -> {
46-
if (!isMoved)
47+
if (!isMoved) {
4748
return super.dispatchTouchEvent(event)
49+
}
50+
isMoved = false
51+
}
52+
MotionEvent.ACTION_CANCEL -> {
4853
isMoved = false
4954
}
50-
MotionEvent.ACTION_CANCEL -> isMoved = false
5155
}
5256
return true
5357
}

0 commit comments

Comments
 (0)