Skip to content
This repository has been archived by the owner on Sep 30, 2024. It is now read-only.

Add builder API for applying WindowInsets #62

Merged
merged 28 commits into from
Jun 8, 2020
Merged

Add builder API for applying WindowInsets #62

merged 28 commits into from
Jun 8, 2020

Conversation

poliver
Copy link
Contributor

@poliver poliver commented Jun 5, 2020

For this most part, this just wraps the existing functionality with a builder API. Initial motivation for this was Insetter not providing a way to consume insets after applying them to a given View. A potential result is insets being applied a second time by child Views/ViewGroups that automatically handle insets internally. I've restricted the scope of the original static utils where possible, and I also added a new consumeSystemWindowInsets attribute to the binding adapter.

Comment on lines +214 to +218
if (consumeSystemWindowInsets) {
return insets.consumeSystemWindowInsets();
} else {
return insets;
}
Copy link
Contributor Author

@poliver poliver Jun 5, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also considered surfacing this so callers can be more granular with how they consume insets. Consuming everything might be overkill, but it solves the initial issue described in the PR description.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, lgtm

Copy link
Contributor Author

@poliver poliver Jun 5, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case, would it make sense to have OnApplyInsetsListener.onApplyInsets return WindowInsetsCompat? I could alternatively add a OnConsumeInsetsListener interface.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also seems like it would make testing Builder.consumeSystemWindowInsets a bit easier

Comment on lines 160 to 162
public void applyToView(@NonNull View view) {
build().setOnApplyInsetsListener(view);
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if this needs to be part of the builder

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this. I'd make the doc a bit clearer though. Something like:

Builds the Insetter instance and sets it as a OnApplyWindowInsetsListener on [view].

Might also be worth returning the built Insetter from the function too.

Phil Oliver added 2 commits June 4, 2020 22:31
@chrisbanes
Copy link
Owner

chrisbanes commented Jun 5, 2020

Looks great to me overall, a much nicer API for devs. Thank you 🙌

While looking through this some of the old API looks pretty crufty. While we're making big API changes, we may as well fix everything in one release 😃

  1. What do we think about the use of EnumSet? While they have an optimal implementation, the API is not so nice. What do you think of moving: @Nullable EnumSet<InsetDimension> dimensions to InsetDimension... dimensions?

  2. The enum InsetDimension name is a bit long and not particularly clear. How about a rename to Insetter.Side? Feels clearer to me.

* @see Insetter#setOnApplyInsetsListener(View)
*/
@NonNull
public Builder setOnApplyInsetsListener(OnApplyInsetsListener onApplyInsetsListener) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Nullable param?

Comment on lines +214 to +218
if (consumeSystemWindowInsets) {
return insets.consumeSystemWindowInsets();
} else {
return insets;
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, lgtm

}
}

public static Builder builder() {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs doc

Comment on lines 160 to 162
public void applyToView(@NonNull View view) {
build().setOnApplyInsetsListener(view);
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this. I'd make the doc a bit clearer though. Something like:

Builds the Insetter instance and sets it as a OnApplyWindowInsetsListener on [view].

Might also be worth returning the built Insetter from the function too.

* @see Insetter#setOnApplyInsetsListener(View)
*/
@NonNull
public Builder consumeSystemWindowInsets(boolean consumeSystemWindowInsets) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should mention the default value of this in the doc.

@poliver
Copy link
Contributor Author

poliver commented Jun 5, 2020

  1. What do we think about the use of EnumSet? While they have an optimal implementation, the API is not so nice. What do you think of moving: @Nullable EnumSet<InsetDimension> dimensions to InsetDimension... dimensions?
  2. The enum InsetDimension name is a bit long and not particularly clear. How about a rename to Insetter.Side? Feels clearer to me.

Renaming to Insetter.Side makes sense. I've added that change. But the change from EnumSet to vararg is adding some complexity between the builder API, the library widgets, etc. What do you think of a Side.Builder() instead?

@ZacSweers
Copy link
Contributor

Really like how the bit field approach turned out 👍

Copy link
Owner

@chrisbanes chrisbanes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really liking where this is heading. Just a few more tweaks and we can :shipit:

public final class SideUtils {

@Sides
public static int create(boolean left, boolean top, boolean right, boolean bottom) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great. Maybe move this to the Side class? Feels more fundamental than being in a utils class.

import android.view.View;
import androidx.core.view.WindowInsetsCompat;
import java.util.EnumSet;
public final class Side {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add a NONE = 0 too, rather than relying on 0 in the Insetter logic.

@@ -24,14 +24,6 @@
*/
public final class SideUtils {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, one last thing. We can now make this pkg-private.

Copy link
Owner

@chrisbanes chrisbanes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect, thanks for the contribution!

@chrisbanes chrisbanes merged commit c570c63 into chrisbanes:master Jun 8, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants