From 3c5584705cb92424d9cb73e2340cceb209689027 Mon Sep 17 00:00:00 2001 From: Valere Date: Thu, 3 Jun 2021 09:06:32 +0200 Subject: [PATCH 1/3] Fix Kotlin migration missing Nullable types --- .../main/java/com/airbnb/epoxy/EpoxyTouchHelperCallback.kt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/epoxy-adapter/src/main/java/com/airbnb/epoxy/EpoxyTouchHelperCallback.kt b/epoxy-adapter/src/main/java/com/airbnb/epoxy/EpoxyTouchHelperCallback.kt index 6cc4b86c7e..bcf13f99ac 100644 --- a/epoxy-adapter/src/main/java/com/airbnb/epoxy/EpoxyTouchHelperCallback.kt +++ b/epoxy-adapter/src/main/java/com/airbnb/epoxy/EpoxyTouchHelperCallback.kt @@ -85,7 +85,7 @@ abstract class EpoxyTouchHelperCallback : ItemTouchHelper.Callback() { dropTargets: List, curX: Int, curY: Int - ): EpoxyViewHolder = chooseDropTarget( + ): EpoxyViewHolder? = chooseDropTarget( selected as EpoxyViewHolder, dropTargets as List, curX, @@ -100,8 +100,8 @@ abstract class EpoxyTouchHelperCallback : ItemTouchHelper.Callback() { dropTargets: List, curX: Int, curY: Int - ): EpoxyViewHolder = - super.chooseDropTarget(selected, dropTargets, curX, curY) as EpoxyViewHolder + ): EpoxyViewHolder? = + super.chooseDropTarget(selected, dropTargets, curX, curY) as? EpoxyViewHolder override fun onSelectedChanged(viewHolder: RecyclerView.ViewHolder?, actionState: Int): Unit = onSelectedChanged(viewHolder as EpoxyViewHolder?, actionState) From 3f55e2d260828417cd36a76e039246c363910146 Mon Sep 17 00:00:00 2001 From: Valere Date: Thu, 3 Jun 2021 11:19:54 +0200 Subject: [PATCH 2/3] Added drag n drop sample to test --- kotlinsample/src/main/AndroidManifest.xml | 1 + .../epoxy/kotlinsample/DragAndDropActivity.kt | 83 +++++++++++++++++++ .../airbnb/epoxy/kotlinsample/MainActivity.kt | 10 +++ .../kotlinsample/StickyHeaderActivity.kt | 3 + 4 files changed, 97 insertions(+) create mode 100644 kotlinsample/src/main/java/com/airbnb/epoxy/kotlinsample/DragAndDropActivity.kt diff --git a/kotlinsample/src/main/AndroidManifest.xml b/kotlinsample/src/main/AndroidManifest.xml index f00ae31717..dff87a72b3 100644 --- a/kotlinsample/src/main/AndroidManifest.xml +++ b/kotlinsample/src/main/AndroidManifest.xml @@ -17,6 +17,7 @@ + \ No newline at end of file diff --git a/kotlinsample/src/main/java/com/airbnb/epoxy/kotlinsample/DragAndDropActivity.kt b/kotlinsample/src/main/java/com/airbnb/epoxy/kotlinsample/DragAndDropActivity.kt new file mode 100644 index 0000000000..4c00eaaef6 --- /dev/null +++ b/kotlinsample/src/main/java/com/airbnb/epoxy/kotlinsample/DragAndDropActivity.kt @@ -0,0 +1,83 @@ +package com.airbnb.epoxy.kotlinsample + +import android.os.Bundle +import android.util.Log +import android.view.View +import android.widget.Toast +import androidx.appcompat.app.AppCompatActivity +import androidx.recyclerview.widget.LinearLayoutManager +import com.airbnb.epoxy.EpoxyModel +import com.airbnb.epoxy.EpoxyRecyclerView +import com.airbnb.epoxy.EpoxyTouchHelper +import com.airbnb.epoxy.TypedEpoxyController +import com.airbnb.epoxy.stickyheader.StickyHeaderLinearLayoutManager + +class DragAndDropActivity : AppCompatActivity() { + private lateinit var recyclerView: EpoxyRecyclerView + private var fruits = mutableListOf( + "Apples", + "Blueberries", + "Bananas", + "Oranges", + "Dragon fruit", + "Mango", + "Avocado", + "Lychee" + ) + + private var epoxyController = object : TypedEpoxyController>() { + + override fun buildModels(data: List?) { + data?.forEach { fruit -> + dataBindingItem { + id("data binding $fruit") + text(fruit) + onClick { _ -> + Toast.makeText( + this@DragAndDropActivity, + "clicked $fruit", + Toast.LENGTH_LONG + ).show() + } + onVisibilityStateChanged { model, view, visibilityState -> + Log.d(TAG, "$model -> $visibilityState") + } + } + } + } + } + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(R.layout.activity) + findViewById(R.id.epoxy_view_stub)?.visibility = View.GONE + + recyclerView = findViewById(R.id.recycler_view) + + EpoxyTouchHelper.initDragging(epoxyController) + .withRecyclerView(recyclerView) + .forVerticalList() + .forAllModels() + .andCallbacks(object : EpoxyTouchHelper.DragCallbacks>() { + override fun onModelMoved( + fromPosition: Int, + toPosition: Int, + modelBeingMoved: EpoxyModel<*>?, + itemView: View? + ) { + Log.d(TAG, "onModelMoved from:$fromPosition -> to:$toPosition") + val removed = fruits.removeAt(fromPosition) + fruits.add(toPosition, removed) + epoxyController.setData(fruits) + } + }) + + recyclerView.layoutManager = LinearLayoutManager(this) + recyclerView.setController(epoxyController) + epoxyController.setData(fruits) + } + + companion object { + private const val TAG = "DragAndDropActivity" + } +} \ No newline at end of file diff --git a/kotlinsample/src/main/java/com/airbnb/epoxy/kotlinsample/MainActivity.kt b/kotlinsample/src/main/java/com/airbnb/epoxy/kotlinsample/MainActivity.kt index 1ac2059ad7..e4a52bc144 100644 --- a/kotlinsample/src/main/java/com/airbnb/epoxy/kotlinsample/MainActivity.kt +++ b/kotlinsample/src/main/java/com/airbnb/epoxy/kotlinsample/MainActivity.kt @@ -135,6 +135,16 @@ class MainActivity : AppCompatActivity() { } } + itemCustomView { + id("custom view $i") + color(Color.MAGENTA) + title("Open Drag and Dropt activity") + listener { _ -> + Toast.makeText(this@MainActivity, "clicked", Toast.LENGTH_LONG).show() + startActivity(Intent(this@MainActivity, DragAndDropActivity::class.java)) + } + } + itemEpoxyHolder { id("view holder $i") title("this is a View Holder item") diff --git a/kotlinsample/src/main/java/com/airbnb/epoxy/kotlinsample/StickyHeaderActivity.kt b/kotlinsample/src/main/java/com/airbnb/epoxy/kotlinsample/StickyHeaderActivity.kt index 40d74552f9..6131e27305 100644 --- a/kotlinsample/src/main/java/com/airbnb/epoxy/kotlinsample/StickyHeaderActivity.kt +++ b/kotlinsample/src/main/java/com/airbnb/epoxy/kotlinsample/StickyHeaderActivity.kt @@ -1,6 +1,7 @@ package com.airbnb.epoxy.kotlinsample import android.os.Bundle +import android.view.View import androidx.appcompat.app.AppCompatActivity import com.airbnb.epoxy.EpoxyRecyclerView import com.airbnb.epoxy.stickyheader.StickyHeaderLinearLayoutManager @@ -16,6 +17,8 @@ class StickyHeaderActivity : AppCompatActivity() { super.onCreate(savedInstanceState) setContentView(R.layout.activity) + findViewById(R.id.epoxy_view_stub)?.visibility = View.GONE + recyclerView = findViewById(R.id.recycler_view) // Sample usage when using [EpoxyAdapter] From 24bfdb7cbebbe9fff9fb4786da5eb2e1a314e9ba Mon Sep 17 00:00:00 2001 From: Valere Date: Thu, 3 Jun 2021 12:26:24 +0200 Subject: [PATCH 3/3] Cleaning --- .../java/com/airbnb/epoxy/kotlinsample/DragAndDropActivity.kt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/kotlinsample/src/main/java/com/airbnb/epoxy/kotlinsample/DragAndDropActivity.kt b/kotlinsample/src/main/java/com/airbnb/epoxy/kotlinsample/DragAndDropActivity.kt index 4c00eaaef6..21ae61ec98 100644 --- a/kotlinsample/src/main/java/com/airbnb/epoxy/kotlinsample/DragAndDropActivity.kt +++ b/kotlinsample/src/main/java/com/airbnb/epoxy/kotlinsample/DragAndDropActivity.kt @@ -10,7 +10,6 @@ import com.airbnb.epoxy.EpoxyModel import com.airbnb.epoxy.EpoxyRecyclerView import com.airbnb.epoxy.EpoxyTouchHelper import com.airbnb.epoxy.TypedEpoxyController -import com.airbnb.epoxy.stickyheader.StickyHeaderLinearLayoutManager class DragAndDropActivity : AppCompatActivity() { private lateinit var recyclerView: EpoxyRecyclerView @@ -80,4 +79,4 @@ class DragAndDropActivity : AppCompatActivity() { companion object { private const val TAG = "DragAndDropActivity" } -} \ No newline at end of file +}