2018-05-02 17:29:58 +02:00
|
|
|
/*
|
|
|
|
* Copyright © 2018 Samuel Holland <samuel@sholland.org>
|
2018-05-02 21:40:01 +02:00
|
|
|
* Copyright © 2018 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
|
2018-05-02 17:29:58 +02:00
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
*/
|
|
|
|
|
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-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;
|
2018-01-07 06:07:17 +01:00
|
|
|
import com.wireguard.android.util.ObservableKeyedList;
|
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
|
|
|
|
2017-07-29 13:09:55 +02:00
|
|
|
/**
|
|
|
|
* Static methods for use by generated code in the Android data binding library.
|
|
|
|
*/
|
|
|
|
|
2018-05-02 07:20:06 +02:00
|
|
|
@SuppressWarnings("unused")
|
2017-07-29 13:09:55 +02:00
|
|
|
public final class BindingAdapters {
|
2018-01-01 09:06:37 +01:00
|
|
|
private BindingAdapters() {
|
|
|
|
// Prevent instantiation.
|
|
|
|
}
|
|
|
|
|
2018-05-02 07:20:06 +02:00
|
|
|
@BindingAdapter("checked")
|
2017-11-26 06:05:39 +01:00
|
|
|
public static void setChecked(final ToggleSwitch view, final boolean checked) {
|
|
|
|
view.setCheckedInternal(checked);
|
|
|
|
}
|
|
|
|
|
2018-05-02 07:20:06 +02:00
|
|
|
@BindingAdapter("filter")
|
2017-10-29 11:06:36 +01:00
|
|
|
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"})
|
2018-01-06 11:07:13 +01:00
|
|
|
public static <E> void setItems(final LinearLayout view,
|
|
|
|
final ObservableList<E> oldList, final int oldLayoutId,
|
|
|
|
final ObservableList<E> newList, final int newLayoutId) {
|
2017-08-23 04:49:55 +02:00
|
|
|
if (oldList == newList && oldLayoutId == newLayoutId)
|
|
|
|
return;
|
2018-01-06 11:07:13 +01:00
|
|
|
ItemChangeListener<E> listener = ListenerUtil.getListener(view, R.id.item_change_listener);
|
2017-08-23 04:49:55 +02:00
|
|
|
// 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,
|
2018-01-07 06:07:17 +01:00
|
|
|
final ObservableKeyedList<K, E> oldList, final int oldLayoutId,
|
|
|
|
final ObservableKeyedList<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-07 06:07:17 +01:00
|
|
|
@SuppressWarnings("unchecked") ObservableKeyedListAdapter<K, E> adapter =
|
|
|
|
(ObservableKeyedListAdapter<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-07 06:07:17 +01:00
|
|
|
adapter = new ObservableKeyedListAdapter<>(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-05-02 07:20:06 +02: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);
|
|
|
|
}
|
2017-07-29 13:09:55 +02:00
|
|
|
}
|