Skip to content

Commit b67ac0d

Browse files
authored
Merge pull request #252 from TaskarCenterAtUW/feature-unit-test-for-x
Undo feature implememtation
2 parents 68120bc + 9715e34 commit b67ac0d

22 files changed

+528
-309
lines changed

GoInfoGame/GoInfoGame/AppQuestManager.swift

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import Foundation
1010
import osmparser
1111
import MapKit
1212
import osmapi
13+
import RealmSwift
1314

1415

1516
// Class that handles data handling and display of annotations
@@ -135,6 +136,56 @@ class AppQuestManager {
135136
let unitsToBeDisplayed = displayUnits.filter { !hiddenIds.contains($0.id) }
136137
return unitsToBeDisplayed
137138
}
139+
140+
// FIXME: Make this function better
141+
// Fetches the quest for a specific changeset based on the element
142+
// If there is no need to show the element after undo, it will return nil
143+
func fetchQuestForChangeset(storedChangesetId: String) -> DisplayUnitWithCoordinate? {
144+
if let changeset = dbInstance.getChangeset(for: storedChangesetId) {
145+
// Get the element based on the changeset
146+
let storedElementId = changeset.elementId
147+
let storedElementType = changeset.elementType
148+
var parserElement: osmparser.Element? = nil
149+
if (storedElementType == .node) {
150+
let storedElement = dbInstance.getNode(id: storedElementId, version: .original)
151+
parserElement = storedElement?.asNode()
152+
}
153+
else if (storedElementType == .way){
154+
let storedElement = dbInstance.getWay(id: storedElementId, version: .original)
155+
parserElement = storedElement?.asWay()
156+
}
157+
if let parserElement = parserElement {
158+
let allQuests = QuestsRepository.shared.applicableQuests
159+
for quest in allQuests {
160+
if quest.quest.filter.isEmpty {
161+
continue
162+
}
163+
if quest.quest.isApplicable(element: parserElement){
164+
let duplicateQuest = quest.quest.copyWithElement(element: parserElement)
165+
// Assign stuff based on the type of element
166+
if parserElement.type == .node {
167+
if let nodObj = parserElement as? osmparser.Node {
168+
let unit = DisplayUnitWithCoordinate(displayUnit: duplicateQuest.displayUnit, coordinateInfo: CLLocationCoordinate2D(latitude: nodObj.position.latitude, longitude: nodObj.position.longitude), id: nodObj.id, isHidden: false)
169+
return unit
170+
}
171+
172+
}
173+
else if parserElement.type == .way {
174+
let position = dbInstance.getCenterForWay(id: String(parserElement.id)) ?? CLLocationCoordinate2D()
175+
let unit = DisplayUnitWithCoordinate(displayUnit: duplicateQuest.displayUnit, coordinateInfo: position, id: parserElement.id, isHidden: false)
176+
return unit
177+
}
178+
179+
}
180+
}
181+
182+
}
183+
184+
185+
186+
}
187+
return nil
188+
}
138189
}
139190

140191

GoInfoGame/GoInfoGame/DataBase/DBModels/StoredChangeset.swift

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,78 @@ import Foundation
99

1010
import RealmSwift
1111
import osmparser
12+
import CoreLocation
13+
import osmapi
1214

1315
enum StoredElementEnum: String, PersistableEnum {
1416
case node
1517
case way
1618
case unknown
19+
20+
func elementType() -> ElementType {
21+
switch self {
22+
case .node:
23+
return .node
24+
case .way:
25+
return .way
26+
default:
27+
return .node
28+
}
29+
}
1730
}
1831

