diff options
Diffstat (limited to 'fastadapter')
7 files changed, 53 insertions, 88 deletions
diff --git a/fastadapter/build.gradle b/fastadapter/build.gradle index 9fd848e..ff58fe8 100644 --- a/fastadapter/build.gradle +++ b/fastadapter/build.gradle @@ -1,4 +1,7 @@ -ext.kauSubModuleMinSdk = kau.Versions.coreMinSdk +import kau.Dependencies +import kau.Versions + +ext.kauSubModuleMinSdk = Versions.coreMinSdk apply from: '../android-lib.gradle' @@ -6,8 +9,8 @@ dependencies { implementation project(':core') api project(':adapter') - api kau.Dependencies.fastAdapter - api kau.Dependencies.fastAdapterCommons + api Dependencies.fastAdapter + api Dependencies.fastAdapter("utils") } apply from: '../artifacts.gradle' diff --git a/fastadapter/src/main/kotlin/ca/allanwang/kau/adapters/AdapterUtils.kt b/fastadapter/src/main/kotlin/ca/allanwang/kau/adapters/AdapterUtils.kt index 17fd09f..655b355 100644 --- a/fastadapter/src/main/kotlin/ca/allanwang/kau/adapters/AdapterUtils.kt +++ b/fastadapter/src/main/kotlin/ca/allanwang/kau/adapters/AdapterUtils.kt @@ -16,9 +16,8 @@ package ca.allanwang.kau.adapters import com.mikepenz.fastadapter.FastAdapter +import com.mikepenz.fastadapter.GenericItem import com.mikepenz.fastadapter.IAdapter -import com.mikepenz.fastadapter.IAdapterExtension -import com.mikepenz.fastadapter.IItem import com.mikepenz.fastadapter.select.SelectExtension /** @@ -28,17 +27,14 @@ import com.mikepenz.fastadapter.select.SelectExtension /** * Add kotlin's generic syntax to better support out types */ -fun <Item : IItem<*, *>> fastAdapter(vararg adapter: IAdapter<out Item>) = - FastAdapter.with<Item, IAdapter<out Item>>(adapter.toList())!! - -inline fun <reified T : IAdapterExtension<Item>, Item : IItem<*, *>> FastAdapter<Item>.getExtension(): T? = - getExtension(T::class.java) +fun <Item : GenericItem> fastAdapter(vararg adapter: IAdapter<out Item>) = + FastAdapter.with<Item, IAdapter<out Item>>(adapter.toList()) /** * Returns selection size, or -1 if selection is disabled */ -inline val <Item : IItem<*, *>> IAdapter<Item>.selectionSize: Int - get() = fastAdapter.getExtension<SelectExtension<Item>, Item>()?.selections?.size ?: -1 +inline val <Item : GenericItem> IAdapter<Item>.selectionSize: Int + get() = fastAdapter?.getExtension<SelectExtension<Item>>()?.selections?.size ?: -1 -inline val <Item : IItem<*, *>> IAdapter<Item>.selectedItems: Set<Item> - get() = fastAdapter.getExtension<SelectExtension<Item>, Item>()?.selectedItems ?: emptySet() +inline val <Item : GenericItem> IAdapter<Item>.selectedItems: Set<Item> + get() = fastAdapter?.getExtension<SelectExtension<Item>>()?.selectedItems ?: emptySet() diff --git a/fastadapter/src/main/kotlin/ca/allanwang/kau/adapters/FastItemThemedAdapter.kt b/fastadapter/src/main/kotlin/ca/allanwang/kau/adapters/FastItemThemedAdapter.kt index 152982f..5531f59 100644 --- a/fastadapter/src/main/kotlin/ca/allanwang/kau/adapters/FastItemThemedAdapter.kt +++ b/fastadapter/src/main/kotlin/ca/allanwang/kau/adapters/FastItemThemedAdapter.kt @@ -23,8 +23,8 @@ import android.widget.TextView import androidx.annotation.RequiresApi import ca.allanwang.kau.ui.createSimpleRippleDrawable import ca.allanwang.kau.utils.adjustAlpha -import com.mikepenz.fastadapter.IItem -import com.mikepenz.fastadapter.commons.adapters.FastItemAdapter +import com.mikepenz.fastadapter.GenericItem +import com.mikepenz.fastadapter.adapters.FastItemAdapter /** * Created by Allan Wang on 2017-06-29. @@ -35,12 +35,23 @@ import com.mikepenz.fastadapter.commons.adapters.FastItemAdapter * This adapter overrides every method where an item is added * If that item extends [ThemableIItem], then the colors will be set */ -class FastItemThemedAdapter<Item : IItem<*, *>>( +class FastItemThemedAdapter<Item : GenericItem>( textColor: Int? = null, backgroundColor: Int? = null, accentColor: Int? = null ) : FastItemAdapter<Item>() { - constructor(colors: ThemableIItemColors) : this(colors.textColor, colors.backgroundColor, colors.accentColor) + constructor(colors: ThemableIItemColors) : this( + colors.textColor, + colors.backgroundColor, + colors.accentColor + ) + + init { + itemAdapter.interceptor = { + injectTheme(it) + it + } + } var textColor: Int? = textColor set(value) { @@ -73,51 +84,11 @@ class FastItemThemedAdapter<Item : IItem<*, *>>( notifyAdapterDataSetChanged() } - override fun add(position: Int, items: List<Item>): FastItemAdapter<Item> { - injectTheme(items) - return super.add(position, items) - } - - override fun add(position: Int, item: Item): FastItemAdapter<Item> { - injectTheme(item) - return super.add(position, item) - } - - override fun add(item: Item): FastItemAdapter<Item> { - injectTheme(item) - return super.add(item) - } - - override fun add(items: List<Item>?): FastItemAdapter<Item> { - injectTheme(items) - return super.add(items) - } - - override fun set(items: List<Item>?): FastItemAdapter<Item> { - injectTheme(items) - return super.set(items) - } - - override fun set(position: Int, item: Item): FastItemAdapter<Item> { - injectTheme(item) - return super.set(position, item) - } - - override fun setNewList(items: List<Item>?, retainFilter: Boolean): FastItemAdapter<Item> { - injectTheme(items) - return super.setNewList(items, retainFilter) - } - - override fun setNewList(items: List<Item>?): FastItemAdapter<Item> { - injectTheme(items) - return super.setNewList(items) - } - - private fun injectTheme(items: Collection<IItem<*, *>?>?) { + private fun injectTheme(items: Collection<GenericItem?>?) { items?.forEach { injectTheme(it) } } - protected fun injectTheme(item: IItem<*, *>?) { + protected fun injectTheme(item: GenericItem?) { if (item is ThemableIItem && item.themeEnabled) { item.textColor = textColor item.backgroundColor = backgroundColor diff --git a/fastadapter/src/main/kotlin/ca/allanwang/kau/adapters/RepeatedClickListener.kt b/fastadapter/src/main/kotlin/ca/allanwang/kau/adapters/RepeatedClickListener.kt index 40b4774..1eba2e0 100644 --- a/fastadapter/src/main/kotlin/ca/allanwang/kau/adapters/RepeatedClickListener.kt +++ b/fastadapter/src/main/kotlin/ca/allanwang/kau/adapters/RepeatedClickListener.kt @@ -17,31 +17,33 @@ package ca.allanwang.kau.adapters import android.view.View import androidx.annotation.IntRange +import com.mikepenz.fastadapter.ClickListener import com.mikepenz.fastadapter.FastAdapter +import com.mikepenz.fastadapter.GenericItem import com.mikepenz.fastadapter.IAdapter -import com.mikepenz.fastadapter.IItem -import com.mikepenz.fastadapter.listeners.OnClickListener /** * Created by Allan Wang on 26/12/17. */ -fun <Item : IItem<*, *>> FastAdapter<Item>.withOnRepeatedClickListener( +fun <Item : GenericItem> FastAdapter<Item>.withOnRepeatedClickListener( count: Int, duration: Long, - event: OnClickListener<Item> -) = - withOnClickListener(RepeatedClickListener(count, duration, event)) + event: ClickListener<Item> +): FastAdapter<Item> { + onClickListener = RepeatedClickListener(count, duration, event) + return this +} /** * Registers and skips each click until the designated [count] clicks are triggered, * each within [duration] from each other. * Only then will the [event] be fired, and everything will be reset. */ -private class RepeatedClickListener<Item : IItem<*, *>>( +private class RepeatedClickListener<Item : GenericItem>( @IntRange(from = 1) val count: Int, @IntRange(from = 1) val duration: Long, - val event: OnClickListener<Item> -) : OnClickListener<Item> { + val event: ClickListener<Item> +) : ClickListener<Item> { init { if (count <= 0) @@ -53,7 +55,7 @@ private class RepeatedClickListener<Item : IItem<*, *>>( private var chain = 0 private var time = -1L - override fun onClick(v: View?, adapter: IAdapter<Item>, item: Item, position: Int): Boolean { + override fun invoke(v: View?, adapter: IAdapter<Item>, item: Item, position: Int): Boolean { val now = System.currentTimeMillis() if (time - now < duration) chain++ @@ -62,7 +64,7 @@ private class RepeatedClickListener<Item : IItem<*, *>>( time = now if (chain == count) { chain = 0 - event.onClick(v, adapter, item, position) + event(v, adapter, item, position) return true } return false diff --git a/fastadapter/src/main/kotlin/ca/allanwang/kau/iitems/CardIItem.kt b/fastadapter/src/main/kotlin/ca/allanwang/kau/iitems/CardIItem.kt index 6e33833..1a2e6cb 100644 --- a/fastadapter/src/main/kotlin/ca/allanwang/kau/iitems/CardIItem.kt +++ b/fastadapter/src/main/kotlin/ca/allanwang/kau/iitems/CardIItem.kt @@ -34,7 +34,7 @@ import ca.allanwang.kau.utils.string import ca.allanwang.kau.utils.toDrawable import ca.allanwang.kau.utils.visible import com.mikepenz.fastadapter.FastAdapter -import com.mikepenz.fastadapter.IItem +import com.mikepenz.fastadapter.GenericItem import com.mikepenz.fastadapter.listeners.ClickEventHook import com.mikepenz.iconics.typeface.IIcon @@ -46,18 +46,18 @@ import com.mikepenz.iconics.typeface.IIcon */ class CardIItem( val builder: Config.() -> Unit = {} -) : KauIItem<CardIItem, CardIItem.ViewHolder>( +) : KauIItem< CardIItem.ViewHolder>( R.layout.kau_iitem_card, ::ViewHolder, R.id.kau_item_card ), ThemableIItem by ThemableIItemDelegate() { companion object { - fun bindClickEvents(fastAdapter: FastAdapter<IItem<*, *>>) { - fastAdapter.withEventHook(object : ClickEventHook<IItem<*, *>>() { + fun bindClickEvents(fastAdapter: FastAdapter<GenericItem>) { + fastAdapter.addEventHook(object : ClickEventHook<GenericItem>() { override fun onBindMany(viewHolder: RecyclerView.ViewHolder): List<View>? { return if (viewHolder is ViewHolder) listOf(viewHolder.card, viewHolder.button) else null } - override fun onClick(v: View, position: Int, adapter: FastAdapter<IItem<*, *>>, item: IItem<*, *>) { + override fun onClick(v: View, position: Int, fastAdapter: FastAdapter<GenericItem>, item: GenericItem) { if (item !is CardIItem) return with(item.configs) { when (v.id) { diff --git a/fastadapter/src/main/kotlin/ca/allanwang/kau/iitems/HeaderIItem.kt b/fastadapter/src/main/kotlin/ca/allanwang/kau/iitems/HeaderIItem.kt index 2c488b1..551cb59 100644 --- a/fastadapter/src/main/kotlin/ca/allanwang/kau/iitems/HeaderIItem.kt +++ b/fastadapter/src/main/kotlin/ca/allanwang/kau/iitems/HeaderIItem.kt @@ -34,7 +34,7 @@ import ca.allanwang.kau.utils.string class HeaderIItem( text: String? = null, var textRes: Int = INVALID_ID -) : KauIItem<HeaderIItem, HeaderIItem.ViewHolder>( +) : KauIItem<HeaderIItem.ViewHolder>( R.layout.kau_iitem_header, { ViewHolder(it) }, R.id.kau_item_header_big_margin_top ), ThemableIItem by ThemableIItemDelegate() { diff --git a/fastadapter/src/main/kotlin/ca/allanwang/kau/iitems/KauIItem.kt b/fastadapter/src/main/kotlin/ca/allanwang/kau/iitems/KauIItem.kt index c66dc01..34a2b7d 100644 --- a/fastadapter/src/main/kotlin/ca/allanwang/kau/iitems/KauIItem.kt +++ b/fastadapter/src/main/kotlin/ca/allanwang/kau/iitems/KauIItem.kt @@ -15,12 +15,9 @@ */ package ca.allanwang.kau.iitems -import android.annotation.SuppressLint import android.view.View import androidx.annotation.LayoutRes import androidx.recyclerview.widget.RecyclerView -import com.mikepenz.fastadapter.IClickable -import com.mikepenz.fastadapter.IItem import com.mikepenz.fastadapter.items.AbstractItem /** @@ -29,14 +26,10 @@ import com.mikepenz.fastadapter.items.AbstractItem * Kotlin implementation of the [AbstractItem] to make things shorter * If only one iitem type extends the given [layoutRes], you may use it as the type and not worry about another id */ -open class KauIItem<Item, VH : RecyclerView.ViewHolder>( - @param:LayoutRes private val layoutRes: Int, +open class KauIItem<VH : RecyclerView.ViewHolder>( + @param:LayoutRes override val layoutRes: Int, private val viewHolder: (v: View) -> VH, - private val type: Int = layoutRes -) : AbstractItem<Item, VH>() where Item : IItem<*, *>, Item : IClickable<*> { - @SuppressLint("ResourceType") - final override fun getType(): Int = type - + override val type: Int = layoutRes +) : AbstractItem<VH>() { final override fun getViewHolder(v: View): VH = viewHolder(v) - final override fun getLayoutRes(): Int = layoutRes } |