Skip to content

Commit

Permalink
Updated Android and Kotlin Gradle Plugins. Fixed lifecycleowner setti…
Browse files Browse the repository at this point in the history
…ng workflow.
  • Loading branch information
wongcain committed Jul 3, 2019
1 parent 98a3138 commit 8669b99
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import androidx.databinding.ObservableList;
import androidx.databinding.OnRebindCallback;
import androidx.databinding.ViewDataBinding;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleOwner;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.RecyclerView.ViewHolder;
Expand Down Expand Up @@ -108,10 +109,12 @@ public ViewDataBinding onCreateBinding(@NonNull LayoutInflater inflater, @Layout

@Override
public void onBindBinding(@NonNull ViewDataBinding binding, int variableId, @LayoutRes int layoutRes, int position, T item) {
boolean bound = itemBinding.bind(binding, item);
binding.setLifecycleOwner(lifecycleOwner);
if (bound) {
tryGetLifecycleOwner();
if (itemBinding.bind(binding, item)) {
binding.executePendingBindings();
if (lifecycleOwner != null) {
binding.setLifecycleOwner(lifecycleOwner);
}
}
}

Expand All @@ -122,9 +125,6 @@ public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
((ObservableList<T>) items).addOnListChangedCallback(callback);
}
this.recyclerView = recyclerView;
if (lifecycleOwner == null) {
lifecycleOwner = Utils.findLifecycleOwner(recyclerView);
}
}

@Override
Expand Down Expand Up @@ -250,6 +250,12 @@ public long getItemId(int position) {
return itemIds == null ? position : itemIds.getItemId(position, items.get(position));
}

private void tryGetLifecycleOwner() {
if (lifecycleOwner == null || lifecycleOwner.getLifecycle().getCurrentState() == Lifecycle.State.DESTROYED) {
lifecycleOwner = Utils.findLifecycleOwner(recyclerView);
}
}

