From 168a057a2a706fa95f56473d3488d493e1a820cc Mon Sep 17 00:00:00 2001 From: saint Date: Tue, 28 May 2024 16:15:07 -0400 Subject: [PATCH] okay so working when you manually save ribbon need to make it save position when you switch ribbon instead of manual save but good progress --- SelectedRibbon.swift | 4 ++-- SelectedRibbonRequest.swift | 11 +++++++++-- gloss/AppDatabase.swift | 4 ++-- gloss/ContentView.swift | 2 +- 4 files changed, 14 insertions(+), 7 deletions(-) diff --git a/SelectedRibbon.swift b/SelectedRibbon.swift index 745ad58..0e16832 100644 --- a/SelectedRibbon.swift +++ b/SelectedRibbon.swift @@ -17,7 +17,7 @@ struct SelectedRibbon: Identifiable, Equatable { /// Int64 is the recommended type for auto-incremented database ids. /// Use nil for players that are not inserted yet in the database. var id: Int64? - var ribbonId: Int64 + var ribbonGroupId: Int64 } extension SelectedRibbon { @@ -32,7 +32,7 @@ extension SelectedRibbon: Codable, FetchableRecord, MutablePersistableRecord { // Define database columns from CodingKeys fileprivate enum Columns { static let id = Column(CodingKeys.id) - static let ribbonId = Column(CodingKeys.ribbonId) + static let ribbonGroupId = Column(CodingKeys.ribbonGroupId) } /// Updates a player id after it has been inserted in the database. diff --git a/SelectedRibbonRequest.swift b/SelectedRibbonRequest.swift index 76a054d..b8ce4b5 100644 --- a/SelectedRibbonRequest.swift +++ b/SelectedRibbonRequest.swift @@ -52,8 +52,15 @@ struct SelectedRibbonRequest: Queryable { // print(ret3) // print("FETCH JOIN RIBBON") - var ret = try Ribbon.fetchAll(db, sql: "SELECT Ribbon.* FROM SelectedRibbon join Ribbon on SelectedRibbon.ribbonId = ribbon.rowId WHERE SelectedRibbon.rowId = 1") // [Player] - // print(ret) + var ret = try Ribbon.fetchAll(db, sql: """ + SELECT Ribbon.* FROM SelectedRibbon \ + JOIN (select distinct r1.* from Ribbon r1 join Ribbon r2 ON \ + r1.undoLevel = r2.currentLevel AND r1.id = r2.id ORDER BY pos ASC) as Ribbon \ + ON SelectedRibbon.ribbonGroupId = Ribbon.groupId \ + WHERE SelectedRibbon.rowId = 1 + """) + // [Player] + print("Selected Ribbon query result: \(ret)") return ret } } diff --git a/gloss/AppDatabase.swift b/gloss/AppDatabase.swift index dc294d7..08091da 100644 --- a/gloss/AppDatabase.swift +++ b/gloss/AppDatabase.swift @@ -69,7 +69,7 @@ struct AppDatabase { try db.create(table: "SelectedRibbon") { t in t.autoIncrementedPrimaryKey("id") - t.column("ribbonId", .integer).notNull() + t.column("ribbonGroupId", .integer).notNull() } try db.create(table: "ScrollState") { t in @@ -394,7 +394,7 @@ extension AppDatabase { scrollId: "1", scrollOffset: 0).inserted(db) - _ = try SelectedRibbon(id: 1, ribbonId: 1).inserted(db) + _ = try SelectedRibbon(id: 1, ribbonGroupId: 1).inserted(db) } } } catch { diff --git a/gloss/ContentView.swift b/gloss/ContentView.swift index ca4f576..8b07deb 100644 --- a/gloss/ContentView.swift +++ b/gloss/ContentView.swift @@ -74,7 +74,7 @@ func goToRibbon(selectedRibbon: Ribbon, refresh.wrappedValue.toggle() - var updateSelectRibbon = SelectedRibbon(id: Int64(1), ribbonId: destRibbon.id!) + var updateSelectRibbon = SelectedRibbon(id: Int64(1), ribbonGroupId: Int64(destRibbon.groupId)) // print("Saving selected ribbon") // print(updateSelectRibbon) do {