Skip to content

Commit f6f8ab5

Browse files
authored
Merge pull request #10 from Softwee/feature/adapter.customization
Added adapter customization
2 parents 19f3dbc + f2f48f2 commit f6f8ab5

File tree

14 files changed

+347
-71
lines changed

14 files changed

+347
-71
lines changed

codeview/build.gradle

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,15 @@ dependencies {
2626
compile fileTree(dir: 'libs', include: ['*.jar'])
2727
testCompile 'junit:junit:4.12'
2828

29-
compile 'com.android.support:appcompat-v7:24.1.1'
30-
compile 'com.android.support:recyclerview-v7:24.1.1'
31-
compile 'com.github.twalcari:java-prettify:1.2.2'
3229
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
30+
compile group: 'org.jetbrains.kotlin', name: 'kotlin-reflect', version: '1.0.3'
31+
32+
compile 'com.android.support:appcompat-v7:24.2.0'
33+
compile 'com.android.support:recyclerview-v7:24.2.0'
34+
compile 'com.github.twalcari:java-prettify:1.2.2'
3335
}
3436
repositories {
3537
mavenCentral()
3638
}
39+
buildscript {
40+
}

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

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,18 @@ package io.github.kbiakov.codeview
33
import android.animation.Animator
44
import android.animation.AnimatorListenerAdapter
55
import android.content.Context
6-
import android.os.Handler
76
import android.support.v7.widget.LinearLayoutManager
87
import android.support.v7.widget.RecyclerView
98
import android.util.AttributeSet
109
import android.view.LayoutInflater
1110
import android.view.View
1211
import android.view.ViewPropertyAnimator
1312
import android.widget.RelativeLayout
13+
import io.github.kbiakov.codeview.adapters.AbstractCodeAdapter
14+
import io.github.kbiakov.codeview.Thread.delayed
15+
import io.github.kbiakov.codeview.adapters.CodeWithNotesAdapter
1416
import io.github.kbiakov.codeview.highlight.ColorTheme
1517
import io.github.kbiakov.codeview.highlight.ColorThemeData
16-
import io.github.kbiakov.codeview.highlight.color
1718
import java.util.*
1819

1920
/**
@@ -55,7 +56,15 @@ class CodeView : RelativeLayout {
5556
* (and awaiting for build) or view was built & code is presented.
5657
*/
5758
private var state: ViewState
59+
set(newState) {
60+
if (newState == ViewState.PRESENTED)
61+
hidePlaceholder()
62+
field = newState
63+
}
5864

65+
/**
66+
* Default constructor.
67+
*/
5968
constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
6069
val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
6170
inflater.inflate(R.layout.layout_code_view, this, true)
@@ -69,13 +78,13 @@ class CodeView : RelativeLayout {
6978
rvCodeContent.layoutManager = LinearLayoutManager(context)
7079
rvCodeContent.isNestedScrollingEnabled = true
7180

72-
tasks = LinkedList()
73-
7481
state = ViewState.BUILD
82+
83+
tasks = LinkedList()
7584
}
7685

