diff options
author | Allan Wang <allanwang@google.com> | 2019-07-27 17:46:04 -0700 |
---|---|---|
committer | Allan Wang <allanwang@google.com> | 2019-07-27 17:46:04 -0700 |
commit | 50c8012e2fe88d6b275d1a3269033590962caa7f (patch) | |
tree | 65ebb32525be83441e6caac8362e3e9ae022bfd1 /fastadapter/src/main/kotlin/ca | |
parent | 01058912580a3ecfe77eaf6af209e1c46a97f2d7 (diff) | |
parent | d94017e6f6df67e80604111028ea16f6c7f4e077 (diff) | |
download | kau-50c8012e2fe88d6b275d1a3269033590962caa7f.tar.gz kau-50c8012e2fe88d6b275d1a3269033590962caa7f.tar.bz2 kau-50c8012e2fe88d6b275d1a3269033590962caa7f.zip |
Fix initial conflicts
Diffstat (limited to 'fastadapter/src/main/kotlin/ca')
6 files changed, 549 insertions, 0 deletions
diff --git a/fastadapter/src/main/kotlin/ca/allanwang/kau/adapters/AdapterUtils.kt b/fastadapter/src/main/kotlin/ca/allanwang/kau/adapters/AdapterUtils.kt new file mode 100644 index 0000000..42fe1a2 --- /dev/null +++ b/fastadapter/src/main/kotlin/ca/allanwang/kau/adapters/AdapterUtils.kt @@ -0,0 +1,44 @@ +/* + * 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/fastadapter/src/main/kotlin/ca/allanwang/kau/adapters/FastItemThemedAdapter.kt b/fastadapter/src/main/kotlin/ca/allanwang/kau/adapters/FastItemThemedAdapter.kt new file mode 100644 index 0000000..01ac0e5 --- /dev/null +++ b/fastadapter/src/main/kotlin/ca/allanwang/kau/adapters/FastItemThemedAdapter.kt @@ -0,0 +1,201 @@ +/* + * 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.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/fastadapter/src/main/kotlin/ca/allanwang/kau/adapters/RepeatedClickListener.kt b/fastadapter/src/main/kotlin/ca/allanwang/kau/adapters/RepeatedClickListener.kt new file mode 100644 index 0000000..3e7d707 --- /dev/null +++ b/fastadapter/src/main/kotlin/ca/allanwang/kau/adapters/RepeatedClickListener.kt @@ -0,0 +1,70 @@ +/* + * 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/fastadapter/src/main/kotlin/ca/allanwang/kau/iitems/CardIItem.kt b/fastadapter/src/main/kotlin/ca/allanwang/kau/iitems/CardIItem.kt new file mode 100644 index 0000000..47c465c --- /dev/null +++ b/fastadapter/src/main/kotlin/ca/allanwang/kau/iitems/CardIItem.kt @@ -0,0 +1,140 @@ +/* + * 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/fastadapter/src/main/kotlin/ca/allanwang/kau/iitems/HeaderIItem.kt b/fastadapter/src/main/kotlin/ca/allanwang/kau/iitems/HeaderIItem.kt new file mode 100644 index 0000000..2c488b1 --- /dev/null +++ b/fastadapter/src/main/kotlin/ca/allanwang/kau/iitems/HeaderIItem.kt @@ -0,0 +1,59 @@ +/* + * 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/fastadapter/src/main/kotlin/ca/allanwang/kau/iitems/KauIItem.kt b/fastadapter/src/main/kotlin/ca/allanwang/kau/iitems/KauIItem.kt new file mode 100644 index 0000000..34a2b7d --- /dev/null +++ b/fastadapter/src/main/kotlin/ca/allanwang/kau/iitems/KauIItem.kt @@ -0,0 +1,35 @@ +/* + * 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 androidx.annotation.LayoutRes +import androidx.recyclerview.widget.RecyclerView +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<VH : RecyclerView.ViewHolder>( + @param:LayoutRes override val layoutRes: Int, + private val viewHolder: (v: View) -> VH, + override val type: Int = layoutRes +) : AbstractItem<VH>() { + final override fun getViewHolder(v: View): VH = viewHolder(v) +} |