Skip to content

Commit 9c061f4

Browse files
committed
fixes to the changeset and undo UI done
- Undo UI is done.
1 parent fae51a7 commit 9c061f4

File tree

9 files changed

+140
-3
lines changed

9 files changed

+140
-3
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/UI/Map/MapView.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,11 @@ struct MapView: View {
391391
case .hideElement(let elementId, let elementName):
392392
shouldShowPolyline = false
393393
viewModel.hideQuest(elementId: elementId, elementName: elementName)
394+
case .undoDone(let changesetId):
395+
shouldShowPolyline = false
396+
showAlert = true
397+
alertMessage = "Changes reverted"
398+
viewModel.refreshMapAfterUndoSumbit(storedChangesetId: changesetId)
394399
}
395400
}
396401
.onReceive(QuestsPublisher.shared.refreshQuest, perform: { _ in
@@ -474,6 +479,7 @@ public enum SheetDismissalScenario {
474479
case synced
475480
case failed(String)
476481
case hideElement(String, String)
482+
case undoDone(String)
477483
}
478484

479485
//TODO: Move to a new file

GoInfoGame/GoInfoGame/UI/Map/MapViewModel.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,19 @@ class MapViewModel: ObservableObject {
158158
// }
159159
}
160160

161+
// The item may have been already resolved or changed.
162+
// Try and add the item to items
163+
func refreshMapAfterUndoSumbit(storedChangesetId: String) {
164+
print("Refreshing map after undo submit")
165+
166+
if let newItem = AppQuestManager.shared.fetchQuestForChangeset(storedChangesetId: storedChangesetId) {
167+
self.items.append(newItem)
168+
} else {
169+
print("No new item got")
170+
}
171+
172+
}
173+
161174
func hideQuest(elementId: String, elementName: String) {
162175
print(elementId)
163176
HiddenQuestManager.shared.hideQuest(elementId: elementId, elementName: elementName, items: &items)

GoInfoGame/GoInfoGame/data/DatasyncManager.swift

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,15 @@ class DatasyncManager {
278278
localWay.tags[key] = value
279279
}
280280
let id = localWay.id
281-
let tags = localWay.tags
281+
var tags = localWay.tags
282+
// The gig tags are not added into the db when pushing directly
283+
// Adding them forcibly here.
284+
if (!exclude_gig_tags) {
285+
let gig_internal_tags = localWay.fetchInternalGigTags()
286+
gig_internal_tags.forEach { (key: String, value: String) in
287+
tags[key] = value
288+
}
289+
}
282290
DispatchQueue.main.async {
283291
_ = DatabaseConnector.shared.addWayTags(id: id, tags: tags, version: newVersion)
284292
}
@@ -324,8 +332,18 @@ class DatasyncManager {
324332
updatedNode.tags[key] = value
325333
}
326334
updatedNode.version = newVersion
335+
336+
// Gig tags are not added to DB when pushing changes
337+
// those are added manually here
338+
if (!exclude_gig_tags) {
339+
let gig_internal_tags = updatedNode.fetchInternalGigTags()
340+
gig_internal_tags.forEach { (key: String, value: String) in
341+
updatedNode.tags[key] = value
342+
}
343+
}
327344
SyncLogger.shared.logStep("Node Updated ----\(updatedNode.tags)")
328345
DispatchQueue.main.async {
346+
329347
_ = DatabaseConnector.shared.addNodeTags(id: updatedNode.id, tags: updatedNode.tags, version: newVersion)
330348
}
331349
continuation.resume(returning: newVersion)
@@ -418,6 +436,8 @@ class DatasyncManager {
418436
return try await updateNode(node: mergedNode, exclude_gig_tags: exclude_gig_tags)
419437
} else {
420438
print("Undo operation is not possible")
439+
// update the original node with the server node.
440+
// FIXME: this is not done. Need to do something.
421441
return fetchedResult.version
422442
}
423443

GoInfoGame/GoInfoGame/quests/QuestProtocols.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class QuestBase {
4848
func updateUndoTags(changeSet: StoredChangeset) {
4949
// Convert from ElementType enum to StoredElementEnum
5050
Task.detached(operation: { @MainActor in
51+
MapViewPublisher.shared.dismissSheet.send(.syncing)
5152
let storedElementType: StoredElementEnum = changeSet.elementType
5253
let changesetId = changeSet.id
5354
do {
@@ -64,7 +65,9 @@ class QuestBase {
6465
DispatchQueue.main.async {
6566
_ = DatabaseConnector.shared.updateChangesetWithUndoResultSuccess(obj: changesetId)
6667
}
67-
68+
MapViewPublisher.shared.dismissSheet.send(.synced)
69+
// MapViewPublisher.shared.dismissSheet.send(.submitted(""))
70+
MapViewPublisher.shared.dismissSheet.send(.undoDone(changesetId))
6871
}
6972
catch {
7073
print("❌ Undo failed: \(error)")

GoInfoGame/GoInfoGame/quests/QuestUndoManager.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class MapUndoManager {
4646
}
4747
}
4848

49+
//FIXME: This is completely removed
4950
extension MapUndoManager {
5051
// func finalizeSuccessfulSubmit(id: Int, type: ElementType) {
5152
// let realm = try! Realm()
@@ -93,6 +94,7 @@ extension Map where Key == String, Value == String {
9394
}
9495

9596
extension StoredElementEnum {
97+
//FIXME: this is shown twice
9698
func toElementType() -> ElementType? {
9799
switch self {
98100
case .node: return .node

GoInfoGame/GoInfoGame/samplepoint.gpx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
waypoint, Xcode will simulate that specific location. If you provide multiple waypoints,
77
Xcode will simulate a route visiting each waypoint.
88
-->
9-
<wpt lat="17.4620366048918" lon="78.380188562477">
9+
<wpt lat="47.616061465392995" lon="-122.23018339129734">
1010
<name>Test Data</name>
1111

1212
<!--

GoInfoGame/osmapi/models/OSMNode.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,26 @@ public struct OSMNode: Codable, OSMPayload, OSMElement, OSMCreatePayload {
5252

5353
}
5454

55+
public func fetchInternalGigTags() -> [String:String] {
56+
57+
var internalTags:[String:String] = [:]
58+
let dateFormatter = DateFormatter()
59+
60+
// Set the date format to "yyyy-MM-dd"
61+
dateFormatter.dateFormat = "yyyy-MM-dd"
62+
63+
// Create a Date object (for example, the current date)
64+
let currentDate = Date()
65+
66+
// Convert the Date object to a formatted string
67+
let formattedDate = dateFormatter.string(from: currentDate)
68+
//"ext:gig_last_updated"
69+
internalTags["ext:gig_last_updated"] = formattedDate
70+
internalTags["ext:gig_complete"] = "yes"
71+
72+
return internalTags
73+
}
74+
5575
///
5676
public func toPayload(exclude_gig_tags: Bool = false) -> String {
5777
var osmNode = "<modify>"

GoInfoGame/osmapi/models/OSMWay.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,28 @@ public struct OSMWayResponse: Codable {
1919
public struct OSMWay: Codable, OSMPayload, OSMElement {
2020
public var isInteresting: Bool? = false
2121
public var isSkippable: Bool? = false
22+
23+
public func fetchInternalGigTags() -> [String:String] {
24+
25+
var internalTags:[String:String] = [:]
26+
let dateFormatter = DateFormatter()
27+
28+
// Set the date format to "yyyy-MM-dd"
29+
dateFormatter.dateFormat = "yyyy-MM-dd"
30+
31+
// Create a Date object (for example, the current date)
32+
let currentDate = Date()
33+
34+
// Convert the Date object to a formatted string
35+
let formattedDate = dateFormatter.string(from: currentDate)
36+
//"ext:gig_last_updated"
37+
internalTags["ext:gig_last_updated"] = formattedDate
38+
internalTags["ext:gig_complete"] = "yes"
39+
40+
return internalTags
41+
}
42+
43+
2244
public func toPayload(exclude_gig_tags: Bool = false) -> String {
2345
var osmNode = "<modify>"
2446
let xmlBuilder = OSMXMLBuilder(rootName: "way")

0 commit comments

Comments
 (0)