diff options
author | Allan Wang <me@allanwang.ca> | 2019-07-01 12:03:18 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-07-01 12:03:18 -0700 |
commit | 9b2f9443e5472f3ef47d6d5085524ed3255a3102 (patch) | |
tree | b1cbf2552e5ecadd1dd2c303fab17afee06c3f16 /adapter/src/main/kotlin | |
parent | 8d927f31b30ac5418e2091089f5d093f57006d9f (diff) | |
parent | 24c59b7681820c73197c2d2f0bf9814f3fb37d2f (diff) | |
download | kau-9b2f9443e5472f3ef47d6d5085524ed3255a3102.tar.gz kau-9b2f9443e5472f3ef47d6d5085524ed3255a3102.tar.bz2 kau-9b2f9443e5472f3ef47d6d5085524ed3255a3102.zip |
Merge pull request #209 from AllanWang/fastadapter-split
Fastadapter split
Diffstat (limited to 'adapter/src/main/kotlin')
6 files changed, 0 insertions, 556 deletions
diff --git a/adapter/src/main/kotlin/ca/allanwang/kau/adapters/AdapterUtils.kt b/adapter/src/main/kotlin/ca/allanwang/kau/adapters/AdapterUtils.kt deleted file mode 100644 index 17fd09f..0000000 --- a/adapter/src/main/kotlin/ca/allanwang/kau/adapters/AdapterUtils.kt +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright 2018 Allan Wang - * - * 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 ca.allanwang.kau.adapters - -import com.mikepenz.fastadapter.FastAdapter -import com.mikepenz.fastadapter.IAdapter -import com.mikepenz.fastadapter.IAdapterExtension -import com.mikepenz.fastadapter.IItem -import com.mikepenz.fastadapter.select.SelectExtension - -/** - * Created by Allan Wang on 2017-11-08. - */ - -/** - * 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) - -/** - * 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 : IItem<*, *>> IAdapter<Item>.selectedItems: Set<Item> - get() = fastAdapter.getExtension<SelectExtension<Item>, Item>()?.selectedItems ?: emptySet() diff --git a/adapter/src/main/kotlin/ca/allanwang/kau/adapters/FastItemThemedAdapter.kt b/adapter/src/main/kotlin/ca/allanwang/kau/adapters/FastItemThemedAdapter.kt deleted file mode 100644 index 152982f..0000000 --- a/adapter/src/main/kotlin/ca/allanwang/kau/adapters/FastItemThemedAdapter.kt +++ /dev/null @@ -1,201 +0,0 @@ -/* - * Copyright 2018 Allan Wang - * - * 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 ca.allanwang.kau.adapters - -import android.content.res.ColorStateList -import android.os.Build -import android.view.View -import android.widget.ImageView -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 - -/** - * Created by Allan Wang on 2017-06-29. - * - * Adapter with a set of colors that will be added to all subsequent items - * Changing a color while the adapter is not empty will reload all items - * - * 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<*, *>>( - textColor: Int? = null, - backgroundColor: Int? = null, - accentColor: Int? = null -) : FastItemAdapter<Item>() { - constructor(colors: ThemableIItemColors) : this(colors.textColor, colors.backgroundColor, colors.accentColor) - - var textColor: Int? = textColor - set(value) { - if (field == value) return - field = value - themeChanged() - } - var backgroundColor: Int? = backgroundColor - set(value) { - if (field == value) return - field = value - themeChanged() - } - var accentColor: Int? = accentColor - set(value) { - if (field == value) return - field = value - themeChanged() - } - - fun setColors(colors: ThemableIItemColors) { - this.textColor = colors.textColor - this.backgroundColor = colors.backgroundColor - this.accentColor = colors.accentColor - } - - fun themeChanged() { - if (adapterItemCount == 0) return - injectTheme(adapterItems) - 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<*, *>?>?) { - items?.forEach { injectTheme(it) } - } - - protected fun injectTheme(item: IItem<*, *>?) { - if (item is ThemableIItem && item.themeEnabled) { - item.textColor = textColor - item.backgroundColor = backgroundColor - item.accentColor = accentColor - } - } -} - -interface ThemableIItemColors { - var textColor: Int? - var backgroundColor: Int? - var accentColor: Int? -} - -class ThemableIItemColorsDelegate : ThemableIItemColors { - override var textColor: Int? = null - override var backgroundColor: Int? = null - override var accentColor: Int? = null -} - -/** - * Interface that needs to be implemented by every iitem - * Holds the color values and has helper methods to inject the colors - */ -interface ThemableIItem : ThemableIItemColors { - var themeEnabled: Boolean - fun bindTextColor(vararg views: TextView?) - fun bindTextColorSecondary(vararg views: TextView?) - fun bindDividerColor(vararg views: View?) - fun bindAccentColor(vararg views: TextView?) - fun bindBackgroundColor(vararg views: View?) - fun bindBackgroundRipple(vararg views: View?) - fun bindIconColor(vararg views: ImageView?) -} - -/** - * The delegate for [ThemableIItem] - */ -class ThemableIItemDelegate : ThemableIItem, ThemableIItemColors by ThemableIItemColorsDelegate() { - override var themeEnabled: Boolean = true - - override fun bindTextColor(vararg views: TextView?) { - val color = textColor ?: return - views.forEach { it?.setTextColor(color) } - } - - override fun bindTextColorSecondary(vararg views: TextView?) { - val color = textColor?.adjustAlpha(0.8f) ?: return - views.forEach { it?.setTextColor(color) } - } - - override fun bindAccentColor(vararg views: TextView?) { - val color = accentColor ?: textColor ?: return - views.forEach { it?.setTextColor(color) } - } - - override fun bindDividerColor(vararg views: View?) { - val color = (textColor ?: accentColor)?.adjustAlpha(0.1f) ?: return - views.forEach { it?.setBackgroundColor(color) } - } - - override fun bindBackgroundColor(vararg views: View?) { - val color = backgroundColor ?: return - views.forEach { it?.setBackgroundColor(color) } - } - - @RequiresApi(Build.VERSION_CODES.LOLLIPOP) - override fun bindBackgroundRipple(vararg views: View?) { - val background = backgroundColor ?: return - val foreground = accentColor ?: textColor ?: backgroundColor - ?: return //default to normal background - val ripple = createSimpleRippleDrawable(foreground, background) - views.forEach { it?.background = ripple } - } - - @RequiresApi(Build.VERSION_CODES.LOLLIPOP) - override fun bindIconColor(vararg views: ImageView?) { - val color = accentColor ?: textColor ?: return - views.forEach { it?.drawable?.setTintList(ColorStateList.valueOf(color)) } - } -} diff --git a/adapter/src/main/kotlin/ca/allanwang/kau/adapters/RepeatedClickListener.kt b/adapter/src/main/kotlin/ca/allanwang/kau/adapters/RepeatedClickListener.kt deleted file mode 100644 index 40b4774..0000000 --- a/adapter/src/main/kotlin/ca/allanwang/kau/adapters/RepeatedClickListener.kt +++ /dev/null @@ -1,70 +0,0 @@ -/* - * Copyright 2018 Allan Wang - * - * 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 ca.allanwang.kau.adapters - -import android.view.View -import androidx.annotation.IntRange -import com.mikepenz.fastadapter.FastAdapter -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( - count: Int, - duration: Long, - event: OnClickListener<Item> -) = - withOnClickListener(RepeatedClickListener(count, duration, event)) - -/** - * 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<*, *>>( - @IntRange(from = 1) val count: Int, - @IntRange(from = 1) val duration: Long, - val event: OnClickListener<Item> -) : OnClickListener<Item> { - - init { - if (count <= 0) - throw IllegalArgumentException("RepeatedClickListener's count must be > 1") - if (duration <= 0) - throw IllegalArgumentException("RepeatedClickListener's duration must be > 1L") - } - - private var chain = 0 - private var time = -1L - - override fun onClick(v: View?, adapter: IAdapter<Item>, item: Item, position: Int): Boolean { - val now = System.currentTimeMillis() - if (time - now < duration) - chain++ - else - chain = 1 - time = now - if (chain == count) { - chain = 0 - event.onClick(v, adapter, item, position) - return true - } - return false - } -} diff --git a/adapter/src/main/kotlin/ca/allanwang/kau/iitems/CardIItem.kt b/adapter/src/main/kotlin/ca/allanwang/kau/iitems/CardIItem.kt deleted file mode 100644 index 6e33833..0000000 --- a/adapter/src/main/kotlin/ca/allanwang/kau/iitems/CardIItem.kt +++ /dev/null @@ -1,140 +0,0 @@ -/* - * Copyright 2018 Allan Wang - * - * 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 ca.allanwang.kau.iitems - -import android.graphics.Color -import android.graphics.drawable.Drawable -import android.view.View -import android.widget.Button -import android.widget.ImageView -import android.widget.LinearLayout -import android.widget.TextView -import androidx.cardview.widget.CardView -import androidx.recyclerview.widget.RecyclerView -import ca.allanwang.kau.adapter.R -import ca.allanwang.kau.adapters.ThemableIItem -import ca.allanwang.kau.adapters.ThemableIItemDelegate -import ca.allanwang.kau.utils.INVALID_ID -import ca.allanwang.kau.utils.drawable -import ca.allanwang.kau.utils.gone -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.listeners.ClickEventHook -import com.mikepenz.iconics.typeface.IIcon - -/** - * Created by Allan Wang on 2017-06-28. - * - * Simple generic card item with an icon, title, description and button - * The icon and button are hidden by default unless values are given - */ -class CardIItem( - val builder: Config.() -> Unit = {} -) : KauIItem<CardIItem, 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<*, *>>() { - 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<*, *>) { - if (item !is CardIItem) return - with(item.configs) { - when (v.id) { - R.id.kau_card_container -> cardClick?.invoke() - R.id.kau_card_button -> buttonClick?.invoke() - else -> { - } - } - } - } - }) - } - } - - val configs = Config().apply { builder() } - - class Config { - var title: String? = null - var titleRes: Int = INVALID_ID - var desc: String? = null - var descRes: Int = INVALID_ID - var button: String? = null - var buttonRes: Int = INVALID_ID - var buttonClick: (() -> Unit)? = null - var cardClick: (() -> Unit)? = null - var image: Drawable? = null - var imageIIcon: IIcon? = null - var imageIIconColor: Int = Color.WHITE - var imageRes: Int = INVALID_ID - } - - override fun bindView(holder: ViewHolder, payloads: MutableList<Any>) { - super.bindView(holder, payloads) - with(holder.itemView.context) context@{ - with(configs) { - holder.title.text = string(titleRes, title) - val descText = string(descRes, desc) - if (descText != null) holder.description.visible().text = descText - val buttonText = string(buttonRes, button) - if (buttonText != null) { - holder.bottomRow.visible() - holder.button.text = buttonText - } - val icon = drawable(imageRes) { - imageIIcon?.toDrawable(this@context, sizeDp = 24, color = imageIIconColor) - ?: image - } - if (icon != null) holder.icon.visible().setImageDrawable(icon) - } - with(holder) { - bindTextColor(title) - bindTextColorSecondary(description) - bindAccentColor(button) - if (configs.imageIIcon != null) bindIconColor(icon) - bindBackgroundRipple(card) - } - } - } - - override fun unbindView(holder: ViewHolder) { - super.unbindView(holder) - with(holder) { - icon.gone().setImageDrawable(null) - title.text = null - description.gone().text = null - bottomRow.gone() - button.setOnClickListener(null) - card.setOnClickListener(null) - } - } - - class ViewHolder(v: View) : RecyclerView.ViewHolder(v) { - val card: CardView = v.findViewById(R.id.kau_card_container) - val icon: ImageView = v.findViewById(R.id.kau_card_image) - val title: TextView = v.findViewById(R.id.kau_card_title) - val description: TextView = v.findViewById(R.id.kau_card_description) - val bottomRow: LinearLayout = v.findViewById(R.id.kau_card_bottom_row) - val button: Button = v.findViewById(R.id.kau_card_button) - } -} diff --git a/adapter/src/main/kotlin/ca/allanwang/kau/iitems/HeaderIItem.kt b/adapter/src/main/kotlin/ca/allanwang/kau/iitems/HeaderIItem.kt deleted file mode 100644 index 2c488b1..0000000 --- a/adapter/src/main/kotlin/ca/allanwang/kau/iitems/HeaderIItem.kt +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Copyright 2018 Allan Wang - * - * 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 ca.allanwang.kau.iitems - -import android.view.View -import android.widget.TextView -import androidx.cardview.widget.CardView -import androidx.recyclerview.widget.RecyclerView -import ca.allanwang.kau.adapter.R -import ca.allanwang.kau.adapters.ThemableIItem -import ca.allanwang.kau.adapters.ThemableIItemDelegate -import ca.allanwang.kau.utils.INVALID_ID -import ca.allanwang.kau.utils.string - -/** - * Created by Allan Wang on 2017-06-28. - * - * Simple Header with lots of padding on the top - * Contains only one text view - */ -class HeaderIItem( - text: String? = null, - var textRes: Int = INVALID_ID -) : KauIItem<HeaderIItem, HeaderIItem.ViewHolder>( - R.layout.kau_iitem_header, { ViewHolder(it) }, R.id.kau_item_header_big_margin_top -), ThemableIItem by ThemableIItemDelegate() { - - var text: String = text ?: "Header Placeholder" - - override fun bindView(holder: ViewHolder, payloads: MutableList<Any>) { - super.bindView(holder, payloads) - holder.text.text = holder.itemView.context.string(textRes, text) - bindTextColor(holder.text) - bindBackgroundColor(holder.container) - } - - override fun unbindView(holder: ViewHolder) { - super.unbindView(holder) - holder.text.text = null - } - - class ViewHolder(v: View) : RecyclerView.ViewHolder(v) { - val text: TextView = v.findViewById(R.id.kau_header_text) - val container: CardView = v.findViewById(R.id.kau_header_container) - } -} diff --git a/adapter/src/main/kotlin/ca/allanwang/kau/iitems/KauIItem.kt b/adapter/src/main/kotlin/ca/allanwang/kau/iitems/KauIItem.kt deleted file mode 100644 index c66dc01..0000000 --- a/adapter/src/main/kotlin/ca/allanwang/kau/iitems/KauIItem.kt +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright 2018 Allan Wang - * - * 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 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 - -/** - * Created by Allan Wang on 2017-07-03. - * - * 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, - 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 - - final override fun getViewHolder(v: View): VH = viewHolder(v) - final override fun getLayoutRes(): Int = layoutRes -} |