Skip to content

Commit 7943508

Browse files
authored
Merge pull request #274 from TaskarCenterAtUW/Bug-2072-Cannot-select-some-answer-choices
Bug 2072 cannot select some answer choices
2 parents ccfdbd4 + 38fe145 commit 7943508

File tree

3 files changed

+41
-7
lines changed

3 files changed

+41
-7
lines changed

GoInfoGame/GoInfoGame/Helpers/Extensions.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import MapKit
1010
import RealmSwift
1111
import ARKit
1212
import SwiftUI
13+
import Combine
1314

1415

1516
// Extension to check if a polyline intersects with a coordinate
@@ -161,4 +162,27 @@ extension UIApplication {
161162
static func window() -> UIWindow? {
162163
return UIApplication.shared.connectedScenes.flatMap { ($0 as? UIWindowScene)?.windows ?? [] }.first { $0.isKeyWindow }
163164
}
165+
166+
var safeAreaBottomInset: CGFloat {
167+
guard let windowScene = connectedScenes.first as? UIWindowScene,
168+
let window = windowScene.windows.first(where: \.isKeyWindow) else {
169+
return 0
170+
}
171+
return window.safeAreaInsets.bottom
172+
}
173+
}
174+
175+
extension Publishers {
176+
static var keyboardHeight: AnyPublisher<CGFloat, Never> {
177+
let willShow = NotificationCenter.default
178+
.publisher(for: UIResponder.keyboardWillShowNotification)
179+
.map { ($0.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect)?.height ?? 0 }
180+
181+
let willHide = NotificationCenter.default
182+
.publisher(for: UIResponder.keyboardWillHideNotification)
183+
.map { _ in CGFloat(0) }
184+
185+
return MergeMany(willShow, willHide)
186+
.eraseToAnyPublisher()
187+
}
164188
}

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,6 @@ struct QuestOptions: View {
112112
label: option.choiceText,
113113
isSelected: currentAnswer == option.value
114114
)
115-
.onLongPressGesture {
116-
selectedImageURL = imageUrl
117-
selectedImageText = option.choiceText
118-
}
119115
} else {
120116
ZStack {
121117
Image("no_image")
@@ -161,7 +157,12 @@ struct QuestOptions: View {
161157
RoundedRectangle(cornerRadius: 8)
162158
.stroke(currentAnswer == option.value ? Color.blue : Color.clear, lineWidth: 3)
163159
)
164-
160+
.onLongPressGesture {
161+
if let imageUrl = option.imageURL, !imageUrl.isEmpty {
162+
selectedImageURL = imageUrl
163+
selectedImageText = option.choiceText
164+
}
165+
}
165166
}
166167
}
167168

@@ -222,7 +223,7 @@ struct QuestOptions: View {
222223
}
223224

224225
#Preview {
225-
QuestOptions(options: [QuestAnswerChoice(value: "yes", choiceText: "Yes, this roadway can be crossed safely.", imageURL: nil, choiceFollowUp: nil),
226+
QuestOptions(options: [QuestAnswerChoice(value: "asphalt", choiceText: "Asphalt", imageURL: "https://raw.githubusercontent.com/TaskarCenterAtUW/tdei-tools/refs/heads/main/images/sidewalk/surface/asphalt_landscape.png", choiceFollowUp: nil),
226227
QuestAnswerChoice(value: "no", choiceText: "No, this roadway is too wide to cross safely.", imageURL: nil, choiceFollowUp: nil)], selectedAnswerId: .constant(UUID()), onChoiceSelected: { qa in
227228

228229
}, questType: GoInfoGame.QuestType.exclusiveChoice, currentAnswer: .constant("Binding<String?>")) { s in

GoInfoGame/GoInfoGame/quests/LongQuests/View/LongForm.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import SwiftUI
99
import CoreLocation
10-
10+
import Combine
1111

1212
struct LongForm: View, QuestForm {
1313

@@ -58,6 +58,8 @@ struct LongForm: View, QuestForm {
5858

5959
@StateObject private var noteViewModel = NotesViewModel()
6060

61+
@State private var keyboardHeight: CGFloat = 0
62+
6163
var body: some View {
6264
ZStack {
6365
VStack(alignment: .leading) {
@@ -192,6 +194,13 @@ struct LongForm: View, QuestForm {
192194
Text("No Quests available")
193195
}
194196
}
197+
.padding(.bottom, keyboardHeight)
198+
.onReceive(Publishers.keyboardHeight) { height in
199+
let safeAreaBottom = UIApplication.shared.safeAreaBottomInset
200+
withAnimation {
201+
keyboardHeight = max(0, height - safeAreaBottom)
202+
}
203+
}
195204
}
196205
.hideKeyboardOnTap()
197206

0 commit comments

Comments
 (0)