From 82bb6f584d32b74b02bd6383ace21b12ff9edf2d Mon Sep 17 00:00:00 2001
From: Eli Hart <konakid@gmail.com>
Date: Fri, 22 Dec 2017 12:05:30 -0800
Subject: [PATCH] Feature to customize carousel padding values individually and
 dynamically (#369)

* Feature to customize carousel padding values individually and dynamically

* add javadoc
---
 .../main/java/com/airbnb/epoxy/Carousel.java  | 92 +++++++++++++++++++
 1 file changed, 92 insertions(+)

diff --git a/epoxy-adapter/src/main/java/com/airbnb/epoxy/Carousel.java b/epoxy-adapter/src/main/java/com/airbnb/epoxy/Carousel.java
index 9e9456588b..b21cfab9a2 100644
--- a/epoxy-adapter/src/main/java/com/airbnb/epoxy/Carousel.java
+++ b/epoxy-adapter/src/main/java/com/airbnb/epoxy/Carousel.java
@@ -296,6 +296,98 @@ public void setPaddingDp(@Dimension(unit = Dimension.DP) int paddingDp) {
     setItemSpacingPx(px);
   }
 
+  /**
+   * Use the {@link Padding} class to specify individual padding values for each side of the
+   * carousel, as well as item spacing.
+   * <p>
+   * A value of null will set all padding and item spacing to 0.
+   */
+  @ModelProp(group = "padding")
+  public void setPadding(@Nullable Padding padding) {
+    if (padding == null) {
+      setPaddingDp(0);
+    } else {
+      setPadding(padding.leftPx, padding.topPx, padding.rightPx, padding.bottomPx);
+      setItemSpacingPx(padding.itemSpacingPx);
+    }
+  }
+
+  /**
+   * Used to specify individual padding values programmatically.
+   *
+   * @see #setPadding(Padding)
+   */
+  public static class Padding {
+    @Px public final int topPx;
+    @Px public final int bottomPx;
+    @Px public final int leftPx;
+    @Px public final int rightPx;
+    @Px public final int itemSpacingPx;
+
+    /**
+     * @param paddingPx     Padding in pixels to add on all sides of the carousel
+     * @param itemSpacingPx Space in pixels to add between each carousel item. Will be implemented
+     *                      via an item decoration.
+     */
+    public Padding(@Px int paddingPx, @Px int itemSpacingPx) {
+      this(paddingPx, paddingPx, paddingPx, paddingPx, itemSpacingPx);
+    }
+
+    /**
+     * @param topPx         Top padding in pixels.
+     * @param bottomPx      Bottom padding in pixels.
+     * @param leftPx        Left padding in pixels.
+     * @param rightPx       Right padding in pixels.
+     * @param itemSpacingPx Space in pixels to add between each carousel item. Will be implemented
+     *                      via an item decoration.
+     */
+    public Padding(@Px int topPx, @Px int bottomPx, @Px int leftPx, @Px int rightPx,
+        @Px int itemSpacingPx) {
+
+      this.topPx = topPx;
+      this.bottomPx = bottomPx;
+      this.leftPx = leftPx;
+      this.rightPx = rightPx;
+      this.itemSpacingPx = itemSpacingPx;
+    }
+
+    @Override
+    public boolean equals(Object o) {
+      if (this == o) {
+        return true;
+      }
+      if (o == null || getClass() != o.getClass()) {
+        return false;
+      }
+
+      Padding padding = (Padding) o;
+
+      if (topPx != padding.topPx) {
+        return false;
+      }
+      if (bottomPx != padding.bottomPx) {
+        return false;
+      }
+      if (leftPx != padding.leftPx) {
+        return false;
+      }
+      if (rightPx != padding.rightPx) {
+        return false;
+      }
+      return itemSpacingPx == padding.itemSpacingPx;
+    }
+
+    @Override
+    public int hashCode() {
+      int result = topPx;
+      result = 31 * result + bottomPx;
+      result = 31 * result + leftPx;
+      result = 31 * result + rightPx;
+      result = 31 * result + itemSpacingPx;
+      return result;
+    }
+  }
+
   @ModelProp
   public void setModels(@NonNull List<? extends EpoxyModel<?>> models) {
     super.setModels(models);