From 637851b6ddc4a22583797a45bdbb72eb9c6dac23 Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Thu, 9 Nov 2017 01:54:02 -0500 Subject: misc (#97) * Update translators * Increase debounce test interval * Clean unnecessary adapter files * Update fastadapter * Add fastadapter helper method * Remove external method * Add better wrap * Add more helpers --- .../ca/allanwang/kau/adapters/AdapterUtils.kt | 22 ++++++ .../ca/allanwang/kau/adapters/ChainedAdapters.kt | 85 ---------------------- .../kau/adapters/FastItemThemedAdapter.kt | 11 +-- .../ca/allanwang/kau/adapters/SectionAdapter.kt | 13 ---- 4 files changed, 24 insertions(+), 107 deletions(-) create mode 100644 adapter/src/main/kotlin/ca/allanwang/kau/adapters/AdapterUtils.kt delete mode 100644 adapter/src/main/kotlin/ca/allanwang/kau/adapters/ChainedAdapters.kt delete mode 100644 adapter/src/main/kotlin/ca/allanwang/kau/adapters/SectionAdapter.kt (limited to 'adapter/src') diff --git a/adapter/src/main/kotlin/ca/allanwang/kau/adapters/AdapterUtils.kt b/adapter/src/main/kotlin/ca/allanwang/kau/adapters/AdapterUtils.kt new file mode 100644 index 0000000..206c66b --- /dev/null +++ b/adapter/src/main/kotlin/ca/allanwang/kau/adapters/AdapterUtils.kt @@ -0,0 +1,22 @@ +package ca.allanwang.kau.adapters + +import com.mikepenz.fastadapter.FastAdapter +import com.mikepenz.fastadapter.IAdapter +import com.mikepenz.fastadapter.IItem +import com.mikepenz.fastadapter.commons.adapters.FastItemAdapter + +/** + * Created by Allan Wang on 2017-11-08. + */ + +/** + * Add kotlin's generic syntax to better support out types + */ +fun > fastAdapter(vararg adapter: IAdapter) = + FastAdapter.with>(adapter.toList())!! + +/** + * Helper to get an [IAdapter] directly from a [FastItemAdapter] + */ +fun > fastAdapter(adapter: IAdapter, fastAdapter: FastItemAdapter) = + fastAdapter(adapter, fastAdapter.itemAdapter) \ No newline at end of file diff --git a/adapter/src/main/kotlin/ca/allanwang/kau/adapters/ChainedAdapters.kt b/adapter/src/main/kotlin/ca/allanwang/kau/adapters/ChainedAdapters.kt deleted file mode 100644 index 2da0cac..0000000 --- a/adapter/src/main/kotlin/ca/allanwang/kau/adapters/ChainedAdapters.kt +++ /dev/null @@ -1,85 +0,0 @@ -package ca.allanwang.kau.adapters - -import android.support.v7.widget.LinearLayoutManager -import android.support.v7.widget.RecyclerView -import ca.allanwang.kau.utils.withLinearAdapter -import com.mikepenz.fastadapter.IItem -import com.mikepenz.fastadapter.adapters.HeaderAdapter -import com.mikepenz.fastadapter.commons.adapters.FastItemAdapter -import org.jetbrains.anko.collections.forEachReversedWithIndex -import java.util.* - -/** - * Created by Allan Wang on 2017-06-27. - * - * Once bounded to a [RecyclerView], this will - * - Chain together a list of [HeaderAdapter]s, backed by a generic [FastItemAdapter] - * - Add a [LinearLayoutManager] to the recycler - * - Add a listener for when a new adapter segment is being used - */ -class ChainedAdapters(vararg items: Pair>) { - private val chain: MutableList>> = mutableListOf(*items) - val baseAdapter: FastItemAdapter> = FastItemAdapter() - private val indexStack = Stack() - var recycler: RecyclerView? = null - val firstVisibleItemPosition: Int - get() = (recycler?.layoutManager as LinearLayoutManager?)?.findFirstVisibleItemPosition() ?: throw IllegalArgumentException("No recyclerview was bounded to the chain adapters") - - fun add(vararg items: Pair>) = add(items.toList()) - - fun add(items: Collection>>): ChainedAdapters { - if (recycler != null) throw IllegalAccessException("Chain adapter is already bounded to a recycler; cannot add directly.") - items.map { it.second }.forEachIndexed { index, sectionAdapter -> sectionAdapter.sectionOrder = chain.size + 1 + index } - chain.addAll(items) - return this - } - - operator fun get(index: Int) = chain[index] - - /** - * Attaches the chain to a recycler - * After this stage, any modifications to the adapters must be done through external references - * You may still get the generic header adapters through the get operator - * Binding the recycler also involves supplying a callback, which returns - * the item (T) associated with the adapter, - * the index (Int) of the current adapter - * and the dy (Int) as given by the scroll listener - */ - fun bindRecyclerView(recyclerView: RecyclerView, onAdapterSectionChanged: (item: T, index: Int, dy: Int) -> Unit) { - if (recycler != null) throw IllegalStateException("Chain adapter is already bounded") - if (chain.isEmpty()) throw IllegalArgumentException("No adapters have been added to the adapters list") - //wrap adapters - chain.map { it.second }.forEachReversedWithIndex { i, headerAdapter -> - if (i == chain.size - 1) headerAdapter.wrap(baseAdapter) - else headerAdapter.wrap(chain[i + 1].second) - } - recycler = recyclerView - indexStack.push(0) - with(recyclerView) { - withLinearAdapter(chain.first().second) - addOnScrollListener(object : RecyclerView.OnScrollListener() { - override fun onScrolled(rv: RecyclerView, dx: Int, dy: Int) { - super.onScrolled(rv, dx, dy) - val topPosition = firstVisibleItemPosition - val currentAdapterIndex = indexStack.peek() - if (dy > 0) { - //look ahead from current adapter - val nextAdapterIndex = (currentAdapterIndex until chain.size).asSequence() - .firstOrNull { - val adapter = chain[it].second - adapter.adapterItemCount > 0 && adapter.getGlobalPosition(adapter.adapterItemCount - 1) >= topPosition - } ?: currentAdapterIndex - if (nextAdapterIndex == currentAdapterIndex) return - indexStack.push(nextAdapterIndex) - onAdapterSectionChanged(chain[indexStack.peek()].first, indexStack.peek(), dy) - } else if (currentAdapterIndex == 0) { - return //All adapters may be empty; in this case, if we are already at the beginning, don't bother checking - } else if (chain[currentAdapterIndex].second.getGlobalPosition(0) > topPosition) { - indexStack.pop() - onAdapterSectionChanged(chain[indexStack.peek()].first, indexStack.peek(), dy) - } - } - }) - } - } -} \ No newline at end of file diff --git a/adapter/src/main/kotlin/ca/allanwang/kau/adapters/FastItemThemedAdapter.kt b/adapter/src/main/kotlin/ca/allanwang/kau/adapters/FastItemThemedAdapter.kt index b1c281a..c3a1c61 100644 --- a/adapter/src/main/kotlin/ca/allanwang/kau/adapters/FastItemThemedAdapter.kt +++ b/adapter/src/main/kotlin/ca/allanwang/kau/adapters/FastItemThemedAdapter.kt @@ -8,9 +8,7 @@ import android.widget.ImageView import android.widget.TextView import ca.allanwang.kau.ui.createSimpleRippleDrawable import ca.allanwang.kau.utils.adjustAlpha -import com.mikepenz.fastadapter.IExpandable import com.mikepenz.fastadapter.IItem -import com.mikepenz.fastadapter.ISubItem import com.mikepenz.fastadapter.commons.adapters.FastItemAdapter /** @@ -100,16 +98,11 @@ class FastItemThemedAdapter>( return super.setNewList(items) } - override fun setSubItems(collapsible: T, subItems: List?): T where S : IItem<*, *>?, T : IItem<*, *>?, T : IExpandable?, S : ISubItem? { - injectTheme(subItems) - return super.setSubItems(collapsible, subItems) - } - - internal fun injectTheme(items: Collection?>?) { + private fun injectTheme(items: Collection?>?) { items?.forEach { injectTheme(it) } } - internal fun injectTheme(item: IItem<*, *>?) { + protected fun injectTheme(item: IItem<*, *>?) { if (item is ThemableIItem && item.themeEnabled) { item.textColor = textColor item.backgroundColor = backgroundColor diff --git a/adapter/src/main/kotlin/ca/allanwang/kau/adapters/SectionAdapter.kt b/adapter/src/main/kotlin/ca/allanwang/kau/adapters/SectionAdapter.kt deleted file mode 100644 index cf7205a..0000000 --- a/adapter/src/main/kotlin/ca/allanwang/kau/adapters/SectionAdapter.kt +++ /dev/null @@ -1,13 +0,0 @@ -package ca.allanwang.kau.adapters - -import com.mikepenz.fastadapter.IItem -import com.mikepenz.fastadapter.adapters.HeaderAdapter - -/** - * Created by Allan Wang on 2017-06-27. - * - * Extension of [HeaderAdapter] where we can define the order - */ -class SectionAdapter>(var sectionOrder: Int = 100) : HeaderAdapter() { - override fun getOrder(): Int = sectionOrder -} \ No newline at end of file -- cgit v1.2.3