Skip to content

Commit b11372b

Browse files
committed
Updated code to make it far easier.
Easier to send the data
1 parent 392e6cd commit b11372b

25 files changed

+180
-68
lines changed

GoInfoGame/GoInfoGame/AppQuestManager.swift

Lines changed: 6 additions & 4 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,15 +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)
8385
// if(quest is SideWalkWidth){
8486
// if let q = quest as? SideWalkWidth {
8587
// q.assignAnsweringHandler()
8688
// }
8789
// }
88-
wayQuests.append(quest)
90+
wayQuests.append(duplicateQuest)
8991
break
9092
}
9193
}

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/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: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ import SwiftUI
1111
struct BusStopLitForm: View, QuestForm {
1212
var action: ((Bool) -> Void)?
1313

14-
func applyAnswer(answer: Bool) {
15-
}
1614
typealias AnswerClass = Bool
1715
var body: some View {
1816
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: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ struct CrossMarkingForm : View,QuestForm{
1313

1414
@State private var selectedAnswer: CrossingAnswer?
1515
@State private var showAlert = false
16-
func applyAnswer(answer: CrossingAnswer) {
17-
}
16+
1817
typealias AnswerClass = CrossingAnswer
1918
let items = [
2019
TextItem(value: CrossingAnswer.yes, titleId: LocalizedStrings.questCrossingYes.localized),
@@ -30,7 +29,7 @@ struct CrossMarkingForm : View,QuestForm{
3029
ForEach(items, id: \.titleId) { item in
3130
RadioItem(textItem: item, isSelected: item.value == selectedAnswer) {
3231
selectedAnswer = item.value
33-
applyAnswer(answer: selectedAnswer ?? .no)
32+
action?(selectedAnswer ?? .no)
3433
}
3534
}}.padding(.top,10)
3635
Divider()

GoInfoGame/GoInfoGame/quests/Handrail/HandRail.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class HandRail: QuestBase,Quest {
4444
}
4545
}
4646

47-
var relationData: Any? = nil
47+
var relationData: Element? = nil
4848

4949
var displayUnit: DisplayUnit {
5050
DisplayUnit(title: self.title, description: "",parent: self,sheetSize: .SMALL)
@@ -67,4 +67,10 @@ class HandRail: QuestBase,Quest {
6767
onAnswer(answer: yesNo)
6868
})
6969
}
70+
71+
func copyWithElement(element: Element) -> any Quest {
72+
let q = HandRail()
73+
q.relationData = element
74+
return q
75+
}
7076
}

GoInfoGame/GoInfoGame/quests/Handrail/HandRailForm.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@ struct HandRailForm: View, QuestForm {
1111
var action: ((Bool) -> Void)?
1212

1313

14-
func applyAnswer(answer: Bool) {
15-
16-
}
17-
1814
typealias AnswerClass = Bool
1915

2016

GoInfoGame/GoInfoGame/quests/QuestProtocols.swift

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,35 @@ protocol Quest {
1919
var wikiLink: String {get}
2020
var changesetComment: String {get}
2121
var form : AnyView {get}
22-
var relationData : Any? {get set}
22+
var relationData : Element? {get set}
2323
func onAnswer(answer:AnswerClass)
24-
2524
var displayUnit: DisplayUnit { get}
2625
var filterExpression : ElementFilterExpression? { get }
26+
27+
func copyWithElement(element: Element) -> any Quest // Not sure.
2728
}
2829

2930
class QuestBase {
3031
public var internalForm: (any QuestForm)? = nil
32+
// Add a custom implementation
33+
34+
public func updateTags(id: Int64, tags:[String:String], type: ElementType){
35+
// Convert from ElementType enum to StoredElementEnum
36+
let storedElementType: StoredElementEnum = type == .way ? .way : .node
37+
let storedId = String(id)
38+
// Create a changeset
39+
let newChangeset = DatabaseConnector.shared.createChangeset(id: storedId, type: storedElementType, tags: tags)
40+
switch (storedElementType){
41+
case .way:
42+
DatabaseConnector.shared.addWayTags(id: storedId, tags: tags)
43+
case .node:
44+
DatabaseConnector.shared.addNodeTags(id: storedId, tags: tags)
45+
case .unknown:
46+
print("Unknown Stored element type received")
47+
}
48+
// Sync using datasyncmanager
49+
50+
}
3151

3252
}
3353
// Adds default method and implementation
@@ -49,6 +69,7 @@ extension Quest {
4969

5070
return try? filter.toElementFilterExpression() // This is a costly operation
5171
}
72+
5273
}
5374

5475

@@ -63,8 +84,6 @@ struct DisplayUnit : Identifiable {
6384
protocol QuestForm {
6485
associatedtype AnswerClass
6586

66-
func applyAnswer(answer:AnswerClass)
67-
6887
var action: ((_ answer:AnswerClass)->Void)? {get set}
6988
}
7089

GoInfoGame/GoInfoGame/quests/SideWalkValidation/SideWalkValidation.swift

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

88
import Foundation
99
import SwiftUI
10+
import osmparser
1011

1112
class SideWalkValidation :Quest {
1213
func onAnswer(answer: SideWalkValidationAnswer) {
@@ -18,10 +19,16 @@ class SideWalkValidation :Quest {
1819
var wikiLink: String = ""
1920
var changesetComment: String = ""
2021
var form : AnyView = AnyView(SideWalkValidationForm())
21-
var relationData: Any? = nil
22+
var relationData: Element? = nil
2223
var displayUnit: DisplayUnit {
2324
DisplayUnit(title: self.title, description: "",parent: self,sheetSize:.LARGE )
2425
}
26+
27+
func copyWithElement(element: Element) -> any Quest {
28+
let q = SideWalkValidation()
29+
q.relationData = element
30+
return q
31+
}
2532
}
2633
enum SideWalkValidationAnswer {
2734
case left

0 commit comments

Comments
 (0)