Skip to content

Commit 77d0f7c

Browse files
committed
[feat]: 투표 수정 api 연결 로직 작성 (#133)
1 parent f94ccec commit 77d0f7c

5 files changed

Lines changed: 40 additions & 13 deletions

File tree

app/src/main/java/com/texthip/thip/data/model/rooms/response/RoomsPostsResponse.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,6 @@ data class PostList(
3535
data class VoteItems(
3636
val voteItemId: Int,
3737
val itemName: String,
38-
val percentage: Int,
38+
val count: Int,
3939
val isVoted: Boolean,
4040
)

app/src/main/java/com/texthip/thip/ui/common/buttons/GroupVoteButton.kt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,19 @@ fun GroupVoteButton(
3939
hasVoted: Boolean = false, // 투표 여부
4040
onOptionSelected: (Int?) -> Unit
4141
) {
42+
val totalVotes = voteItems.sumOf { it.count }
43+
4244
Column(
4345
modifier = modifier.fillMaxWidth(),
4446
verticalArrangement = Arrangement.spacedBy(10.dp)
4547
) {
4648
voteItems.forEachIndexed { index, item ->
4749
val isSelected = selectedIndex == index
48-
val votePercent = if (hasVoted) item.percentage.coerceIn(0, 100) else 0
50+
val votePercent = if (totalVotes > 0) {
51+
(item.count.toFloat() / totalVotes * 100).toInt()
52+
} else {
53+
0
54+
}
4955

5056
val animatedPercent by animateFloatAsState(
5157
targetValue = votePercent / 100f,
@@ -104,7 +110,7 @@ fun GroupVoteButton(
104110
)
105111
if (hasVoted) {
106112
Text(
107-
text = "${item.percentage}%",
113+
text = "${item.count}",
108114
color = textColor,
109115
style = fontStyle
110116
)

app/src/main/java/com/texthip/thip/ui/common/forms/BorderedTextField.kt

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ fun BorderedTextField(
3838
hint: String,
3939
text: String,
4040
canRemove: Boolean = true,
41+
isEnabled: Boolean = true,
4142
onTextChange: (String) -> Unit,
4243
onDelete: () -> Unit,
4344
onRemoveField: () -> Unit
@@ -46,11 +47,15 @@ fun BorderedTextField(
4647
val interactionSource = remember { MutableInteractionSource() }
4748
val isFocused by interactionSource.collectIsFocusedAsState()
4849

49-
val iconRes = when {
50-
isFocused && text.isEmpty() -> R.drawable.ic_x_circle_darkgrey
51-
isFocused && text.isNotEmpty() -> R.drawable.ic_x_circle_grey
52-
!isFocused && canRemove -> R.drawable.ic_delete
53-
else -> null
50+
val iconRes = if (isEnabled) {
51+
when {
52+
isFocused && text.isEmpty() -> R.drawable.ic_x_circle_darkgrey
53+
isFocused && text.isNotEmpty() -> R.drawable.ic_x_circle_grey
54+
!isFocused && canRemove -> R.drawable.ic_delete
55+
else -> null
56+
}
57+
} else {
58+
null
5459
}
5560

5661
val iconEnabled = when (iconRes) {
@@ -74,6 +79,7 @@ fun BorderedTextField(
7479
BasicTextField(
7580
value = text,
7681
onValueChange = onTextChange,
82+
enabled = isEnabled,
7783
textStyle = myStyle.copy(color = colors.White),
7884
singleLine = true,
7985
modifier = Modifier

app/src/main/java/com/texthip/thip/ui/group/note/component/VoteInputSection.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ fun VoteInputSection(
3737
onAddOption: () -> Unit,
3838
onRemoveOption: (index: Int) -> Unit,
3939
modifier: Modifier = Modifier,
40+
isEnabled: Boolean = true,
4041
maxOptionLength: Int = 20,
4142
maxOptions: Int = 5
4243
) {
@@ -45,6 +46,7 @@ fun VoteInputSection(
4546
Column(
4647
modifier = modifier
4748
.clickable(
49+
enabled = isEnabled,
4850
indication = null,
4951
interactionSource = remember { MutableInteractionSource() }
5052
) {
@@ -55,6 +57,7 @@ fun VoteInputSection(
5557
BasicTextField(
5658
value = title,
5759
onValueChange = { if (it.length <= maxOptionLength) onTitleChange(it) },
60+
// enabled = isEnabled,
5861
textStyle = typography.smalltitle_m500_s18_h24.copy(color = colors.White),
5962
modifier = Modifier
6063
.fillMaxWidth(),
@@ -81,11 +84,12 @@ fun VoteInputSection(
8184
onDelete = { onOptionChange(index, "") }, // x 버튼 클릭 시 내용 삭제
8285
onRemoveField = { if (canRemove) onRemoveOption(index) }, // 쓰레기통 클릭 시 항목 제거
8386
canRemove = canRemove,
87+
isEnabled = isEnabled,
8488
hint = stringResource(R.string.vote_content_placeholder)
8589
)
8690
}
8791

88-
if (options.size < maxOptions) {
92+
if (options.size < maxOptions && isEnabled) {
8993
Button(
9094
onClick = {
9195
focusManager.clearFocus() // 항목 추가 시 포커스 해제

app/src/main/java/com/texthip/thip/ui/group/note/screen/GroupVoteCreateScreen.kt

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,22 @@ fun GroupVoteCreateScreen(
4545
recentPage: Int,
4646
totalPage: Int,
4747
isOverviewPossible: Boolean,
48+
postId: Int?,
49+
page: Int?,
50+
isOverview: Boolean?,
51+
title: String?,
52+
options: List<String>?,
4853
onBackClick: () -> Unit,
4954
onNavigateBackWithResult: () -> Unit,
5055
viewModel: GroupVoteCreateViewModel = hiltViewModel()
5156
) {
5257
val uiState by viewModel.uiState.collectAsStateWithLifecycle()
5358

5459
LaunchedEffect(Unit) {
55-
viewModel.initialize(roomId, recentPage, totalPage, isOverviewPossible)
60+
viewModel.initialize(
61+
roomId, recentPage, totalPage, isOverviewPossible,
62+
postId, page, isOverview, title, options
63+
)
5664
}
5765

5866
LaunchedEffect(uiState.isSuccess) {
@@ -85,7 +93,8 @@ fun GroupVoteCreateContent(
8593
) {
8694
Column {
8795
InputTopAppBar(
88-
title = stringResource(R.string.create_vote),
96+
title = if (uiState.isEditMode) stringResource(R.string.edit_vote)
97+
else stringResource(R.string.create_vote),
8998
isRightButtonEnabled = uiState.isFormFilled,
9099
onLeftClick = onBackClick,
91100
onRightClick = { onEvent(GroupVoteCreateEvent.CreateVoteClicked) }
@@ -106,7 +115,8 @@ fun GroupVoteCreateContent(
106115
isEligible = uiState.isGeneralReviewEnabled,
107116
bookTotalPage = uiState.bookTotalPage,
108117
onInfoClick = { showTooltip = true },
109-
onInfoPositionCaptured = { iconCoordinates.value = it }
118+
onInfoPositionCaptured = { iconCoordinates.value = it },
119+
isEnabled = !uiState.isEditMode
110120
)
111121

112122
VoteInputSection(
@@ -119,7 +129,8 @@ fun GroupVoteCreateContent(
119129
onAddOption = { onEvent(GroupVoteCreateEvent.AddOptionClicked) },
120130
onRemoveOption = { index ->
121131
onEvent(GroupVoteCreateEvent.RemoveOptionClicked(index))
122-
}
132+
},
133+
isEnabled = !uiState.isEditMode
123134
)
124135
}
125136
}

0 commit comments

Comments
 (0)