@@ -18,18 +18,22 @@ import androidx.compose.ui.semantics.semantics
18
18
import androidx.compose.ui.text.style.TextOverflow
19
19
import androidx.compose.ui.tooling.preview.Preview
20
20
import androidx.compose.ui.unit.dp
21
+ import androidx.compose.ui.unit.sp
21
22
import androidx.wear.compose.material.SplitToggleChip
22
23
import androidx.wear.compose.material.Switch
23
24
import androidx.wear.compose.material.SwitchDefaults
24
25
import androidx.wear.compose.material.Text
25
26
import androidx.wear.compose.material.ToggleChipDefaults
26
27
import androidx.wear.tooling.preview.devices.WearDevices
28
+ import com.example.util.simpletimetracker.presentation.remember.rememberDurationSince
27
29
import com.example.util.simpletimetracker.wearrpc.Activity
28
30
import com.example.util.simpletimetracker.wearrpc.Tag
31
+ import java.time.Duration
29
32
import java.time.Instant
30
- import java.time.LocalDateTime
31
- import java.time.ZoneId
32
- import java.time.format.DateTimeFormatter
33
+
34
+ private const val ISO_HOURS_MINUTES_PARTS_REGEX = " (\\ d[HM])(?!$)"
35
+ private const val DECIMAL_SEPARATOR_AND_FRACTIONAL_PART_REGEX = " \\ .\\ d+"
36
+ private const val ISO_MISSING_MINUTES_REGEX = " (\\ d+H) (\\ d+S)"
33
37
34
38
@Composable
35
39
fun ActivityChip (
@@ -46,7 +50,7 @@ fun ActivityChip(
46
50
" "
47
51
}
48
52
val tagString = if (tagsList.isNotEmpty()) {
49
- " ( $tagsList ) "
53
+ " - $tagsList "
50
54
} else {
51
55
" "
52
56
}
@@ -59,7 +63,7 @@ fun ActivityChip(
59
63
Row (modifier = Modifier .height(IntrinsicSize .Min )) {
60
64
ActivityIcon (activityIcon = activity.icon)
61
65
Text (
62
- text = activity.name + tagString ,
66
+ text = activity.name,
63
67
maxLines = 1 ,
64
68
overflow = TextOverflow .Ellipsis ,
65
69
modifier = Modifier .padding(start = 4 .dp),
@@ -68,7 +72,22 @@ fun ActivityChip(
68
72
},
69
73
secondaryLabel = {
70
74
if (startedAt != null ) {
71
- Text (" Since ${recentTimestampToString(startedAt)} " )
75
+ var startedDiff = rememberDurationSince(epochMillis = startedAt)
76
+
77
+ Text (
78
+ text = durationToLabel(startedDiff) + tagString,
79
+ maxLines = 1 ,
80
+ overflow = TextOverflow .Ellipsis ,
81
+ color = Color .White ,
82
+ fontSize = 10 .sp,
83
+ modifier = Modifier .padding(
84
+ start = if (tagString.isNotEmpty()) {
85
+ 2 .dp
86
+ } else {
87
+ 22 .dp
88
+ },
89
+ ),
90
+ )
72
91
} else {
73
92
null
74
93
}
@@ -108,18 +127,12 @@ fun ActivityChip(
108
127
)
109
128
}
110
129
111
- fun recentTimestampToString (epochMillis : Long ): String {
112
- // Someday, it would be nice for this to show nicer time strings
113
- // e.g. "a few minutes ago", "yesterday", etc.
114
- val time = LocalDateTime .ofInstant(
115
- Instant .ofEpochMilli(epochMillis),
116
- ZoneId .systemDefault(),
117
- )
118
- return if (time > LocalDateTime .ofInstant(Instant .now(), ZoneId .systemDefault()).minusDays(1 )) {
119
- time.format(DateTimeFormatter .ISO_LOCAL_TIME )
120
- } else {
121
- time.format(DateTimeFormatter .ISO_DATE_TIME ).replace(" T" , " " )
122
- }
130
+ fun durationToLabel (duration : Duration ): String {
131
+ return duration.toString()
132
+ .substring(2 ) // remove "PT" at the beginning of the string representation
133
+ .replace(ISO_HOURS_MINUTES_PARTS_REGEX .toRegex(), " $1 " )
134
+ .replace(DECIMAL_SEPARATOR_AND_FRACTIONAL_PART_REGEX .toRegex(), " " )
135
+ .replace(ISO_MISSING_MINUTES_REGEX .toRegex(), " $1 0M $2" ).lowercase()
123
136
}
124
137
125
138
@Preview(device = WearDevices .LARGE_ROUND )
@@ -164,14 +177,18 @@ fun White() {
164
177
@Preview(device = WearDevices .LARGE_ROUND )
165
178
@Composable
166
179
fun CurrentlyRunning () {
167
- ActivityChip (Activity (456 , " Sleeping" , " 🛏️" , 0xFFABCDEF ), startedAt = 1706751601000L )
180
+ ActivityChip (
181
+ Activity (456 , " Sleeping" , " 🛏️" , 0xFFABCDEF ),
182
+ startedAt = Instant .now().toEpochMilli() - 360000 ,
183
+ )
168
184
}
169
185
170
186
@Preview(device = WearDevices .LARGE_ROUND )
171
187
@Composable
172
188
fun CurrentlyRunningWithTags () {
173
189
ActivityChip (
174
- Activity (456 , " Sleeping" , " 🛏️" , 0xFFABCDEF ), startedAt = 1706751601000L ,
190
+ Activity (456 , " Sleeping" , " 🛏️" , 0xFFABCDEF ),
191
+ startedAt = Instant .now().toEpochMilli() - 360000 ,
175
192
tags = arrayOf(
176
193
Tag (id = 2 , name = " Work" , isGeneral = true , color = 0xFFFFAA22 ),
177
194
Tag (id = 4 , name = " Hotel" , isGeneral = false , color = 0xFFABCDEF ),
0 commit comments