Skip to content

Commit 5cf998d

Browse files
authored
Merge pull request #54 from TaskarCenterAtUW/feature-answer-base
Feature answer base
2 parents 1c736ca + b11372b commit 5cf998d

26 files changed

+290
-81
lines changed

GoInfoGame/GoInfoGame/AppQuestManager.swift

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,10 @@ class AppQuestManager {
6464
if quest.isApplicable(element: node){
6565
// Create a duplicate of the quest
6666
// Create a display Unit
67-
let unit = DisplayUnitWithCoordinate(displayUnit: quest.displayUnit, coordinateInfo: CLLocationCoordinate2D(latitude: node.position.latitude, longitude: node.position.longitude), id: node.id)
67+
let duplicateQuest = quest.copyWithElement(element: node)
68+
let unit = DisplayUnitWithCoordinate(displayUnit: duplicateQuest.displayUnit, coordinateInfo: CLLocationCoordinate2D(latitude: node.position.latitude, longitude: node.position.longitude), id: node.id)
6869
displayUnits.append(unit)
69-
nodeQuests.append(quest)
70+
nodeQuests.append(duplicateQuest)
7071
break
7172
}
7273
}
@@ -77,11 +78,16 @@ class AppQuestManager {
7778
if quest.isApplicable(element: way){
7879
// Create a duplicate of the quest
7980
// Need to add another here.
81+
let duplicateQuest = quest.copyWithElement(element: way)
8082
let position = dbInstance.getCenterForWay(id: String(way.id)) ?? CLLocationCoordinate2D()
81-
let unit = DisplayUnitWithCoordinate(displayUnit: quest.displayUnit, coordinateInfo: position, id: way.id)
83+
let unit = DisplayUnitWithCoordinate(displayUnit: duplicateQuest.displayUnit, coordinateInfo: position, id: way.id)
8284
displayUnits.append(unit)
83-
84-
wayQuests.append(quest)
85+
// if(quest is SideWalkWidth){
86+
// if let q = quest as? SideWalkWidth {
87+
// q.assignAnsweringHandler()
88+
// }
89+
// }
90+
wayQuests.append(duplicateQuest)
8591
break
8692
}
8793
}

GoInfoGame/GoInfoGame/DataBase/DatabaseConnector.swift

Lines changed: 58 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ class DatabaseConnector {
2323
}
2424
}
2525

