Skip to content

Commit 563e57e

Browse files
committed
improve duration dialog
1 parent 20672eb commit 563e57e

File tree

1 file changed

+26
-9
lines changed
  • features/feature_dialogs/src/main/java/com/example/util/simpletimetracker/feature_dialogs/duration/customView

1 file changed

+26
-9
lines changed

features/feature_dialogs/src/main/java/com/example/util/simpletimetracker/feature_dialogs/duration/customView/DurationView.kt

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -136,22 +136,35 @@ class DurationView @JvmOverloads constructor(
136136
// Center text
137137
canvas.translate(textStartHorizontal, textStartVertical)
138138

139-
var text = data.hours.format()
140-
canvas.drawText(text, 0f, 0f, textPaint)
141-
canvas.translate(textPaint.measureText(text), 0f)
139+
val hoursText = data.hours.format()
140+
val minutesText = data.minutes.format()
141+
val secondsText = data.seconds.format()
142+
val hoursNotEmpty = textHasValues(hoursText)
143+
val minutesNotEmpty = hoursNotEmpty || textHasValues(minutesText)
144+
val secondsNotEmpty = hoursNotEmpty || minutesNotEmpty || textHasValues(secondsText)
145+
146+
var color = if (hoursNotEmpty) textColor else legendTextColor
147+
textPaint.color = color
148+
legendTextPaint.color = color
149+
canvas.drawText(hoursText, 0f, 0f, textPaint)
150+
canvas.translate(textPaint.measureText(hoursText), 0f)
142151
canvas.drawText(hourString, 0f, 0f, legendTextPaint)
143152
canvas.translate(legendTextPaint.measureText(hourString) + legendPadding, 0f)
144153

145-
text = data.minutes.format()
146-
canvas.drawText(text, 0f, 0f, textPaint)
147-
canvas.translate(textPaint.measureText(text), 0f)
154+
color = if (minutesNotEmpty) textColor else legendTextColor
155+
textPaint.color = color
156+
legendTextPaint.color = color
157+
canvas.drawText(minutesText, 0f, 0f, textPaint)
158+
canvas.translate(textPaint.measureText(minutesText), 0f)
148159
canvas.drawText(minuteString, 0f, 0f, legendTextPaint)
149160
canvas.translate(legendTextPaint.measureText(minuteString) + legendPadding, 0f)
150161

151162
if (data.showSeconds) {
152-
text = data.seconds.format()
153-
canvas.drawText(text, 0f, 0f, textPaint)
154-
canvas.translate(textPaint.measureText(text), 0f)
163+
color = if (secondsNotEmpty) textColor else legendTextColor
164+
textPaint.color = color
165+
legendTextPaint.color = color
166+
canvas.drawText(secondsText, 0f, 0f, textPaint)
167+
canvas.translate(textPaint.measureText(secondsText), 0f)
155168
canvas.drawText(secondString, 0f, 0f, legendTextPaint)
156169
canvas.translate(legendTextPaint.measureText(secondString), 0f)
157170
}
@@ -166,6 +179,10 @@ class DurationView @JvmOverloads constructor(
166179
paint.textSize = desiredTextSize
167180
}
168181

182+
private fun textHasValues(text: String): Boolean {
183+
return text != "00"
184+
}
185+
169186
private fun Long.format(): String {
170187
return this.toDuration()
171188
}

0 commit comments

Comments
 (0)