Home | History | Annotate | Download | only in adapters
      1 /*
      2  * Copyright (C) 2015 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package android.databinding.adapters;
     17 
     18 import android.databinding.BindingAdapter;
     19 import android.databinding.InverseBindingListener;
     20 import android.databinding.InverseBindingMethod;
     21 import android.databinding.InverseBindingMethods;
     22 import android.widget.DatePicker;
     23 import android.widget.DatePicker.OnDateChangedListener;
     24 import com.android.databinding.library.baseAdapters.R;
     25 
     26 @InverseBindingMethods({
     27         @InverseBindingMethod(type = DatePicker.class, attribute = "android:year"),
     28         @InverseBindingMethod(type = DatePicker.class, attribute = "android:month"),
     29         @InverseBindingMethod(type = DatePicker.class, attribute = "android:day", method = "getDayOfMonth"),
     30 })
     31 public class DatePickerBindingAdapter {
     32     @BindingAdapter(value = {"android:year", "android:month", "android:day",
     33             "android:onDateChanged", "android:yearAttrChanged",
     34             "android:monthAttrChanged", "android:dayAttrChanged"}, requireAll = false)
     35     public static void setListeners(DatePicker view, int year, int month, int day,
     36             final OnDateChangedListener listener, final InverseBindingListener yearChanged,
     37             final InverseBindingListener monthChanged, final InverseBindingListener dayChanged) {
     38         if (year == 0) {
     39             year = view.getYear();
     40         }
     41         if (day == 0) {
     42             day = view.getDayOfMonth();
     43         }
     44         if (yearChanged == null && monthChanged == null && dayChanged == null) {
     45             view.init(year, month, day, listener);
     46         } else {
     47             DateChangedListener oldListener = ListenerUtil.getListener(view, R.id.onDateChanged);
     48             if (oldListener == null) {
     49                 oldListener = new DateChangedListener();
     50                 ListenerUtil.trackListener(view, oldListener, R.id.onDateChanged);
     51             }
     52             oldListener.setListeners(listener, yearChanged, monthChanged, dayChanged);
     53             view.init(year, month, day, oldListener);
     54         }
     55     }
     56 
     57     private static class DateChangedListener implements OnDateChangedListener {
     58         OnDateChangedListener mListener;
     59         InverseBindingListener mYearChanged;
     60         InverseBindingListener mMonthChanged;
     61         InverseBindingListener mDayChanged;
     62 
     63         public void setListeners(OnDateChangedListener listener, InverseBindingListener yearChanged,
     64                 InverseBindingListener monthChanged, InverseBindingListener dayChanged) {
     65             mListener = listener;
     66             mYearChanged = yearChanged;
     67             mMonthChanged = monthChanged;
     68             mDayChanged = dayChanged;
     69         }
     70 
     71         @Override
     72         public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
     73             if (mListener != null) {
     74                 mListener.onDateChanged(view, year, monthOfYear, dayOfMonth);
     75             }
     76             if (mYearChanged != null) {
     77                 mYearChanged.onChange();
     78             }
     79             if (mMonthChanged != null) {
     80                 mMonthChanged.onChange();
     81             }
     82             if (mDayChanged != null) {
     83                 mDayChanged.onChange();
     84             }
     85         }
     86     }
     87 }
     88