[go: nahoru, domu]

Remove default `EmojiGridRows`.

If the value is not set, EmojiView will be created with an equal height and width. This way the emojis are always user friendly.

Test: NA
Change-Id: Id4ed788954f30eab36fe9df94a488b2a45678cf6
diff --git a/emoji2/emoji2-emojipicker/samples/src/main/res/layout-land/main.xml b/emoji2/emoji2-emojipicker/samples/src/main/res/layout-land/main.xml
index 6f076ad..7c10005 100644
--- a/emoji2/emoji2-emojipicker/samples/src/main/res/layout-land/main.xml
+++ b/emoji2/emoji2-emojipicker/samples/src/main/res/layout-land/main.xml
@@ -46,6 +46,5 @@
         android:id="@+id/emoji_picker"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
-        app:emojiGridColumns="20"
-        app:emojiGridRows="5" />
+        app:emojiGridColumns="20" />
 </LinearLayout>
diff --git a/emoji2/emoji2-emojipicker/samples/src/main/res/layout/main.xml b/emoji2/emoji2-emojipicker/samples/src/main/res/layout/main.xml
index 085f307..f79a375 100644
--- a/emoji2/emoji2-emojipicker/samples/src/main/res/layout/main.xml
+++ b/emoji2/emoji2-emojipicker/samples/src/main/res/layout/main.xml
@@ -46,6 +46,5 @@
         android:id="@+id/emoji_picker"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
-        app:emojiGridRows="15"
         app:emojiGridColumns="9" />
 </LinearLayout>
diff --git a/emoji2/emoji2-emojipicker/src/main/java/androidx/emoji2/emojipicker/EmojiPickerBodyAdapter.kt b/emoji2/emoji2-emojipicker/src/main/java/androidx/emoji2/emojipicker/EmojiPickerBodyAdapter.kt
index 66f3bef..7f8fa6b9 100644
--- a/emoji2/emoji2-emojipicker/src/main/java/androidx/emoji2/emojipicker/EmojiPickerBodyAdapter.kt
+++ b/emoji2/emoji2-emojipicker/src/main/java/androidx/emoji2/emojipicker/EmojiPickerBodyAdapter.kt
@@ -33,7 +33,7 @@
 internal class EmojiPickerBodyAdapter(
     private val context: Context,
     private val emojiGridColumns: Int,
-    private val emojiGridRows: Float,
+    private val emojiGridRows: Float?,
     private val stickyVariantProvider: StickyVariantProvider,
     private val emojiPickerItemsProvider: () -> EmojiPickerItems,
     private val onEmojiPickedListener: EmojiPickerBodyAdapter.(EmojiViewItem) -> Unit,
@@ -43,25 +43,25 @@
     private var emojiCellHeight: Int? = null
 
     @UiThread
-    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder =
-        when (viewType.toItemType()) {
+    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
+        emojiCellWidth = emojiCellWidth ?: (getParentWidth(parent) / emojiGridColumns)
+        emojiCellHeight =
+            emojiCellHeight ?: emojiGridRows?.let { getEmojiCellTotalHeight(parent) / it }
+                ?.toInt() ?: emojiCellWidth
+
+        return when (viewType.toItemType()) {
             ItemType.CATEGORY_TITLE -> createSimpleHolder(R.layout.category_text_view, parent)
             ItemType.PLACEHOLDER_TEXT -> createSimpleHolder(
                 R.layout.empty_category_text_view, parent
             ) {
-                minimumHeight =
-                    emojiCellHeight ?: (getEmojiCellTotalHeight(parent) / (emojiGridRows)).toInt()
-                        .also { emojiCellHeight = it }
+                minimumHeight = emojiCellHeight!!
             }
 
             ItemType.EMOJI -> {
                 EmojiViewHolder(context,
                     parent,
-                    emojiCellWidth ?: (getParentWidth(parent) / emojiGridColumns).also {
-                        emojiCellWidth = it
-                    },
-                    emojiCellHeight ?: (getEmojiCellTotalHeight(parent) / (emojiGridRows)).toInt()
-                        .also { emojiCellHeight = it },
+                    emojiCellWidth!!,
+                    emojiCellHeight!!,
                     layoutInflater,
                     stickyVariantProvider,
                      emojiViewItem ->
@@ -82,6 +82,7 @@
                     })
             }
         }
+    }
 
     override fun onBindViewHolder(viewHolder: ViewHolder, position: Int) {
         val item = emojiPickerItemsProvider().getBodyItem(position)
diff --git a/emoji2/emoji2-emojipicker/src/main/java/androidx/emoji2/emojipicker/EmojiPickerConstants.kt b/emoji2/emoji2-emojipicker/src/main/java/androidx/emoji2/emojipicker/EmojiPickerConstants.kt
index 479e1ff..0956f96 100644
--- a/emoji2/emoji2-emojipicker/src/main/java/androidx/emoji2/emojipicker/EmojiPickerConstants.kt
+++ b/emoji2/emoji2-emojipicker/src/main/java/androidx/emoji2/emojipicker/EmojiPickerConstants.kt
@@ -22,9 +22,6 @@
     // The default number of body columns.
     const val DEFAULT_BODY_COLUMNS = 9
 
-    // The default number of body rows.
-    const val DEFAULT_BODY_ROWS = 7.5f
-
     // The default number of rows of recent items held.
     const val DEFAULT_MAX_RECENT_ITEM_ROWS = 3
 
diff --git a/emoji2/emoji2-emojipicker/src/main/java/androidx/emoji2/emojipicker/EmojiPickerView.kt b/emoji2/emoji2-emojipicker/src/main/java/androidx/emoji2/emojipicker/EmojiPickerView.kt
index 918a7d6..c7f1d07 100644
--- a/emoji2/emoji2-emojipicker/src/main/java/androidx/emoji2/emojipicker/EmojiPickerView.kt
+++ b/emoji2/emoji2-emojipicker/src/main/java/androidx/emoji2/emojipicker/EmojiPickerView.kt
@@ -50,19 +50,21 @@
         internal var emojiCompatLoaded: Boolean = false
     }
 
