2017-12-26 21:12:37 +01:00
|
|
|
package com.wireguard.android.databinding;
|
2017-07-29 13:09:55 +02:00
|
|
|
|
|
|
|
import android.databinding.BindingAdapter;
|
|
|
|
import android.databinding.ObservableList;
|
2017-08-23 04:49:55 +02:00
|
|
|
import android.databinding.adapters.ListenerUtil;
|
2017-08-16 11:30:42 +02:00
|
|
|
import android.graphics.Typeface;
|
2017-08-23 04:48:42 +02:00
|
|
|
import android.text.InputFilter;
|
2017-08-23 04:49:55 +02:00
|
|
|
import android.widget.LinearLayout;
|
2017-07-29 13:09:55 +02:00
|
|
|
import android.widget.ListView;
|
2017-08-16 11:30:42 +02:00
|
|
|
import android.widget.TextView;
|
2017-07-29 13:09:55 +02:00
|
|
|
|
2017-11-08 06:34:20 +01:00
|
|
|
import com.wireguard.android.R;
|
2018-01-06 11:04:42 +01:00
|
|
|
import com.wireguard.android.util.Keyed;
|
|
|
|
import com.wireguard.android.util.KeyedObservableList;
|
2017-12-26 21:12:57 +01:00
|
|
|
import com.wireguard.android.widget.ToggleSwitch;
|
2018-01-06 11:06:56 +01:00
|
|
|
import com.wireguard.android.widget.ToggleSwitch.OnBeforeCheckedChangeListener;
|
2017-11-08 06:34:20 +01:00
|
|
|
|
2018-01-01 09:06:37 +01:00
|
|
|
import org.threeten.bp.Instant;
|
|
|
|
import org.threeten.bp.ZoneId;
|
|
|
|
import org.threeten.bp.ZonedDateTime;
|
|
|
|
import org.threeten.bp.format.DateTimeFormatter;
|
|
|
|
|
2017-07-29 13:09:55 +02:00
|
|
|
/**
|
|
|
|
* Static methods for use by generated code in the Android data binding library.
|
|
|
|
*/
|
|
|
|
|
2018-01-01 09:06:37 +01:00
|
|
|
@SuppressWarnings({"unused", "WeakerAccess"})
|
2017-07-29 13:09:55 +02:00
|
|
|
public final class BindingAdapters {
|
2018-01-01 09:06:37 +01:00
|
|
|
private BindingAdapters() {
|
|
|
|
// Prevent instantiation.
|
|
|
|
}
|
|
|
|
|
|
|
|
@BindingAdapter({"checked"})
|
2017-11-26 06:05:39 +01:00
|
|
|
public static void setChecked(final ToggleSwitch view, final boolean checked) {
|
|
|
|
view.setCheckedInternal(checked);
|
|
|
|
}
|
|
|
|
|
2017-10-29 11:06:36 +01:00
|
|
|
@BindingAdapter({"filter"})
|
|
|
|
public static void setFilter(final TextView view, final InputFilter filter) {
|
|
|
|
view.setFilters(new InputFilter[]{filter});
|
|
|
|
}
|
|
|
|
|
2017-07-29 13:09:55 +02:00
|
|
|
@BindingAdapter({"items", "layout"})
|
2017-08-23 04:49:55 +02:00
|
|
|
public static <T> void setItems(final LinearLayout view,
|
|
|
|
final ObservableList<T> oldList, final int oldLayoutId,
|
|
|
|
final ObservableList<T> newList, final int newLayoutId) {
|
|
|
|
if (oldList == newList && oldLayoutId == newLayoutId)
|
|
|
|
return;
|
|
|
|
ItemChangeListener<T> listener = ListenerUtil.getListener(view, R.id.item_change_listener);
|
|
|
|
// If the layout changes, any existing listener must be replaced.
|
|
|
|
if (listener != null && oldList != null && oldLayoutId != newLayoutId) {
|
|
|
|
listener.setList(null);
|
|
|
|
listener = null;
|
2017-11-08 07:07:29 +01:00
|
|
|
// Stop tracking the old listener.
|
|
|
|
ListenerUtil.trackListener(view, null, R.id.item_change_listener);
|
2017-08-23 04:49:55 +02:00
|
|
|
}
|
|
|
|
// Avoid adding a listener when there is no new list or layout.
|
|
|
|
if (newList == null || newLayoutId == 0)
|
|
|
|
return;
|
|
|
|
if (listener == null) {
|
|
|
|
listener = new ItemChangeListener<>(view, newLayoutId);
|
|
|
|
ListenerUtil.trackListener(view, listener, R.id.item_change_listener);
|
|
|
|
}
|
|
|
|
// Either the list changed, or this is an entirely new listener because the layout changed.
|
|
|
|
listener.setList(newList);
|
|
|
|
}
|
|
|
|
|
|
|
|
@BindingAdapter({"items", "layout"})
|
2018-01-06 11:04:42 +01:00
|
|
|
public static <K, E extends Keyed<? extends K>>
|
|
|
|
void setItems(final ListView view,
|
|
|
|
final KeyedObservableList<K, E> oldList, final int oldLayoutId,
|
|
|
|
final KeyedObservableList<K, E> newList, final int newLayoutId) {
|
2017-11-08 07:07:29 +01:00
|
|
|
if (oldList == newList && oldLayoutId == newLayoutId)
|
2017-07-29 13:09:55 +02:00
|
|
|
return;
|
|
|
|
// The ListAdapter interface is not generic, so this cannot be checked.
|
2018-01-06 11:04:42 +01:00
|
|
|
@SuppressWarnings("unchecked") KeyedObservableListAdapter<K, E> adapter =
|
|
|
|
(KeyedObservableListAdapter<K, E>) view.getAdapter();
|
2017-07-29 13:09:55 +02:00
|
|
|
// If the layout changes, any existing adapter must be replaced.
|
2017-11-08 07:07:29 +01:00
|
|
|
if (adapter != null && oldList != null && oldLayoutId != newLayoutId) {
|
|
|
|
adapter.setList(null);
|
2017-07-29 13:09:55 +02:00
|
|
|
adapter = null;
|
2017-11-08 07:07:29 +01:00
|
|
|
}
|
|
|
|
// Avoid setting an adapter when there is no new list or layout.
|
|
|
|
if (newList == null || newLayoutId == 0)
|
|
|
|
return;
|
2017-07-29 13:09:55 +02:00
|
|
|
if (adapter == null) {
|
2018-01-06 11:04:42 +01:00
|
|
|
adapter = new KeyedObservableListAdapter<>(view.getContext(), newLayoutId, newList);
|
2017-11-08 07:07:29 +01:00
|
|
|
view.setAdapter(adapter);
|
2017-07-29 13:09:55 +02:00
|
|
|
}
|
2017-11-08 07:07:29 +01:00
|
|
|
// Either the list changed, or this is an entirely new listener because the layout changed.
|
|
|
|
adapter.setList(newList);
|
2017-07-29 13:09:55 +02:00
|
|
|
}
|
|
|
|
|
2018-01-01 09:06:37 +01:00
|
|
|
@BindingAdapter({"onBeforeCheckedChanged"})
|
2017-11-26 06:05:39 +01:00
|
|
|
public static void setOnBeforeCheckedChanged(final ToggleSwitch view,
|
2018-01-06 11:06:56 +01:00
|
|
|
final OnBeforeCheckedChangeListener listener) {
|
2017-11-26 06:05:39 +01:00
|
|
|
view.setOnBeforeCheckedChangeListener(listener);
|
|
|
|
}
|
|
|
|
|
2018-01-01 09:06:37 +01:00
|
|
|
@BindingAdapter({"android:text"})
|
|
|
|
public static void setText(final TextView view, final Instant instant) {
|
|
|
|
if (instant == null || Instant.EPOCH.equals(instant)) {
|
|
|
|
view.setText(R.string.never);
|
|
|
|
} else {
|
|
|
|
final ZoneId defaultZone = ZoneId.systemDefault();
|
|
|
|
final ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(instant, defaultZone);
|
|
|
|
view.setText(zonedDateTime.format(DateTimeFormatter.RFC_1123_DATE_TIME));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-16 11:30:42 +02:00
|
|
|
@BindingAdapter({"android:textStyle"})
|
2017-08-23 04:48:42 +02:00
|
|
|
public static void setTextStyle(final TextView view, final Typeface typeface) {
|
2017-08-16 11:30:42 +02:00
|
|
|
view.setTypeface(typeface);
|
|
|
|
}
|
2017-07-29 13:09:55 +02:00
|
|
|
}
|