Skip to content

Commit fd7cd45

Browse files
authored
Merge pull request #15 from Softwee/develop
Improved adapter with ability to customization
2 parents b4a6820 + 4af73a1 commit fd7cd45

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+4897
-115
lines changed

codeview/build.gradle

+5-3
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@ 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+
31+
compile 'com.android.support:appcompat-v7:24.2.0'
32+
compile 'com.android.support:recyclerview-v7:24.2.0'
3333
}
3434
repositories {
3535
mavenCentral()
3636
}
37+
buildscript {
38+
}

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

+43-28
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 { // 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
}
@@ -180,6 +195,13 @@ class CodeView : RelativeLayout {
180195
adapter.codeListener = listener
181196
}
182197

198+
/**
199+
* Remove code listener.
200+
*/
201+
fun removeCodeListener() = addTask {
202+
adapter.codeListener = null
203+
}
204+
183205
/**
184206
* Control shadows visibility to provide more sensitive UI.
185207
*
@@ -202,7 +224,7 @@ class CodeView : RelativeLayout {
202224
ViewState.BUILD ->
203225
build(content)
204226
ViewState.PREPARE ->
205-
Thread.delayed {
227+
delayed {
206228
update(content)
207229
}
208230
ViewState.PRESENTED ->
@@ -224,8 +246,8 @@ class CodeView : RelativeLayout {
224246
measurePlaceholder(linesCount)
225247
state = ViewState.PREPARE
226248

227-
Thread.delayed {
228-
rvCodeContent.adapter = CodeContentAdapter(context, content)
249+
delayed {
250+
rvCodeContent.adapter = CodeWithNotesAdapter(context, content)
229251
processBuildTasks()
230252
setupShadows()
231253
hidePlaceholder()
@@ -264,13 +286,13 @@ class CodeView : RelativeLayout {
264286
val lineHeight = dpToPx(context, 24)
265287
val topPadding = dpToPx(context, 8)
266288

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

270292
val height = linesCount * lineHeight + padding
271293

272-
vPlaceholder.layoutParams = RelativeLayout.LayoutParams(
273-
RelativeLayout.LayoutParams.MATCH_PARENT, height)
294+
vPlaceholder.layoutParams = LayoutParams(
295+
LayoutParams.MATCH_PARENT, height)
274296
vPlaceholder.visibility = View.VISIBLE
275297
}
276298

@@ -296,16 +318,9 @@ class CodeView : RelativeLayout {
296318
* Provides listener to code line clicks.
297319
*/
298320
interface OnCodeLineClickListener {
299-
fun onCodeLineClicked(n: Int)
321+
fun onCodeLineClicked(n: Int, line: String)
300322
}
301323

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-
309324
/**
310325
* More readable form for animation listener (hi, iOS & Cocoa Touch!).
311326
*

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

+9-1
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)