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
7 changes: 7 additions & 0 deletions codeview/src/main/java/io/github/kbiakov/codeview/CodeView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,13 @@ class CodeView : RelativeLayout {
adapter.codeListener = listener
}

/**
* Remove code listener.
*/
fun removeCodeListener() = addTask {
adapter.codeListener = null
}

/**
* Control shadows visibility to provide more sensitive UI.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,10 @@ abstract class AbstractCodeAdapter<T> : RecyclerView.Adapter<AbstractCodeAdapter
*
* @param context Context
* @param entity Entity to init view
* @param isFirst Is first footer view
* @return Footer view
*/
abstract fun createFooter(context: Context, entity: T): View
abstract fun createFooter(context: Context, entity: T, isFirst: Boolean): View

// - Helpers (for accessors)

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

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

holder.llLineFooter.addView(footerView)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.github.kbiakov.codeview.adapters

import android.content.Context
import io.github.kbiakov.codeview.views.DiffModel
import io.github.kbiakov.codeview.views.LineDiffView

/**
* @class CodeWithDiffsAdapter
*
* Code content adapter with ability to add diffs (additions & deletions) in footer.
*
* @author Kirill Biakov
*/
class CodeWithDiffsAdapter : AbstractCodeAdapter<DiffModel> {
/**
* Default constructor.
*/
constructor(context: Context, content: String) : super(context, content)

/**
* Create footer view.
*
* @param context Context
* @param entity Note content
* @param isFirst Is first footer
*/
override fun createFooter(context: Context, entity: DiffModel, isFirst: Boolean) =
LineDiffView.create(context, entity)
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ class CodeWithNotesAdapter : AbstractCodeAdapter<String> {
*
* @param context Context
* @param entity Note content
* @param isFirst Is first footer view
*/
override fun createFooter(context: Context, entity: String) =
override fun createFooter(context: Context, entity: String, isFirst: Boolean) =
LineNoteView.create(context,
text = entity,
isFirst = isFirst,
bgColor = colorTheme.bgNum.color(),
textColor = colorTheme.noteColor.color())
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package io.github.kbiakov.codeview.views

import android.content.Context
import android.support.v4.content.ContextCompat
import android.view.LayoutInflater
import android.widget.RelativeLayout
import android.widget.TextView
import io.github.kbiakov.codeview.R
import io.github.kbiakov.codeview.highlight.MonoFontCache

/**
* @class CodeDiffView
*
* View to present code difference (additions & deletions).
*
* @author Kirill Biakov
*/
class LineDiffView : RelativeLayout {

private val tvLineDiff: TextView
private val tvLineContent: TextView

/**
* Default constructor.
*/
constructor(context: Context) : super(context) {
val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
inflater.inflate(R.layout.item_code_diff, this, true)

tvLineDiff = findViewById(R.id.tv_line_diff) as TextView
tvLineContent = findViewById(R.id.tv_line_content) as TextView
}

companion object Factory {
/**
* Simple factory method to create code diff view.
*
* @param context Context
* @param model Diff model
* @return Created line diff view
*/
fun create(context: Context, model: DiffModel): LineDiffView {
val diffView = LineDiffView(context)
diffView.tvLineDiff.text = if (model.isAddition) "+" else "-"
diffView.tvLineContent.text = model.content
diffView.tvLineContent.typeface = MonoFontCache.getInstance(context).typeface

diffView.setBackgroundColor(ContextCompat.getColor(context,
if (model.isAddition)
R.color.diff_add_background
else R.color.diff_del_background))

return diffView
}
}
}

/**
* Model to provide code difference (additions & deletions).
*
* @author Kirill Biako
*/
data class DiffModel(val content: String, val isAddition: Boolean = true)
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package io.github.kbiakov.codeview.views

import android.content.Context
import android.widget.TextView
import io.github.kbiakov.codeview.R
import io.github.kbiakov.codeview.dpToPx

/**
* @class LineNoteView
Expand All @@ -18,17 +20,25 @@ class LineNoteView(context: Context?) : TextView(context) {
*
* @param context Context
* @param text Note text
* @param isFirst Is first footer view
* @param bgColor Background color
* @param textColor Text Color
* @return Created line note view
*/
fun create(context: Context, text: String, bgColor: Int, textColor: Int): LineNoteView {
fun create(context: Context, text: String, isFirst: Boolean, bgColor: Int, textColor: Int): LineNoteView {
val noteView = LineNoteView(context)
noteView.textSize = 12f
noteView.text = text
noteView.setTextColor(textColor)
noteView.setBackgroundColor(bgColor)

val dp8 = dpToPx(context, 8)

val leftPadding = context.resources.getDimension(
R.dimen.line_num_width).toInt() + dpToPx(context, 14)

noteView.setPadding(leftPadding, if (isFirst) dp8 else 0, dp8, dp8)

return noteView
}
}
Expand Down
29 changes: 29 additions & 0 deletions codeview/src/main/res/layout/item_code_diff.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
android:id="@+id/tv_line_diff"
android:layout_width="@dimen/line_num_width"
android:layout_height="@dimen/line_height"
android:gravity="center"
android:fontFamily="monospace"
android:textSize="@dimen/line_text_size"
android:text="@string/stub_line_num"/>

<TextView
android:id="@+id/tv_line_content"
android:layout_width="wrap_content"
android:layout_height="@dimen/line_height"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_toRightOf="@+id/tv_line_diff"
android:gravity="center_vertical"
android:fontFamily="monospace"
android:singleLine="true"
android:textSize="@dimen/line_text_size"
android:text="@string/stub_line_content"/>

</RelativeLayout>
10 changes: 5 additions & 5 deletions codeview/src/main/res/layout/item_code_line.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,24 @@

<TextView
android:id="@+id/tv_line_num"
android:layout_width="32dp"
android:layout_height="24dp"
android:layout_width="@dimen/line_num_width"
android:layout_height="@dimen/line_height"
android:gravity="center"
android:fontFamily="monospace"
android:textSize="12sp"
android:textSize="@dimen/line_text_size"
android:text="@string/stub_line_num"/>

<TextView
android:id="@+id/tv_line_content"
android:layout_width="wrap_content"
android:layout_height="24dp"
android:layout_height="@dimen/line_height"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_toRightOf="@+id/tv_line_num"
android:gravity="center_vertical"
android:fontFamily="monospace"
android:singleLine="true"
android:textSize="12sp"
android:textSize="@dimen/line_text_size"
android:text="@string/stub_line_content"/>

<LinearLayout
Expand Down
3 changes: 3 additions & 0 deletions codeview/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@
<color name="code_content">#2c2d30</color>
<color name="code_content_background">#e9edf4</color>
<color name="note">#4c5d6e</color>

<color name="diff_add_background">#EAFFEA</color>
<color name="diff_del_background">#FFECEC</color>
</resources>
6 changes: 6 additions & 0 deletions codeview/src/main/res/values/dimens.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="line_num_width">32dp</dimen>
<dimen name="line_height">24dp</dimen>
<dimen name="line_text_size">12sp</dimen>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public CustomAdapter(@NotNull Context context, @NotNull String content) {

@NotNull
@Override
public View createFooter(@NotNull Context context, CustomModel entity) {
public View createFooter(@NotNull Context context, CustomModel entity, boolean isFirst) {
View footerView = LayoutInflater.from(context).inflate(R.layout.custom_footer, null);
((TextView) footerView.findViewById(R.id.tv_footer_title)).setText(entity.firstName);
((TextView) footerView.findViewById(R.id.tv_footer_description)).setText(entity.lastName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@

import org.jetbrains.annotations.NotNull;

import io.github.kbiakov.codeview.adapters.CodeWithDiffsAdapter;
import io.github.kbiakov.codeview.adapters.CodeWithNotesAdapter;
import io.github.kbiakov.codeview.highlight.ColorTheme;
import io.github.kbiakov.codeview.CodeView;
import io.github.kbiakov.codeview.OnCodeLineClickListener;
import io.github.kbiakov.codeview.views.DiffModel;

public class ListingsActivity extends AppCompatActivity {

Expand Down Expand Up @@ -64,5 +66,17 @@ public void onCodeLineClicked(int n, @NotNull String line) {
adapter.addFooterEntity(n, new CustomAdapter.CustomModel("Line " + (n + 1), line));
}
});

/**
* 4: diff adapter with footer views
*/

final CodeWithDiffsAdapter diffsAdapter = new CodeWithDiffsAdapter(this, getString(R.string.listing_py));
codeView.setAdapter(diffsAdapter);
codeView.highlightCode("python");
codeView.removeCodeListener();

diffsAdapter.addFooterEntity(15, new DiffModel(getString(R.string.py_addition_16), true));
diffsAdapter.addFooterEntity(10, new DiffModel(getString(R.string.py_deletion_11), false));
}
}
60 changes: 34 additions & 26 deletions example/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,32 +41,6 @@
&lt;SCRIPT language=\"JavaScript\"&gt;fulltime();&lt;/SCRIPT&gt;
</string>

<string name="listing_py" formatted="false">
from timeit import Timer\n
\n
tmp = \"Python 3.2.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32.\"\n
\n
def case1(): # А. инкрементальные конкатенации в цикле\n
s = \"\"\n
for i in range(10000):\n
s += tmp\n

def case2(): # Б. через промежуточный список и метод join\n
s = []\n
for i in range(10000):\n
s.append(tmp)\n
s = \"\".join(s)\n
\n
def case3(): # В. списковое выражение и метод join\n
return \"\".join([tmp for i in range(10000)])\n
\n
def case4(): # Г. генераторное выражение и метод join\n
return \"\".join(tmp for i in range(10000))\n
\n
for v in range(1,5):\n
print (Timer(\"func()\",\"from __main__ import case%s as func\" % v).timeit(200))\n
</string>

<string name="listing_md">
3. CodeView and related adapter.\n
\n
Expand Down Expand Up @@ -119,4 +93,38 @@
}
</string>

<string name="listing_py" formatted="false">
from timeit import Timer\n
\n
tmp = \"Python 3.2.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32.\"\n
\n
def case1(): # А. инкрементальные конкатенации в цикле\n
s = \"\"\n
for i in range(10000):\n
s += tmp\n

def case2(): # Б. через промежуточный список и метод join\n
s = []\n
for i in range(10000):\n
s.append(tmp)\n
s = \"\".join(s)\n
\n
def case3(): # В. списковое выражение и метод join\n
return \"\".join([tmp for i in range(10000)])\n
\n
def case4(): # Г. генераторное выражение и метод join\n
return \"\".join(tmp for i in range(10000))\n
\n
for v in range(1,5):\n
print (Timer(\"func()\",\"from __main__ import case%s as func\" % v).timeit(200))\n
</string>

<string name="py_addition_16">
return \"\".join([tmp for i in range(10000)])
</string>

<string name="py_deletion_11">
for i in range(100500):
</string>

</resources>