Skip to content

Instantly share code, notes, and snippets.

View muthuraj57's full-sized avatar

Muthuraj muthuraj57

View GitHub Profile
abstract class RecyclerBaseAdapter : RecyclerView.Adapter<RecyclerViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) =
RecyclerViewHolder(DataBindingUtil.inflate<ViewDataBinding>(LayoutInflater.from(parent.context), viewType, parent, false))
override fun onBindViewHolder(holder: RecyclerViewHolder, position: Int) {
getViewModel(position)
?.let {
val bindingSuccess = holder.binding.setVariable(BR.viewModel, it)
if (!bindingSuccess) {
@muthuraj57
muthuraj57 / ScanMap.kt
Created September 19, 2017 07:09
Variant of scan operator but supports different return type than source observable. Used mainly to calculate Diff for recyclerView.
inline fun <T, R> Observable<List<T>>.scanMap(crossinline func2: (List<T>, List<T>) -> R): Observable<R> {
return this.startWith(emptyList<T>()) //emit a empty list first, otherwise the .buffer() below won't emit at first (needs 2 emissions to emit)
.buffer(2, 1) //buffer the previous and current emission
.filter { it.size >= 2 }
.map { func2.invoke(it[0], it[1]) }
}
@muthuraj57
muthuraj57 / ScanMap.kt
Created September 19, 2017 07:09
Variant of scan operator but supports different return type than source observable. Used mainly to calculate Diff for recyclerView.
inline fun <T, R> Observable<List<T>>.scanMap(crossinline func2: (List<T>, List<T>) -> R): Observable<R> {
return this.startWith(emptyList<T>()) //emit a empty list first, otherwise the .buffer() below won't emit at first (needs 2 emissions to emit)
.buffer(2, 1) //buffer the previous and current emission
.filter { it.size >= 2 }
.map { func2.invoke(it[0], it[1]) }
}
public class AvatarView extends AppCompatImageView {
/*
* Path of them image to be clipped (to be shown)
* */
Path clipPath;
/*
* Place holder drawable (with background color and initials)
* */
@Override
protected void onDraw(Canvas canvas) {
if (shape == RECTANGLE) {
canvas.drawRoundRect(rectF, cornerRadius, cornerRadius, borderPaint);
clipPath.addRoundRect(rectF, cornerRadius, cornerRadius, Path.Direction.CW);
} else {
canvas.drawCircle(rectF.centerX(), rectF.centerY(), (rectF.height() / 2) - borderWidth, borderPaint);
clipPath.addCircle(rectF.centerX(), rectF.centerY(), (rectF.height() / 2), Path.Direction.CW);
/*
* Set the canvas bounds here
* */
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int screenWidth = MeasureSpec.getSize(widthMeasureSpec);
int screenHeight = MeasureSpec.getSize(heightMeasureSpec);
rectF.set(0, 0, screenWidth, screenHeight);
}
/*
* Create placeholder drawable
* */
private void setDrawable() {
drawable = new Drawable() {
@Override
public void draw(@NonNull Canvas canvas) {
int centerX = Math.round(canvas.getWidth() * 0.5f);
int centerY = Math.round(canvas.getHeight() * 0.5f);
/*
* Return user short name
* */
public static String getShortName(String name) {
String[] strings = name.split(" ");//no i18n
String shortName;
if (strings.length == 1) {
shortName = strings[0].substring(0, 2);
} else {
shortName = strings[0].substring(0, 1) + strings[1].substring(0, 1);
/*
* Initialize fields
* */
protected void init() {
rectF = new RectF();
clipPath = new Path();
imageSize = getResources().getDimensionPixelSize(R.dimen.avatar_size);
cornerRadius = (int) Utils.dpToPixel(2, getResources());
/*
* Path of them image to be clipped (to be shown)
* */
Path clipPath;
/*
* Place holder drawable (with background color and initials)
* */
Drawable drawable;