1
0
mirror of https://github.com/AllanWang/Frost-for-Facebook.git synced 2024-11-10 13:02:35 +01:00

Programmatically bind link buttons

This commit is contained in:
Allan Wang 2017-06-30 22:02:25 -07:00
parent f6ae04c42c
commit e923730a26
2 changed files with 46 additions and 34 deletions

View File

@ -2,6 +2,7 @@ package com.pitchedapps.frost
import android.graphics.Color
import android.support.constraint.ConstraintLayout
import android.support.constraint.ConstraintSet
import android.support.v7.widget.RecyclerView
import android.view.View
import android.widget.ImageView
@ -91,36 +92,53 @@ class AboutActivity : AboutActivityBase(R.string::class.java, configBuilder = {
override fun bindView(holder: ViewHolder, payloads: MutableList<Any>?) {
super.bindView(holder, payloads)
with(holder) {
bindIconColor(rate, github)
bindIconColor(*icons.toTypedArray())
bindBackgroundColor(container)
}
}
fun theme(vararg images: ImageView) {
val ripple = createSimpleRippleDrawable(accentColor!!, Color.TRANSPARENT)
images.forEach {
it.background = ripple
it.drawable.setTint(textColor!!)
}
}
class ViewHolder(v: View) : RecyclerView.ViewHolder(v) {
val container: ConstraintLayout by bindView(R.id.about_icons_container)
val rate: ImageView by bindView(R.id.about_rate)
val github: ImageView by bindView(R.id.about_github)
val icons: List<ImageView>
/**
* There are a lot of constraints to be added to each item just to have them chained properly
* My as well do it programmatically
* Initializing the viewholder will setup the icons, scale type and background of all icons,
* link their click listeners and chain them together via a horizontal spread
*/
init {
val c = itemView.context
setup(rate, GoogleMaterial.Icon.gmd_star, { c.startPlayStoreLink(R.string.play_store_package_id) })
setup(github, CommunityMaterial.Icon.cmd_github_circle, { c.startLink("https://github.com/AllanWang/Frost-for-Facebook") })
val iconData = arrayOf<Pair<Pair<Int, IIcon>, () -> Unit>>(
R.id.about_rate to GoogleMaterial.Icon.gmd_star to { c.startPlayStoreLink(R.string.play_store_package_id) },
R.id.about_reddit to CommunityMaterial.Icon.cmd_reddit to { c.startLink("https://www.reddit.com/r/FrostForFacebook/") },
R.id.about_github to CommunityMaterial.Icon.cmd_github_circle to { c.startLink("https://github.com/AllanWang/Frost-for-Facebook") }
).map {
//decouple and setup, then only return the image
(pair, onClick) ->
val (id, icon: IIcon) = pair
val image = itemView.findViewById<ImageView>(id)
setup(image, icon, onClick)
Pair(image, id)
}
icons = iconData.map { it.first }
val ids = iconData.map { it.second }.toIntArray()
val set = ConstraintSet()
set.clone(container)
set.createHorizontalChain(ConstraintSet.PARENT_ID, ConstraintSet.LEFT, ConstraintSet.PARENT_ID, ConstraintSet.RIGHT,
ids, null, ConstraintSet.CHAIN_SPREAD_INSIDE)
set.applyTo(container)
}
fun setup(image: ImageView, icon: IIcon, onClick: () -> Unit) {
image.setImageDrawable(icon.toDrawable(itemView.context, 32))
image.setOnClickListener({ onClick() })
with(image) {
setImageDrawable(icon.toDrawable(context, 32))
scaleType = ImageView.ScaleType.CENTER
background = context.resolveDrawable(android.R.attr.selectableItemBackgroundBorderless)
setOnClickListener({ onClick() })
}
}
}
}
}

View File

@ -1,6 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?><!--
A greatly compacted link layout with just the ids
The constraints are handled programmatically in AboutActivity
-->
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/about_icons_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
@ -10,24 +12,16 @@
<ImageView
android:id="@+id/about_rate"
android:layout_width="@dimen/kau_avatar_bounds"
android:layout_height="@dimen/kau_avatar_bounds"
android:background="?android:attr/selectableItemBackgroundBorderless"
android:scaleType="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/about_github"
app:layout_constraintHorizontal_chainStyle="spread_inside"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
android:layout_height="@dimen/kau_avatar_bounds" />
<ImageView
android:id="@id/about_github"
android:id="@+id/about_reddit"
android:layout_width="@dimen/kau_avatar_bounds"
android:layout_height="@dimen/kau_avatar_bounds"
android:background="?android:attr/selectableItemBackgroundBorderless"
android:scaleType="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/about_rate"
app:layout_constraintTop_toTopOf="parent" />
android:layout_height="@dimen/kau_avatar_bounds" />
<ImageView
android:id="@+id/about_github"
android:layout_width="@dimen/kau_avatar_bounds"
android:layout_height="@dimen/kau_avatar_bounds" />
</android.support.constraint.ConstraintLayout>