Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions codeview/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,15 @@ dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'

compile 'com.android.support:appcompat-v7:24.1.1'
compile 'com.android.support:recyclerview-v7:24.1.1'
compile 'com.github.twalcari:java-prettify:1.2.2'
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
compile group: 'org.jetbrains.kotlin', name: 'kotlin-reflect', version: '1.0.3'

compile 'com.android.support:appcompat-v7:24.2.0'
compile 'com.android.support:recyclerview-v7:24.2.0'
compile 'com.github.twalcari:java-prettify:1.2.2'
}
repositories {
mavenCentral()
}
buildscript {
}
64 changes: 36 additions & 28 deletions codeview/src/main/java/io/github/kbiakov/codeview/CodeView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@ package io.github.kbiakov.codeview
import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.content.Context
import android.os.Handler
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.view.ViewPropertyAnimator
import android.widget.RelativeLayout
import io.github.kbiakov.codeview.adapters.AbstractCodeAdapter
import io.github.kbiakov.codeview.Thread.delayed
import io.github.kbiakov.codeview.adapters.CodeWithNotesAdapter
import io.github.kbiakov.codeview.highlight.ColorTheme
import io.github.kbiakov.codeview.highlight.ColorThemeData
import io.github.kbiakov.codeview.highlight.color
import java.util.*

/**
Expand Down Expand Up @@ -55,7 +56,15 @@ class CodeView : RelativeLayout {
* (and awaiting for build) or view was built & code is presented.
*/
private var state: ViewState
set(newState) {
if (newState == ViewState.PRESENTED)
hidePlaceholder()
field = newState
}

/**
* Default constructor.
*/
constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
inflater.inflate(R.layout.layout_code_view, this, true)
Expand All @@ -69,13 +78,13 @@ class CodeView : RelativeLayout {
rvCodeContent.layoutManager = LinearLayoutManager(context)
rvCodeContent.isNestedScrollingEnabled = true

tasks = LinkedList()

state = ViewState.BUILD

tasks = LinkedList()
}

/**
* Code view states.
* Code view state to control build flow.
*/
enum class ViewState {
BUILD,
Expand All @@ -84,22 +93,28 @@ class CodeView : RelativeLayout {
}

/**
* Public getter for accessing view state.
* It may be useful if code view state is unknown.
* If code view was built it is not safe to use operations chaining.
* Public getters for checking view state.
* May be useful when code view state is unknown.
* If view was built it is unsafe to use operations chaining.
*
* @return Result of state check
*/
fun getState() = state
fun isBuilding() = state == ViewState.BUILD
fun isPrepared() = state == ViewState.PREPARE
fun isPresented() = state == ViewState.PRESENTED

/**
* Accessor/mutator to reduce frequently used actions.
*/
var adapter: CodeContentAdapter
var adapter: AbstractCodeAdapter<*>
get() {
return rvCodeContent.adapter as CodeContentAdapter
return rvCodeContent.adapter as AbstractCodeAdapter<*>
}
set(adapter) {
rvCodeContent.adapter = adapter
state = ViewState.PRESENTED
delayed { // to prevent UI overhead & initialization inconsistency
rvCodeContent.adapter = adapter
state = ViewState.PRESENTED
}
}

// - Build processor
Expand All @@ -116,7 +131,7 @@ class CodeView : RelativeLayout {
ViewState.BUILD ->
tasks.add(task)
ViewState.PREPARE ->
Thread.delayed(task)
delayed(body = task)
ViewState.PRESENTED ->
task()
}
Expand Down Expand Up @@ -202,7 +217,7 @@ class CodeView : RelativeLayout {
ViewState.BUILD ->
build(content)
ViewState.PREPARE ->
Thread.delayed {
delayed {
update(content)
}
ViewState.PRESENTED ->
Expand All @@ -224,8 +239,8 @@ class CodeView : RelativeLayout {
measurePlaceholder(linesCount)
state = ViewState.PREPARE

Thread.delayed {
rvCodeContent.adapter = CodeContentAdapter(context, content)
delayed {
rvCodeContent.adapter = CodeWithNotesAdapter(context, content)
processBuildTasks()
setupShadows()
hidePlaceholder()
Expand Down Expand Up @@ -264,13 +279,13 @@ class CodeView : RelativeLayout {
val lineHeight = dpToPx(context, 24)
val topPadding = dpToPx(context, 8)

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

val height = linesCount * lineHeight + padding

vPlaceholder.layoutParams = RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT, height)
vPlaceholder.layoutParams = LayoutParams(
LayoutParams.MATCH_PARENT, height)
vPlaceholder.visibility = View.VISIBLE
}

Expand All @@ -296,16 +311,9 @@ class CodeView : RelativeLayout {
* Provides listener to code line clicks.
*/
interface OnCodeLineClickListener {
fun onCodeLineClicked(n: Int)
fun onCodeLineClicked(n: Int, line: String)
}

/**
* Extension for delayed block call.
*
* @param body Operation body
*/
fun Thread.delayed(body: () -> Unit) = Handler().postDelayed(body, 150)

/**
* More readable form for animation listener (hi, iOS & Cocoa Touch!).
*
Expand Down
10 changes: 9 additions & 1 deletion codeview/src/main/java/io/github/kbiakov/codeview/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ fun extractLines(source: String) = listOf(*source.split("\n").toTypedArray())
* @param content Source
* @return Spanned HTML string
*/
@Suppress("DEPRECATION")
@Suppress("deprecation")
fun html(content: String): Spanned =
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N)
Html.fromHtml(content, Html.FROM_HTML_MODE_LEGACY)
Expand All @@ -69,6 +69,14 @@ object Thread {
Handler(Looper.getMainLooper()).post(body)
}

/**
* Delayed block call.
*
* @param body Operation body
* @param delayMs Delay in m
*/
fun delayed(delayMs: Long = 150, body: () -> Unit) = Handler().postDelayed(body, delayMs)

// - Extensions for block manipulations

fun (() -> Unit).async(isAsync: Boolean = true) {
Expand Down
Loading