Skip to content

Commit 6744ba8

Browse files
author
e16din
committed
Fix bugs, update performance.
1 parent aa3b383 commit 6744ba8

File tree

14 files changed

+177
-132
lines changed

14 files changed

+177
-132
lines changed

.gitignore

+58-9
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
### Android ###
12
# Built application files
23
*.apk
34
*.ap_
45

5-
# Files for the ART/Dalvik VM
6+
# Files for the Dalvik VM
67
*.dex
78

89
# Java class files
@@ -11,11 +12,17 @@
1112
# Generated files
1213
bin/
1314
gen/
14-
out/
1515

1616
# Gradle files
1717
.gradle/
1818
build/
19+
/*/build/
20+
21+
# Ignore Gradle GUI config
22+
gradle-app.setting
23+
24+
# Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored)
25+
!gradle-wrapper.jar
1926

2027
# Local configuration file (sdk path, etc)
2128
local.properties
@@ -26,15 +33,57 @@ proguard/
2633
# Log Files
2734
*.log
2835

29-
# Android Studio Navigation editor temp files
30-
.navigation/
36+
### Android Patch ###
37+
gen-external-apklibs
3138

32-
# Android Studio captures folder
33-
captures/
3439

35-
# Intellij
40+
### Intellij ###
41+
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm
42+
3643
*.iml
44+
45+
## Directory-based project format:
3746
.idea/
47+
# if you remove the above rule, at least ignore the following:
48+
49+
# User-specific stuff:
50+
# .idea/workspace.xml
51+
# .idea/tasks.xml
52+
# .idea/dictionaries
53+
54+
# Sensitive or high-churn files:
55+
# .idea/dataSources.ids
56+
# .idea/dataSources.xml
57+
# .idea/sqlDataSources.xml
58+
# .idea/dynamic.xml
59+
# .idea/uiDesigner.xml
60+
61+
# Gradle:
62+
# .idea/gradle.xml
63+
# .idea/libraries
64+
65+
# Mongo Explorer plugin:
66+
# .idea/mongoSettings.xml
3867

39-
# Keystore files
40-
*.jks
68+
## File-based project format:
69+
*.ipr
70+
*.iws
71+
72+
## Plugin-specific files:
73+
74+
# IntelliJ
75+
/out/
76+
77+
# mpeltonen/sbt-idea plugin
78+
.idea_modules/
79+
80+
# JIRA plugin
81+
atlassian-ide-plugin.xml
82+
83+
# Crashlytics plugin (for Android Studio and IntelliJ)
84+
com_crashlytics_export_strings.xml
85+
crashlytics.properties
86+
crashlytics-build.properties
87+
*.iml
88+
89+
*.iml

codeview/build.gradle

-3
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@ android {
2323
}
2424

2525
dependencies {
26-
compile fileTree(dir: 'libs', include: ['*.jar'])
27-
testCompile 'junit:junit:4.12'
28-
2926
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
3027

3128
compile 'com.android.support:appcompat-v7:24.2.0'

codeview/src/androidTest/java/io/github/kbiakov/codeview/ApplicationTest.java

-13
This file was deleted.

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

+55-40
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,15 @@ class CodeView : RelativeLayout {
4141
private val vShadowBottomLine: View
4242
private val vShadowBottomContent: View
4343

44+
internal var colorTheme: ColorThemeData
45+
set(colorTheme) {
46+
field = colorTheme
47+
}
48+
49+
init {
50+
colorTheme = ColorTheme.SOLARIZED_LIGHT.with()
51+
}
52+
4453
/**
4554
* Core view to draw code by lines.
4655
*/
@@ -57,8 +66,9 @@ class CodeView : RelativeLayout {
5766
*/
5867
private var state: ViewState
5968
set(newState) {
60-
if (newState == ViewState.PRESENTED)
69+
if (newState == ViewState.PRESENTED) {
6170
hidePlaceholder()
71+
}
6272
field = newState
6373
}
6474

@@ -86,6 +96,8 @@ class CodeView : RelativeLayout {
8696
/**
8797
* Code view state to control build flow.
8898
*/
99+
//todo: remove states and tasks
100+
//todo: or sort task positions (first we need to set adapter, then other actions and call highlight task on finish)
89101
enum class ViewState {
90102
BUILD,
91103
PREPARE,
@@ -100,19 +112,20 @@ class CodeView : RelativeLayout {
100112
* @return Result of state check
101113
*/
102114
fun isBuilding() = state == ViewState.BUILD
115+
103116
fun isPrepared() = state == ViewState.PREPARE
104117
fun isPresented() = state == ViewState.PRESENTED
105118

106119
/**
107120
* Accessor/mutator to reduce frequently used actions.
108121
*/
109-
var adapter: AbstractCodeAdapter<*>
122+
var adapter: AbstractCodeAdapter<*>?
110123
get() {
111124
return rvCodeContent.adapter as AbstractCodeAdapter<*>
112125
}
113126
set(adapter) {
127+
rvCodeContent.adapter = adapter
114128
delayed { // prevent UI overhead & initialization inconsistency
115-
rvCodeContent.adapter = adapter
116129
state = ViewState.PRESENTED
117130
}
118131
}
@@ -157,13 +170,15 @@ class CodeView : RelativeLayout {
157170
*/
158171

159172
// default color theme provided by enum
160-
fun setColorTheme(colorTheme: ColorTheme) = addTask {
161-
adapter.colorTheme = colorTheme.with()
173+
fun colorTheme(colorTheme: ColorTheme): CodeView {
174+
this.colorTheme = colorTheme.with()
175+
return this
162176
}
163177

164178
// custom color theme provided by user
165-
fun setColorTheme(colorTheme: ColorThemeData) = addTask {
166-
adapter.colorTheme = colorTheme
179+
fun colorTheme(colorTheme: ColorThemeData): CodeView {
180+
this.colorTheme = colorTheme
181+
return this
167182
}
168183

169184
/**
@@ -172,19 +187,19 @@ class CodeView : RelativeLayout {
172187
*
173188
* @param language Language to highlight
174189
*/
175-
fun highlightCode(language: String) = addTask {
176-
adapter.highlightCode(language) {
177-
refreshAnimated()
190+
fun highlight(language: String? = null) {
191+
if (adapter == null) {
192+
throw IllegalStateException("Please set adapter or use codeContent() before highlight()")
178193
}
179-
}
180194

181-
/**
182-
* Highlight code with trying to classify by code snippet.
183-
* It shows not highlighted code & then when classified refreshes view.
184-
*/
185-
fun highlightCode() = addTask {
186-
adapter.highlightCode {
187-
refreshAnimated()
195+
adapter?.highlight(language) {
196+
animate().setDuration(Utils.DELAY * 2)// * 2 to wait notifyDataSetChanged
197+
.alpha(.1f)
198+
199+
delayed {
200+
animate().alpha(1f)
201+
adapter?.notifyDataSetChanged()
202+
}
188203
}
189204
}
190205

@@ -194,15 +209,24 @@ class CodeView : RelativeLayout {
194209
*
195210
* @param listener Code line click listener
196211
*/
197-
fun setCodeListener(listener: OnCodeLineClickListener) = addTask {
198-
adapter.codeListener = listener
212+
fun codeListener(listener: OnCodeLineClickListener): CodeView {
213+
if (adapter == null) {
214+
throw IllegalStateException("Please set adapter or use codeContent() before highlight()")
215+
}
216+
217+
adapter?.codeListener = listener
218+
return this
199219
}
200220

201221
/**
202222
* Remove code listener.
203223
*/
204224
fun removeCodeListener() = addTask {
205-
adapter.codeListener = null
225+
if (adapter == null) {
226+
throw IllegalStateException("Please set adapter or use codeContent() before highlight()")
227+
}
228+
229+
adapter?.codeListener = null
206230
}
207231

208232
/**
@@ -222,7 +246,7 @@ class CodeView : RelativeLayout {
222246
*
223247
* @param content Code content
224248
*/
225-
fun setCodeContent(content: String) {
249+
fun codeContent(content: String): CodeView {
226250
when (state) {
227251
ViewState.BUILD ->
228252
build(content)
@@ -233,6 +257,8 @@ class CodeView : RelativeLayout {
233257
ViewState.PRESENTED ->
234258
update(content)
235259
}
260+
261+
return this
236262
}
237263

238264
/**
@@ -249,8 +275,9 @@ class CodeView : RelativeLayout {
249275
measurePlaceholder(linesCount)
250276
state = ViewState.PREPARE
251277

278+
rvCodeContent.adapter = CodeWithNotesAdapter(context, content, colorTheme)
279+
252280
delayed {
253-
rvCodeContent.adapter = CodeWithNotesAdapter(context, content)
254281
processBuildTasks()
255282
setupShadows()
256283
hidePlaceholder()
@@ -266,7 +293,7 @@ class CodeView : RelativeLayout {
266293
private fun update(content: String) {
267294
state = ViewState.PREPARE
268295
measurePlaceholder(extractLines(content).size)
269-
adapter.updateCodeContent(content)
296+
adapter?.updateCodeContent(content)
270297
hidePlaceholder()
271298
state = ViewState.PRESENTED
272299
}
@@ -277,7 +304,7 @@ class CodeView : RelativeLayout {
277304
* Border shadows will shown if presented full code listing.
278305
* It helps user to see what part of content are scrolled & hidden.
279306
*/
280-
private fun setupShadows() = setShadowsVisible(!adapter.isFullShowing)
307+
private fun setupShadows() = setShadowsVisible(!adapter?.isFullShowing!!)
281308

282309
/**
283310
* Placeholder fills space at start and stretched to marked up view size
@@ -287,15 +314,11 @@ class CodeView : RelativeLayout {
287314
*/
288315
private fun measurePlaceholder(linesCount: Int) {
289316
val lineHeight = dpToPx(context, 24)
290-
val topPadding = dpToPx(context, 8)
291-
292-
// double padding (top & bottom), one is enough for single line view
293-
val padding = (if (linesCount > 1) 2 else 1) * topPadding
317+
val verticalPadding = dpToPx(context, 8)
294318

295-
val height = linesCount * lineHeight + padding
319+
val height = linesCount * lineHeight + verticalPadding
296320

297-
vPlaceholder.layoutParams = LayoutParams(
298-
LayoutParams.MATCH_PARENT, height)
321+
vPlaceholder.layoutParams = LayoutParams(LayoutParams.MATCH_PARENT, height)
299322

300323
vPlaceholder.alpha = 1f
301324
}
@@ -306,14 +329,6 @@ class CodeView : RelativeLayout {
306329
.setDuration(350)
307330
.alpha(0f)
308331
.didAnimated { vPlaceholder.alpha = 0f }
309-
310-
private fun refreshAnimated() = animate()
311-
.setDuration(150)
312-
.alpha(.2f)
313-
.didAnimated {
314-
adapter.notifyDataSetChanged()
315-
animate().alpha(1f)
316-
}
317332
}
318333

319334
/**

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

+7-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ import java.io.BufferedReader
1010
import java.io.InputStreamReader
1111
import java.util.concurrent.Executors
1212

13+
class Utils {
14+
companion object {
15+
val DELAY: Long = 250
16+
}
17+
}
18+
1319
/**
1420
* Get px by dip value.
1521
*
@@ -75,7 +81,7 @@ object Thread {
7581
* @param body Operation body
7682
* @param delayMs Delay in m
7783
*/
78-
fun delayed(delayMs: Long = 150, body: () -> Unit) = Handler().postDelayed(body, delayMs)
84+
fun delayed(delayMs: Long = Utils.DELAY, body: () -> Unit) = Handler().postDelayed(body, delayMs)
7985

8086
// - Extensions for block manipulations
8187

0 commit comments

Comments
 (0)