Fix for multiple listeners of insets

This commit is contained in:
Avently 2020-09-16 04:49:26 +03:00
parent 5ada0ae2c7
commit a1e4ef9e8e

View file

@ -17,15 +17,19 @@
*/ */
package org.schabi.newpipe.views; package org.schabi.newpipe.views;
import android.annotation.TargetApi;
import android.content.Context; import android.content.Context;
import android.graphics.Rect; import android.graphics.Rect;
import android.os.Build;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.view.WindowInsets;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.coordinatorlayout.widget.CoordinatorLayout; import androidx.coordinatorlayout.widget.CoordinatorLayout;
import org.schabi.newpipe.R;
public final class FocusAwareCoordinator extends CoordinatorLayout { public final class FocusAwareCoordinator extends CoordinatorLayout {
private final Rect childFocus = new Rect(); private final Rect childFocus = new Rect();
@ -63,4 +67,41 @@ public final class FocusAwareCoordinator extends CoordinatorLayout {
requestChildRectangleOnScreen(child, childFocus, false); requestChildRectangleOnScreen(child, childFocus, false);
} }
} }
/**
* Applies window insets to all children, not just for the first who consume the insets.
* Makes possible for multiple fragments to co-exist. Without this code
* the first ViewGroup who consumes will be the last who receive the insets
*/
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public WindowInsets dispatchApplyWindowInsets(final WindowInsets insets) {
boolean consumed = false;
for (int i = 0; i < getChildCount(); i++) {
final View child = getChildAt(i);
final WindowInsets res = child.dispatchApplyWindowInsets(insets);
if (res.isConsumed()) {
consumed = true;
}
}
if (consumed) {
insets.consumeSystemWindowInsets();
}
return insets;
}
/**
* Adjusts player's controls manually because fitsSystemWindows doesn't work when multiple
* receivers adjust its bounds. So when two listeners are present (like in profile page)
* the player's controls will not receive insets. This method fixes it
*/
@Override
protected boolean fitSystemWindows(final Rect insets) {
final ViewGroup controls = findViewById(R.id.playbackControlRoot);
if (controls != null) {
controls.setPadding(insets.left, insets.top, insets.right, insets.bottom);
}
return super.fitSystemWindows(insets);
}
} }