Skip to content

Commit 9e0d75a

Browse files
committed
implemented storing undo updates in DB and handled undo user UI
1 parent 2fb0a7f commit 9e0d75a

File tree

4 files changed

+24
-1
lines changed

4 files changed

+24
-1
lines changed

GoInfoGame/GoInfoGame/DataBase/DBModels/StoredChangeset.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ class StoredChangeset: Object {
4343
@Persisted var point: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: 0.0, longitude: 0.0)
4444
@Persisted var nodes: List<Int64> = List<Int64>()
4545
@Persisted var updatedVersion: Int = -1
46+
@Persisted var isUndoCompleted: Bool = false
47+
@Persisted var undoOn: Date? = nil
4648

4749
public func asOSMWay(isUndo: Bool = false) -> OSMWay {
4850
var storage = originalTags.toDictionary()

GoInfoGame/GoInfoGame/DataBase/DatabaseConnector.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,21 @@ class DatabaseConnector {
440440
}
441441
}
442442

443+
func updateChangesetWithUndoResultSuccess(obj:String) -> StoredChangeset? {
444+
guard let changeset = realm.object(ofType: StoredChangeset.self, forPrimaryKey: obj) else {
445+
return nil
446+
}
447+
do {
448+
try realm.write {
449+
changeset.undoOn = Date()
450+
changeset.isUndoCompleted = true
451+
}
452+
return changeset
453+
} catch (let error){
454+
return nil
455+
}
456+
}
457+
443458
func updateNodeVersion(nodeId: String, version:Int) -> StoredNode?{
444459
let intId = Int(nodeId) ?? -1
445460
guard let theNode = getNode(id: intId, version: .original) else { return nil }

GoInfoGame/GoInfoGame/quests/QuestProtocols.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class QuestBase {
4141
// Convert from ElementType enum to StoredElementEnum
4242
Task.detached(operation: { @MainActor in
4343
let storedElementType: StoredElementEnum = changeSet.elementType
44+
let changesetId = changeSet.id
4445
do {
4546
var result: (result: Bool, version: Int) = (false, -1)
4647
switch storedElementType {
@@ -51,6 +52,11 @@ class QuestBase {
5152
case .unknown:
5253
print("❌ Undo failed: Element type not found")
5354
}
55+
print("undo result \(result)")
56+
DispatchQueue.main.async {
57+
_ = DatabaseConnector.shared.updateChangesetWithUndoResultSuccess(obj: changesetId)
58+
}
59+
5460
}
5561
catch {
5662
print("❌ Undo failed: \(error)")

GoInfoGame/GoInfoGame/quests/QuestUndoManager.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class MapUndoManager {
3838
func getUndoItems() -> [UndoItem] {
3939
var items: [UndoItem] = []
4040

41-
let editedNodes = realm.objects(StoredChangeset.self).filter("changesetId == 0")
41+
let editedNodes = realm.objects(StoredChangeset.self).filter("changesetId == 0 && isUndoCompleted == false")
4242
for edited in editedNodes {
4343
let keys = Array(edited.tags.keys)
4444
if !keys.isEmpty {

0 commit comments

Comments
 (0)