Skip to content

Commit bb203e7

Browse files
[refactor] LocalConfiguration BoxWithConstraints 로 대체
KMP/CMP 에선 LocalConfiguration 을 사용할 수 없음 따라서 BoxWithConstraints 를 통해 화면의 세로, 가로 길이를 구하는 방식으로 대체
1 parent 2756af2 commit bb203e7

File tree

3 files changed

+151
-154
lines changed

3 files changed

+151
-154
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.nexters.bandalart
22

3+
import androidx.compose.foundation.layout.BoxWithConstraints
34
import androidx.compose.foundation.layout.height
45
import androidx.compose.foundation.layout.padding
56
import androidx.compose.material3.Scaffold
@@ -10,37 +11,38 @@ import androidx.compose.material3.SnackbarResult
1011
import androidx.compose.runtime.Composable
1112
import androidx.compose.runtime.remember
1213
import androidx.compose.ui.Modifier
13-
import androidx.compose.ui.platform.LocalConfiguration
1414
import androidx.compose.ui.unit.dp
1515
import com.nexters.bandalart.navigation.BandalartNavHost
1616
import com.nexters.bandalart.ui.BandalartSnackbar
1717

1818
@Composable
1919
fun BandalartApp() {
20-
val snackbarHostState = remember { SnackbarHostState() }
21-
val height = LocalConfiguration.current.screenHeightDp
20+
BoxWithConstraints {
21+
val snackbarHostState = remember { SnackbarHostState() }
22+
val screenHeight = maxHeight.value
2223

23-
Scaffold(
24-
snackbarHost = {
25-
SnackbarHost(
26-
modifier = Modifier
27-
.padding(bottom = (height - 96).dp)
28-
.height(36.dp),
29-
hostState = snackbarHostState,
30-
snackbar = {
31-
BandalartSnackbar(message = it.visuals.message)
24+
Scaffold(
25+
snackbarHost = {
26+
SnackbarHost(
27+
modifier = Modifier
28+
.padding(bottom = (screenHeight - 96).dp)
29+
.height(36.dp),
30+
hostState = snackbarHostState,
31+
snackbar = {
32+
BandalartSnackbar(message = it.visuals.message)
33+
},
34+
)
35+
},
36+
) { innerPadding ->
37+
BandalartNavHost(
38+
modifier = Modifier.padding(innerPadding),
39+
onShowSnackbar = { message ->
40+
snackbarHostState.showSnackbar(
41+
message = message,
42+
duration = SnackbarDuration.Short,
43+
) == SnackbarResult.ActionPerformed
3244
},
3345
)
34-
},
35-
) { innerPadding ->
36-
BandalartNavHost(
37-
modifier = Modifier.padding(innerPadding),
38-
onShowSnackbar = { message ->
39-
snackbarHostState.showSnackbar(
40-
message = message,
41-
duration = SnackbarDuration.Short,
42-
) == SnackbarResult.ActionPerformed
43-
},
44-
)
46+
}
4547
}
4648
}

composeApp/src/commonMain/kotlin/com/nexters/bandalart/feature/home/ui/bandalart/BandalartChart.kt

+63-65
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.nexters.bandalart.feature.home.ui.bandalart
22

33
import androidx.compose.foundation.background
44
import androidx.compose.foundation.layout.Box
5+
import androidx.compose.foundation.layout.BoxWithConstraints
56
import androidx.compose.foundation.layout.fillMaxWidth
67
import androidx.compose.foundation.shape.RoundedCornerShape
78
import androidx.compose.runtime.Composable
@@ -10,8 +11,6 @@ import androidx.compose.ui.Modifier
1011
import androidx.compose.ui.draw.clip
1112
import androidx.compose.ui.layout.Layout
1213
import androidx.compose.ui.layout.layoutId
13-
import androidx.compose.ui.platform.LocalConfiguration
14-
import androidx.compose.ui.platform.LocalContext
1514
import androidx.compose.ui.unit.Constraints
1615
import androidx.compose.ui.unit.dp
1716
import bandalart.composeapp.generated.resources.Res
@@ -33,82 +32,81 @@ fun BandalartChart(
3332
onHomeUiAction: (HomeUiAction) -> Unit,
3433
modifier: Modifier = Modifier,
3534
) {
36-
val screenWidthDp = LocalConfiguration.current.screenWidthDp.dp
37-
val paddedMaxWidth = remember(screenWidthDp) {
38-
screenWidthDp - (15.dp * 2)
39-
}
35+
BoxWithConstraints {
36+
val paddedMaxWidth = remember(maxWidth) { maxWidth - (15.dp * 2) }
4037

41-
val subCellList = persistentListOf(
42-
SubCell(2, 3, 1, 1, bandalartCellData.children[0]),
43-
SubCell(3, 2, 1, 0, bandalartCellData.children[1]),
44-
SubCell(3, 2, 1, 1, bandalartCellData.children[2]),
45-
SubCell(2, 3, 0, 1, bandalartCellData.children[3]),
46-
)
38+
val subCellList = persistentListOf(
39+
SubCell(2, 3, 1, 1, bandalartCellData.children[0]),
40+
SubCell(3, 2, 1, 0, bandalartCellData.children[1]),
41+
SubCell(3, 2, 1, 1, bandalartCellData.children[2]),
42+
SubCell(2, 3, 0, 1, bandalartCellData.children[3]),
43+
)
4744

48-
Layout(
49-
modifier = modifier
50-
.fillMaxWidth()
51-
.clip(RoundedCornerShape(8.dp)),
52-
content = {
53-
for (index in subCellList.indices) {
45+
Layout(
46+
modifier = modifier
47+
.fillMaxWidth()
48+
.clip(RoundedCornerShape(8.dp)),
49+
content = {
50+
for (index in subCellList.indices) {
51+
Box(
52+
modifier = Modifier
53+
.layoutId(stringResource(Res.string.home_layout_id, index + 1))
54+
.clip(RoundedCornerShape(12.dp))
55+
.background(color = Gray300),
56+
) {
57+
BandalartCellGrid(
58+
bandalartData = bandalartData,
59+
subCell = subCellList[index],
60+
rows = subCellList[index].rowCnt,
61+
cols = subCellList[index].colCnt,
62+
onHomeUiAction = onHomeUiAction,
63+
)
64+
}
65+
}
5466
Box(
5567
modifier = Modifier
56-
.layoutId(stringResource(Res.string.home_layout_id, index + 1))
57-
.clip(RoundedCornerShape(12.dp))
58-
.background(color = Gray300),
68+
.layoutId(stringResource(Res.string.home_main_id))
69+
.clip(RoundedCornerShape(10.dp))
70+
.background(color = bandalartData.mainColor.toColor()),
5971
) {
60-
BandalartCellGrid(
72+
BandalartCell(
73+
cellType = CellType.MAIN,
6174
bandalartData = bandalartData,
62-
subCell = subCellList[index],
63-
rows = subCellList[index].rowCnt,
64-
cols = subCellList[index].colCnt,
75+
cellData = bandalartCellData,
6576
onHomeUiAction = onHomeUiAction,
6677
)
6778
}
68-
}
69-
Box(
70-
modifier = Modifier
71-
.layoutId(stringResource(Res.string.home_main_id))
72-
.clip(RoundedCornerShape(10.dp))
73-
.background(color = bandalartData.mainColor.toColor()),
74-
) {
75-
BandalartCell(
76-
cellType = CellType.MAIN,
77-
bandalartData = bandalartData,
78-
cellData = bandalartCellData,
79-
onHomeUiAction = onHomeUiAction,
80-
)
81-
}
82-
},
83-
) { measurables, constraints ->
84-
val sub1 = measurables.first { it.layoutId == "Sub 1" }
85-
val sub2 = measurables.first { it.layoutId == "Sub 2" }
86-
val sub3 = measurables.first { it.layoutId == "Sub 3" }
87-
val sub4 = measurables.first { it.layoutId == "Sub 4" }
88-
val main = measurables.first { it.layoutId == "Main" }
79+
},
80+
) { measurables, constraints ->
81+
val sub1 = measurables.first { it.layoutId == "Sub 1" }
82+
val sub2 = measurables.first { it.layoutId == "Sub 2" }
83+
val sub3 = measurables.first { it.layoutId == "Sub 3" }
84+
val sub4 = measurables.first { it.layoutId == "Sub 4" }
85+
val main = measurables.first { it.layoutId == "Main" }
8986

90-
val chartWidth = paddedMaxWidth.roundToPx()
91-
val mainWidth = chartWidth / 5
92-
val padding = 1.dp.roundToPx()
87+
val chartWidth = paddedMaxWidth.roundToPx()
88+
val mainWidth = chartWidth / 5
89+
val padding = 1.dp.roundToPx()
9390

94-
val mainConstraints = Constraints.fixed(width = mainWidth, height = mainWidth)
95-
val sub1Constraints = Constraints.fixed(width = mainWidth * 3 - padding, height = mainWidth * 2 - padding)
96-
val sub2Constraints = Constraints.fixed(width = mainWidth * 2 - padding, height = mainWidth * 3 - padding)
97-
val sub3Constraints = Constraints.fixed(width = mainWidth * 2 - padding, height = mainWidth * 3 - padding)
98-
val sub4Constraints = Constraints.fixed(width = mainWidth * 3 - padding, height = mainWidth * 2 - padding)
91+
val mainConstraints = Constraints.fixed(width = mainWidth, height = mainWidth)
92+
val sub1Constraints = Constraints.fixed(width = mainWidth * 3 - padding, height = mainWidth * 2 - padding)
93+
val sub2Constraints = Constraints.fixed(width = mainWidth * 2 - padding, height = mainWidth * 3 - padding)
94+
val sub3Constraints = Constraints.fixed(width = mainWidth * 2 - padding, height = mainWidth * 3 - padding)
95+
val sub4Constraints = Constraints.fixed(width = mainWidth * 3 - padding, height = mainWidth * 2 - padding)
9996

100-
val mainPlaceable = main.measure(mainConstraints)
101-
val sub1Placeable = sub1.measure(sub1Constraints)
102-
val sub2Placeable = sub2.measure(sub2Constraints)
103-
val sub3Placeable = sub3.measure(sub3Constraints)
104-
val sub4Placeable = sub4.measure(sub4Constraints)
97+
val mainPlaceable = main.measure(mainConstraints)
98+
val sub1Placeable = sub1.measure(sub1Constraints)
99+
val sub2Placeable = sub2.measure(sub2Constraints)
100+
val sub3Placeable = sub3.measure(sub3Constraints)
101+
val sub4Placeable = sub4.measure(sub4Constraints)
105102

106-
layout(width = chartWidth, height = chartWidth) {
107-
mainPlaceable.place(x = mainWidth * 2, y = mainWidth * 2)
108-
sub1Placeable.place(x = 0, y = 0)
109-
sub2Placeable.place(x = mainWidth * 3 + padding, y = 0)
110-
sub3Placeable.place(x = 0, y = mainWidth * 2 + padding)
111-
sub4Placeable.place(x = mainWidth * 2 + padding, y = mainWidth * 3 + padding)
103+
layout(width = chartWidth, height = chartWidth) {
104+
mainPlaceable.place(x = mainWidth * 2, y = mainWidth * 2)
105+
sub1Placeable.place(x = 0, y = 0)
106+
sub2Placeable.place(x = mainWidth * 3 + padding, y = 0)
107+
sub3Placeable.place(x = 0, y = mainWidth * 2 + padding)
108+
sub4Placeable.place(x = mainWidth * 2 + padding, y = mainWidth * 3 + padding)
109+
}
112110
}
113111
}
114112
}

composeApp/src/commonMain/kotlin/com/nexters/bandalart/feature/home/ui/bandalart/BandalartSkeletonScreen.kt

+63-66
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.nexters.bandalart.feature.home.ui.bandalart
33
import androidx.compose.foundation.background
44
import androidx.compose.foundation.layout.Arrangement
55
import androidx.compose.foundation.layout.Box
6+
import androidx.compose.foundation.layout.BoxWithConstraints
67
import androidx.compose.foundation.layout.Column
78
import androidx.compose.foundation.layout.Row
89
import androidx.compose.foundation.layout.Spacer
@@ -32,8 +33,6 @@ import androidx.compose.ui.graphics.Color
3233
import androidx.compose.ui.graphics.Color.Companion.White
3334
import androidx.compose.ui.layout.Layout
3435
import androidx.compose.ui.layout.layoutId
35-
import androidx.compose.ui.platform.LocalConfiguration
36-
import androidx.compose.ui.platform.LocalContext
3736
import androidx.compose.ui.text.font.FontWeight
3837
import androidx.compose.ui.unit.Constraints
3938
import androidx.compose.ui.unit.Dp
@@ -244,84 +243,82 @@ fun BandalartSkeletonChart(
244243
mainBrush: Brush,
245244
modifier: Modifier = Modifier,
246245
) {
247-
val context = LocalContext.current
248-
val screenWidthDp = LocalConfiguration.current.screenWidthDp.dp
249-
val paddedMaxWidth = remember(screenWidthDp) {
250-
screenWidthDp - (15.dp * 2)
251-
}
246+
BoxWithConstraints {
247+
val paddedMaxWidth = remember(maxWidth) { maxWidth - (15.dp * 2) }
252248

253-
val subCellList = persistentListOf(
254-
SkeletonSubCell(2, 3, 1, 1, null),
255-
SkeletonSubCell(3, 2, 1, 0, null),
256-
SkeletonSubCell(3, 2, 1, 1, null),
257-
SkeletonSubCell(2, 3, 0, 1, null),
258-
)
249+
val subCellList = persistentListOf(
250+
SkeletonSubCell(2, 3, 1, 1, null),
251+
SkeletonSubCell(3, 2, 1, 0, null),
252+
SkeletonSubCell(3, 2, 1, 1, null),
253+
SkeletonSubCell(2, 3, 0, 1, null),
254+
)
259255

260-
Layout(
261-
modifier = Modifier
262-
.fillMaxWidth()
263-
.clip(RoundedCornerShape(8.dp)),
264-
content = {
265-
for (index in subCellList.indices) {
256+
Layout(
257+
modifier = Modifier
258+
.fillMaxWidth()
259+
.clip(RoundedCornerShape(8.dp)),
260+
content = {
261+
for (index in subCellList.indices) {
262+
Box(
263+
modifier
264+
.layoutId(stringResource(Res.string.home_layout_id, index + 1))
265+
.clip(RoundedCornerShape(12.dp))
266+
.background(color = Gray200),
267+
) {
268+
SkeletonCellGrid(
269+
rows = subCellList[index].rowCnt,
270+
cols = subCellList[index].colCnt,
271+
subCell = subCellList[index],
272+
taskBrush = taskBrush,
273+
subBrush = subBrush,
274+
mainBrush = mainBrush,
275+
)
276+
}
277+
}
266278
Box(
267279
modifier
268-
.layoutId(stringResource(Res.string.home_layout_id, index + 1))
269-
.clip(RoundedCornerShape(12.dp))
270-
.background(color = Gray200),
280+
.layoutId(stringResource(Res.string.home_main_id))
281+
.clip(RoundedCornerShape(10.dp))
282+
.background(brush = taskBrush),
271283
) {
272-
SkeletonCellGrid(
273-
rows = subCellList[index].rowCnt,
274-
cols = subCellList[index].colCnt,
275-
subCell = subCellList[index],
284+
SkeletonCell(
285+
cellType = CellType.MAIN,
276286
taskBrush = taskBrush,
277287
subBrush = subBrush,
278288
mainBrush = mainBrush,
279289
)
280290
}
281-
}
282-
Box(
283-
modifier
284-
.layoutId(stringResource(Res.string.home_main_id))
285-
.clip(RoundedCornerShape(10.dp))
286-
.background(brush = taskBrush),
287-
) {
288-
SkeletonCell(
289-
cellType = CellType.MAIN,
290-
taskBrush = taskBrush,
291-
subBrush = subBrush,
292-
mainBrush = mainBrush,
293-
)
294-
}
295-
},
296-
) { measurables, constraints ->
297-
val sub1 = measurables.first { it.layoutId == "Sub 1" }
298-
val sub2 = measurables.first { it.layoutId == "Sub 2" }
299-
val sub3 = measurables.first { it.layoutId == "Sub 3" }
300-
val sub4 = measurables.first { it.layoutId == "Sub 4" }
301-
val main = measurables.first { it.layoutId == "Main" }
291+
},
292+
) { measurables, constraints ->
293+
val sub1 = measurables.first { it.layoutId == "Sub 1" }
294+
val sub2 = measurables.first { it.layoutId == "Sub 2" }
295+
val sub3 = measurables.first { it.layoutId == "Sub 3" }
296+
val sub4 = measurables.first { it.layoutId == "Sub 4" }
297+
val main = measurables.first { it.layoutId == "Main" }
302298

303-
val chartWidth = paddedMaxWidth.roundToPx()
304-
val mainWidth = chartWidth / 5
305-
val padding = 1.dp.roundToPx()
299+
val chartWidth = paddedMaxWidth.roundToPx()
300+
val mainWidth = chartWidth / 5
301+
val padding = 1.dp.roundToPx()
306302

307-
val mainConstraints = Constraints.fixed(width = mainWidth, height = mainWidth)
308-
val sub1Constraints = Constraints.fixed(width = mainWidth * 3 - padding, height = mainWidth * 2 - padding)
309-
val sub2Constraints = Constraints.fixed(width = mainWidth * 2 - padding, height = mainWidth * 3 - padding)
310-
val sub3Constraints = Constraints.fixed(width = mainWidth * 2 - padding, height = mainWidth * 3 - padding)
311-
val sub4Constraints = Constraints.fixed(width = mainWidth * 3 - padding, height = mainWidth * 2 - padding)
303+
val mainConstraints = Constraints.fixed(width = mainWidth, height = mainWidth)
304+
val sub1Constraints = Constraints.fixed(width = mainWidth * 3 - padding, height = mainWidth * 2 - padding)
305+
val sub2Constraints = Constraints.fixed(width = mainWidth * 2 - padding, height = mainWidth * 3 - padding)
306+
val sub3Constraints = Constraints.fixed(width = mainWidth * 2 - padding, height = mainWidth * 3 - padding)
307+
val sub4Constraints = Constraints.fixed(width = mainWidth * 3 - padding, height = mainWidth * 2 - padding)
312308

313-
val mainPlaceable = main.measure(mainConstraints)
314-
val sub1Placeable = sub1.measure(sub1Constraints)
315-
val sub2Placeable = sub2.measure(sub2Constraints)
316-
val sub3Placeable = sub3.measure(sub3Constraints)
317-
val sub4Placeable = sub4.measure(sub4Constraints)
309+
val mainPlaceable = main.measure(mainConstraints)
310+
val sub1Placeable = sub1.measure(sub1Constraints)
311+
val sub2Placeable = sub2.measure(sub2Constraints)
312+
val sub3Placeable = sub3.measure(sub3Constraints)
313+
val sub4Placeable = sub4.measure(sub4Constraints)
318314

319-
layout(width = chartWidth, height = chartWidth) {
320-
mainPlaceable.place(x = mainWidth * 2, y = mainWidth * 2)
321-
sub1Placeable.place(x = 0, y = 0)
322-
sub2Placeable.place(x = mainWidth * 3 + padding, y = 0)
323-
sub3Placeable.place(x = 0, y = mainWidth * 2 + padding)
324-
sub4Placeable.place(x = mainWidth * 2 + padding, y = mainWidth * 3 + padding)
315+
layout(width = chartWidth, height = chartWidth) {
316+
mainPlaceable.place(x = mainWidth * 2, y = mainWidth * 2)
317+
sub1Placeable.place(x = 0, y = 0)
318+
sub2Placeable.place(x = mainWidth * 3 + padding, y = 0)
319+
sub3Placeable.place(x = 0, y = mainWidth * 2 + padding)
320+
sub4Placeable.place(x = mainWidth * 2 + padding, y = mainWidth * 3 + padding)
321+
}
325322
}
326323
}
327324
}

0 commit comments

Comments
 (0)