Krllus

DataBindingSampleModel

Dec 12th, 2017
426
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.73 KB | None | 0 0
  1. package com.example.joao.berrantinho.model;
  2.  
  3. import android.databinding.Bindable;
  4. import android.databinding.BindingAdapter;
  5. import android.databinding.InverseBindingAdapter;
  6. import android.databinding.Observable;
  7. import android.databinding.PropertyChangeRegistry;
  8. import android.widget.EditText;
  9.  
  10. import com.example.joao.berrantinho.BR;
  11.  
  12. import java.util.Locale;
  13.  
  14. /**
  15.  * Created by Joao Carlos on 12/11/17.
  16.  * Biox Pecuaria Moderna
  17.  */
  18.  
  19.  
  20. public class SampleModel implements Observable {
  21.     private PropertyChangeRegistry pcr = new PropertyChangeRegistry();
  22.  
  23.     private String sampleText;
  24.     private Double numberOne;
  25.     private Double numberTwo;
  26.  
  27.     public SampleModel(String text) {
  28.         this.sampleText = text;
  29.         this.numberOne = 0d;
  30.         this.numberTwo = 0d;
  31.     }
  32.  
  33.     @Bindable
  34.     public String getSampleText() {
  35.         return sampleText;
  36.     }
  37.  
  38.     public void setSampleText(String sampleText) {
  39.         this.sampleText = sampleText;
  40.         pcr.notifyChange(this, BR.sampleText);
  41.     }
  42.  
  43.     @Bindable
  44.     public Double getNumberOne() {
  45.         return numberOne;
  46.     }
  47.  
  48.     public void setNumberOne(Double numberOne) {
  49.         this.numberOne = numberOne;
  50.         pcr.notifyChange(this, BR.numberOne);
  51.     }
  52.  
  53.     @Bindable
  54.     public Double getNumberTwo() {
  55.         return numberTwo;
  56.     }
  57.  
  58.     public void setNumberTwo(Double numberTwo) {
  59.         this.numberTwo = numberTwo;
  60.         pcr.notifyChange(this, BR.numberTwo);
  61.     }
  62.  
  63.     public String sum() {
  64.         Double s = numberOne + numberTwo;
  65.         return s.toString();
  66.     }
  67.  
  68.     @Override
  69.     public void addOnPropertyChangedCallback(OnPropertyChangedCallback callback) {
  70.         pcr.add(callback);
  71.     }
  72.  
  73.     @Override
  74.     public void removeOnPropertyChangedCallback(OnPropertyChangedCallback callback) {
  75.         pcr.remove(callback);
  76.     }
  77.  
  78.     @BindingAdapter({"android:text"})
  79.     public static void setText(EditText editText, Double value) {
  80.         final CharSequence oldText = editText.getText();
  81.         if (!haveContentsChanged(value, oldText))
  82.             return;
  83.         editText.setText(String.format(Locale.getDefault(), "%.2f", value));
  84.     }
  85.  
  86.     @InverseBindingAdapter(attribute = "android:text")
  87.     public static Double getTextString(EditText editText) {
  88.         String txt = editText.getText().toString();
  89.         return "".equals(txt) ? 0d : Double.parseDouble(txt.replace(",", "."));
  90.     }
  91.  
  92.     private static boolean haveContentsChanged(Double value, CharSequence oldText) {
  93.         String oldContent = oldText.toString();
  94.         Double oldDouble = "".equals(oldContent) ? 0d : Double.parseDouble(oldContent);
  95.         return !oldDouble.equals(value);
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment