Skip to content

Instantly share code, notes, and snippets.

@jesselima
Last active April 17, 2019 17:56
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save jesselima/43f464ac8366cd555eb973784e9b543a to your computer and use it in GitHub Desktop.
It's a sample with binding adapters. Still need improvements and more samples.
package com.udacity.popularmovies.shared;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.databinding.BindingAdapter;
import androidx.databinding.BindingConversion;
import androidx.databinding.InverseMethod;
import com.squareup.picasso.Picasso;
import com.udacity.popularmovies.R;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.util.concurrent.TimeUnit;
/**
* Created by JesseFariasdeLima on 4/17/2019.
* This is a part of the project adnd-popular-movies.
*/
public class BindingConversions {
private static final String EMPTY_STRING = "";
private static final String LOG_TAG = BindingConversions.class.getSimpleName();
/////////////////////////////////////////////////
/**
* How to use this got set isVisible property
* <TextView
* ...
* app:isVisible="@{post.hasComments()}" />
*
* @param view
* @param isVisible
*/
@BindingAdapter("isVisible")
public static void setIsVisible(View view, boolean isVisible) {
if (isVisible) view.setVisibility(View.VISIBLE);
else view.setVisibility(View.INVISIBLE);
}
@BindingAdapter("checkIfZeroAndFormat")
public static void setRelativeTimeSpanString(TextView textView, int value) {
if (value == 0) {
textView.setText(textView.getResources().getString(R.string.not_available));
} else {
textView.setText(String.valueOf(formatNumberToString(value)));
}
}
@BindingAdapter("formatIntegerValue")
public static void formatNumberValue(TextView textView, Integer amount) {
Log.d(LOG_TAG, "*** Binding: " + amount);
textView.setText(formatNumberToString(amount));
}
/**
* When called must receive a number that will be converted to a String with the pattern $###,###,###.##
*
* @param numberToBeFormatted is the number (Buget or Revenue) to be formated.
* @return a the number as String properly formated.
*/
private static String formatNumberToString(int numberToBeFormatted) {
DecimalFormat myFormatter = new DecimalFormat("$###,###,###.##");
return myFormatter.format(numberToBeFormatted);
}
private static String formatNumberToString(double numberToBeFormatted) {
DecimalFormat myFormatter = new DecimalFormat("$###,###,###.##");
return myFormatter.format(numberToBeFormatted);
}
@BindingAdapter({"imageUrl", "error"})
public static void loadImage(ImageView view, String url, Drawable error) {
Picasso.get().load(url).error(error).into(view);
}
/** HOW TO USE IT:
* <ImageView
* app:imageUrl="@{venue.imageUrl}"
* app:error="@{@drawable/venueError}" />
**/
/**
* The adapter is called if both imageUrl and error are used for an ImageView object and imageUrl
* is a string and error is a Drawable. If you want the adapter to be called when any of the
* attributes is set, you can set the optional requireAll flag of the adapter to false, as
* shown in the following example:
*/
@BindingAdapter(value={"imageUrl", "placeholder"}, requireAll=false)
public static void setImageUrl(ImageView imageView, String url, Drawable placeHolder) {
if (url == null) {
//imageView.setImageDrawable(placeholder);
} else {
//MyImageLoader.loadInto(imageView, url, placeholder);
}
}
// ********************************************
// XX to String
@BindingConversion
@InverseMethod("convertToInteger")
public static String convertToString(@Nullable Integer d) {
return d != null ? d.toString() : EMPTY_STRING;
}
@BindingConversion
@InverseMethod("convertToIntegerZeroIsEmpty")
public static String convertToStringZeroIsEmpty(@Nullable Integer d) {
return d != null && d > 0 ? d.toString() : EMPTY_STRING;
}
@BindingConversion
public static String convertToString(@Nullable Long d) {
return d != null ? d.toString() : EMPTY_STRING;
}
@BindingConversion
public static String convertToString(boolean d) {
return String.valueOf(d);
}
@BindingConversion
@InverseMethod("convertToDouble")
public static String convertToString(@Nullable Double d) {
return d != null ? d.toString() : EMPTY_STRING;
}
@BindingConversion
public static String convertToString(String s) {
return s != null ? s : EMPTY_STRING;
}
// XX to Integer
@BindingConversion
public static Integer convertToInteger(String d) {
try {
return Integer.parseInt(d);
} catch (NumberFormatException e) {
return 0;
}
}
@BindingConversion
public static Integer convertToIntegerZeroIsEmpty(String d) {
return convertToInteger(d);
}
// XX to Double
@BindingConversion
@Nullable
public static Double convertToDouble(@Nullable String d) {
return d != null ? Double.parseDouble(d) : null;
}
// Misc
@BindingAdapter({"android:text"})
public static void setText(View view, TextView textView) {
textView.setText(view != null ? "se condition true" : "if condition false");
}
/*
@BindingAdapter("met_helperText")
public static void setVisibility(MaterialEditText view, String text) {
view.setHelperText(text);
}
*/
@BindingAdapter("android:visibility")
public static void setVisibility(View view, boolean visible) {
view.setVisibility(visible ? View.VISIBLE : View.GONE);
}
/*
@BindingAdapter("relativeTimeSpanString")
public static void setRelativeTimeSpanString(TextView view, long tstSeconds) {
if (DateUtils.isToday(TimeUnit.SECONDS.toMillis(tstSeconds))) {
view.setText(DateFormat.getTimeInstance(DateFormat.SHORT).format(TimeUnit.SECONDS.toMillis(tstSeconds)));
} else {
view.setText(DateFormat.getDateInstance(DateFormat.SHORT).format(TimeUnit.SECONDS.toMillis(tstSeconds)));
}
}
*/
@BindingAdapter("lastTransition")
public static void setLastTransition(TextView view, int transition) {
switch (transition) {
case 0:
view.setText(("region_unknown"));
break;
case 1:
view.setText(("region_unknown"));
break;
case 2:
view.setText(("region_unknown"));
break;
}
}
/**
* Event handlers may only be used with interfaces or abstract classes with one abstract method,
* as shown in the following example:
* @param view
* @param oldValue
* @param newValue
*/
// @BindingAdapter("android:onLayoutChange")
// public static void setOnLayoutChangeListener(View view,
// View.OnLayoutChangeListener oldValue,
// View.OnLayoutChangeListener newValue) {
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
// if (oldValue != null) {
// view.removeOnLayoutChangeListener(oldValue);
// }
// if (newValue != null) {
// view.addOnLayoutChangeListener(newValue);
// }
// }
// }
/** TODO Info: Use this event handler in your layout as follows:
* <View
* android:onLayoutChange="@{() -> handler.layoutChanged()}"/>
*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment