Skip to content

Commit 9ef7227

Browse files
author
Achyut Kumar M
committed
deselect an already selected option
1 parent 1c4e80b commit 9ef7227

File tree

1 file changed

+26
-15
lines changed

1 file changed

+26
-15
lines changed

GoInfoGame/GoInfoGame/quests/LongQuests/Components/QuestOptions.swift

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,19 @@ struct QuestOptions: View {
8989
LazyVGrid(columns: columns, spacing: 16) {
9090
ForEach(options, id: \.id) { option in
9191
Button(action: {
92-
selectedAnswerId = option.id
93-
onChoiceSelected(option)
92+
if selectedAnswerId == option.id {
93+
// Deselect
94+
selectedAnswerId = nil
95+
currentAnswer = nil
96+
selectedImageURL = nil
97+
selectedImageText = nil
98+
// Don't call onChoiceSelected if you want to ignore blank assignment
99+
} else {
100+
// Select
101+
selectedAnswerId = option.id
102+
currentAnswer = option.value
103+
onChoiceSelected(option)
104+
}
94105
}) {
95106
VStack(spacing: 8) {
96107
if let imageUrl = option.imageURL, !imageUrl.isEmpty {
@@ -104,7 +115,6 @@ struct QuestOptions: View {
104115
.onLongPressGesture {
105116
selectedImageURL = imageUrl
106117
selectedImageText = option.choiceText
107-
108118
}
109119
} else {
110120
ZStack {
@@ -113,18 +123,17 @@ struct QuestOptions: View {
113123
.scaledToFill()
114124
.frame(width: 100, height: 100)
115125
.clipped()
116-
//add grey border to image
117126
.overlay(
118127
RoundedRectangle(cornerRadius: 8)
119128
.stroke(Color.gray, lineWidth: 1)
120129
)
121130

122131
ZStack {
123132
let strokeOffsets: [(CGFloat, CGFloat)] = [
124-
(-1, -1), (1, -1),
125-
(-1, 1), (1, 1),
126-
(0, -1), (0, 1),
127-
(-1, 0), (1, 0)
133+
(-1, -1), (1, -1),
134+
(-1, 1), (1, 1),
135+
(0, -1), (0, 1),
136+
(-1, 0), (1, 0)
128137
]
129138

130139
ForEach(0..<strokeOffsets.count, id: \.self) { i in
@@ -141,20 +150,22 @@ struct QuestOptions: View {
141150
.shadow(color: Color.black.opacity(0.7), radius: 4, x: 0, y: 2)
142151
}
143152
}
144-
145153
}
146154
}
147155
}
148-
.overlay(
149-
RoundedRectangle(cornerRadius: 8)
150-
.stroke(currentAnswer == option.value ? Color.blue : Color.clear, lineWidth: 3)
151-
)
156+
.overlay(
157+
RoundedRectangle(cornerRadius: 8)
158+
.stroke(currentAnswer == option.value ? Color.blue : Color.clear, lineWidth: 3)
159+
)
160+
152161
}
153162
}
163+
154164

155165
// Place follow-up button separately after the grid
156-
if let selected = options.first(where: { $0.id == selectedAnswerId }),
157-
selected.choiceFollowUp != nil {
166+
if let selected = selectedAnswerId.flatMap({ id in
167+
options.first(where: { $0.id == id && $0.choiceFollowUp != nil })
168+
}) {
158169
Button(action: {
159170
uploadPhoto(true)
160171
}) {

0 commit comments

Comments
 (0)