1932
// Represents one stored way
2033
class StoredChangeset: Object {
2134
@Persisted(primaryKey: true) var id: String = UUID().uuidString // Internal ID
2235
// Type of change -> can be node or way
2336
@Persisted var elementType : StoredElementEnum = .unknown
24-
@Persisted var elementId: String = "" // The ID of the element
37+
@Persisted var elementId: Int // The ID of the element
2538
@Persisted var tags = Map<String,String>()
39+
@Persisted var originalTags = Map<String,String>()
2640
@Persisted var changesetId: Int = -1 // Initial value of changeset // To be figured out later as index
2741
@Persisted var timestamp : String = "" // User time stamp
42+
@Persisted var version: Int = 0
43+
@Persisted var point: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: 0.0, longitude: 0.0)
44+
@Persisted var nodes: List<Int64> = List<Int64>()
45+
@Persisted var updatedVersion: Int = -1
46+
@Persisted var isUndoCompleted: Bool = false
47+
@Persisted var undoOn: Date? = nil
48+
49+
public func asOSMWay(isUndo: Bool = false) -> OSMWay {
50+
var storage = originalTags.toDictionary()
51+
if !isUndo {
52+
for tag in tags {
53+
storage[tag.key] = tag.value
54+
}
55+
}
56+
let nodes = Array(nodes.map { Int($0) })
57+
return OSMWay(type: "way",
58+
id: elementId,
59+
timestamp: Date(),
60+
version: isUndo ? updatedVersion : version,
61+
changeset: -1,
62+
user: "",
63+
uid: -1,
64+
nodes: nodes,
65+
tags: storage)
66+
}
67+
68+
public func asOSMNode(isUndo: Bool = false) -> OSMNode {
69+
var storage = originalTags.toDictionary()
70+
if !isUndo {
71+
for tag in tags {
72+
storage[tag.key] = tag.value
73+
}
74+
}
75+
return OSMNode(type: "node",
76+
id: elementId,
77+
lat: point.latitude,
78+
lon: point.longitude,
79+
timestamp: Date(),
80+
version: isUndo ? updatedVersion : version,
81+
changeset: -1,
82+
user: "",
83+
uid: -1,
84+
tags: storage)
85+
}
2886
}

GoInfoGame/GoInfoGame/DataBase/DBModels/StoredElement.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class StoredElement : Object {
1818
public func asNode() -> Node {
1919
let position = LatLon(latitude: 0.0, longitude: 0.0)
2020
var theTags: [String:String] = [:]
21-
for (key,value) in tags.asKeyValueSequence(){
21+
for (key,value) in tags{
2222
theTags[key] = value
2323
}
2424
let n = Node(id: Int64(id), version: version, tags: theTags, timestampEdited: 0, position: position)

GoInfoGame/GoInfoGame/DataBase/DBModels/StoredNode.swift

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,10 @@ class StoredNode : Object {
3131
public func asNode() -> Node {
3232
let position = LatLon(latitude: point.latitude , longitude: point.longitude)
3333
var theTags: [String:String] = [:]
34-
for (key,value) in tags.asKeyValueSequence(){
34+
for (key,value) in tags{
3535
theTags[key] = value
3636
}
3737
let n = Node(id: Int64(id), version: version, tags: theTags, timestampEdited: 0, position: position)
3838
return n
3939
}
40-
41-
public func asOSMNode() -> OSMNode {
42-
var storage = [String: String]()
43-
for tag in tags {
44-
storage[tag.key] = tag.value
45-
}
46-
return OSMNode(type: "node", id: id, lat: point.latitude, lon: point.longitude, timestamp: Date(), version: version, changeset: -1, user: "", uid: -1, tags: storage)
47-
}
4840
}

GoInfoGame/GoInfoGame/DataBase/DBModels/StoredWay.swift

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class StoredWay: Object {
4444

4545
public func asWay() -> Way {
4646
var theTags: [String:String] = [:]
47-
for (key,value) in tags.asKeyValueSequence(){
47+
for (key,value) in tags{
4848
theTags[key] = value
4949
}
5050
let nodeList:[Int64] = nodes.map({$0})
@@ -59,13 +59,4 @@ class StoredWay: Object {
5959
way.polyline = latLong
6060
return way
6161
}
62-
63-
public func asOSMWay() -> OSMWay {
64-
var storage = [String: String]()
65-
for tag in tags {
66-
storage[tag.key] = tag.value
67-
}
68-
let nodes = Array(nodes.map { Int($0) })
69-
return OSMWay(type: "way", id: id, timestamp: Date(), version: version, changeset: -1, user: "", uid: -1, nodes: nodes, tags: storage)
70-
}
7162
}

0 commit comments

Comments
 (0)