7786
/**
78-
* Code view states.
87+
* Code view state to control build flow.
7988
*/
8089
enum class ViewState {
8190
BUILD,
@@ -84,22 +93,28 @@ class CodeView : RelativeLayout {
8493
}
8594

8695
/**
87-
* Public getter for accessing view state.
88-
* It may be useful if code view state is unknown.
89-
* If code view was built it is not safe to use operations chaining.
96+
* Public getters for checking view state.
97+
* May be useful when code view state is unknown.
98+
* If view was built it is unsafe to use operations chaining.
99+
*
100+
* @return Result of state check
90101
*/
91-
fun getState() = state
102+
fun isBuilding() = state == ViewState.BUILD
103+
fun isPrepared() = state == ViewState.PREPARE
104+
fun isPresented() = state == ViewState.PRESENTED
92105

93106
/**
94107
* Accessor/mutator to reduce frequently used actions.
95108
*/
96-
var adapter: CodeContentAdapter
109+
var adapter: AbstractCodeAdapter<*>
97110
get() {
98-
return rvCodeContent.adapter as CodeContentAdapter
111+
return rvCodeContent.adapter as AbstractCodeAdapter<*>
99112
}
100113
set(adapter) {
101-
rvCodeContent.adapter = adapter
102-
state = ViewState.PRESENTED
114+
delayed { // to prevent UI overhead & initialization inconsistency
115+
rvCodeContent.adapter = adapter
116+
state = ViewState.PRESENTED
117+
}
103118
}
104119

105120
// - Build processor
@@ -116,7 +131,7 @@ class CodeView : RelativeLayout {
116131
ViewState.BUILD ->
117132
tasks.add(task)
118133
ViewState.PREPARE ->
119-
Thread.delayed(task)
134+
delayed(body = task)
120135
ViewState.PRESENTED ->
121136
task()
122137
}
@@ -202,7 +217,7 @@ class CodeView : RelativeLayout {
202217
ViewState.BUILD ->
203218
build(content)
204219
ViewState.PREPARE ->
205-
Thread.delayed {
220+
delayed {
206221
update(content)
207222
}
208223
ViewState.PRESENTED ->
@@ -224,8 +239,8 @@ class CodeView : RelativeLayout {
224239
measurePlaceholder(linesCount)
225240
state = ViewState.PREPARE
226241

227-
Thread.delayed {
228-
rvCodeContent.adapter = CodeContentAdapter(context, content)
242+
delayed {
243+
rvCodeContent.adapter = CodeWithNotesAdapter(context, content)
229244
processBuildTasks()
230245
setupShadows()
231246
hidePlaceholder()
@@ -264,13 +279,13 @@ class CodeView : RelativeLayout {
264279
val lineHeight = dpToPx(context, 24)
265280
val topPadding = dpToPx(context, 8)
266281

267-
// double padding (top & bottom) for big view, one is enough for small
282+
// double padding (top & bottom), one is enough for single line view
268283
val padding = (if (linesCount > 1) 2 else 1) * topPadding
269284

270285
val height = linesCount * lineHeight + padding
271286

272-
vPlaceholder.layoutParams = RelativeLayout.LayoutParams(
273-
RelativeLayout.LayoutParams.MATCH_PARENT, height)
287+
vPlaceholder.layoutParams = LayoutParams(
288+
LayoutParams.MATCH_PARENT, height)
274289
vPlaceholder.visibility = View.VISIBLE
275290
}
276291

@@ -296,16 +311,9 @@ class CodeView : RelativeLayout {
296311
* Provides listener to code line clicks.
297312
*/
298313
interface OnCodeLineClickListener {
299-
fun onCodeLineClicked(n: Int)
314+
fun onCodeLineClicked(n: Int, line: String)
300315
}
301316

302-
/**
303-
* Extension for delayed block call.
304-
*
305-
* @param body Operation body
306-
*/
307-
fun Thread.delayed(body: () -> Unit) = Handler().postDelayed(body, 150)
308-
309317
/**
310318
* More readable form for animation listener (hi, iOS & Cocoa Touch!).
311319
*

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ fun extractLines(source: String) = listOf(*source.split("\n").toTypedArray())
4343
* @param content Source
4444
* @return Spanned HTML string
4545
*/
46-
@Suppress("DEPRECATION")
46+
@Suppress("deprecation")
4747
fun html(content: String): Spanned =
4848
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N)
4949
Html.fromHtml(content, Html.FROM_HTML_MODE_LEGACY)
@@ -69,6 +69,14 @@ object Thread {
6969
Handler(Looper.getMainLooper()).post(body)
7070
}
7171

72+
/**
73+
* Delayed block call.
74+
*
75+
* @param body Operation body
76+
* @param delayMs Delay in m
77+
*/
78+
fun delayed(delayMs: Long = 150, body: () -> Unit) = Handler().postDelayed(body, delayMs)
79+
7280
// - Extensions for block manipulations
7381

7482
fun (() -> Unit).async(isAsync: Boolean = true) {

0 commit comments

Comments
 (0)