Commit e1913469 authored by Konstantin Tskhovrebov's avatar Konstantin Tskhovrebov 🤖

Add GitDiffView.

parent add4cbdc
......@@ -8,7 +8,6 @@ import kotlinx.android.synthetic.main.item_merge_request_change.view.*
import ru.terrakok.gitlabclient.R
import ru.terrakok.gitlabclient.entity.mergerequest.MergeRequestChange
import ru.terrakok.gitlabclient.extension.inflate
import ru.terrakok.gitlabclient.ui.global.GitDiffViewController
/**
* Created by Eugene Shapovalov (@CraggyHaggy) on 26.10.18.
......@@ -35,8 +34,6 @@ class MergeRequestChangeAdapterDelegate : AdapterDelegate<MutableList<MergeReque
private inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
private lateinit var mergeRequestChange: MergeRequestChange
private val gitDiffViewController: GitDiffViewController = GitDiffViewController(itemView.changeFile)
fun bind(mergeRequestChange: MergeRequestChange) {
this.mergeRequestChange = mergeRequestChange
with(itemView) {
......@@ -52,20 +49,20 @@ class MergeRequestChangeAdapterDelegate : AdapterDelegate<MutableList<MergeReque
val index = it.lastIndexOf("/")
it.substring(if (index != -1) index + 1 else 0)
}
gitDiffViewController.setText(mergeRequestChange.diff)
gitDiffView.setGitDiffText(mergeRequestChange.diff)
changeAddedCount.text = context.getString(
R.string.merge_request_changes_added_count,
gitDiffViewController.getAddedLineCount()
gitDiffView.getAddedLineCount()
)
changeDeletedCount.text = context.getString(
R.string.merge_request_changes_deleted_count,
gitDiffViewController.getDeletedLineCount()
gitDiffView.getDeletedLineCount()
)
}
}
fun recycle() {
gitDiffViewController.release()
itemView.gitDiffView.release()
}
}
}
\ No newline at end of file
......@@ -14,19 +14,23 @@
* limitations under the License.
*/
package ru.terrakok.gitlabclient.ui.global
package ru.terrakok.gitlabclient.ui.global.view.custom
import android.content.Context
import android.graphics.*
import android.text.SpannableString
import android.text.SpannableStringBuilder
import android.text.TextUtils
import android.text.style.ForegroundColorSpan
import android.text.style.LineBackgroundSpan
import android.util.AttributeSet
import android.widget.TextView
class GitDiffViewController(
private val view: TextView
) {
class GitDiffView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : TextView(context, attrs, defStyleAttr) {
private val additionColor: Int = Color.parseColor("#CCFFCC")
private val deletionColor: Int = Color.parseColor("#FFDDDD")
......@@ -36,7 +40,7 @@ class GitDiffViewController(
private var addedLineCount = 0
private var deletedLineCount = 0
fun setText(text: CharSequence) {
fun setGitDiffText(text: CharSequence) {
if (!TextUtils.isEmpty(text)) {
val diff = text.toString()
val split = diff.split("\\r?\\n|\\r".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
......@@ -72,7 +76,7 @@ class GitDiffViewController(
val spannableDiff = SpannableString(token)
// Span for line color (where transparent is considered as default)
if (color != Color.TRANSPARENT) {
val span = DiffLineSpan(color, view.paddingLeft)
val span = DiffLineSpan(color, paddingLeft)
spannableDiff.setSpan(span, 0, token.length, SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE)
}
// Span for text color (where transparent is considered as default)
......@@ -84,10 +88,10 @@ class GitDiffViewController(
builder.append(spannableDiff)
}
}
view.text = builder
setText(builder)
}
} else {
view.text = text
setText(text)
}
}
......
......@@ -73,8 +73,8 @@
app:layout_constraintTop_toTopOf="@id/changeFileName"
tools:text="-2" />
<TextView
android:id="@+id/changeFile"
<ru.terrakok.gitlabclient.ui.global.view.custom.GitDiffView
android:id="@+id/gitDiffView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment