From da8e132f1227ddb151f80d86e4f6d86fc6fbff4e Mon Sep 17 00:00:00 2001 From: Zach Bryant Date: Tue, 16 Jul 2019 10:40:14 -0700 Subject: [PATCH 1/5] ListComponent.removeData() returns int --- .../java/com/yelp/android/bento/components/ListComponent.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/bento/src/main/java/com/yelp/android/bento/components/ListComponent.java b/bento/src/main/java/com/yelp/android/bento/components/ListComponent.java index 4dccb92..f7f3bd1 100644 --- a/bento/src/main/java/com/yelp/android/bento/components/ListComponent.java +++ b/bento/src/main/java/com/yelp/android/bento/components/ListComponent.java @@ -89,14 +89,16 @@ public void appendData(@NonNull List data) { * Removes the provided data items from the list. * * @param data The data item to remove from the list. + * @return an int representing the index that the removed data occupied */ - public void removeData(@NonNull T data) { + public int removeData(@NonNull T data) { int index = mData.indexOf(data); // Check if the object indeed is in the list. if (index != -1) { mData.remove(index); notifyItemRangeRemoved(getRemoveIndexStart(index), getRemoveItemCount()); } + return index; } /** From ebc03b7182e1359d589c775345c8af6b807af44a Mon Sep 17 00:00:00 2001 From: Zach Bryant Date: Tue, 16 Jul 2019 12:37:07 -0700 Subject: [PATCH 2/5] ListComponent can insert data at a specified position --- .../yelp/android/bento/components/ListComponent.java | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/bento/src/main/java/com/yelp/android/bento/components/ListComponent.java b/bento/src/main/java/com/yelp/android/bento/components/ListComponent.java index f7f3bd1..d418b30 100644 --- a/bento/src/main/java/com/yelp/android/bento/components/ListComponent.java +++ b/bento/src/main/java/com/yelp/android/bento/components/ListComponent.java @@ -85,6 +85,17 @@ public void appendData(@NonNull List data) { notifyItemRangeInserted(oldSize, sizeChange); } + /** + * Inserts the specified data at the specified index in the component data. + * + * @param index + * @param data + */ + public void insertData(int index, @NonNull T data) { + mData.add(index, data); + notifyItemRangeInserted(index, index + 1); + } + /** * Removes the provided data items from the list. * From 6d4fbe21f87024903df6e80d6ef88c29ba7d25a0 Mon Sep 17 00:00:00 2001 From: Zach Bryant Date: Tue, 16 Jul 2019 12:39:30 -0700 Subject: [PATCH 3/5] comments --- .../java/com/yelp/android/bento/components/ListComponent.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bento/src/main/java/com/yelp/android/bento/components/ListComponent.java b/bento/src/main/java/com/yelp/android/bento/components/ListComponent.java index d418b30..b97357c 100644 --- a/bento/src/main/java/com/yelp/android/bento/components/ListComponent.java +++ b/bento/src/main/java/com/yelp/android/bento/components/ListComponent.java @@ -88,8 +88,8 @@ public void appendData(@NonNull List data) { /** * Inserts the specified data at the specified index in the component data. * - * @param index - * @param data + * @param index the index to insert the given data + * @param data the data to be inserted */ public void insertData(int index, @NonNull T data) { mData.add(index, data); From 34f75ebfd5644ee66b81a315e53280d965e338de Mon Sep 17 00:00:00 2001 From: Zach Bryant Date: Wed, 12 Feb 2020 04:28:48 -0500 Subject: [PATCH 4/5] ignore .idea/ --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index d2203c9..8b39b9f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ .* *.iml +.idea gradle.log local.properties From acb0b0ed170e7d0f0ce93785135dedc2f0c3af00 Mon Sep 17 00:00:00 2001 From: Zach Bryant Date: Wed, 12 Feb 2020 04:53:31 -0500 Subject: [PATCH 5/5] account for dividers when inserting/removing data and added sample app component to demonstrate/test methods --- .../bentosampleapp/ListViewActivity.kt | 25 +++++++++++++++++-- .../bento/components/ListComponent.java | 13 +++++----- 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/bento-sample-app/src/main/java/com/yelp/android/bentosampleapp/ListViewActivity.kt b/bento-sample-app/src/main/java/com/yelp/android/bentosampleapp/ListViewActivity.kt index 269c575..c7ee895 100644 --- a/bento-sample-app/src/main/java/com/yelp/android/bentosampleapp/ListViewActivity.kt +++ b/bento-sample-app/src/main/java/com/yelp/android/bentosampleapp/ListViewActivity.kt @@ -1,6 +1,7 @@ package com.yelp.android.bentosampleapp import android.os.Bundle +import android.util.Log import android.view.Menu import android.view.MenuItem import android.widget.ArrayAdapter @@ -44,6 +45,7 @@ class ListViewActivity : AppCompatActivity() { addCarouselComponent(controller) addArrayAdapterComponent(controller) addAnimatedComponent(controller) + addListComponentRemovableItems(controller) } override fun onCreateOptionsMenu(menu: Menu): Boolean { @@ -79,8 +81,7 @@ class ListViewActivity : AppCompatActivity() { controller.addComponent(simpleComponent) } - - private fun addListComponent(controller: ComponentController) { + private fun addListComponent(controller: ComponentController, removable: Boolean = false) { with(ListComponent(null, ListComponentExampleViewHolder::class.java)) { setStartGap(50) @@ -90,6 +91,26 @@ class ListViewActivity : AppCompatActivity() { } } + private fun addListComponentRemovableItems(controller: ComponentController) { + val component = ListComponent(null, ListComponentExampleViewHolder::class.java) + with(component) { + setStartGap(50) + setData(listOf("List element 1")) + toggleDivider(true) + controller.addComponent(this) + + listOf(1, 1, 1, 2, 4).forEachIndexed { index, i -> insertData(i, "List element ${index + 2}") } + insertData(0, "List element First") + insertData(0, "List element First") + insertData(1 + component.count / 2, "List element Last") + removeData("List element First") + val index = removeData("List element 1") + insertData(index, "List element 1") + removeData("List element 2") + removeData("List element nonexistent") + } + } + private fun addArrayAdapterComponent(controller: ComponentController) { val arrayAdapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, android.R.id.text1, (1 until 42).map { "ArrayAdapter element $it" }) diff --git a/bento/src/main/java/com/yelp/android/bento/components/ListComponent.java b/bento/src/main/java/com/yelp/android/bento/components/ListComponent.java index b97357c..38e7df9 100644 --- a/bento/src/main/java/com/yelp/android/bento/components/ListComponent.java +++ b/bento/src/main/java/com/yelp/android/bento/components/ListComponent.java @@ -86,21 +86,22 @@ public void appendData(@NonNull List data) { } /** - * Inserts the specified data at the specified index in the component data. + * Inserts a single list item at the specified index in the list. * - * @param index the index to insert the given data - * @param data the data to be inserted + * @param index The index used to insert the given data in the list. + * @param data The list item to be inserted. */ public void insertData(int index, @NonNull T data) { mData.add(index, data); - notifyItemRangeInserted(index, index + 1); + // Update 2 items if dividers are showing. + notifyItemRangeInserted(index, mShouldShowDivider ? 2 : 1); } /** - * Removes the provided data items from the list. + * Removes the first occurrence of the specified data item from the list. * * @param data The data item to remove from the list. - * @return an int representing the index that the removed data occupied + * @return The index that the data occupied. Returns -1 if nothing was removed. */ public int removeData(@NonNull T data) { int index = mData.indexOf(data);