generated from android/android-dev-challenge-compose
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathDisplay.kt
124 lines (116 loc) · 4.42 KB
/
Display.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
* Copyright 2021 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mobnetic.newtonstimer.timer
import androidx.compose.animation.animateColorAsState
import androidx.compose.foundation.layout.BoxWithConstraints
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.withStyle
import androidx.compose.ui.unit.Constraints
import androidx.compose.ui.unit.Density
import androidx.compose.ui.unit.IntSize
import androidx.compose.ui.unit.TextUnit
import com.mobnetic.newtonstimer.formatMinTwoDigits
import newtonstimer.composeapp.generated.resources.Res
import newtonstimer.composeapp.generated.resources.Roboto_Thin
import org.jetbrains.compose.resources.Font
import kotlin.time.Duration.Companion.milliseconds
@Composable
fun Display(
viewModel: NewtonsTimerViewModel,
modifier: Modifier = Modifier,
) {
BoxWithConstraints(modifier, Alignment.Center) {
val digitsColor by animateColorAsState(MaterialTheme.colors.primaryVariant)
val separatorsColor by animateColorAsState(MaterialTheme.colors.onBackground)
val fontSize = LocalDensity.current.calculateTimerFontSizeIn(constraints)
Text(
text = formatTime(
viewModel.displayedMillis,
viewModel.isConfigured,
separatorsColor,
fontSize
),
color = digitsColor,
fontFamily = FontFamily(Font(Res.font.Roboto_Thin)),
fontSize = fontSize,
textAlign = TextAlign.Center
)
}
}
private fun formatTime(
millis: Long,
isConfigured: Boolean,
separatorsColor: Color,
fontSize: TextUnit,
) = AnnotatedString.Builder("").apply {
val minutes = millis.milliseconds.inWholeMinutes % 60
val seconds = millis.milliseconds.inWholeSeconds % 60
val tenthsOfSecond = millis % 1000 / 100
when (isConfigured) {
true -> appendConfiguredTime(minutes, seconds, tenthsOfSecond, separatorsColor)
false -> appendNotConfiguredTime(minutes, seconds, separatorsColor, fontSize)
}
}.toAnnotatedString()
private fun AnnotatedString.Builder.appendConfiguredTime(
minutes: Long,
seconds: Long,
tenthsOfSecond: Long,
separatorsColor: Color,
) {
val separatorsStyle = SpanStyle(separatorsColor)
if (minutes > 0) {
append(minutes.formatMinTwoDigits())
withStyle(separatorsStyle) { append(":") }
}
append(seconds.formatMinTwoDigits())
withStyle(separatorsStyle) { append(".") }
append(tenthsOfSecond.formatMinTwoDigits())
}
private fun AnnotatedString.Builder.appendNotConfiguredTime(
minutes: Long,
seconds: Long,
separatorsColor: Color,
fontSize: TextUnit,
) {
val separatorsStyle = SpanStyle(separatorsColor, fontSize = fontSize / 3)
append(minutes.formatMinTwoDigits())
withStyle(separatorsStyle) { append("m ") }
append(seconds.formatMinTwoDigits())
withStyle(separatorsStyle) { append("s") }
}
@Composable
private fun Density.calculateTimerFontSizeIn(constraints: Constraints): TextUnit {
val bounds = IntSize(constraints.maxWidth, constraints.maxHeight)
return remember(this, bounds) {
(bounds.width * TIMER_FONT_SIZE_HORIZONTAL_FRACTION)
.coerceAtMost(bounds.height * TIMER_FONT_SIZE_VERTICAL_FRACTION)
.toSp()
}
}
private const val TIMER_FONT_SIZE_HORIZONTAL_FRACTION = 0.24f
private const val TIMER_FONT_SIZE_VERTICAL_FRACTION = 0.9f