private static class WeakReferenceOnListChangedCallback<T> extends ObservableList.OnListChangedCallback<ObservableList<T>> {
final WeakReference<BindingRecyclerViewAdapter<T>> adapterRef;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ObservableList;
import androidx.databinding.ViewDataBinding;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleOwner;

/**
Expand All @@ -35,7 +36,6 @@ public class BindingListViewAdapter<T> extends BaseAdapter implements BindingCol
private ItemIsEnabled<? super T> itemIsEnabled;
@Nullable
private LifecycleOwner lifecycleOwner;
private boolean failedToGetLifecycleOwner;

/**
* Constructs a new instance with the given item count.
Expand Down Expand Up @@ -106,10 +106,11 @@ public ViewDataBinding onCreateBinding(@NonNull LayoutInflater inflater, @Layout

@Override
public void onBindBinding(@NonNull ViewDataBinding binding, int variableId, @LayoutRes int layoutRes, int position, T item) {
boolean bound = itemBinding.bind(binding, item);
binding.setLifecycleOwner(lifecycleOwner);
if (bound) {
if (itemBinding.bind(binding, item)) {
binding.executePendingBindings();
if (lifecycleOwner != null) {
binding.setLifecycleOwner(lifecycleOwner);
}
}
}

Expand Down Expand Up @@ -203,12 +204,9 @@ public final View getDropDownView(int position, @Nullable View convertView, @Non
}
}

private void tryGetLifecycleOwner(ViewGroup parent) {
if (!failedToGetLifecycleOwner && lifecycleOwner == null) {
lifecycleOwner = Utils.findLifecycleOwner(parent);
if (lifecycleOwner == null) {
failedToGetLifecycleOwner = true;
}
private void tryGetLifecycleOwner(View view) {
if (lifecycleOwner == null || lifecycleOwner.getLifecycle().getCurrentState() == Lifecycle.State.DESTROYED) {
lifecycleOwner = Utils.findLifecycleOwner(view);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ObservableList;
import androidx.databinding.ViewDataBinding;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleOwner;
import androidx.viewpager.widget.PagerAdapter;

Expand All @@ -31,7 +32,6 @@ public class BindingViewPagerAdapter<T> extends PagerAdapter implements BindingC
private PageTitles<T> pageTitles;
@Nullable
private LifecycleOwner lifecycleOwner;
private boolean failedToGetLifecycleOwner;
private List<View> views = new ArrayList<>();

@Override
Expand Down Expand Up @@ -93,11 +93,11 @@ public ViewDataBinding onCreateBinding(@NonNull LayoutInflater inflater, @Layout

@Override
public void onBindBinding(@NonNull ViewDataBinding binding, int variableId, @LayoutRes int layoutRes, int position, T item) {
boolean bound = itemBinding.bind(binding, item);
binding.setLifecycleOwner(lifecycleOwner);
if (bound) {
if (itemBinding.bind(binding, item)) {
binding.executePendingBindings();
binding.setLifecycleOwner(lifecycleOwner);
if (lifecycleOwner != null) {
binding.setLifecycleOwner(lifecycleOwner);
}
}
}

Expand Down Expand Up @@ -142,12 +142,9 @@ public Object instantiateItem(@NonNull ViewGroup container, int position) {
return view;
}

private void tryGetLifecycleOwner(ViewGroup parent) {
if (!failedToGetLifecycleOwner && lifecycleOwner == null) {
lifecycleOwner = Utils.findLifecycleOwner(parent);
if (lifecycleOwner == null) {
failedToGetLifecycleOwner = true;
}
private void tryGetLifecycleOwner(View view) {
if (lifecycleOwner == null || lifecycleOwner.getLifecycle().getCurrentState() == Lifecycle.State.DESTROYED) {
lifecycleOwner = Utils.findLifecycleOwner(view);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
import androidx.annotation.MainThread;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.Observable;
import androidx.databinding.OnRebindCallback;
import androidx.databinding.ViewDataBinding;

import android.os.Looper;
Expand All @@ -16,21 +14,13 @@
import androidx.annotation.LayoutRes;
import androidx.lifecycle.LifecycleOwner;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
* Helper databinding utilities. May be made public some time in the future if they prove to be
* useful.
*/
class Utils {
private static final String TAG = "BCAdapters";

@Nullable
private static Field lifecycleOwnerField;
private static boolean fieldFaild;

/**
* Helper to throw an exception when {@link androidx.databinding.ViewDataBinding#setVariable(int,
* Object)} returns false.
Expand All @@ -44,43 +34,22 @@ static void throwMissingVariable(ViewDataBinding binding, int bindingVariable, @
}

/**
* Returns the lifecycle owner associated with the given view. This currently requires the view
* to use databinding and uses reflection. This will hopefully be replaced with a better
* implementation once https://issuetracker.google.com/issues/112929938 gets implemented.
* Returns the lifecycle owner associated with the given view. Tries to get lifecycle owner first
* from ViewDataBinding, else from View Context if view is not data-bound.
*/
@Nullable
@MainThread
static LifecycleOwner findLifecycleOwner(View view) {
ViewDataBinding binding = DataBindingUtil.findBinding(view);
if (binding == null) {
return null;
LifecycleOwner lifecycleOwner = null;
if (binding != null) {
lifecycleOwner = binding.getLifecycleOwner();
}
return getLifecycleOwner(binding);
}

/**
* Returns the lifecycle owner from a {@code ViewDataBinding} using reflection.
*/
@Nullable
@MainThread
private static LifecycleOwner getLifecycleOwner(ViewDataBinding binding) {
if (!fieldFaild && lifecycleOwnerField == null) {
try {
lifecycleOwnerField = ViewDataBinding.class.getDeclaredField("mLifecycleOwner");
lifecycleOwnerField.setAccessible(true);
} catch (NoSuchFieldException e) {
fieldFaild = true;
return null;
}
}
if (lifecycleOwnerField == null) {
return null;
}
try {
return (LifecycleOwner) lifecycleOwnerField.get(binding);
} catch (IllegalAccessException e) {
return null;
Context ctx = view.getContext();
if (lifecycleOwner == null && ctx instanceof LifecycleOwner) {
lifecycleOwner = (LifecycleOwner) ctx;
}
return lifecycleOwner;
}

/**
Expand All @@ -93,15 +62,4 @@ static void ensureChangeOnMainThread() {
}
}

/**
* Constructs a binding adapter class from it's class name using reflection.
*/
@SuppressWarnings("unchecked")
static <T, A extends BindingCollectionAdapter<T>> A createClass(Class<? extends BindingCollectionAdapter> adapterClass, ItemBinding<T> itemBinding) {
try {
return (A) adapterClass.getConstructor(ItemBinding.class).newInstance(itemBinding);
} catch (Exception e1) {
throw new RuntimeException(e1);
}
}
}
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
ext.kotlin_version = '1.3.0'
ext.agp_version = '3.3.0'
ext.kotlin_version = '1.3.31'
ext.agp_version = '3.4.1'
repositories {
google()
jcenter()
Expand Down

0 comments on commit 8669b99

Please sign in to comment.