Skip to content

Commit f68c1e7

Browse files
authored
Merge pull request #17 from Softwee/develop
Fixed animation issues
2 parents 7f5b38b + 8699eb4 commit f68c1e7

File tree

2 files changed

+46
-35
lines changed

2 files changed

+46
-35
lines changed

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

+10-7
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ class CodeView : RelativeLayout {
122122
/**
123123
* Add task to build queue. Otherwise (for prepared view) performs
124124
* task delayed or immediately (for built view).
125+
*
125126
* A little part of view builder.
126127
*
127128
* @param task Task to process
@@ -149,8 +150,8 @@ class CodeView : RelativeLayout {
149150
// - View builder
150151

151152
/**
152-
* Specify color theme: syntax colors (need to highlighting) & related to
153-
* code view (numeration color & background, content backgrounds).
153+
* Specify color theme: syntax colors (need to highlighting) & related
154+
* to code view (numeration color & background, content backgrounds).
154155
*
155156
* @param colorTheme Default or custom color theme
156157
*/
@@ -172,7 +173,9 @@ class CodeView : RelativeLayout {
172173
* @param language Language to highlight
173174
*/
174175
fun highlightCode(language: String) = addTask {
175-
adapter.highlightCode(language)
176+
adapter.highlightCode(language) {
177+
refreshAnimated()
178+
}
176179
}
177180

178181
/**
@@ -208,7 +211,7 @@ class CodeView : RelativeLayout {
208211
* @param isVisible Shadows visibility
209212
*/
210213
fun setShadowsVisible(isVisible: Boolean = true) = addTask {
211-
val visibility = if (isVisible) View.VISIBLE else GONE
214+
val visibility = if (isVisible) VISIBLE else GONE
212215
vShadowRight.visibility = visibility
213216
vShadowBottomLine.visibility = visibility
214217
vShadowBottomContent.visibility = visibility
@@ -237,7 +240,7 @@ class CodeView : RelativeLayout {
237240
*
238241
* When layout have multiple code views it becomes a very expensive task.
239242
* Some task proceeds asynchronously, some not, but what is key point:
240-
* it should starts delayed a little bit to show necessary UI immediately.
243+
* it should be started delayed a little bit to show necessary UI immediately.
241244
*
242245
* @param content Code content
243246
*/
@@ -293,7 +296,7 @@ class CodeView : RelativeLayout {
293296

294297
vPlaceholder.layoutParams = LayoutParams(
295298
LayoutParams.MATCH_PARENT, height)
296-
vPlaceholder.visibility = View.VISIBLE
299+
vPlaceholder.alpha = 1f
297300
}
298301

299302
// - Animations
@@ -302,7 +305,7 @@ class CodeView : RelativeLayout {
302305
.setDuration(350)
303306
.alpha(0f)
304307
.didAnimated {
305-
vPlaceholder.visibility = View.GONE
308+
vPlaceholder.alpha = 0f
306309
}
307310

308311
private fun refreshAnimated() = animate()

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

+36-28
Original file line numberDiff line numberDiff line change
@@ -128,31 +128,16 @@ abstract class AbstractCodeAdapter<T> : RecyclerView.Adapter<AbstractCodeAdapter
128128
notifyDataSetChanged()
129129
}
130130

131-
/**
132-
* Highlight code content.
133-
*
134-
* @param language Programming language to highlight
135-
*/
136-
fun highlightCode(language: String) {
137-
async() {
138-
highlighting(language)
139-
}
140-
}
141-
142131
/**
143132
* Highlight code content.
144133
*
145134
* @param onReady Callback when content is highlighted
135+
* @param language Programming language to highlight
146136
*/
147-
fun highlightCode(onReady: () -> Unit) {
137+
fun highlightCode(language: String? = null, onReady: () -> Unit) {
148138
async() {
149-
val processor = CodeProcessor.getInstance(mContext)
150-
151-
val language = if (processor.isTrained)
152-
processor.classify(mContent).get()
153-
else CodeClassifier.DEFAULT_LANGUAGE
154-
155-
highlighting(language, onReady)
139+
val classifiedLanguage = language ?: classifyContent()
140+
highlighting(classifiedLanguage, onReady)
156141
}
157142
}
158143

@@ -168,23 +153,46 @@ abstract class AbstractCodeAdapter<T> : RecyclerView.Adapter<AbstractCodeAdapter
168153

169154
// - Helpers (for accessors)
170155

171-
private fun updateContent(codeLines: List<String>, onUpdated: () -> Unit) {
172-
ui {
173-
mLines = codeLines
174-
onUpdated()
175-
}
176-
}
156+
/**
157+
* Classify current code content.
158+
*
159+
* @return Classified language
160+
*/
161+
private fun classifyContent(): String {
162+
val processor = CodeProcessor.getInstance(mContext)
177163

178-
private fun refresh() = {
179-
notifyDataSetChanged()
164+
val language = if (processor.isTrained)
165+
processor.classify(mContent).get()
166+
else CodeClassifier.DEFAULT_LANGUAGE
167+
168+
return language
180169
}
181170

182-
private fun highlighting(language: String, onReady: () -> Unit = refresh()) {
171+
/**
172+
* Highlight code content by language.
173+
*
174+
* @param language Language to highlight
175+
* @param onReady Callback
176+
*/
177+
private fun highlighting(language: String, onReady: () -> Unit) {
183178
val code = CodeHighlighter.highlight(language, mContent, colorTheme)
184179
val lines = extractLines(code)
185180
updateContent(lines, onReady)
186181
}
187182

183+
/**
184+
* Return control to UI-thread when highlighted content is ready.
185+
*
186+
* @param codeLines Highlighted code lines
187+
* @param onUpdated Control callback
188+
*/
189+
private fun updateContent(codeLines: List<String>, onUpdated: () -> Unit) {
190+
ui {
191+
mLines = codeLines
192+
onUpdated()
193+
}
194+
}
195+
188196
private fun showAllBottomNote() = mContext.getString(R.string.show_all)
189197

190198
private fun monoTypeface() = MonoFontCache.getInstance(mContext).typeface

0 commit comments

Comments
 (0)