@@ -4,14 +4,15 @@ import android.content.Context
44import android.content.res.Resources
55import android.support.annotation.AttrRes
66import android.support.annotation.ColorInt
7+ import android.support.constraint.ConstraintLayout
78import android.support.v4.content.ContextCompat
89import android.text.SpannableStringBuilder
910import android.text.Spanned
1011import android.text.style.ForegroundColorSpan
1112import android.util.AttributeSet
1213import android.util.TypedValue
1314import android.view.View
14- import android.widget.LinearLayout
15+ import androidx.view.isVisible
1516import arrow.core.Option
1617import kotlinx.android.synthetic.main.merge_event_details_layout.view.*
1718import net.squanchy.R
@@ -20,81 +21,76 @@ import net.squanchy.schedule.domain.view.Event
2021import net.squanchy.schedule.domain.view.Place
2122import net.squanchy.support.lang.getOrThrow
2223import net.squanchy.support.text.parseHtml
24+ import org.joda.time.DateTimeZone
25+ import org.joda.time.LocalDateTime
2326import org.joda.time.format.DateTimeFormat
2427
25- // TODO flatten this layout as a ConstraintLayout
2628class EventDetailsLayout @JvmOverloads constructor(
2729 context : Context ,
2830 attrs : AttributeSet ? ,
2931 defStyle : Int = 0
30- ) : LinearLayout (context, attrs, defStyle) {
32+ ) : ConstraintLayout (context, attrs, defStyle) {
3133
32- init {
33- super .setOrientation(LinearLayout .VERTICAL )
34- }
35-
36- override fun setOrientation (orientation : Int ) {
37- throw UnsupportedOperationException (" Changing orientation is not supported for EventDetailsLayout" )
38- }
34+ private val dateTimeFormatter = DateTimeFormat .forPattern(WHEN_DATE_TIME_FORMAT )
3935
4036 override fun onFinishInflate () {
4137 super .onFinishInflate()
4238
43- View . inflate(context, R .layout.merge_event_details_layout, this )
39+ inflate(context, R .layout.merge_event_details_layout, this )
4440 }
4541
4642 fun updateWith (event : Event ) {
47- updateWhen(event)
48- updateWhere(event)
43+ updateWhen(event.startTime, event.timeZone )
44+ updateWhere(event.place )
4945 updateLevel(event.experienceLevel)
5046 updateDescription(event.description)
5147 }
5248
53- private fun updateWhen (event : Event ) {
54- val formatter = DateTimeFormat .forPattern( WHEN_DATE_TIME_FORMAT ). withZone(event. timeZone)
55- whenTextView .text = formatter.print (event. startTime.toDateTime())
56- whenContainer.visibility = View . VISIBLE
49+ private fun updateWhen (startTime : LocalDateTime , timeZone : DateTimeZone ) {
50+ val formatter = dateTimeFormatter. withZone(timeZone)
51+ whenValue .text = formatter.print (startTime.toDateTime(timeZone ))
52+ whenGroup.isVisible = true
5753 }
5854
59- private fun updateWhere (event : Event ) {
60- if (event. place.isDefined()) {
61- whereContainer.visibility = View . VISIBLE
62- whereTextView .text = placeTextFrom(event. place.getOrThrow())
55+ private fun updateWhere (place : Option < Place > ) {
56+ if (place.isDefined()) {
57+ whereGroup.isVisible = true
58+ whereValue .text = place.getOrThrow().toPlaceLabel( )
6359 } else {
64- whereContainer.visibility = View . GONE
60+ whereGroup.isVisible = false
6561 }
6662 }
6763
68- private fun placeTextFrom ( place : Place ): CharSequence {
69- val builder = SpannableStringBuilder (place. name)
70- if (place. floor.isDefined()) {
71- val floorLabel = place. floor.getOrThrow()
64+ private fun Place. toPlaceLabel ( ): CharSequence {
65+ val builder = SpannableStringBuilder (name)
66+ if (floor.isDefined()) {
67+ val floorLabel = floor.getOrThrow()
7268 builder.append(" " )
7369 .append(floorLabel)
7470 .setSpan(
75- createColorSpan(whereTextView , android.R .attr.textColorSecondary),
76- builder.length - floorLabel.length,
77- builder.length,
78- Spanned .SPAN_EXCLUSIVE_EXCLUSIVE
71+ createColorSpan(whereValue , android.R .attr.textColorSecondary),
72+ builder.length - floorLabel.length,
73+ builder.length,
74+ Spanned .SPAN_EXCLUSIVE_EXCLUSIVE
7975 )
8076 }
8177 return builder
8278 }
8379
8480 private fun updateLevel (level : Option <ExperienceLevel >) {
8581 if (level.isDefined()) {
86- levelContainer.visibility = View . VISIBLE
82+ levelGroup.isVisible = true
8783
8884 val experienceLevel = level.getOrThrow()
89- levelTextView .setText(experienceLevel.labelStringResId)
85+ levelValue .setText(experienceLevel.labelStringResId)
9086 tintCompoundDrawableEnd(experienceLevel)
9187 } else {
92- levelContainer.visibility = View . GONE
88+ levelGroup.isVisible = false
9389 }
9490 }
9591
9692 private fun tintCompoundDrawableEnd (experienceLevel : ExperienceLevel ) {
97- val compoundDrawables = levelTextView .compoundDrawablesRelative
93+ val compoundDrawables = levelValue .compoundDrawablesRelative
9894 val endCompoundDrawable = compoundDrawables[2 ]
9995 endCompoundDrawable?.setTint(ContextCompat .getColor(context, experienceLevel.colorResId))
10096 }
@@ -113,12 +109,10 @@ class EventDetailsLayout @JvmOverloads constructor(
113109
114110 private fun updateDescription (description : Option <String >) {
115111 if (description.isDefined()) {
116- descriptionHeader.visibility = View .VISIBLE
117- descriptionTextView.visibility = View .VISIBLE
118- descriptionTextView.text = parseHtml(description.getOrThrow())
112+ descriptionGroup.isVisible = true
113+ descriptionValue.text = description.getOrThrow().parseHtml()
119114 } else {
120- descriptionHeader.visibility = View .GONE
121- descriptionTextView.visibility = View .GONE
115+ descriptionGroup.isVisible = false
122116 }
123117 }
124118
0 commit comments