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/allanwang/kau/iitems | |
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/allanwang/kau/iitems')
3 files changed, 234 insertions, 0 deletions
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) +} |