Skip to content

Commit bc37036

Browse files
committed
fix wear font scale issues
1 parent 970edf7 commit bc37036

File tree

8 files changed

+152
-22
lines changed

8 files changed

+152
-22
lines changed

app/src/base/java/com/example/util/simpletimetracker/WearModule.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import javax.inject.Inject
1212
interface WearModule {
1313

1414
@Binds
15-
fun NoopWearInteractor.bindWearInteractor(): WearInteractor
15+
fun bindWearInteractor(impl: NoopWearInteractor): WearInteractor
1616

1717
class NoopWearInteractor @Inject constructor() : WearInteractor {
1818
override suspend fun update() {

features/feature_settings/src/main/java/com/example/util/simpletimetracker/feature_settings/viewModel/delegate/SettingsRatingViewModelDelegate.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class SettingsRatingViewModelDelegate @Inject constructor(
6060

6161
private fun onSupportDevelopmentClick() {
6262
router.execute(
63-
OpenLinkParams(link = resourceRepo.getString(R.string.support_development_link))
63+
OpenLinkParams(link = resourceRepo.getString(R.string.support_development_link)),
6464
)
6565
}
6666

wear/src/main/java/com/example/util/simpletimetracker/presentation/ui/components/ActivityChip.kt

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import androidx.compose.runtime.Composable
1515
import androidx.compose.runtime.Immutable
1616
import androidx.compose.ui.Modifier
1717
import androidx.compose.ui.graphics.Color
18+
import androidx.compose.ui.platform.LocalDensity
1819
import androidx.compose.ui.text.style.TextOverflow
1920
import androidx.compose.ui.tooling.preview.Preview
2021
import androidx.compose.ui.unit.dp
@@ -51,7 +52,7 @@ fun ActivityChip(
5152
ACTIVITY_RUNNING_VIEW_HEIGHT
5253
} else {
5354
ACTIVITY_VIEW_HEIGHT
54-
}
55+
} * LocalDensity.current.fontScale
5556
Chip(
5657
modifier = Modifier
5758
.height(height.dp)
@@ -123,6 +124,14 @@ fun Sample() {
123124
)
124125
}
125126

127+
@Preview(device = WearDevices.LARGE_ROUND, fontScale = 2f)
128+
@Composable
129+
fun SampleFontScale() {
130+
ActivityChip(
131+
ActivityChipState(0, "Cooking", WearActivityIcon.Text("🎉"), 0xFF123456),
132+
)
133+
}
134+
126135
@Preview(device = WearDevices.LARGE_ROUND)
127136
@Composable
128137
fun SampleSleep() {
@@ -180,6 +189,17 @@ fun CurrentlyRunning() {
180189
)
181190
}
182191

192+
@Preview(device = WearDevices.LARGE_ROUND, fontScale = 2f)
193+
@Composable
194+
fun CurrentlyRunningFontScale() {
195+
ActivityChip(
196+
ActivityChipState(
197+
0, "Sleeping", WearActivityIcon.Text("🛏️"), 0xFFABCDEF,
198+
startedAt = Instant.now().toEpochMilli() - 365000,
199+
),
200+
)
201+
}
202+
183203
@Preview(device = WearDevices.LARGE_ROUND)
184204
@Composable
185205
fun CurrentlyRunningLoading() {
@@ -204,6 +224,18 @@ fun CurrentlyRunningWithTags() {
204224
)
205225
}
206226

227+
@Preview(device = WearDevices.LARGE_ROUND, fontScale = 2f)
228+
@Composable
229+
fun CurrentlyRunningWithTagsFontScale() {
230+
ActivityChip(
231+
ActivityChipState(
232+
0, "Sleeping", WearActivityIcon.Text("🛏️"), 0xFFABCDEF,
233+
startedAt = Instant.now().toEpochMilli() - 365000,
234+
tagString = "Work, Hotel",
235+
),
236+
)
237+
}
238+
207239
@Preview(device = WearDevices.LARGE_ROUND)
208240
@Composable
209241
fun CurrentlyRunningWithTagsLoading() {

wear/src/main/java/com/example/util/simpletimetracker/presentation/ui/components/ActivityChipCompact.kt

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ fun ActivityChipCompact(
8585
text = text,
8686
maxLines = 1,
8787
overflow = TextOverflow.Ellipsis,
88-
fontSize = 10.sp,
88+
fontSize = 10.scaledSp(),
8989
letterSpacing = (-0.3).sp,
9090
fontWeight = FontWeight.Bold,
9191
)
@@ -165,6 +165,20 @@ private fun PreviewRunning() {
165165
)
166166
}
167167

168+
@Preview(device = WearDevices.LARGE_ROUND, fontScale = 2f)
169+
@Composable
170+
private fun PreviewRunningFontScale() {
171+
ActivityChipCompact(
172+
modifier = Modifier.size(48.dp),
173+
state = ActivityChipCompatState(
174+
id = 0,
175+
icon = WearActivityIcon.Text("🎉"),
176+
color = 0xFF123456,
177+
startedAt = Instant.now().toEpochMilli() - 36500000,
178+
),
179+
)
180+
}
181+
168182
@Preview(device = WearDevices.LARGE_ROUND)
169183
@Composable
170184
private fun PreviewRunningLoading() {

wear/src/main/java/com/example/util/simpletimetracker/presentation/ui/components/SettingsCheckbox.kt

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66
package com.example.util.simpletimetracker.presentation.ui.components
77

88
import androidx.compose.foundation.clickable
9-
import androidx.compose.foundation.layout.Column
9+
import androidx.compose.foundation.layout.Box
1010
import androidx.compose.foundation.layout.Row
1111
import androidx.compose.foundation.layout.fillMaxWidth
1212
import androidx.compose.foundation.layout.padding
1313
import androidx.compose.runtime.Composable
14+
import androidx.compose.ui.Alignment
1415
import androidx.compose.ui.Modifier
1516
import androidx.compose.ui.graphics.Color
1617
import androidx.compose.ui.text.font.FontWeight
@@ -33,28 +34,35 @@ fun SettingsCheckbox(
3334
.fillMaxWidth()
3435
.clickable(onClick = onClick),
3536
) {
36-
Column(
37-
Modifier
37+
Text(
38+
modifier = Modifier
3839
.padding(vertical = 3.dp)
3940
.padding(horizontal = 4.dp)
4041
.fillMaxWidth()
4142
.weight(1f),
43+
text = state.text,
44+
fontWeight = FontWeight.Medium,
45+
)
46+
Box(
47+
contentAlignment = Alignment.Center,
4248
) {
49+
// Dummy text for centering.
4350
Text(
44-
text = state.text,
45-
fontWeight = FontWeight.Medium,
51+
modifier = Modifier
52+
.padding(vertical = 3.dp)
53+
.padding(horizontal = 4.dp),
54+
text = "",
55+
)
56+
Checkbox(
57+
checked = state.checked,
58+
colors = CheckboxDefaults.colors(
59+
checkedBoxColor = ColorAccent,
60+
checkedCheckmarkColor = ColorAccent,
61+
uncheckedBoxColor = Color.White,
62+
uncheckedCheckmarkColor = Color.White,
63+
),
4664
)
4765
}
48-
Checkbox(
49-
modifier = Modifier,
50-
checked = state.checked,
51-
colors = CheckboxDefaults.colors(
52-
checkedBoxColor = ColorAccent,
53-
checkedCheckmarkColor = ColorAccent,
54-
uncheckedBoxColor = Color.White,
55-
uncheckedCheckmarkColor = Color.White,
56-
),
57-
)
5866
}
5967
}
6068

@@ -70,6 +78,18 @@ private fun SettingsCheckboxPreview() {
7078
)
7179
}
7280

81+
@Preview(device = WearDevices.LARGE_ROUND, fontScale = 2f)
82+
@Composable
83+
private fun SettingsCheckboxFontScalePreview() {
84+
SettingsCheckbox(
85+
state = SettingsItem.CheckBox(
86+
type = SettingsItemType.ShowCompactList,
87+
text = "Check box",
88+
checked = false,
89+
),
90+
)
91+
}
92+
7393
@Preview(device = WearDevices.LARGE_ROUND)
7494
@Composable
7595
private fun SettingsCheckboxCheckedPreview() {

wear/src/main/java/com/example/util/simpletimetracker/presentation/ui/components/TagChip.kt

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import androidx.compose.runtime.Immutable
1313
import androidx.compose.runtime.remember
1414
import androidx.compose.ui.Modifier
1515
import androidx.compose.ui.graphics.Color
16+
import androidx.compose.ui.platform.LocalDensity
1617
import androidx.compose.ui.text.style.TextOverflow
1718
import androidx.compose.ui.tooling.preview.Preview
1819
import androidx.compose.ui.unit.dp
@@ -71,9 +72,11 @@ private fun SingleSelectTagChip(
7172
val onClickState = remember(state.id) {
7273
{ onClick(state.id) }
7374
}
75+
val height = ACTIVITY_VIEW_HEIGHT.dp *
76+
LocalDensity.current.fontScale
7477
Chip(
7578
modifier = Modifier
76-
.height(ACTIVITY_VIEW_HEIGHT.dp)
79+
.height(height)
7780
.fillMaxWidth(),
7881
onClick = onClickState,
7982
label = {
@@ -106,9 +109,11 @@ private fun MultiSelectTagChip(
106109
val onClickState = remember(state.id) {
107110
{ onClick(state.id) }
108111
}
112+
val height = ACTIVITY_VIEW_HEIGHT.dp *
113+
LocalDensity.current.fontScale
109114
SplitToggleChip(
110115
modifier = Modifier
111-
.height(ACTIVITY_VIEW_HEIGHT.dp)
116+
.height(height)
112117
.fillMaxWidth(),
113118
checked = state.checked,
114119
onCheckedChange = onCheckedChange,
@@ -162,6 +167,20 @@ private fun Default() {
162167
)
163168
}
164169

170+
@Preview(device = WearDevices.LARGE_ROUND, fontScale = 2f)
171+
@Composable
172+
private fun DefaultWithFontScale() {
173+
TagChip(
174+
state = TagChipState(
175+
id = 123,
176+
name = "Sleep",
177+
color = 0xFF123456,
178+
checked = false,
179+
mode = TagChipState.TagSelectionMode.SINGLE,
180+
),
181+
)
182+
}
183+
165184
@Preview(device = WearDevices.LARGE_ROUND)
166185
@Composable
167186
private fun Loading() {
@@ -191,6 +210,20 @@ private fun MultiSelectMode() {
191210
)
192211
}
193212

213+
@Preview(device = WearDevices.LARGE_ROUND, fontScale = 2f)
214+
@Composable
215+
private fun MultiSelectModeFontScale() {
216+
TagChip(
217+
state = TagChipState(
218+
id = 123,
219+
name = "Sleep",
220+
color = 0xFF654321,
221+
checked = false,
222+
mode = TagChipState.TagSelectionMode.MULTI,
223+
),
224+
)
225+
}
226+
194227
@Preview(device = WearDevices.LARGE_ROUND)
195228
@Composable
196229
private fun MultiSelectChecked() {

wear/src/main/java/com/example/util/simpletimetracker/presentation/ui/components/TagSelectionButton.kt

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import androidx.compose.runtime.Immutable
1313
import androidx.compose.runtime.remember
1414
import androidx.compose.ui.Modifier
1515
import androidx.compose.ui.graphics.Color
16+
import androidx.compose.ui.platform.LocalDensity
1617
import androidx.compose.ui.text.style.TextAlign
1718
import androidx.compose.ui.text.style.TextOverflow
1819
import androidx.compose.ui.tooling.preview.Preview
@@ -41,9 +42,11 @@ fun TagSelectionButton(
4142
val onClickState = remember(state) {
4243
{ onClick(state.buttonType) }
4344
}
45+
val height = ACTIVITY_VIEW_HEIGHT.dp *
46+
LocalDensity.current.fontScale
4447
Chip(
4548
modifier = Modifier
46-
.height(ACTIVITY_VIEW_HEIGHT.dp)
49+
.height(height)
4750
.fillMaxWidth(),
4851
onClick = onClickState,
4952
label = {
@@ -105,3 +108,15 @@ private fun Loading() {
105108
),
106109
)
107110
}
111+
112+
@Preview(device = WearDevices.LARGE_ROUND, fontScale = 2f)
113+
@Composable
114+
private fun FontScaled() {
115+
TagSelectionButton(
116+
state = TagSelectionButtonState(
117+
text = "Temp",
118+
color = ColorInactive,
119+
buttonType = TagListState.Item.ButtonType.Untagged,
120+
),
121+
)
122+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.example.util.simpletimetracker.presentation.ui.components
2+
3+
import androidx.compose.runtime.Composable
4+
import androidx.compose.ui.platform.LocalDensity
5+
import androidx.compose.ui.unit.TextUnit
6+
import androidx.compose.ui.unit.sp
7+
8+
@Composable
9+
fun Int.scaledSp(): TextUnit {
10+
val value: Int = this
11+
return with(LocalDensity.current) {
12+
val fontScale = this.fontScale
13+
val textSize = value / fontScale
14+
textSize.sp
15+
}
16+
}

0 commit comments

Comments
 (0)