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 public class MultiArgTestAdapter {
     25 
     26     public static String join(BaseMultiBindingClass... classes) {
     27         StringBuilder sb = new StringBuilder();
     28         for(BaseMultiBindingClass instance : classes) {
     29             sb.append(instance == null ? "??" : instance.getValue());
     30         }
     31         return sb.toString();
     32     }
     33 
     34     public static String join(String... strings) {
     35         StringBuilder sb = new StringBuilder();
     36         for(String str : strings) {
     37             sb.append(str == null ? "??" : str);
     38         }
     39         return sb.toString();
     40 
     41     }
     42 
     43     @BindingAdapter({"android:class1", "android:class2"})
     44     public static void setBoth(TextView view, MultiBindingClass1 class1,
     45                                         MultiBindingClass2 class2) {
     46         view.setText(join(class1, class2));
     47     }
     48 
     49     @BindingAdapter({"android:class1str", "android:class2str"})
     50     public static void setBoth(TextView view, String str1,
     51                                String str2) {
     52         view.setText(join(str1, str2));
     53     }
     54 
     55     @BindingAdapter({"android:class1"})
     56     public static void setClass1(TextView view, MultiBindingClass1 class1) {
     57         view.setText(class1.getValue());
     58     }
     59 
     60     @BindingAdapter({"android:classStr"})
     61     public static void setClassStr(TextView view, String str) {
     62         view.setText(str);
     63     }
     64 
     65     @BindingAdapter("android:class2")
     66     public static void setClass2(TextView view, MultiBindingClass2 class2) {
     67         view.setText(class2.getValue());
     68     }
     69 
     70     @BindingAdapter("android:val3")
     71     public static void setWithOldValue(TextView view, String oldValue, String newValue) {
     72         view.setText(String.format("%s -> %s", oldValue, newValue));
     73     }
     74 
     75     @BindingAdapter({"android:val3", "android:val4"})
     76     public static void set2WithOldValues(TextView view, String oldValue1, String oldValue2,
     77             String newValue1, String newValue2) {
     78         view.setText(String.format("%s, %s -> %s, %s", oldValue1, oldValue2, newValue1, newValue2));
     79     }
     80 
     81     public static class MultiBindingClass1 extends BaseMultiBindingClass {
     82 
     83     }
     84 
     85     public static class MultiBindingClass2 extends BaseMultiBindingClass {
     86 
     87     }
     88 
     89     public static class BaseMultiBindingClass extends BaseObservable {
     90         View mSetOn;
     91         @Bindable
     92         String mValue;
     93         public View getSetOn() {
     94             return mSetOn;
     95         }
     96 
     97         public String getValue() {
     98             return mValue;
     99         }
    100 
    101         public void setValue(String value, boolean notify) {
    102             mValue = value;
    103             if (notify) {
    104                 notifyPropertyChanged(BR.value);
    105             }
    106         }
    107 
    108         public void clear() {
    109             mSetOn = null;
    110         }
    111     }
    112 }
    113