Skip to content

Commit

Permalink
Merge pull request #17 from Softwee/develop
Browse files Browse the repository at this point in the history
Fixed animation issues
  • Loading branch information
kbiakov authored Aug 31, 2016
2 parents 7f5b38b + 8699eb4 commit f68c1e7
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 35 deletions.
17 changes: 10 additions & 7 deletions codeview/src/main/java/io/github/kbiakov/codeview/CodeView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ class CodeView : RelativeLayout {
/**
* Add task to build queue. Otherwise (for prepared view) performs
* task delayed or immediately (for built view).
*
* A little part of view builder.
*
* @param task Task to process
Expand Down Expand Up @@ -149,8 +150,8 @@ class CodeView : RelativeLayout {
// - View builder

/**
* Specify color theme: syntax colors (need to highlighting) & related to
* code view (numeration color & background, content backgrounds).
* Specify color theme: syntax colors (need to highlighting) & related
* to code view (numeration color & background, content backgrounds).
*
* @param colorTheme Default or custom color theme
*/
Expand All @@ -172,7 +173,9 @@ class CodeView : RelativeLayout {
* @param language Language to highlight
*/
fun highlightCode(language: String) = addTask {
adapter.highlightCode(language)
adapter.highlightCode(language) {
refreshAnimated()
}
}

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

vPlaceholder.layoutParams = LayoutParams(
LayoutParams.MATCH_PARENT, height)
vPlaceholder.visibility = View.VISIBLE
vPlaceholder.alpha = 1f
}

// - Animations
Expand All @@ -302,7 +305,7 @@ class CodeView : RelativeLayout {
.setDuration(350)
.alpha(0f)
.didAnimated {
vPlaceholder.visibility = View.GONE
vPlaceholder.alpha = 0f
}

private fun refreshAnimated() = animate()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,31 +128,16 @@ abstract class AbstractCodeAdapter<T> : RecyclerView.Adapter<AbstractCodeAdapter
notifyDataSetChanged()
}

/**
* Highlight code content.
*
* @param language Programming language to highlight
*/
fun highlightCode(language: String) {
async() {
highlighting(language)
}
}

/**
* Highlight code content.
*
* @param onReady Callback when content is highlighted
* @param language Programming language to highlight
*/
fun highlightCode(onReady: () -> Unit) {
fun highlightCode(language: String? = null, onReady: () -> Unit) {
async() {
val processor = CodeProcessor.getInstance(mContext)

val language = if (processor.isTrained)
processor.classify(mContent).get()
else CodeClassifier.DEFAULT_LANGUAGE

highlighting(language, onReady)
val classifiedLanguage = language ?: classifyContent()
highlighting(classifiedLanguage, onReady)
}
}

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

// - Helpers (for accessors)

private fun updateContent(codeLines: List<String>, onUpdated: () -> Unit) {
ui {
mLines = codeLines
onUpdated()
}
}
/**
* Classify current code content.
*
* @return Classified language
*/
private fun classifyContent(): String {
val processor = CodeProcessor.getInstance(mContext)

private fun refresh() = {
notifyDataSetChanged()
val language = if (processor.isTrained)
processor.classify(mContent).get()
else CodeClassifier.DEFAULT_LANGUAGE

return language
}

private fun highlighting(language: String, onReady: () -> Unit = refresh()) {
/**
* Highlight code content by language.
*
* @param language Language to highlight
* @param onReady Callback
*/
private fun highlighting(language: String, onReady: () -> Unit) {
val code = CodeHighlighter.highlight(language, mContent, colorTheme)
val lines = extractLines(code)
updateContent(lines, onReady)
}

/**
* Return control to UI-thread when highlighted content is ready.
*
* @param codeLines Highlighted code lines
* @param onUpdated Control callback
*/
private fun updateContent(codeLines: List<String>, onUpdated: () -> Unit) {
ui {
mLines = codeLines
onUpdated()
}
}

private fun showAllBottomNote() = mContext.getString(R.string.show_all)

private fun monoTypeface() = MonoFontCache.getInstance(mContext).typeface
Expand Down

0 comments on commit f68c1e7

Please sign in to comment.