Skip to content

Instantly share code, notes, and snippets.

@miensol
Last active August 29, 2015 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miensol/44ac5af33a60ad60cab7 to your computer and use it in GitHub Desktop.
Save miensol/44ac5af33a60ad60cab7 to your computer and use it in GitHub Desktop.
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="form"
type="pl.brightinventions.databinding.RegisterForm"/>
</data>
<RelativeLayout android:layout_width="match_parent"
android:layout_height="match_parent"
style="@style/Container.Padded"
tools:context=".EditorActivity"
android:animateLayoutChanges="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/first_name_hint"
android:visibility="@{form.firstNameLabelVisibility}"
android:theme="@style/Label.Small"/>
<EditText
android:id="@+id/first_name"
android:text="@{form.firstName}"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:hint="@string/first_name_hint"
android:inputType="textPersonName"/>
<TextView
android:id="@+id/error"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/register"
android:text="@{form.error}"/>
<Button
android:id="@+id/register"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/register"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
</layout>
import pl.brightinventions.databinding.databinding.ActivityRegisterBinding;
public class RegisterActivity extends Activity {
RegisterApi registerApi; // inject or create remote api
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityRegisterBinding viewDataBinding = DataBindingUtil.setContentView(this, R.layout.activity_register);
final RegisterForm form = new RegisterForm();
View root = viewDataBinding.getRoot();
EditText firstName = (EditText) root.findViewById(R.id.first_name);
firstName.addTextChangedListener(new TextWatcherBase() {
@Override
public void afterTextChanged(Editable s) {
form.setFirstNameFromView(s.toString());
}
});
viewDataBinding.setForm(form);
findViewById(R.id.register).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
registerApi.register(form.newRegisterData(), new OnRegistrationCompleted(form));
}
});
}
private static final class OnRegistrationCompleted
implements Action2<RegisterApi.RegistrationDto,Throwable> {
private final RegisterForm form;
public OnRegistrationCompleted(RegisterForm form) {
this.form = form;
}
@Override
public void call(RegisterApi.RegistrationDto registrationDto, Throwable error) {
this.form.setError(error);
}
}
}
public class RegisterForm extends BaseObservable {
@Bindable
public String firstName;
@Bindable
public int getFirstNameLabelVisibility(){
return TextUtils.isEmpty(firstName) ? View.INVISIBLE : View.VISIBLE;
}
public void setFirstNameFromView(String firstNameFromView) {
firstName = firstNameFromView;
notifyPropertyChanged(pl.brightinventions.databinding.BR.firstNameLabelVisibility);
}
@Bindable
public String error;
public void setError(Throwable error) {
this.error = error.getLocalizedMessage();
notifyPropertyChanged(pl.brightinventions.databinding.BR.error);
}
public RegisterApi.RegistrationDto newRegisterData() {
RegisterApi.RegistrationDto dto = new RegisterApi.RegistrationDto();
dto.firstName = firstName;
return dto;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment