|
| 1 | +package io.github.kbiakov.codeview.views |
| 2 | + |
| 3 | +import android.content.Context |
| 4 | +import android.support.v4.content.ContextCompat |
| 5 | +import android.view.LayoutInflater |
| 6 | +import android.widget.RelativeLayout |
| 7 | +import android.widget.TextView |
| 8 | +import io.github.kbiakov.codeview.R |
| 9 | +import io.github.kbiakov.codeview.highlight.MonoFontCache |
| 10 | + |
| 11 | +/** |
| 12 | + * @class CodeDiffView |
| 13 | + * |
| 14 | + * View to present code difference (additions & deletions). |
| 15 | + * |
| 16 | + * @author Kirill Biakov |
| 17 | + */ |
| 18 | +class LineDiffView : RelativeLayout { |
| 19 | + |
| 20 | + private val tvLineDiff: TextView |
| 21 | + private val tvLineContent: TextView |
| 22 | + |
| 23 | + /** |
| 24 | + * Default constructor. |
| 25 | + */ |
| 26 | + constructor(context: Context) : super(context) { |
| 27 | + val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater |
| 28 | + inflater.inflate(R.layout.item_code_diff, this, true) |
| 29 | + |
| 30 | + tvLineDiff = findViewById(R.id.tv_line_diff) as TextView |
| 31 | + tvLineContent = findViewById(R.id.tv_line_content) as TextView |
| 32 | + } |
| 33 | + |
| 34 | + companion object Factory { |
| 35 | + /** |
| 36 | + * Simple factory method to create code diff view. |
| 37 | + * |
| 38 | + * @param context Context |
| 39 | + * @param model Diff model |
| 40 | + * @return Created line diff view |
| 41 | + */ |
| 42 | + fun create(context: Context, model: DiffModel): LineDiffView { |
| 43 | + val diffView = LineDiffView(context) |
| 44 | + diffView.tvLineDiff.text = if (model.isAddition) "+" else "-" |
| 45 | + diffView.tvLineContent.text = model.content |
| 46 | + diffView.tvLineContent.typeface = MonoFontCache.getInstance(context).typeface |
| 47 | + |
| 48 | + diffView.setBackgroundColor(ContextCompat.getColor(context, |
| 49 | + if (model.isAddition) |
| 50 | + R.color.diff_add_background |
| 51 | + else R.color.diff_del_background)) |
| 52 | + |
| 53 | + return diffView |
| 54 | + } |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +/** |
| 59 | + * Model to provide code difference (additions & deletions). |
| 60 | + * |
| 61 | + * @author Kirill Biako |
| 62 | + */ |
| 63 | +data class DiffModel(val content: String, val isAddition: Boolean = true) |
0 commit comments