+    private var _emojiGridRows: Float? = null
     /**
      * The number of rows of the emoji picker.
      *
-     * Default value([EmojiPickerConstants.DEFAULT_BODY_ROWS]: 7.5) will be used if emojiGridRows
-     * is set to non-positive value. Float value indicates that we will display partial of the last
-     * row and have content down, so the users get the idea that they can scroll down for more
-     * contents.
+     * Optional field. If not set, the value will be calculated based on parent view height and
+     * [emojiGridColumns].
+     * Float value indicates that the picker could display the last row partially, so the users get
+     * the idea that they can scroll down for more contents.
      * @attr ref androidx.emoji2.emojipicker.R.styleable.EmojiPickerView_emojiGridRows
      */
-    var emojiGridRows: Float = EmojiPickerConstants.DEFAULT_BODY_ROWS
+    var emojiGridRows: Float
+        get() = _emojiGridRows ?: -1F
         set(value) {
-            field = if (value > 0) value else EmojiPickerConstants.DEFAULT_BODY_ROWS
-            // this step is to ensure the layout refresh when emojiGridRows is reset
+            _emojiGridRows = value.takeIf { it > 0 }
+            // Refresh when emojiGridRows is reset
             if (isLaidOut) {
                 showEmojiPickerView()
             }
@@ -77,8 +79,8 @@
      */
     var emojiGridColumns: Int = EmojiPickerConstants.DEFAULT_BODY_COLUMNS
         set(value) {
-            field = if (value > 0) value else EmojiPickerConstants.DEFAULT_BODY_COLUMNS
-            // this step is to ensure the layout refresh when emojiGridColumns is reset
+            field = value.takeIf { it > 0 } ?: EmojiPickerConstants.DEFAULT_BODY_COLUMNS
+            // Refresh when emojiGridColumns is reset
             if (isLaidOut) {
                 showEmojiPickerView()
             }
@@ -100,10 +102,11 @@
     init {
         val typedArray: TypedArray =
             context.obtainStyledAttributes(attrs, R.styleable.EmojiPickerView, 0, 0)
-        emojiGridRows = typedArray.getFloat(
-            R.styleable.EmojiPickerView_emojiGridRows,
-            EmojiPickerConstants.DEFAULT_BODY_ROWS
-        )
+        _emojiGridRows = with(R.styleable.EmojiPickerView_emojiGridRows) {
+            if (typedArray.hasValue(this)) {
+                typedArray.getFloat(this, 0F)
+            } else null
+        }
         emojiGridColumns = typedArray.getInt(
             R.styleable.EmojiPickerView_emojiGridColumns,
             EmojiPickerConstants.DEFAULT_BODY_COLUMNS
@@ -146,7 +149,7 @@
         return EmojiPickerBodyAdapter(
             context,
             emojiGridColumns,
-            emojiGridRows,
+            _emojiGridRows,
             stickyVariantProvider,
             emojiPickerItemsProvider = { emojiPickerItems },
              emojiViewItem ->