Skip to content

Commit fae51a7

Browse files
committed
updating nods and ways DB with updated tags and version
1 parent 4e56cce commit fae51a7

File tree

2 files changed

+19
-74
lines changed

2 files changed

+19
-74
lines changed

GoInfoGame/GoInfoGame/DataBase/DatabaseConnector.swift

Lines changed: 11 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -248,54 +248,23 @@ class DatabaseConnector {
248248
@param tags `[String:String]` map of the added tags
249249
@return `StoredWay`
250250
*/
251-
func addWayTags(id: String, tags: [String: String]) -> StoredWay? {
252-
let intId = Int(id) ?? -1
251+
func addWayTags(id: Int, tags: [String: String], version: Int) -> StoredWay? {
253252

254253
// Step 1: Try to get the editable copy first
255-
if let editable = getWay(id: intId, version: .edited) {
254+
if let editable = getWay(id: id, version: .original) {
256255
// Step 2: Update the existing editable copy
257256
do {
258257
try realm.write {
258+
editable.tags.removeAll()
259259
tags.forEach { editable.tags[$0.key] = $0.value }
260+
editable.version = version
260261
}
261262
} catch {
262263
print("Error while writing tags")
263264
}
264265
return editable
265266
}
266-
267-
// Step 3: If not found, create from original
268-
guard let original = getWay(id: intId, version: .original) else {
269-
print("❌ Original not found")
270-
return nil
271-
}
272-
273-
let copy = StoredWay()
274-
copy.id = original.id
275-
copy.version = original.version
276-
copy.timestamp = original.timestamp
277-
278-
original.tags.forEach { entry in
279-
copy.tags[entry.key] = entry.value
280-
}
281-
282-
copy.nodes.append(objectsIn: original.nodes)
283-
copy.polyline.append(objectsIn: original.polyline)
284-
copy.isOriginal = false
285-
copy.generateCompoundId()
286-
287-
// Step 4: Apply the new tags to this new editable copy
288-
tags.forEach { copy.tags[$0.key] = $0.value }
289-
290-
do {
291-
try realm.write {
292-
realm.add(copy)
293-
}
294-
} catch {
295-
print("Error while saving editable copy")
296-
}
297-
298-
return copy
267+
return nil
299268
}
300269

301270

@@ -305,56 +274,24 @@ class DatabaseConnector {
305274
@param tags [String:String] map of the added tags
306275
@return StoredNode
307276
*/
308-
func addNodeTags(id: String, tags: [String: String]) -> StoredNode? {
309-
let intId = Int(id) ?? -1
310-
print("🟣 addNodeTags called for id: \(intId) with tags: \(tags)")
277+
func addNodeTags(id: Int, tags: [String: String], version: Int) -> StoredNode? {
278+
print("🟣 addNodeTags called for id: \(id) with tags: \(tags)")
311279

312-
if let editable = getNode(id: intId, version: .edited) {
280+
if let editable = getNode(id: id, version: .original) {
313281
print("✏️ Editable node exists: \(editable.compoundId)")
314282
do {
315283
try realm.write {
284+
editable.tags.removeAll()
316285
tags.forEach { editable.tags[$0.key] = $0.value }
286+
editable.version = version
317287
}
318288
print("✅ Updated editable node.")
319289
} catch {
320290
print("❌ Error while writing node tags: \(error)")
321291
}
322292
return editable
323293
}
324-
325-
guard let original = getNode(id: intId, version: .original) else {
326-
print("❌ Original node not found for id: \(intId)")
327-
return nil
328-
}
329-
330-
print("📄 Creating editable copy from original: \(original.compoundId)")
331-
332-
let copy = StoredNode()
333-
copy.id = original.id
334-
copy.version = original.version
335-
copy.timestamp = original.timestamp
336-
copy.point = original.point
337-
copy.isOriginal = false
338-
copy.generateCompoundId()
339-
340-
print("🛠️ New compoundId: \(copy.compoundId)")
341-
342-
original.tags.forEach { entry in
343-
copy.tags[entry.key] = entry.value
344-
}
345-
346-
tags.forEach { copy.tags[$0.key] = $0.value }
347-
348-
do {
349-
try realm.write {
350-
realm.add(copy)
351-
}
352-
print("✅ Editable node copy saved to DB: \(copy.compoundId)")
353-
} catch {
354-
print("❌ Error while saving editable node copy: \(error)")
355-
}
356-
357-
return copy
294+
return nil
358295
}
359296

360297

GoInfoGame/GoInfoGame/data/DatasyncManager.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,11 @@ class DatasyncManager {
277277
localWay.tags.forEach { (key: String, value: String) in
278278
localWay.tags[key] = value
279279
}
280+
let id = localWay.id
281+
let tags = localWay.tags
282+
DispatchQueue.main.async {
283+
_ = DatabaseConnector.shared.addWayTags(id: id, tags: tags, version: newVersion)
284+
}
280285
continuation.resume(returning: newVersion)
281286

282287
case .failure(let error):
@@ -320,6 +325,9 @@ class DatasyncManager {
320325
}
321326
updatedNode.version = newVersion
322327
SyncLogger.shared.logStep("Node Updated ----\(updatedNode.tags)")
328+
DispatchQueue.main.async {
329+
_ = DatabaseConnector.shared.addNodeTags(id: updatedNode.id, tags: updatedNode.tags, version: newVersion)
330+
}
323331
continuation.resume(returning: newVersion)
324332
case .failure(let error):
325333
print(error)

0 commit comments

Comments
 (0)