26+
/**
27+
Saves the OPElements into the database. This method does not remove the old data. However, it updates the
28+
data with same id
29+
@param elements List of OPElement
30+
*/
2631
func saveElements(_ elements: [OPElement]) {
2732
// Save the elements appropriately
2833
// Get the ways and nodes out
@@ -84,7 +89,10 @@ class DatabaseConnector {
8489
print("Elements to be skipped or something happened")
8590
}
8691
}
87-
92+
/**
93+
Earlier implementation of saveElements. This used to save only the Way type of objects.
94+
This is not used anymore
95+
*/
8896
func saveElements(_ elements: [OPWay]) {
8997
do {
9098
try realm.write {
@@ -128,15 +136,25 @@ class DatabaseConnector {
128136
print("Error saving elements to Realm: \(error)")
129137
}
130138
}
131-
139+
/**
140+
Fetches all the StoredNodes in the Database
141+
@returns a Results object containing StoredNodes
142+
*/
132143
func getNodes() -> Results<StoredNode> {
133144
return realm.objects(StoredNode.self)
134145
}
135-
146+
/**
147+
Fetches all the storedWays in the Database
148+
@returns a Results object containing StoredWay
149+
*/
136150
func getWays() -> Results<StoredWay> {
137151
return realm.objects(StoredWay.self)
138152
}
139-
153+
/**
154+
Fetches the center of a given StoredWay
155+
@param id String value of the way ID
156+
@returns CLLocationCoordinate2D the center location
157+
*/
140158
func getCenterForWay(id: String) -> CLLocationCoordinate2D? {
141159
// Get all the objects for the way
142160
guard let way = realm.object(ofType: StoredWay.self, forPrimaryKey: Int(id)) else {
@@ -159,15 +177,28 @@ class DatabaseConnector {
159177
return CLLocationCoordinate2D(latitude: 0, longitude: 0)
160178

161179
}
162-
180+
/**
181+
Fetches a single node from the database
182+
@param id: String value of the node ID
183+
@return StoredNode
184+
*/
163185
func getNode(id:String) -> StoredNode? {
164186
return realm.object(ofType: StoredNode.self, forPrimaryKey: Int(id))
165187
}
166-
188+
/**
189+
Fetches single Way from the database
190+
@param id: String value of the wayId
191+
@return StoredWay
192+
*/
167193
func getWay(id: String) -> StoredWay? {
168194
return realm.object(ofType: StoredWay.self, forPrimaryKey: Int(id))
169195
}
170-
196+
/**
197+
Adds tags to the existing node and stores the same
198+
@param id : String value of the node ID
199+
@param tags [String:String] map of the added tags
200+
@return StoredNode
201+
*/
171202
func addNodeTags(id: String, tags:[String: String]) -> StoredNode? {
172203
guard let theNode = getNode(id: id) else { return nil }
173204
do {
@@ -185,7 +216,12 @@ class DatabaseConnector {
185216
return theNode
186217
}
187218

188-
219+
/**
220+
Adds tags to the existing way and stores the same
221+
@param id: String value of the way ID
222+
@param tags `[String:String]` map of the added tags
223+
@return `StoredWay`
224+
*/
189225
func addWayTags(id: String, tags:[String:String]) -> StoredWay? {
190226
guard let theWay = getWay(id: id) else { return nil }
191227

@@ -196,7 +232,13 @@ class DatabaseConnector {
196232
realm.add(theWay, update: .all) // Test this
197233
return theWay
198234
}
199-
235+
/**
236+
Creates a changeset for an element with specific ID. This does not store the updated nodes. That is to be done separately
237+
- parameter id: String id of the changed element
238+
- parameter type: StoredElementEnum type of the changed element (either way or node)
239+
- parameter tags [String:String] tags changed with this
240+
- Returns: An instance of `StoredChangeset`
241+
*/
200242
func createChangeset(id:String, type: StoredElementEnum, tags:[String:String]) -> StoredChangeset? {
201243
let storedChangeset = StoredChangeset()
202244
storedChangeset.elementId = id
@@ -215,15 +257,20 @@ class DatabaseConnector {
215257
}
216258
return storedChangeset
217259
}
218-
260+
/// Fetches the changeset objects from the database
261+
/// - parameter synced: Optional variable of whether synced or non synced
262+
/// - Returns: an instance of `Results<StoredChangeset>`
219263
func getChangesets(synced: Bool = false) -> Results<StoredChangeset> {
220264

221265
if (synced == true){
222266
return realm.objects(StoredChangeset.self).where({$0.changesetId != -1 })
223267
}
224268
return realm.objects(StoredChangeset.self).where({$0.changesetId == -1 })
225269
}
226-
270+
/// Assigns changesetId for a stored changeset
271+
/// - parameter obj: Internal id for the changeset in the database (unique ID)
272+
/// - parameter changesetId: Assigned changeset ID from the server
273+
/// - Returns updated `StoredChangeset`
227274
func assignChangesetId(obj:String, changesetId: Int) -> StoredChangeset? {
228275
guard let changeset = realm.object(ofType: StoredChangeset.self, forPrimaryKey: obj) else {
229276
return nil

GoInfoGame/GoInfoGame/UI/CustomComponents/YesNoView.swift

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,49 @@
66
//
77

88
import SwiftUI
9+
enum YesNoAnswer {
10+
case yes
11+
case no
12+
case other
13+
case unknown
14+
}
15+
916

1017
struct YesNoView: View {
11-
@State private var showAlert = false
18+
19+
var action: ((_ answer:YesNoAnswer) -> Void)?
20+
21+
init(action: ((_ answer: YesNoAnswer) -> Void)? = nil) {
22+
self.action = action
23+
}
1224
var body: some View {
1325
HStack(spacing: 0) {
1426
Spacer()
1527
Button {
16-
showAlert = true
28+
action?(.other)
1729
} label: {
1830
Text(LocalizedStrings.otherAnswers.localized)
1931
.foregroundColor(.orange).font(.body)
2032
.frame(maxWidth: .infinity)
2133
}
22-
.alert(isPresented: $showAlert) {
23-
Alert(title: Text("More Questions"))
24-
}
2534
.frame(minHeight: 50)
2635
Spacer()
2736
Divider().background(Color.gray).frame(height: 30)
2837
Button(LocalizedStrings.questGenericHasFeatureYes.localized) {
38+
action?(.yes)
2939
}
3040
.foregroundColor(.orange)
3141
.frame(minWidth: 20, maxWidth: 80, minHeight: 50)
3242
Divider().background(Color.gray).frame(height: 30)
33-
Button(LocalizedStrings.questGenericHasFeatureNo.localized) {
43+
Button {
44+
45+
action?(.no)
46+
} label: {
47+
Text(LocalizedStrings.questGenericHasFeatureNo.localized)
48+
.foregroundColor(.orange).font(.body)
49+
.frame(maxWidth: .infinity)
3450
}
35-
.foregroundColor(.orange)
36-
.frame(minWidth: 20, maxWidth: 80, minHeight: 50)
51+
3752
}
3853
}
3954
}

GoInfoGame/GoInfoGame/quests/BusStopLit/BusStopLit.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ class BusStopLit: Quest{
3434
var wikiLink: String = ""
3535
var changesetComment: String = ""
3636
var form: AnyView = AnyView(BusStopLitForm())
37-
var relationData: Any? = nil
37+
var relationData: Element? = nil
3838
func onAnswer(answer: WayLitOrIsStepsAnswer) {
39+
3940
}
4041
var displayUnit: DisplayUnit {
4142
DisplayUnit(title: self.title, description: "",parent: self,sheetSize:.SMALL )
@@ -54,4 +55,10 @@ class BusStopLit: Quest{
5455
return _internalExpression
5556
}
5657
}
58+
59+
func copyWithElement(element: Element) -> any Quest {
60+
let q = BusStopLit()
61+
q.relationData = element
62+
return q
63+
}
5764
}

GoInfoGame/GoInfoGame/quests/BusStopLit/BusStopLitForm.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import Foundation
99
import SwiftUI
1010

1111
struct BusStopLitForm: View, QuestForm {
12-
func applyAnswer(answer: Bool) {
13-
}
12+
var action: ((Bool) -> Void)?
13+
1414
typealias AnswerClass = Bool
1515
var body: some View {
1616
VStack{

GoInfoGame/GoInfoGame/quests/CrossingMarking/CrossMarking.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class CrossMarking :Quest {
2020
var wikiLink: String = ""
2121
var changesetComment: String = ""
2222
var form: AnyView = AnyView(CrossMarkingForm())
23-
var relationData: Any? = nil
23+
var relationData: Element? = nil
2424
var displayUnit: DisplayUnit {
2525
DisplayUnit(title: self.title, description: "",parent: self,sheetSize:.MEDIUM )
2626
}
@@ -38,6 +38,12 @@ class CrossMarking :Quest {
3838
return _internalExpression
3939
}
4040
}
41+
42+
func copyWithElement(element: Element) -> any Quest {
43+
let q = CrossMarking()
44+
q.relationData = element
45+
return q
46+
}
4147
}
4248

4349
enum CrossingAnswer: String, CaseIterable {

GoInfoGame/GoInfoGame/quests/CrossingMarking/CrossMarkingForm.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ import Foundation
99
import SwiftUI
1010

1111
struct CrossMarkingForm : View,QuestForm{
12+
var action: ((CrossingAnswer) -> Void)?
13+
1214
@State private var selectedAnswer: CrossingAnswer?
1315
@State private var showAlert = false
14-
func applyAnswer(answer: CrossingAnswer) {
15-
}
16+
1617
typealias AnswerClass = CrossingAnswer
1718
let items = [
1819
TextItem(value: CrossingAnswer.yes, titleId: LocalizedStrings.questCrossingYes.localized),
@@ -28,7 +29,7 @@ struct CrossMarkingForm : View,QuestForm{
2829
ForEach(items, id: \.titleId) { item in
2930
RadioItem(textItem: item, isSelected: item.value == selectedAnswer) {
3031
selectedAnswer = item.value
31-
applyAnswer(answer: selectedAnswer ?? .no)
32+
action?(selectedAnswer ?? .no)
3233
}
3334
}}.padding(.top,10)
3435
Divider()

GoInfoGame/GoInfoGame/quests/Handrail/HandRail.swift

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import SwiftUI
99
import Foundation
1010
import osmparser
1111

12-
class HandRail: Quest {
12+
class HandRail: QuestBase,Quest {
1313

1414
func onAnswer(answer: Bool) {
1515

@@ -38,9 +38,13 @@ class HandRail: Quest {
3838

3939
var changesetComment: String = "Specify whether steps have handrails"
4040

41-
var form: AnyView = AnyView(HandRailForm())
41+
var form: AnyView {
42+
get{
43+
return AnyView(self.internalForm as! HandRailForm)
44+
}
45+
}
4246

43-
var relationData: Any? = nil
47+
var relationData: Element? = nil
4448

4549
var displayUnit: DisplayUnit {
4650
DisplayUnit(title: self.title, description: "",parent: self,sheetSize: .SMALL)
@@ -57,4 +61,16 @@ class HandRail: Quest {
5761
return _internalExpression
5862
}
5963
}
64+
override init() {
65+
super.init()
66+
self.internalForm = HandRailForm(action: { [self] yesNo in
67+
onAnswer(answer: yesNo)
68+
})
69+
}
70+
71+
func copyWithElement(element: Element) -> any Quest {
72+
let q = HandRail()
73+
q.relationData = element
74+
return q
75+
}
6076
}

GoInfoGame/GoInfoGame/quests/Handrail/HandRailForm.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@
88
import SwiftUI
99

1010
struct HandRailForm: View, QuestForm {
11+
var action: ((Bool) -> Void)?
1112

12-
func applyAnswer(answer: Bool) {
13-
14-
}
1513

1614
typealias AnswerClass = Bool
1715

0 commit comments

Comments
 (0)