1
1
package com.alamkanak.weekview
2
2
3
3
import android.graphics.RectF
4
- import android.graphics.Typeface
5
4
import android.text.SpannableStringBuilder
6
5
import android.text.StaticLayout
7
- import android.text.TextPaint
8
- import android.text.TextUtils
9
- import android.text.TextUtils.TruncateAt.END
10
- import android.text.style.StyleSpan
11
- import com.alamkanak.weekview.WeekViewEvent.TextResource
12
6
import kotlin.math.roundToInt
13
7
14
8
internal class AllDayEventsUpdater <T : Any >(
15
9
private val view : WeekView <T >,
16
10
private val config : WeekViewConfigWrapper ,
17
11
private val cache : WeekViewCache <T >,
18
- private val chipsCache : EventChipsCache <T >,
19
- private val emojiTextProcessor : EmojiTextProcessor = EmojiTextProcessor ()
12
+ private val chipsCache : EventChipsCache <T >
20
13
) : Updater {
21
14
22
- private val context = view.context
23
- private val rectCalculator = EventChipRectCalculator < T >(config )
15
+ private val boundsCalculator = EventChipBoundsCalculator < T >(config)
16
+ private val spannableStringBuilder = SpannableStringBuilder ( )
24
17
25
18
private var previousHorizontalOrigin: Float? = null
26
19
private var dummyTextLayout: StaticLayout ? = null
@@ -61,13 +54,13 @@ internal class AllDayEventsUpdater<T : Any>(
61
54
eventChip : EventChip <T >,
62
55
startPixel : Float
63
56
) {
64
- val chipRect = rectCalculator .calculateAllDayEvent(eventChip, startPixel)
57
+ val chipRect = boundsCalculator .calculateAllDayEvent(eventChip, startPixel)
65
58
eventChip.bounds = if (chipRect.isValidEventBounds) chipRect else null
66
59
67
60
if (chipRect.isValidEventBounds) {
68
61
val textLayout = calculateChipTextLayout(eventChip)
69
- textLayout?. let { layout ->
70
- cache.allDayEventLayouts[eventChip] = layout
62
+ if ( textLayout != null ) {
63
+ cache.allDayEventLayouts[eventChip] = textLayout
71
64
}
72
65
}
73
66
}
@@ -96,31 +89,25 @@ internal class AllDayEventsUpdater<T : Any>(
96
89
return dummyTextLayout
97
90
}
98
91
99
- val title = when (val resource = event.titleResource) {
100
- is TextResource .Id -> context.getString(resource.resId)
101
- is TextResource .Value -> resource.text
102
- null -> " "
103
- }
104
-
105
- val modifiedTitle = emojiTextProcessor.process(title)
106
- val text = SpannableStringBuilder (modifiedTitle)
107
- text.setSpan(StyleSpan (Typeface .BOLD ))
92
+ spannableStringBuilder.clear()
93
+ val title = event.title.emojify()
94
+ spannableStringBuilder.append(title)
108
95
109
- val location = when (val resource = event.locationResource) {
110
- is TextResource .Id -> context.getString(resource.resId)
111
- is TextResource .Value -> resource.text
112
- null -> null
113
- }
96
+ // val title = event.title.emojify()
97
+ // val text = SpannableStringBuilder(title)
98
+ // text.setSpan(StyleSpan(Typeface.BOLD))
114
99
100
+ val location = event.location?.emojify()
115
101
if (location != null ) {
116
- val modifiedLocation = emojiTextProcessor.process(location )
117
- text .append(' ' ).append(modifiedLocation )
102
+ spannableStringBuilder.append( ' ' )
103
+ spannableStringBuilder .append(location )
118
104
}
119
105
106
+ val text = spannableStringBuilder.build()
120
107
val availableWidth = width.toInt()
121
108
122
- val textPaint = event .getTextPaint(context, config )
123
- val textLayout = TextLayoutBuilder .build(text, textPaint, availableWidth)
109
+ val textPaint = config .getTextPaint(event )
110
+ val textLayout = text.toTextLayout( textPaint, availableWidth)
124
111
val lineHeight = textLayout.height / textLayout.lineCount
125
112
126
113
// For an all day event, we display just one line
@@ -134,11 +121,11 @@ internal class AllDayEventsUpdater<T : Any>(
134
121
* Creates a dummy text layout that is only used to determine the height of all-day events.
135
122
*/
136
123
private fun createDummyTextLayout (
137
- event : WeekViewEvent <T >
124
+ event : ResolvedWeekViewEvent <T >
138
125
): StaticLayout {
139
126
if (dummyTextLayout == null ) {
140
- val textPaint = event .getTextPaint(context, config )
141
- dummyTextLayout = TextLayoutBuilder .build( " " , textPaint, width = 0 )
127
+ val textPaint = config .getTextPaint(event )
128
+ dummyTextLayout = " " .toTextLayout( textPaint, width = 0 )
142
129
}
143
130
return checkNotNull(dummyTextLayout)
144
131
}
@@ -148,7 +135,7 @@ internal class AllDayEventsUpdater<T : Any>(
148
135
availableWidth : Int ,
149
136
existingTextLayout : StaticLayout
150
137
): StaticLayout {
151
- val textPaint = event .getTextPaint(context, config )
138
+ val textPaint = config .getTextPaint(event )
152
139
val bounds = checkNotNull(bounds)
153
140
val width = bounds.width().roundToInt() - (config.eventPaddingHorizontal * 2 )
154
141
@@ -160,7 +147,7 @@ internal class AllDayEventsUpdater<T : Any>(
160
147
return existingTextLayout
161
148
}
162
149
163
- return TextLayoutBuilder .build(ellipsized, textPaint, width)
150
+ return ellipsized.toTextLayout( textPaint, width)
164
151
}
165
152
166
153
private val RectF .isValidEventBounds: Boolean
@@ -169,18 +156,4 @@ internal class AllDayEventsUpdater<T : Any>(
169
156
top < view.height &&
170
157
right > config.timeColumnWidth &&
171
158
bottom > 0 )
172
-
173
- private operator fun RectF.component1 () = left
174
-
175
- private operator fun RectF.component2 () = top
176
-
177
- private operator fun RectF.component3 () = right
178
-
179
- private operator fun RectF.component4 () = bottom
180
-
181
- private fun CharSequence.ellipsized (
182
- textPaint : TextPaint ,
183
- availableArea : Int ,
184
- truncateAt : TextUtils .TruncateAt = END
185
- ): CharSequence = TextUtils .ellipsize(this , textPaint, availableArea.toFloat(), truncateAt)
186
159
}
0 commit comments