Home | History | Annotate | Download | only in adapter
      1 /*
      2  * Copyright (C) 2015 The Android Open Source Project
      3  * Licensed under the Apache License, Version 2.0 (the "License");
      4  * you may not use this file except in compliance with the License.
      5  * You may obtain a copy of the License at
      6  *      http://www.apache.org/licenses/LICENSE-2.0
      7  * Unless required by applicable law or agreed to in writing, software
      8  * distributed under the License is distributed on an "AS IS" BASIS,
      9  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     10  * See the License for the specific language governing permissions and
     11  * limitations under the License.
     12  */
     13 
     14 package android.databinding.testapp.adapter;
     15 
     16 
     17 import android.databinding.BaseObservable;
     18 import android.databinding.Bindable;
     19 import android.databinding.BindingAdapter;
     20 import android.view.View;
     21 import android.databinding.testapp.BR;
     22 import android.widget.TextView;
     23 
     24 import org.apache.commons.lang3.StringUtils;
     25 
     26 public class MultiArgTestAdapter {
     27 
     28     public static String join(BaseMultiBindingClass... classes) {
     29         StringBuilder sb = new StringBuilder();
     30         for(BaseMultiBindingClass instance : classes) {
     31             sb.append(instance == null ? "??" : instance.getValue());
     32         }
     33         return sb.toString();
     34     }
     35 
     36     public static String join(String... strings) {
     37         StringBuilder sb = new StringBuilder();
     38         for(String str : strings) {
     39             sb.append(str == null ? "??" : str);
     40         }
     41         return sb.toString();
     42 
     43     }
     44 
     45     @BindingAdapter({"android:class1", "android:class2"})
     46     public static void setBoth(TextView view, MultiBindingClass1 class1,
     47                                         MultiBindingClass2 class2) {
     48         view.setText(join(class1, class2));
     49     }
     50 
     51     @BindingAdapter({"android:class1str", "android:class2str"})
     52     public static void setBoth(TextView view, String str1,
     53                                String str2) {
     54         view.setText(join(str1, str2));
     55     }
     56 
     57     @BindingAdapter({"android:class1"})
     58     public static void setClass1(TextView view, MultiBindingClass1 class1) {
     59         view.setText(class1.getValue());
     60     }
     61 
     62     @BindingAdapter({"android:classStr"})
     63     public static void setClassStr(TextView view, String str) {
     64         view.setText(str);
     65     }
     66 
     67     @BindingAdapter("android:class2")
     68     public static void setClass2(TextView view, MultiBindingClass2 class2) {
     69         view.setText(class2.getValue());
     70     }
     71 
     72     @BindingAdapter("android:val3")
     73     public static void setWithOldValue(TextView view, String oldValue, String newValue) {
     74         view.setText(String.format("%s -> %s", oldValue, newValue));
     75     }
     76 
     77     @BindingAdapter({"android:val3", "android:val4"})
     78     public static void set2WithOldValues(TextView view, String oldValue1, String oldValue2,
     79             String newValue1, String newValue2) {
     80         view.setText(String.format("%s, %s -> %s, %s", oldValue1, oldValue2, newValue1, newValue2));
     81     }
     82 
     83     public static class MultiBindingClass1 extends BaseMultiBindingClass {
     84 
     85     }
     86 
     87     public static class MultiBindingClass2 extends BaseMultiBindingClass {
     88 
     89     }
     90 
     91     public static class BaseMultiBindingClass extends BaseObservable {
     92         View mSetOn;
     93         @Bindable
     94         String mValue;
     95         public View getSetOn() {
     96             return mSetOn;
     97         }
     98 
     99         public String getValue() {
    100             return mValue;
    101         }
    102 
    103         public void setValue(String value, boolean notify) {
    104             mValue = value;
    105             if (notify) {
    106                 notifyPropertyChanged(BR.value);
    107             }
    108         }
    109 
    110         public void clear() {
    111             mSetOn = null;
    112         }
    113     }
    114 }
    115