Skip to content

Commit 4cfaaff

Browse files
authored
Merge pull request #13 from Softwee/feature/example.update
Created diff view as footer example
2 parents 6169a1c + 78a45f1 commit 4cfaaff

File tree

13 files changed

+208
-38
lines changed

13 files changed

+208
-38
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,13 @@ class CodeView : RelativeLayout {
195195
adapter.codeListener = listener
196196
}
197197

198+
/**
199+
* Remove code listener.
200+
*/
201+
fun removeCodeListener() = addTask {
202+
adapter.codeListener = null
203+
}
204+
198205
/**
199206
* Control shadows visibility to provide more sensitive UI.
200207
*

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,10 @@ abstract class AbstractCodeAdapter<T> : RecyclerView.Adapter<AbstractCodeAdapter
161161
*
162162
* @param context Context
163163
* @param entity Entity to init view
164+
* @param isFirst Is first footer view
164165
* @return Footer view
165166
*/
166-
abstract fun createFooter(context: Context, entity: T): View
167+
abstract fun createFooter(context: Context, entity: T, isFirst: Boolean): View
167168

168169
// - Helpers (for accessors)
169170

@@ -247,9 +248,7 @@ abstract class AbstractCodeAdapter<T> : RecyclerView.Adapter<AbstractCodeAdapter
247248
var isFirst = true
248249

249250
it.forEach { entity ->
250-
val footerView = createFooter(mContext, entity)
251-
val dp8 = dpToPx(mContext, 8)
252-
footerView.setPadding(dpToPx(mContext, 46), if (isFirst) dp8 else 0, dp8, dp8)
251+
val footerView = createFooter(mContext, entity, isFirst)
253252

254253
holder.llLineFooter.addView(footerView)
255254

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package io.github.kbiakov.codeview.adapters
2+
3+
import android.content.Context
4+
import io.github.kbiakov.codeview.views.DiffModel
5+
import io.github.kbiakov.codeview.views.LineDiffView
6+
7+
/**
8+
* @class CodeWithDiffsAdapter
9+
*
10+
* Code content adapter with ability to add diffs (additions & deletions) in footer.
11+
*
12+
* @author Kirill Biakov
13+
*/
14+
class CodeWithDiffsAdapter : AbstractCodeAdapter<DiffModel> {
15+
/**
16+
* Default constructor.
17+
*/
18+
constructor(context: Context, content: String) : super(context, content)
19+
20+
/**
21+
* Create footer view.
22+
*
23+
* @param context Context
24+
* @param entity Note content
25+
* @param isFirst Is first footer
26+
*/
27+
override fun createFooter(context: Context, entity: DiffModel, isFirst: Boolean) =
28+
LineDiffView.create(context, entity)
29+
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@ class CodeWithNotesAdapter : AbstractCodeAdapter<String> {
2222
*
2323
* @param context Context
2424
* @param entity Note content
25+
* @param isFirst Is first footer view
2526
*/
26-
override fun createFooter(context: Context, entity: String) =
27+
override fun createFooter(context: Context, entity: String, isFirst: Boolean) =
2728
LineNoteView.create(context,
2829
text = entity,
30+
isFirst = isFirst,
2931
bgColor = colorTheme.bgNum.color(),
3032
textColor = colorTheme.noteColor.color())
3133
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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)

codeview/src/main/java/io/github/kbiakov/codeview/views/LineNoteView.kt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package io.github.kbiakov.codeview.views
22

33
import android.content.Context
44
import android.widget.TextView
5+
import io.github.kbiakov.codeview.R
6+
import io.github.kbiakov.codeview.dpToPx
57

68
/**
79
* @class LineNoteView
@@ -18,17 +20,25 @@ class LineNoteView(context: Context?) : TextView(context) {
1820
*
1921
* @param context Context
2022
* @param text Note text
23+
* @param isFirst Is first footer view
2124
* @param bgColor Background color
2225
* @param textColor Text Color
2326
* @return Created line note view
2427
*/
25-
fun create(context: Context, text: String, bgColor: Int, textColor: Int): LineNoteView {
28+
fun create(context: Context, text: String, isFirst: Boolean, bgColor: Int, textColor: Int): LineNoteView {
2629
val noteView = LineNoteView(context)
2730
noteView.textSize = 12f
2831
noteView.text = text
2932
noteView.setTextColor(textColor)
3033
noteView.setBackgroundColor(bgColor)
3134

35+
val dp8 = dpToPx(context, 8)
36+
37+
val leftPadding = context.resources.getDimension(
38+
R.dimen.line_num_width).toInt() + dpToPx(context, 14)
39+
40+
noteView.setPadding(leftPadding, if (isFirst) dp8 else 0, dp8, dp8)
41+
3242
return noteView
3343
}
3444
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<RelativeLayout
3+
xmlns:android="http://schemas.android.com/apk/res/android"
4+
android:layout_width="match_parent"
5+
android:layout_height="wrap_content">
6+
7+
<TextView
8+
android:id="@+id/tv_line_diff"
9+
android:layout_width="@dimen/line_num_width"
10+
android:layout_height="@dimen/line_height"
11+
android:gravity="center"
12+
android:fontFamily="monospace"
13+
android:textSize="@dimen/line_text_size"
14+
android:text="@string/stub_line_num"/>
15+
16+
<TextView
17+
android:id="@+id/tv_line_content"
18+
android:layout_width="wrap_content"
19+
android:layout_height="@dimen/line_height"
20+
android:layout_marginLeft="16dp"
21+
android:layout_marginRight="16dp"
22+
android:layout_toRightOf="@+id/tv_line_diff"
23+
android:gravity="center_vertical"
24+
android:fontFamily="monospace"
25+
android:singleLine="true"
26+
android:textSize="@dimen/line_text_size"
27+
android:text="@string/stub_line_content"/>
28+
29+
</RelativeLayout>

codeview/src/main/res/layout/item_code_line.xml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,24 @@
66

77
<TextView
88
android:id="@+id/tv_line_num"
9-
android:layout_width="32dp"
10-
android:layout_height="24dp"
9+
android:layout_width="@dimen/line_num_width"
10+
android:layout_height="@dimen/line_height"
1111
android:gravity="center"
1212
android:fontFamily="monospace"
13-
android:textSize="12sp"
13+
android:textSize="@dimen/line_text_size"
1414
android:text="@string/stub_line_num"/>
1515

1616
<TextView
1717
android:id="@+id/tv_line_content"
1818
android:layout_width="wrap_content"
19-
android:layout_height="24dp"
19+
android:layout_height="@dimen/line_height"
2020
android:layout_marginLeft="16dp"
2121
android:layout_marginRight="16dp"
2222
android:layout_toRightOf="@+id/tv_line_num"
2323
android:gravity="center_vertical"
2424
android:fontFamily="monospace"
2525
android:singleLine="true"
26-
android:textSize="12sp"
26+
android:textSize="@dimen/line_text_size"
2727
android:text="@string/stub_line_content"/>
2828

2929
<LinearLayout

codeview/src/main/res/values/colors.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,7 @@
55
<color name="code_content">#2c2d30</color>
66
<color name="code_content_background">#e9edf4</color>
77
<color name="note">#4c5d6e</color>
8+
9+
<color name="diff_add_background">#EAFFEA</color>
10+
<color name="diff_del_background">#FFECEC</color>
811
</resources>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
<dimen name="line_num_width">32dp</dimen>
4+
<dimen name="line_height">24dp</dimen>
5+
<dimen name="line_text_size">12sp</dimen>
6+
</resources>

0 commit comments

Comments
 (0)