From fb84f1a0ad49c4cdb60fa12da8dd13f6e2ecb208 Mon Sep 17 00:00:00 2001 From: Drakeet Date: Mon, 9 Dec 2019 21:53:49 +0800 Subject: [PATCH] Add ItemViewDelegate & onCreateViewHolder(context, _) (#292) This PR adds a new `ItemViewDelegate` which is renamed from the old `ItemViewBinder`, and the old `ItemViewBinder` now inherits from `ItemViewDelegate` and provides a compatible `LayoutInflater` version of `ItemViewDelegate` implementation. This change is compatible with the old API, so all projects using _MultiType_ can be upgraded without any changes. But if you need to use a better `ItemViewDelegate` with a `Context` parameter, you need to change to inherit `ItemViewDelegate` by yourself. --- .../drakeet/multitype/ClassLinkerBridge.kt | 10 +- ...eption.kt => DelegateNotFoundException.kt} | 4 +- .../com/drakeet/multitype/ItemViewBinder.kt | 215 +---------------- .../com/drakeet/multitype/ItemViewDelegate.kt | 220 ++++++++++++++++++ .../com/drakeet/multitype/JavaClassLinker.kt | 8 +- .../drakeet/multitype/KotlinClassLinker.kt | 8 +- .../kotlin/com/drakeet/multitype/Linker.kt | 10 +- .../com/drakeet/multitype/MultiTypeAdapter.kt | 73 +++--- .../com/drakeet/multitype/OneToManyBuilder.kt | 12 +- .../drakeet/multitype/OneToManyEndpoint.kt | 10 +- .../com/drakeet/multitype/OneToManyFlow.kt | 6 +- .../main/kotlin/com/drakeet/multitype/Type.kt | 2 +- .../kotlin/com/drakeet/multitype/Types.kt | 2 +- ...wBinderTest.kt => ItemViewDelegateTest.kt} | 20 +- .../drakeet/multitype/MultiTypeAdapterTest.kt | 40 ++-- .../com/drakeet/multitype/MultiTypeTest.kt | 12 +- .../com/drakeet/multitype/MutableTypesTest.kt | 4 +- .../drakeet/multitype/OneToManyBuilderTest.kt | 20 +- ...ingViewBinder.kt => StringViewDelegate.kt} | 7 +- ...mViewBinder.kt => TestItemViewDelegate.kt} | 6 +- .../sample/one2many/DuplicateTypesTest.kt | 12 +- 21 files changed, 359 insertions(+), 342 deletions(-) rename library/src/main/kotlin/com/drakeet/multitype/{BinderNotFoundException.kt => DelegateNotFoundException.kt} (80%) create mode 100644 library/src/main/kotlin/com/drakeet/multitype/ItemViewDelegate.kt rename library/src/test/kotlin/com/drakeet/multitype/{ItemViewBinderTest.kt => ItemViewDelegateTest.kt} (75%) rename library/src/test/kotlin/com/drakeet/multitype/{StringViewBinder.kt => StringViewDelegate.kt} (76%) rename library/src/test/kotlin/com/drakeet/multitype/{TestItemViewBinder.kt => TestItemViewDelegate.kt} (81%) diff --git a/library/src/main/kotlin/com/drakeet/multitype/ClassLinkerBridge.kt b/library/src/main/kotlin/com/drakeet/multitype/ClassLinkerBridge.kt index 5e996886..d320d318 100644 --- a/library/src/main/kotlin/com/drakeet/multitype/ClassLinkerBridge.kt +++ b/library/src/main/kotlin/com/drakeet/multitype/ClassLinkerBridge.kt @@ -21,24 +21,24 @@ package com.drakeet.multitype */ internal class ClassLinkerBridge private constructor( private val javaClassLinker: JavaClassLinker, - private val binders: Array> + private val delegates: Array> ) : Linker { override fun index(position: Int, item: T): Int { val indexedClass = javaClassLinker.index(position, item) - val index = binders.indexOfFirst { it.javaClass == indexedClass } + val index = delegates.indexOfFirst { it.javaClass == indexedClass } if (index != -1) return index throw IndexOutOfBoundsException( - "The binders'(${binders.contentToString()}) you registered do not contain this ${indexedClass.name}." + "The delegates'(${delegates.contentToString()}) you registered do not contain this ${indexedClass.name}." ) } companion object { fun toLinker( javaClassLinker: JavaClassLinker, - binders: Array> + delegates: Array> ): Linker { - return ClassLinkerBridge(javaClassLinker, binders) + return ClassLinkerBridge(javaClassLinker, delegates) } } } diff --git a/library/src/main/kotlin/com/drakeet/multitype/BinderNotFoundException.kt b/library/src/main/kotlin/com/drakeet/multitype/DelegateNotFoundException.kt similarity index 80% rename from library/src/main/kotlin/com/drakeet/multitype/BinderNotFoundException.kt rename to library/src/main/kotlin/com/drakeet/multitype/DelegateNotFoundException.kt index aadef14e..9c915b99 100644 --- a/library/src/main/kotlin/com/drakeet/multitype/BinderNotFoundException.kt +++ b/library/src/main/kotlin/com/drakeet/multitype/DelegateNotFoundException.kt @@ -19,6 +19,6 @@ package com.drakeet.multitype /** * @author Drakeet Xu */ -internal class BinderNotFoundException(clazz: Class<*>) : RuntimeException( - "Have you registered the ${clazz.name} type and its binder to the adapter or types?" +internal class DelegateNotFoundException(clazz: Class<*>) : RuntimeException( + "Have you registered the ${clazz.name} type and its delegate or binder?" ) diff --git a/library/src/main/kotlin/com/drakeet/multitype/ItemViewBinder.kt b/library/src/main/kotlin/com/drakeet/multitype/ItemViewBinder.kt index a46de8a5..c660c929 100644 --- a/library/src/main/kotlin/com/drakeet/multitype/ItemViewBinder.kt +++ b/library/src/main/kotlin/com/drakeet/multitype/ItemViewBinder.kt @@ -1,219 +1,20 @@ -/* - * Copyright (c) 2016-present. Drakeet Xu - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - package com.drakeet.multitype +import android.content.Context import android.view.LayoutInflater import android.view.ViewGroup import androidx.recyclerview.widget.RecyclerView -import androidx.recyclerview.widget.RecyclerView.LayoutManager -import androidx.recyclerview.widget.RecyclerView.ViewHolder -/*** +/** + * This is a compatible version of [ItemViewDelegate]. + * @see ItemViewDelegate * @author Drakeet Xu */ -abstract class ItemViewBinder { - - @Suppress("PropertyName") - internal var _adapter: MultiTypeAdapter? = null - - /** - * Gets the associated [MultiTypeAdapter]. - * @since v2.3.4 - */ - val adapter: MultiTypeAdapter - get() { - if (_adapter == null) { - throw IllegalStateException( - "This $this has not been attached to MultiTypeAdapter yet. " + - "You should not call the method before registering the binder." - ) - } - return _adapter!! - } - - /** - * Gets or sets the items of the associated [MultiTypeAdapter]. - * @see MultiTypeAdapter.items - * @since v4.0.0 - */ - var adapterItems: List - get() = adapter.items - set(value) { - adapter.items = value - } - - abstract fun onCreateViewHolder(inflater: LayoutInflater, parent: ViewGroup): VH - - /** - * Called by MultiTypeAdapter to display the data with its view holder. This method should - * update the contents of the [ViewHolder.itemView] to reflect the given item. - * - * If you need the position of an item later on (e.g. in a click listener), use - * `ViewHolder#getAdapterPosition()` which will have the updated adapter position. - * - * Override `onBindViewHolder(ViewHolder, Object, List)` instead if your ItemViewBinder - * can handle efficient partial bind. - * - * @param holder The ViewHolder which should be updated to represent the contents of the - * given item in the items data set. - * @param item The item within the MultiTypeAdapter's items data set. - */ - abstract fun onBindViewHolder(holder: VH, item: T) +abstract class ItemViewBinder : ItemViewDelegate() { - /** - * Called by MultiTypeAdapter to display the data with its view holder. This method should - * update the contents of the [ViewHolder.itemView] to reflect the given item. - * - * If you need the position of an item later on (e.g. in a click listener), use - * [ViewHolder.getAdapterPosition] which will have the updated adapter position. - * - * Partial bind vs full bind: - * - * The payloads parameter is a merge list from [MultiTypeAdapter.notifyItemChanged] [MultiTypeAdapter.notifyItemRangeChanged]. - * If the payloads list is not empty, the ViewHolder is currently bound to old data and - * ItemViewBinder may run an efficient partial update using the payload info. - * If the payload is empty, ItemViewBinder must run a full bind. - * ItemViewBinder should not assume that the payload passed in notify methods will be - * received by onBindViewHolder(). For example when the view is not attached to the screen, - * the payload in notifyItemChange() will be simply dropped. - * - * This implementation calls the `onBindViewHolder(ViewHolder, Object)` by default. - * - * @param holder The ViewHolder which should be updated to represent the contents of the - * given item in the items data set. - * @param item The item within the MultiTypeAdapter's items data set. - * @param payloads A non-null list of merged payloads. Can be empty list if requires full - * update. - * @since v2.5.0 - */ - open fun onBindViewHolder(holder: VH, item: T, payloads: List) { - onBindViewHolder(holder, item) + final override fun onCreateViewHolder(context: Context, parent: ViewGroup): VH { + return onCreateViewHolder(LayoutInflater.from(context), parent) } - /** - * Get the adapter position of current item, - * the internal position equals to [ViewHolder.getAdapterPosition]. - * - * **NOTE**: Below v2.3.5 we may provide getPosition() method to get the position, - * It exists BUG, and sometimes can not get the correct position, - * it is recommended to immediately stop using it and use the new - * `getPosition(ViewHolder)` instead. - * - * @param holder The ViewHolder to call holder.getAdapterPosition(). - * @return The adapter position. - * @since v2.3.5. If below v2.3.5, use [ViewHolder.getAdapterPosition] instead. - */ - fun getPosition(holder: ViewHolder): Int { - return holder.adapterPosition - } - - /** - * Return the stable ID for the `item`. If [RecyclerView.Adapter.hasStableIds] - * would return false this method should return [RecyclerView.NO_ID]. The default - * implementation of this method returns [RecyclerView.NO_ID]. - * - * @param item The item within the MultiTypeAdapter's items data set to query - * @return the stable ID of the item - * @see RecyclerView.Adapter.setHasStableIds - * @since v3.2.0 - */ - @Suppress("UNUSED_PARAMETER") - open fun getItemId(item: T): Long = RecyclerView.NO_ID - - /** - * Called when a view created by this [ItemViewBinder] has been recycled. - * - * A view is recycled when a [LayoutManager] decides that it no longer - * needs to be attached to its parent [RecyclerView]. This can be because it has - * fallen out of visibility or a set of cached views represented by views still - * attached to the parent RecyclerView. If an item view has large or expensive data - * bound to it such as large bitmaps, this may be a good place to release those - * resources. - * - * RecyclerView calls this method right before clearing ViewHolder's internal data and - * sending it to RecycledViewPool. - * - * @param holder The ViewHolder for the view being recycled - * @since v3.1.0 - */ - open fun onViewRecycled(holder: VH) {} - - /** - * Called by the RecyclerView if a ViewHolder created by this Adapter cannot be recycled - * due to its transient state. Upon receiving this callback, Adapter can clear the - * animation(s) that effect the View's transient state and return `true` so that - * the View can be recycled. Keep in mind that the View in question is already removed from - * the RecyclerView. - * - * In some cases, it is acceptable to recycle a View although it has transient state. Most - * of the time, this is a case where the transient state will be cleared in - * [.onBindViewHolder] call when View is rebound to a new item. - * For this reason, RecyclerView leaves the decision to the Adapter and uses the return - * value of this method to decide whether the View should be recycled or not. - * - * Note that when all animations are created by [RecyclerView.ItemAnimator], you - * should never receive this callback because RecyclerView keeps those Views as children - * until their animations are complete. This callback is useful when children of the item - * views create animations which may not be easy to implement using an [ ]. - * - * You should *never* fix this issue by calling - * `holder.itemView.setHasTransientState(false);` unless you've previously called - * `holder.itemView.setHasTransientState(true);`. Each - * `View.setHasTransientState(true)` call must be matched by a - * `View.setHasTransientState(false)` call, otherwise, the state of the View - * may become inconsistent. You should always prefer to end or cancel animations that are - * triggering the transient state instead of handling it manually. - * - * @param holder The ViewHolder containing the View that could not be recycled due to its - * transient state. - * @return True if the View should be recycled, false otherwise. Note that if this method - * returns `true`, RecyclerView *will ignore* the transient state of - * the View and recycle it regardless. If this method returns `false`, - * RecyclerView will check the View's transient state again before giving a final decision. - * Default implementation returns false. - * @since v3.1.0 - */ - open fun onFailedToRecycleView(holder: VH): Boolean { - return false - } - - /** - * Called when a view created by this [ItemViewBinder] has been attached to a window. - * - * This can be used as a reasonable signal that the view is about to be seen - * by the user. If the [ItemViewBinder] previously freed any resources in - * [onViewDetachedFromWindow][.onViewDetachedFromWindow] - * those resources should be restored here. - * - * @param holder Holder of the view being attached - * @since v3.1.0 - */ - open fun onViewAttachedToWindow(holder: VH) {} - - /** - * Called when a view created by this [ItemViewBinder] has been detached from its - * window. - * - * Becoming detached from the window is not necessarily a permanent condition; - * the consumer of an Adapter's views may choose to cache views offscreen while they - * are not visible, attaching and detaching them as appropriate. - * - * @param holder Holder of the view being detached - * @since v3.1.0 - */ - open fun onViewDetachedFromWindow(holder: VH) {} + abstract fun onCreateViewHolder(inflater: LayoutInflater, parent: ViewGroup): VH } diff --git a/library/src/main/kotlin/com/drakeet/multitype/ItemViewDelegate.kt b/library/src/main/kotlin/com/drakeet/multitype/ItemViewDelegate.kt new file mode 100644 index 00000000..566b9647 --- /dev/null +++ b/library/src/main/kotlin/com/drakeet/multitype/ItemViewDelegate.kt @@ -0,0 +1,220 @@ +/* + * Copyright (c) 2016-present. Drakeet Xu + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.drakeet.multitype + +import android.content.Context +import android.view.ViewGroup +import androidx.recyclerview.widget.RecyclerView +import androidx.recyclerview.widget.RecyclerView.LayoutManager +import androidx.recyclerview.widget.RecyclerView.ViewHolder + +/*** + * @author Drakeet Xu + * @since v4.1.0 + */ +abstract class ItemViewDelegate { + + @Suppress("PropertyName") + internal var _adapter: MultiTypeAdapter? = null + + /** + * Gets the associated [MultiTypeAdapter]. + * @since v2.3.4 + */ + val adapter: MultiTypeAdapter + get() { + if (_adapter == null) { + throw IllegalStateException( + "This $this has not been attached to MultiTypeAdapter yet. " + + "You should not call the method before registering the delegate." + ) + } + return _adapter!! + } + + /** + * Gets or sets the items of the associated [MultiTypeAdapter]. + * @see MultiTypeAdapter.items + * @since v4.0.0 + */ + var adapterItems: List + get() = adapter.items + set(value) { + adapter.items = value + } + + abstract fun onCreateViewHolder(context: Context, parent: ViewGroup): VH + + /** + * Called by MultiTypeAdapter to display the data with its view holder. This method should + * update the contents of the [ViewHolder.itemView] to reflect the given item. + * + * If you need the position of an item later on (e.g. in a click listener), use + * `ViewHolder#getAdapterPosition()` which will have the updated adapter position. + * + * Override `onBindViewHolder(ViewHolder, Object, List)` instead if your ItemViewDelegate + * can handle efficient partial bind. + * + * @param holder The ViewHolder which should be updated to represent the contents of the + * given item in the items data set. + * @param item The item within the MultiTypeAdapter's items data set. + */ + abstract fun onBindViewHolder(holder: VH, item: T) + + /** + * Called by MultiTypeAdapter to display the data with its view holder. This method should + * update the contents of the [ViewHolder.itemView] to reflect the given item. + * + * If you need the position of an item later on (e.g. in a click listener), use + * [ViewHolder.getAdapterPosition] which will have the updated adapter position. + * + * Partial bind vs full bind: + * + * The payloads parameter is a merge list from [MultiTypeAdapter.notifyItemChanged] [MultiTypeAdapter.notifyItemRangeChanged]. + * If the payloads list is not empty, the ViewHolder is currently bound to old data and + * ItemViewDelegate may run an efficient partial update using the payload info. + * If the payload is empty, ItemViewDelegate must run a full bind. + * ItemViewDelegate should not assume that the payload passed in notify methods will be + * received by onBindViewHolder(). For example when the view is not attached to the screen, + * the payload in notifyItemChange() will be simply dropped. + * + * This implementation calls the `onBindViewHolder(ViewHolder, Object)` by default. + * + * @param holder The ViewHolder which should be updated to represent the contents of the + * given item in the items data set. + * @param item The item within the MultiTypeAdapter's items data set. + * @param payloads A non-null list of merged payloads. Can be empty list if requires full + * update. + * @since v2.5.0 + */ + open fun onBindViewHolder(holder: VH, item: T, payloads: List) { + onBindViewHolder(holder, item) + } + + /** + * Get the adapter position of current item, + * the internal position equals to [ViewHolder.getAdapterPosition]. + * + * **NOTE**: Below v2.3.5 we may provide getPosition() method to get the position, + * It exists BUG, and sometimes can not get the correct position, + * it is recommended to immediately stop using it and use the new + * `getPosition(ViewHolder)` instead. + * + * @param holder The ViewHolder to call holder.getAdapterPosition(). + * @return The adapter position. + * @since v2.3.5. If below v2.3.5, use [ViewHolder.getAdapterPosition] instead. + */ + fun getPosition(holder: ViewHolder): Int { + return holder.adapterPosition + } + + /** + * Return the stable ID for the `item`. If [RecyclerView.Adapter.hasStableIds] + * would return false this method should return [RecyclerView.NO_ID]. The default + * implementation of this method returns [RecyclerView.NO_ID]. + * + * @param item The item within the MultiTypeAdapter's items data set to query + * @return the stable ID of the item + * @see RecyclerView.Adapter.setHasStableIds + * @since v3.2.0 + */ + @Suppress("UNUSED_PARAMETER") + open fun getItemId(item: T): Long = RecyclerView.NO_ID + + /** + * Called when a view created by this [ItemViewDelegate] has been recycled. + * + * A view is recycled when a [LayoutManager] decides that it no longer + * needs to be attached to its parent [RecyclerView]. This can be because it has + * fallen out of visibility or a set of cached views represented by views still + * attached to the parent RecyclerView. If an item view has large or expensive data + * bound to it such as large bitmaps, this may be a good place to release those + * resources. + * + * RecyclerView calls this method right before clearing ViewHolder's internal data and + * sending it to RecycledViewPool. + * + * @param holder The ViewHolder for the view being recycled + * @since v3.1.0 + */ + open fun onViewRecycled(holder: VH) {} + + /** + * Called by the RecyclerView if a ViewHolder created by this Adapter cannot be recycled + * due to its transient state. Upon receiving this callback, Adapter can clear the + * animation(s) that effect the View's transient state and return `true` so that + * the View can be recycled. Keep in mind that the View in question is already removed from + * the RecyclerView. + * + * In some cases, it is acceptable to recycle a View although it has transient state. Most + * of the time, this is a case where the transient state will be cleared in + * [.onBindViewHolder] call when View is rebound to a new item. + * For this reason, RecyclerView leaves the decision to the Adapter and uses the return + * value of this method to decide whether the View should be recycled or not. + * + * Note that when all animations are created by [RecyclerView.ItemAnimator], you + * should never receive this callback because RecyclerView keeps those Views as children + * until their animations are complete. This callback is useful when children of the item + * views create animations which may not be easy to implement using an [ ]. + * + * You should *never* fix this issue by calling + * `holder.itemView.setHasTransientState(false);` unless you've previously called + * `holder.itemView.setHasTransientState(true);`. Each + * `View.setHasTransientState(true)` call must be matched by a + * `View.setHasTransientState(false)` call, otherwise, the state of the View + * may become inconsistent. You should always prefer to end or cancel animations that are + * triggering the transient state instead of handling it manually. + * + * @param holder The ViewHolder containing the View that could not be recycled due to its + * transient state. + * @return True if the View should be recycled, false otherwise. Note that if this method + * returns `true`, RecyclerView *will ignore* the transient state of + * the View and recycle it regardless. If this method returns `false`, + * RecyclerView will check the View's transient state again before giving a final decision. + * Default implementation returns false. + * @since v3.1.0 + */ + open fun onFailedToRecycleView(holder: VH): Boolean { + return false + } + + /** + * Called when a view created by this [ItemViewDelegate] has been attached to a window. + * + * This can be used as a reasonable signal that the view is about to be seen + * by the user. If the [ItemViewDelegate] previously freed any resources in + * [onViewDetachedFromWindow][.onViewDetachedFromWindow] + * those resources should be restored here. + * + * @param holder Holder of the view being attached + * @since v3.1.0 + */ + open fun onViewAttachedToWindow(holder: VH) {} + + /** + * Called when a view created by this [ItemViewDelegate] has been detached from its + * window. + * + * Becoming detached from the window is not necessarily a permanent condition; + * the consumer of an Adapter's views may choose to cache views offscreen while they + * are not visible, attaching and detaching them as appropriate. + * + * @param holder Holder of the view being detached + * @since v3.1.0 + */ + open fun onViewDetachedFromWindow(holder: VH) {} +} diff --git a/library/src/main/kotlin/com/drakeet/multitype/JavaClassLinker.kt b/library/src/main/kotlin/com/drakeet/multitype/JavaClassLinker.kt index 7a1e3a5c..05f142c5 100644 --- a/library/src/main/kotlin/com/drakeet/multitype/JavaClassLinker.kt +++ b/library/src/main/kotlin/com/drakeet/multitype/JavaClassLinker.kt @@ -17,19 +17,19 @@ package com.drakeet.multitype /** - * An interface to link the items and binders by the classes of binders. + * An interface to link the items and delegates by the classes of delegates. * * @author Drakeet Xu */ interface JavaClassLinker { /** - * Returns the class of your registered binders for your item. + * Returns the class of your registered delegates for your item. * * @param position The position in items * @param item The item - * @return The index of your registered binders + * @return The index of your registered delegates * @see OneToManyEndpoint.withJavaClassLinker */ - fun index(position: Int, item: T): Class> + fun index(position: Int, item: T): Class> } diff --git a/library/src/main/kotlin/com/drakeet/multitype/KotlinClassLinker.kt b/library/src/main/kotlin/com/drakeet/multitype/KotlinClassLinker.kt index bfbd1b0b..3a222be0 100644 --- a/library/src/main/kotlin/com/drakeet/multitype/KotlinClassLinker.kt +++ b/library/src/main/kotlin/com/drakeet/multitype/KotlinClassLinker.kt @@ -19,19 +19,19 @@ package com.drakeet.multitype import kotlin.reflect.KClass /** - * An interface to link the items and binders by the classes of binders. + * An interface to link the items and delegates by the classes of delegates. * * @author Drakeet Xu */ interface KotlinClassLinker { /** - * Returns the class of your registered binders for your item. + * Returns the class of your registered delegates for your item. * * @param position The position in items * @param item The item - * @return The index of your registered binders + * @return The index of your registered delegates * @see OneToManyEndpoint.withJavaClassLinker */ - fun index(position: Int, item: T): KClass> + fun index(position: Int, item: T): KClass> } diff --git a/library/src/main/kotlin/com/drakeet/multitype/Linker.kt b/library/src/main/kotlin/com/drakeet/multitype/Linker.kt index f0fd65a5..0b93e4df 100644 --- a/library/src/main/kotlin/com/drakeet/multitype/Linker.kt +++ b/library/src/main/kotlin/com/drakeet/multitype/Linker.kt @@ -19,22 +19,22 @@ package com.drakeet.multitype import androidx.annotation.IntRange /** - * An interface to link the items and binders by the array index. + * An interface to link the items and delegates by the array index. * * @author Drakeet Xu */ interface Linker { /** - * Returns the index of your registered binders for your item. The result should be in range of - * `[0, one-to-multiple-binders.length)`. + * Returns the index of your registered delegates for your item. The result should be in range of + * `[0, one-to-multiple-delegates.length)`. * * Note: The argument of [OneToManyFlow.to] is the - * one-to-multiple-binders. + * one-to-multiple-delegates. * * @param position The position in items * @param item The data item - * @return The index of your registered binders + * @return The index of your registered delegates * @see OneToManyFlow.to * @see OneToManyEndpoint.withLinker */ diff --git a/library/src/main/kotlin/com/drakeet/multitype/MultiTypeAdapter.kt b/library/src/main/kotlin/com/drakeet/multitype/MultiTypeAdapter.kt index bdb9d106..79f0dd6f 100644 --- a/library/src/main/kotlin/com/drakeet/multitype/MultiTypeAdapter.kt +++ b/library/src/main/kotlin/com/drakeet/multitype/MultiTypeAdapter.kt @@ -17,7 +17,6 @@ package com.drakeet.multitype import android.util.Log -import android.view.LayoutInflater import android.view.ViewGroup import androidx.annotation.CheckResult import androidx.recyclerview.widget.RecyclerView @@ -44,8 +43,8 @@ open class MultiTypeAdapter @JvmOverloads constructor( ) : RecyclerView.Adapter() { /** - * Registers a type class and its item view binder. If you have registered the class, - * it will override the original binder(s). Note that the method is non-thread-safe + * Registers a type class and its item view delegate. If you have registered the class, + * it will override the original delegate(s). Note that the method is non-thread-safe * so that you should not use it in concurrent operation. * * Note that the method should not be called after @@ -53,30 +52,30 @@ open class MultiTypeAdapter @JvmOverloads constructor( * again. * * @param clazz the class of a item - * @param binder the item view binder + * @param delegate the item view delegate * @param T the item data type * */ - fun register(clazz: Class, binder: ItemViewBinder) { + fun register(clazz: Class, delegate: ItemViewDelegate) { unregisterAllTypesIfNeeded(clazz) - register(Type(clazz, binder, DefaultLinker())) + register(Type(clazz, delegate, DefaultLinker())) } - inline fun register(binder: ItemViewBinder) { - register(T::class.java, binder) + inline fun register(delegate: ItemViewDelegate) { + register(T::class.java, delegate) } - fun register(clazz: KClass, binder: ItemViewBinder) { - register(clazz.java, binder) + fun register(clazz: KClass, delegate: ItemViewDelegate) { + register(clazz.java, delegate) } internal fun register(type: Type) { types.register(type) - type.binder._adapter = this + type.delegate._adapter = this } /** - * Registers a type class to multiple item view binders. If you have registered the - * class, it will override the original binder(s). Note that the method is non-thread-safe + * Registers a type class to multiple item view delegates. If you have registered the + * class, it will override the original delegate(s). Note that the method is non-thread-safe * so that you should not use it in concurrent operation. * * Note that the method should not be called after @@ -84,7 +83,7 @@ open class MultiTypeAdapter @JvmOverloads constructor( * * @param clazz the class of a item * @param the item data type - * @return [OneToManyFlow] for setting the binders + * @return [OneToManyFlow] for setting the delegates * @see [register] */ @CheckResult @@ -100,7 +99,7 @@ open class MultiTypeAdapter @JvmOverloads constructor( /** * Registers all of the contents in the specified [Types]. If you have registered a - * class, it will override the original binder(s). Note that the method is non-thread-safe + * class, it will override the original delegate(s). Note that the method is non-thread-safe * so that you should not use it in concurrent operation. * * Note that the method should not be called after @@ -125,9 +124,7 @@ open class MultiTypeAdapter @JvmOverloads constructor( } override fun onCreateViewHolder(parent: ViewGroup, indexViewType: Int): ViewHolder { - val inflater = LayoutInflater.from(parent.context) - val binder = types.getType(indexViewType).binder - return binder.onCreateViewHolder(inflater, parent) + return types.getType(indexViewType).delegate.onCreateViewHolder(parent.context, parent) } override fun onBindViewHolder(holder: ViewHolder, position: Int) { @@ -136,41 +133,41 @@ open class MultiTypeAdapter @JvmOverloads constructor( override fun onBindViewHolder(holder: ViewHolder, position: Int, payloads: List) { val item = items[position] - getOutBinderByViewHolder(holder).onBindViewHolder(holder, item, payloads) + getOutDelegateByViewHolder(holder).onBindViewHolder(holder, item, payloads) } override fun getItemCount(): Int = items.size /** - * Called to return the stable ID for the item, and passes the event to its associated binder. + * Called to return the stable ID for the item, and passes the event to its associated delegate. * * @param position Adapter position to query * @return the stable ID of the item at position - * @see ItemViewBinder.getItemId + * @see ItemViewDelegate.getItemId * @see RecyclerView.Adapter.setHasStableIds * @since v3.2.0 */ override fun getItemId(position: Int): Long { val item = items[position] val itemViewType = getItemViewType(position) - return types.getType(itemViewType).binder.getItemId(item) + return types.getType(itemViewType).delegate.getItemId(item) } /** * Called when a view created by this adapter has been recycled, and passes the event to its - * associated binder. + * associated delegate. * * @param holder The ViewHolder for the view being recycled * @see RecyclerView.Adapter.onViewRecycled - * @see ItemViewBinder.onViewRecycled + * @see ItemViewDelegate.onViewRecycled */ override fun onViewRecycled(holder: ViewHolder) { - getOutBinderByViewHolder(holder).onViewRecycled(holder) + getOutDelegateByViewHolder(holder).onViewRecycled(holder) } /** * Called by the RecyclerView if a ViewHolder created by this Adapter cannot be recycled - * due to its transient state, and passes the event to its associated item view binder. + * due to its transient state, and passes the event to its associated item view delegate. * * @param holder The ViewHolder containing the View that could not be recycled due to its * transient state. @@ -180,49 +177,49 @@ open class MultiTypeAdapter @JvmOverloads constructor( * RecyclerView will check the View's transient state again before giving a final decision. * Default implementation returns false. * @see RecyclerView.Adapter.onFailedToRecycleView - * @see ItemViewBinder.onFailedToRecycleView + * @see ItemViewDelegate.onFailedToRecycleView */ override fun onFailedToRecycleView(holder: ViewHolder): Boolean { - return getOutBinderByViewHolder(holder).onFailedToRecycleView(holder) + return getOutDelegateByViewHolder(holder).onFailedToRecycleView(holder) } /** * Called when a view created by this adapter has been attached to a window, and passes the - * event to its associated item view binder. + * event to its associated item view delegate. * * @param holder Holder of the view being attached * @see RecyclerView.Adapter.onViewAttachedToWindow - * @see ItemViewBinder.onViewAttachedToWindow + * @see ItemViewDelegate.onViewAttachedToWindow */ override fun onViewAttachedToWindow(holder: ViewHolder) { - getOutBinderByViewHolder(holder).onViewAttachedToWindow(holder) + getOutDelegateByViewHolder(holder).onViewAttachedToWindow(holder) } /** * Called when a view created by this adapter has been detached from its window, and passes - * the event to its associated item view binder. + * the event to its associated item view delegate. * * @param holder Holder of the view being detached * @see RecyclerView.Adapter.onViewDetachedFromWindow - * @see ItemViewBinder.onViewDetachedFromWindow + * @see ItemViewDelegate.onViewDetachedFromWindow */ override fun onViewDetachedFromWindow(holder: ViewHolder) { - getOutBinderByViewHolder(holder).onViewDetachedFromWindow(holder) + getOutDelegateByViewHolder(holder).onViewDetachedFromWindow(holder) } - private fun getOutBinderByViewHolder(holder: ViewHolder): ItemViewBinder { + private fun getOutDelegateByViewHolder(holder: ViewHolder): ItemViewDelegate { @Suppress("UNCHECKED_CAST") - return types.getType(holder.itemViewType).binder as ItemViewBinder + return types.getType(holder.itemViewType).delegate as ItemViewDelegate } - @Throws(BinderNotFoundException::class) + @Throws(DelegateNotFoundException::class) internal fun indexInTypesOf(position: Int, item: Any): Int { val index = types.firstIndexOf(item.javaClass) if (index != -1) { val linker = types.getType(index).linker return index + linker.index(position, item) } - throw BinderNotFoundException(item.javaClass) + throw DelegateNotFoundException(item.javaClass) } private fun unregisterAllTypesIfNeeded(clazz: Class<*>) { diff --git a/library/src/main/kotlin/com/drakeet/multitype/OneToManyBuilder.kt b/library/src/main/kotlin/com/drakeet/multitype/OneToManyBuilder.kt index 0b65f9fc..79ed3d6b 100644 --- a/library/src/main/kotlin/com/drakeet/multitype/OneToManyBuilder.kt +++ b/library/src/main/kotlin/com/drakeet/multitype/OneToManyBuilder.kt @@ -26,13 +26,13 @@ internal class OneToManyBuilder( private val clazz: Class ) : OneToManyFlow, OneToManyEndpoint { - private var binders: Array>? = null + private var delegates: Array>? = null @SafeVarargs @CheckResult(suggest = "#withLinker(Linker)") - override fun to(vararg binders: ItemViewBinder) = apply { + override fun to(vararg delegates: ItemViewDelegate) = apply { @Suppress("UNCHECKED_CAST") - this.binders = binders as Array> + this.delegates = delegates as Array> } override fun withLinker(linker: Linker) { @@ -40,12 +40,12 @@ internal class OneToManyBuilder( } override fun withJavaClassLinker(javaClassLinker: JavaClassLinker) { - withLinker(ClassLinkerBridge.toLinker(javaClassLinker, binders!!)) + withLinker(ClassLinkerBridge.toLinker(javaClassLinker, delegates!!)) } private fun doRegister(linker: Linker) { - for (binder in binders!!) { - adapter.register(Type(clazz, binder, linker)) + for (delegate in delegates!!) { + adapter.register(Type(clazz, delegate, linker)) } } } diff --git a/library/src/main/kotlin/com/drakeet/multitype/OneToManyEndpoint.kt b/library/src/main/kotlin/com/drakeet/multitype/OneToManyEndpoint.kt index 4b497922..ad6f280f 100644 --- a/library/src/main/kotlin/com/drakeet/multitype/OneToManyEndpoint.kt +++ b/library/src/main/kotlin/com/drakeet/multitype/OneToManyEndpoint.kt @@ -26,7 +26,7 @@ import kotlin.reflect.KClass interface OneToManyEndpoint { /** - * Sets a linker to link the items and binders by array index. + * Sets a linker to link the items and delegates by array index. * * @param linker the row linker * @see Linker @@ -42,16 +42,16 @@ interface OneToManyEndpoint { } /** - * Sets a class linker to link the items and binders by the class instance of binders. + * Sets a class linker to link the items and delegates by the class instance of delegates. * * @param javaClassLinker the class linker * @see JavaClassLinker */ fun withJavaClassLinker(javaClassLinker: JavaClassLinker) - private fun withJavaClassLinker(classLinker: (position: Int, item: T) -> Class>) { + private fun withJavaClassLinker(classLinker: (position: Int, item: T) -> Class>) { withJavaClassLinker(object : JavaClassLinker { - override fun index(position: Int, item: T): Class> { + override fun index(position: Int, item: T): Class> { return classLinker(position, item) } }) @@ -61,7 +61,7 @@ interface OneToManyEndpoint { withJavaClassLinker { position, item -> classLinker.index(position, item).java } } - fun withKotlinClassLinker(classLinker: (position: Int, item: T) -> KClass>) { + fun withKotlinClassLinker(classLinker: (position: Int, item: T) -> KClass>) { withJavaClassLinker { position, item -> classLinker(position, item).java } } } diff --git a/library/src/main/kotlin/com/drakeet/multitype/OneToManyFlow.kt b/library/src/main/kotlin/com/drakeet/multitype/OneToManyFlow.kt index f3c46dca..69af0fad 100644 --- a/library/src/main/kotlin/com/drakeet/multitype/OneToManyFlow.kt +++ b/library/src/main/kotlin/com/drakeet/multitype/OneToManyFlow.kt @@ -26,11 +26,11 @@ import androidx.annotation.CheckResult interface OneToManyFlow { /** - * Sets some item view binders to the item type. + * Sets some item view delegates to the item type. * - * @param binders the item view binders + * @param delegates the item view delegates * @return end flow operator */ @CheckResult - fun to(vararg binders: ItemViewBinder): OneToManyEndpoint + fun to(vararg delegates: ItemViewDelegate): OneToManyEndpoint } diff --git a/library/src/main/kotlin/com/drakeet/multitype/Type.kt b/library/src/main/kotlin/com/drakeet/multitype/Type.kt index 4fd04b79..c49e8cea 100644 --- a/library/src/main/kotlin/com/drakeet/multitype/Type.kt +++ b/library/src/main/kotlin/com/drakeet/multitype/Type.kt @@ -21,6 +21,6 @@ package com.drakeet.multitype */ data class Type( val clazz: Class, - val binder: ItemViewBinder, + val delegate: ItemViewDelegate, val linker: Linker ) diff --git a/library/src/main/kotlin/com/drakeet/multitype/Types.kt b/library/src/main/kotlin/com/drakeet/multitype/Types.kt index d9cfe61d..bb9247cb 100644 --- a/library/src/main/kotlin/com/drakeet/multitype/Types.kt +++ b/library/src/main/kotlin/com/drakeet/multitype/Types.kt @@ -17,7 +17,7 @@ package com.drakeet.multitype /** - * An ordered collection to hold the types, binders and linkers. + * An ordered collection to hold the types, delegates and linkers. * * @author Drakeet Xu */ diff --git a/library/src/test/kotlin/com/drakeet/multitype/ItemViewBinderTest.kt b/library/src/test/kotlin/com/drakeet/multitype/ItemViewDelegateTest.kt similarity index 75% rename from library/src/test/kotlin/com/drakeet/multitype/ItemViewBinderTest.kt rename to library/src/test/kotlin/com/drakeet/multitype/ItemViewDelegateTest.kt index 2d7632b5..d5e93687 100644 --- a/library/src/test/kotlin/com/drakeet/multitype/ItemViewBinderTest.kt +++ b/library/src/test/kotlin/com/drakeet/multitype/ItemViewDelegateTest.kt @@ -22,7 +22,7 @@ import org.junit.Test /** * @author Drakeet Xu */ -class ItemViewBinderTest { +class ItemViewDelegateTest { @Test fun shouldGetNonNullAdapter() { @@ -31,12 +31,12 @@ class ItemViewBinderTest { val empty = arrayListOf() adapter.items = empty - val binder = TestItemViewBinder() - adapter.register(TestItem::class.java, binder) + val delegate = TestItemViewDelegate() + adapter.register(TestItem::class.java, delegate) - empty.add(TestItem("ItemViewBinderTest")) + empty.add(TestItem("ItemViewDelegateTest")) try { - binder.notifyTestItemAdded() + delegate.notifyTestItemAdded() } catch (e: Exception) { e.printStackTrace() exception = e @@ -51,15 +51,15 @@ class ItemViewBinderTest { val empty = ArrayList() adapter.items = empty - val binder = TestItemViewBinder() + val delegate = TestItemViewDelegate() - empty.add(TestItem("ItemViewBinderTest")) - binder.notifyTestItemAdded() + empty.add(TestItem("ItemViewDelegateTest")) + delegate.notifyTestItemAdded() - adapter.register(binder) + adapter.register(delegate) } - class TestItemViewBinder : com.drakeet.multitype.TestItemViewBinder() { + class TestItemViewDelegate : com.drakeet.multitype.TestItemViewDelegate() { fun notifyTestItemAdded() { assertThat(adapter).isNotNull() assertThat(adapter.toString()).isNotNull() diff --git a/library/src/test/kotlin/com/drakeet/multitype/MultiTypeAdapterTest.kt b/library/src/test/kotlin/com/drakeet/multitype/MultiTypeAdapterTest.kt index a23dbd74..f0b5c259 100644 --- a/library/src/test/kotlin/com/drakeet/multitype/MultiTypeAdapterTest.kt +++ b/library/src/test/kotlin/com/drakeet/multitype/MultiTypeAdapterTest.kt @@ -38,16 +38,14 @@ class MultiTypeAdapterTest { private val parent: ViewGroup = mock() private val context: Context = mock() - private val mockedItemViewBinder: TestItemViewBinder = mock() - private val inflater: LayoutInflater = mock() + private val mockedItemViewDelegate: TestItemViewDelegate = mock() - private val itemViewBinder = TestItemViewBinder() + private val itemViewDelegate = TestItemViewDelegate() @Before @Throws(Exception::class) fun setUp() { whenever(parent.context).thenReturn(context) - whenever(context.getSystemService(anyString())).thenReturn(inflater) } @Test @@ -64,57 +62,57 @@ class MultiTypeAdapterTest { } @Test - fun shouldOverrideRegisteredBinder() { + fun shouldOverrideRegisteredDelegate() { val adapter = MultiTypeAdapter() - adapter.register(TestItem::class, itemViewBinder) + adapter.register(TestItem::class, itemViewDelegate) assertThat(adapter.types.size).isEqualTo(1) - assertThat(itemViewBinder).isEqualTo(adapter.types.getType(0).binder) + assertThat(itemViewDelegate).isEqualTo(adapter.types.getType(0).delegate) - val newBinder = TestItemViewBinder() - adapter.register(TestItem::class, newBinder) - assertThat(newBinder).isEqualTo(adapter.types.getType(0).binder) + val newDelegate = TestItemViewDelegate() + adapter.register(TestItem::class, newDelegate) + assertThat(newDelegate).isEqualTo(adapter.types.getType(0).delegate) } @Test - fun shouldNotOverrideRegisteredBinderWhenToMany() { + fun shouldNotOverrideRegisteredDelegateWhenToMany() { val adapter = MultiTypeAdapter() - val binder2 = TestItemViewBinder() + val delegate2 = TestItemViewDelegate() adapter.register(TestItem::class) - .to(itemViewBinder, binder2) + .to(itemViewDelegate, delegate2) .withLinker { _, _ -> -1 } assertThat(adapter.types.getType(0).clazz).isEqualTo(TestItem::class.java) assertThat(adapter.types.getType(1).clazz).isEqualTo(TestItem::class.java) - assertThat(itemViewBinder).isEqualTo(adapter.types.getType(0).binder) - assertThat(binder2).isEqualTo(adapter.types.getType(1).binder) + assertThat(itemViewDelegate).isEqualTo(adapter.types.getType(0).delegate) + assertThat(delegate2).isEqualTo(adapter.types.getType(1).delegate) } @Test fun testOnCreateViewHolder() { val adapter = MultiTypeAdapter() - adapter.register(TestItem::class, mockedItemViewBinder) + adapter.register(TestItem::class, mockedItemViewDelegate) val item = TestItem("testOnCreateViewHolder") adapter.items = listOf(item) val type = adapter.getItemViewType(0) adapter.onCreateViewHolder(parent, type) - verify(mockedItemViewBinder).onCreateViewHolder(inflater, parent) + verify(mockedItemViewDelegate).onCreateViewHolder(context, parent) } @Test fun testOnBindViewHolder() { val adapter = MultiTypeAdapter() - adapter.register(TestItem::class, mockedItemViewBinder) + adapter.register(TestItem::class, mockedItemViewDelegate) val item = TestItem("testOnCreateViewHolder") adapter.items = listOf(item) - val holder: TestItemViewBinder.ViewHolder = mock() + val holder: TestItemViewDelegate.ViewHolder = mock() whenever(holder.itemViewType).thenReturn(adapter.getItemViewType(0)) adapter.onBindViewHolder(holder, 0) - verify(mockedItemViewBinder).onBindViewHolder(eq(holder), eq(item), anyList()) + verify(mockedItemViewDelegate).onBindViewHolder(eq(holder), eq(item), anyList()) val payloads = emptyList() adapter.onBindViewHolder(holder, 0, payloads) - verify(mockedItemViewBinder, times(2)).onBindViewHolder(holder, item, payloads) + verify(mockedItemViewDelegate, times(2)).onBindViewHolder(holder, item, payloads) } } diff --git a/library/src/test/kotlin/com/drakeet/multitype/MultiTypeTest.kt b/library/src/test/kotlin/com/drakeet/multitype/MultiTypeTest.kt index 072fe29f..35c5c13b 100644 --- a/library/src/test/kotlin/com/drakeet/multitype/MultiTypeTest.kt +++ b/library/src/test/kotlin/com/drakeet/multitype/MultiTypeTest.kt @@ -34,39 +34,39 @@ class MultiTypeTest { @Test fun shouldEqualToRegisteredKClass() { - adapter.register(String::class, StringViewBinder()) + adapter.register(String::class, StringViewDelegate()) assertThat(adapter.types.getType(0).clazz).isEqualTo(String::class.java) } @Test fun shouldEqualToRegisteredKClass_Reified() { - adapter.register(StringViewBinder()) + adapter.register(StringViewDelegate()) assertThat(adapter.types.getType(0).clazz).isEqualTo(String::class.java) } @Test fun shouldEqualToRegisteredOneToManyKClass() { adapter.register(String::class) - .to(StringViewBinder()) + .to(StringViewDelegate()) .withLinker(simpleLinker) assertThat(adapter.types.getType(0).clazz).isEqualTo(String::class.java) } @Test fun shouldEqualToRegisteredKClass_TypePool() { - adapter.types.register(Type(String::class.java, StringViewBinder(), simpleLinker)) + adapter.types.register(Type(String::class.java, StringViewDelegate(), simpleLinker)) assertThat(adapter.types.getType(0).clazz).isEqualTo(String::class.java) } @Test fun shouldUnregisterKClass_TypePool() { - adapter.types.register(Type(String::class.java, StringViewBinder(), simpleLinker)) + adapter.types.register(Type(String::class.java, StringViewDelegate(), simpleLinker)) assertThat(adapter.types.unregister(String::class.java)).isTrue() } @Test fun shouldEqualToRegisteredFirstKClass_TypePool() { - adapter.types.register(Type(String::class.java, StringViewBinder(), simpleLinker)) + adapter.types.register(Type(String::class.java, StringViewDelegate(), simpleLinker)) assertThat(adapter.types.firstIndexOf(String::class.java)).isEqualTo(0) } } diff --git a/library/src/test/kotlin/com/drakeet/multitype/MutableTypesTest.kt b/library/src/test/kotlin/com/drakeet/multitype/MutableTypesTest.kt index 46ed93cc..cff7b0aa 100644 --- a/library/src/test/kotlin/com/drakeet/multitype/MutableTypesTest.kt +++ b/library/src/test/kotlin/com/drakeet/multitype/MutableTypesTest.kt @@ -34,8 +34,8 @@ class MutableTypesTest { @Before fun register() { types = MutableTypes() - types.register(Type(TestItem::class.java, TestItemViewBinder(), DefaultLinker())) - types.register(Type(RegisteredSubClass::class.java, TestItemViewBinder(), DefaultLinker())) + types.register(Type(TestItem::class.java, TestItemViewDelegate(), DefaultLinker())) + types.register(Type(RegisteredSubClass::class.java, TestItemViewDelegate(), DefaultLinker())) } @Test diff --git a/library/src/test/kotlin/com/drakeet/multitype/OneToManyBuilderTest.kt b/library/src/test/kotlin/com/drakeet/multitype/OneToManyBuilderTest.kt index b5d899d8..854ba8ab 100644 --- a/library/src/test/kotlin/com/drakeet/multitype/OneToManyBuilderTest.kt +++ b/library/src/test/kotlin/com/drakeet/multitype/OneToManyBuilderTest.kt @@ -56,16 +56,16 @@ class OneToManyBuilderTest { }) } - private abstract class DataItemViewBinder : ItemViewBinder() + private abstract class DataItemViewDelegate : ItemViewDelegate() @Test fun testWithJavaClassLinker() { - val itemViewBinder1 = mock>() - val itemViewBinder2 = mock() - oneToManyBuilder.to(itemViewBinder1, itemViewBinder2) + val itemViewDelegate1 = mock>() + val itemViewDelegate2 = mock() + oneToManyBuilder.to(itemViewDelegate1, itemViewDelegate2) oneToManyBuilder.withJavaClassLinker(object : JavaClassLinker { - override fun index(position: Int, item: Data): Class> { - return if (position == 3) itemViewBinder1.javaClass else itemViewBinder2.javaClass + override fun index(position: Int, item: Data): Class> { + return if (position == 3) itemViewDelegate1.javaClass else itemViewDelegate2.javaClass } }) verify(adapter, times(2)).register(check> { @@ -75,7 +75,7 @@ class OneToManyBuilderTest { }) oneToManyBuilder.withKotlinClassLinker { position, item -> - if (position == 3) itemViewBinder1::class else itemViewBinder2::class + if (position == 3) itemViewDelegate1::class else itemViewDelegate2::class } verify(adapter, times(4)).register(check> { assertThat(it.clazz).isEqualTo(Data::class.java) @@ -83,8 +83,8 @@ class OneToManyBuilderTest { assertThat(it.linker.index(5, Data())).isEqualTo(1) }) oneToManyBuilder.withKotlinClassLinker(object : KotlinClassLinker { - override fun index(position: Int, item: Data): KClass> { - return if (position == 3) itemViewBinder1::class else itemViewBinder2::class + override fun index(position: Int, item: Data): KClass> { + return if (position == 3) itemViewDelegate1::class else itemViewDelegate2::class } }) verify(adapter, times(6)).register(check> { @@ -93,4 +93,4 @@ class OneToManyBuilderTest { assertThat(it.linker.index(5, Data())).isEqualTo(1) }) } -} \ No newline at end of file +} diff --git a/library/src/test/kotlin/com/drakeet/multitype/StringViewBinder.kt b/library/src/test/kotlin/com/drakeet/multitype/StringViewDelegate.kt similarity index 76% rename from library/src/test/kotlin/com/drakeet/multitype/StringViewBinder.kt rename to library/src/test/kotlin/com/drakeet/multitype/StringViewDelegate.kt index 8a2783ac..cd807d4b 100644 --- a/library/src/test/kotlin/com/drakeet/multitype/StringViewBinder.kt +++ b/library/src/test/kotlin/com/drakeet/multitype/StringViewDelegate.kt @@ -17,6 +17,7 @@ package com.drakeet.multitype import android.R +import android.content.Context import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -25,10 +26,10 @@ import androidx.recyclerview.widget.RecyclerView /** * @author Drakeet Xu */ -class StringViewBinder : ItemViewBinder() { +class StringViewDelegate : ItemViewDelegate() { - override fun onCreateViewHolder(inflater: LayoutInflater, parent: ViewGroup): ViewHolder { - return ViewHolder(inflater.inflate(R.layout.test_list_item, parent, false)) + override fun onCreateViewHolder(context: Context, parent: ViewGroup): ViewHolder { + return ViewHolder(LayoutInflater.from(context).inflate(R.layout.test_list_item, parent, false)) } override fun onBindViewHolder(holder: ViewHolder, item: String) {} diff --git a/library/src/test/kotlin/com/drakeet/multitype/TestItemViewBinder.kt b/library/src/test/kotlin/com/drakeet/multitype/TestItemViewDelegate.kt similarity index 81% rename from library/src/test/kotlin/com/drakeet/multitype/TestItemViewBinder.kt rename to library/src/test/kotlin/com/drakeet/multitype/TestItemViewDelegate.kt index 89969f36..fef57e2f 100644 --- a/library/src/test/kotlin/com/drakeet/multitype/TestItemViewBinder.kt +++ b/library/src/test/kotlin/com/drakeet/multitype/TestItemViewDelegate.kt @@ -16,7 +16,7 @@ package com.drakeet.multitype -import android.view.LayoutInflater +import android.content.Context import android.view.View import android.view.ViewGroup import androidx.recyclerview.widget.RecyclerView @@ -24,9 +24,9 @@ import androidx.recyclerview.widget.RecyclerView /** * @author Drakeet Xu */ -open class TestItemViewBinder : ItemViewBinder() { +open class TestItemViewDelegate : ItemViewDelegate() { - override fun onCreateViewHolder(inflater: LayoutInflater, parent: ViewGroup): ViewHolder { + override fun onCreateViewHolder(context: Context, parent: ViewGroup): ViewHolder { throw NotImplementedError() } diff --git a/sample/src/androidTest/kotlin/com/drakeet/multitype/sample/one2many/DuplicateTypesTest.kt b/sample/src/androidTest/kotlin/com/drakeet/multitype/sample/one2many/DuplicateTypesTest.kt index 68f9d1b5..902b0a23 100644 --- a/sample/src/androidTest/kotlin/com/drakeet/multitype/sample/one2many/DuplicateTypesTest.kt +++ b/sample/src/androidTest/kotlin/com/drakeet/multitype/sample/one2many/DuplicateTypesTest.kt @@ -59,7 +59,7 @@ class DuplicateTypesTest { } assertThat(adapter.types.size).isEqualTo(1) assertThat(adapter.types.getType(0).clazz).isEqualTo(Data::class.java) - assertThat(adapter.types.getType(0).binder.javaClass).isEqualTo(DataType1ViewBinder::class.java) + assertThat(adapter.types.getType(0).delegate.javaClass).isEqualTo(DataType1ViewBinder::class.java) } @Test @@ -73,7 +73,7 @@ class DuplicateTypesTest { } assertThat(adapter.types.size).isEqualTo(1) assertThat(adapter.types.getType(0).clazz).isEqualTo(Data::class.java) - assertThat(adapter.types.getType(0).binder.javaClass).isEqualTo(DataType2ViewBinder::class.java) + assertThat(adapter.types.getType(0).delegate.javaClass).isEqualTo(DataType2ViewBinder::class.java) } @Test @@ -101,11 +101,11 @@ class DuplicateTypesTest { assertEquals( DataType2ViewBinder::class.java, - adapter.types.getType(0).binder::class.java + adapter.types.getType(0).delegate::class.java ) assertEquals( DataType1ViewBinder::class.java, - adapter.types.getType(1).binder::class.java + adapter.types.getType(1).delegate::class.java ) assertSame(linker, adapter.types.getType(0).linker) @@ -140,11 +140,11 @@ class DuplicateTypesTest { assertEquals( DataType1ViewBinder::class.java, - adapter.types.getType(0).binder::class.java + adapter.types.getType(0).delegate::class.java ) assertEquals( DataType2ViewBinder::class.java, - adapter.types.getType(1).binder::class.java + adapter.types.getType(1).delegate::class.java ) assertSame(linker, adapter.types.getType(0).linker)