Skip to content

Commit

Permalink
expose anchorMinWidth and anchorMaxWidth as attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
saschoar committed Jul 16, 2019
1 parent b65b841 commit d8c16af
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ package com.transferwise.sequencelayout

import android.content.Context
import android.content.res.TypedArray
import androidx.annotation.StringRes
import androidx.annotation.StyleRes
import androidx.core.widget.TextViewCompat
import android.util.AttributeSet
import android.view.View
import android.widget.TableRow
import android.widget.TextView
import androidx.annotation.Dimension
import androidx.annotation.StringRes
import androidx.annotation.StyleRes
import androidx.core.widget.TextViewCompat
import kotlinx.android.synthetic.main.sequence_step.view.*
import kotlin.math.max

/**
* Step, represented in a row inside of {@link com.transferwise.sequencelayout.SequenceLayout}.
Expand All @@ -20,6 +22,8 @@ import kotlinx.android.synthetic.main.sequence_step.view.*
* android:layout_height="wrap_content"
* app:active="true"
* app:anchor="Anchor"
* app:anchorMinWidth="20dp"
* app:anchorMaxWidth="80dp"
* app:anchorTextAppearance="@style/TextAppearance.AppCompat.Small"
* app:subtitle="This is a subtitle"
* app:subtitleTextAppearance="@style/TextAppearance.AppCompat.Body1"
Expand All @@ -29,6 +33,9 @@ import kotlinx.android.synthetic.main.sequence_step.view.*
*
* @attr ref com.transferwise.sequencelayout.R.styleable#SequenceStep_anchor
* @attr ref com.transferwise.sequencelayout.R.styleable#SequenceStep_anchorTextAppearance
* @attr ref com.transferwise.sequencelayout.R.styleable#SequenceStep_anchorMinWidth
* @attr ref com.transferwise.sequencelayout.R.styleable#SequenceStep_anchorMaxWidth
* @attr ref com.transferwise.sequencelayout.R.styleable#SequenceStep_anchorTextAppearance
* @attr ref com.transferwise.sequencelayout.R.styleable#SequenceStep_title
* @attr ref com.transferwise.sequencelayout.R.styleable#SequenceStep_titleTextAppearance
* @attr ref com.transferwise.sequencelayout.R.styleable#SequenceStep_subtitle
Expand Down Expand Up @@ -57,6 +64,7 @@ public class SequenceStep(context: Context?, attrs: AttributeSet?)
R.style.SequenceStep)

setupAnchor(attributes)
setupAnchorWidth(attributes)
setupAnchorTextAppearance(attributes)
setupTitle(attributes)
setupTitleTextAppearance(attributes)
Expand All @@ -78,6 +86,20 @@ public class SequenceStep(context: Context?, attrs: AttributeSet?)
this.anchor.minWidth = resources.getDimensionPixelSize(R.dimen.sequence_anchor_min_width)
}

/**
* Sets the anchor max width
*/
public fun setAnchorMaxWidth(@Dimension(unit = Dimension.PX) maxWidth: Int) {
anchor.maxWidth = maxWidth
}

/**
* Sets the anchor min width
*/
public fun setAnchorMinWidth(@Dimension(unit = Dimension.PX) minWidth: Int) {
anchor.minWidth = minWidth
}

/**
* Sets the anchor text appearance
*
Expand Down Expand Up @@ -164,7 +186,7 @@ public class SequenceStep(context: Context?, attrs: AttributeSet?)
}

fun getDotOffset(): Int =
(Math.max(getViewHeight(anchor), getViewHeight(title)) - 8.toPx()) / 2 //TODO dynamic dot height
(max(getViewHeight(anchor), getViewHeight(title)) - 8.toPx()) / 2 //TODO dynamic dot height

private fun setupAnchor(attributes: TypedArray) {
if (!attributes.hasValue(R.styleable.SequenceStep_anchor)) {
Expand All @@ -174,6 +196,11 @@ public class SequenceStep(context: Context?, attrs: AttributeSet?)
}
}

private fun setupAnchorWidth(attributes: TypedArray) {
setAnchorMinWidth(attributes.getDimensionPixelSize(R.styleable.SequenceStep_anchorMinWidth, 0))
setAnchorMaxWidth(attributes.getDimensionPixelSize(R.styleable.SequenceStep_anchorMaxWidth, Integer.MAX_VALUE))
}

private fun setupSubtitle(attributes: TypedArray) {
if (!attributes.hasValue(R.styleable.SequenceStep_subtitle)) {
subtitle.visibility = View.GONE
Expand Down Expand Up @@ -213,12 +240,9 @@ public class SequenceStep(context: Context?, attrs: AttributeSet?)
}

private fun verticallyCenter(vararg views: View) {
var maxHeight = 0
for (view in views) {
val height = getViewHeight(view)
maxHeight = Math.max(maxHeight, height)
}
for (view in views) {
val maxHeight = views.map(::getViewHeight).max() ?: 0

views.forEach { view ->
val height = getViewHeight(view)
(view.layoutParams as MarginLayoutParams).topMargin = (maxHeight - height) / 2
view.requestLayout()
Expand All @@ -231,5 +255,4 @@ public class SequenceStep(context: Context?, attrs: AttributeSet?)
} else {
view.measuredHeight
}

}
1 change: 0 additions & 1 deletion sequencelayout/src/main/res/layout/sequence_step.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
android:layout_height="wrap_content"
android:layout_marginEnd="28dp"
android:ellipsize="end"
android:maxWidth="@dimen/sequence_anchor_max_width"
android:paddingEnd="16dp"
android:paddingRight="16dp"
android:paddingStart="0dp"
Expand Down
4 changes: 4 additions & 0 deletions sequencelayout/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
<attr name="subtitle" format="string"/>
<attr name="subtitleTextAppearance" format="reference"/>
<attr name="active" format="boolean"/>
<attr name="anchorMinWidth" format="dimension"/>
<attr name="anchorMaxWidth" format="dimension"/>
</declare-styleable>

<style name="SequenceLayout">
Expand All @@ -25,6 +27,8 @@
<item name="anchorTextAppearance">@style/TextAppearance.AppCompat.Small</item>
<item name="titleTextAppearance">@style/TextAppearance.AppCompat.Small</item>
<item name="subtitleTextAppearance">@style/TextAppearance.AppCompat.Body1</item>
<item name="anchorMinWidth">@dimen/sequence_anchor_min_width</item>
<item name="anchorMaxWidth">@dimen/sequence_anchor_max_width</item>
</style>

</resources>

0 comments on commit d8c16af

Please sign in to comment.