11package net.squanchy.eventdetails.widget
22
33import android.content.Context
4+ import android.graphics.Color
45import android.support.annotation.AttrRes
6+ import android.support.annotation.ColorInt
57import android.support.constraint.ConstraintLayout
68import android.support.v4.content.ContextCompat
79import android.text.SpannableStringBuilder
810import android.text.Spanned
911import android.text.style.ForegroundColorSpan
1012import android.util.AttributeSet
1113import android.view.View
14+ import android.widget.TextView
1215import androidx.view.isVisible
1316import arrow.core.Option
1417import kotlinx.android.synthetic.main.merge_event_details_layout.view.*
1518import net.squanchy.R
1619import net.squanchy.eventdetails.domain.view.ExperienceLevel
1720import net.squanchy.schedule.domain.view.Event
1821import net.squanchy.schedule.domain.view.Place
22+ import net.squanchy.schedule.domain.view.Track
1923import net.squanchy.support.content.res.getColorFromAttribute
2024import net.squanchy.support.lang.getOrThrow
2125import net.squanchy.support.text.parseHtml
@@ -41,6 +45,7 @@ class EventDetailsLayout @JvmOverloads constructor(
4145 updateWhen(event.startTime, event.timeZone)
4246 updateWhere(event.place)
4347 updateLevel(event.experienceLevel)
48+ updateTrack(event.track)
4449 updateDescription(event.description)
4550 }
4651
@@ -75,28 +80,45 @@ class EventDetailsLayout @JvmOverloads constructor(
7580 return builder
7681 }
7782
83+ private fun createColorSpan (targetView : View , @AttrRes attributeResId : Int ): ForegroundColorSpan {
84+ val color = targetView.context.theme.getColorFromAttribute(attributeResId)
85+ return ForegroundColorSpan (color)
86+ }
87+
7888 private fun updateLevel (level : Option <ExperienceLevel >) {
7989 if (level.isDefined()) {
8090 levelGroup.isVisible = true
8191
8292 val experienceLevel = level.getOrThrow()
8393 levelValue.setText(experienceLevel.labelStringResId)
84- tintCompoundDrawableEnd(experienceLevel)
94+ val experienceColor = ContextCompat .getColor(context, experienceLevel.colorResId)
95+ tintCompoundDrawableEnd(levelValue, experienceColor)
8596 } else {
8697 levelGroup.isVisible = false
8798 }
8899 }
89100
90- private fun tintCompoundDrawableEnd (experienceLevel : ExperienceLevel ) {
91- val compoundDrawables = levelValue .compoundDrawablesRelative
101+ private fun tintCompoundDrawableEnd (textView : TextView , @ColorInt color : Int ) {
102+ val compoundDrawables = textView .compoundDrawablesRelative
92103 val endCompoundDrawable = compoundDrawables[2 ]
93- endCompoundDrawable?.setTint(ContextCompat .getColor(context, experienceLevel.colorResId) )
104+ endCompoundDrawable?.setTint(color )
94105 }
95106
96- private fun createColorSpan (targetView : View , @AttrRes attributeResId : Int ): ForegroundColorSpan {
97- val color = targetView.context.theme.getColorFromAttribute(attributeResId)
98- return ForegroundColorSpan (color)
107+ private fun updateTrack (trackOption : Option <Track >) {
108+ if (trackOption.isDefined()) {
109+ trackGroup.isVisible = true
110+
111+ val track = trackOption.getOrThrow()
112+ trackValue.text = track.name
113+ track.accentColor?.let {
114+ val trackColor = Color .parseColor(it)
115+ tintCompoundDrawableEnd(trackValue, trackColor)
116+ }
117+ } else {
118+ trackGroup.isVisible = false
119+ }
99120 }
121+
100122 private fun updateDescription (description : Option <String >) {
101123 if (description.isDefined()) {
102124 descriptionGroup.isVisible = true
0 commit comments