diff --git a/RibbonRequest.swift b/RibbonRequest.swift index 4ad2793..ad6ccc3 100644 --- a/RibbonRequest.swift +++ b/RibbonRequest.swift @@ -52,7 +52,7 @@ struct RibbonRequest: Queryable { var sql = """ select distinct r1.* from Ribbon r1 join Ribbon r2 ON \ - r1.undoLevel = r2.currentLevel AND r1.id = r2.id + r1.undoLevel = r2.currentLevel AND r1.id = r2.id ORDER BY pos ASC """ do { diff --git a/gloss/AppDatabase.swift b/gloss/AppDatabase.swift index 8850abc..dc294d7 100644 --- a/gloss/AppDatabase.swift +++ b/gloss/AppDatabase.swift @@ -54,8 +54,9 @@ struct AppDatabase { try db.create(table: "Ribbon") { t in t.autoIncrementedPrimaryKey("id") t.column("pos", .integer).notNull() - t.column("group", .integer).notNull() + t.column("groupId", .integer).notNull() t.column("undoLevel", .integer).notNull() + t.column("currentLevel", .integer).notNull() t.column("minLevel", .integer).notNull() t.column("maxLevel", .integer).notNull() @@ -78,7 +79,7 @@ struct AppDatabase { } // change this to nuke/remake the database - try db.create(table: "foo1") { t in + try db.create(table: "foo3") { t in t.autoIncrementedPrimaryKey("id") t.column("ribbonId", .integer).notNull() } @@ -201,6 +202,85 @@ extension AppDatabase { } } + func bumpRibbon(_ ribbon: inout Ribbon) async throws { + let totalLevels = 3 + var level = ribbon.currentLevel + let maxLevel = ribbon.maxLevel + var delLevels2 = [Int]() + while level != maxLevel { + level = (level + 1) % totalLevels + delLevels2.append(level) + } + print("del levels") + let delLevels = delLevels2 + print(delLevels) + + let newMax = (ribbon.currentLevel + 1) % totalLevels + 1 + let newCurrent = newMax + let newMin = newMax == ribbon.minLevel ? (ribbon.minLevel + 1) % totalLevels + 1 + : ribbon.minLevel + + print(newMin) + print(newMax) + print(newCurrent) + ribbon.minLevel = newMin + ribbon.maxLevel = newMax + ribbon.undoLevel = newCurrent + ribbon.currentLevel = newCurrent + ribbon.id = nil + + do { + try await dbWriter.write { [ribbon] db in + for l in delLevels { + try db.execute(sql: """ + DELETE FROM Ribbon \ + WHERE groupId = ? \ + AND undoLevel = ? + """, arguments: [ribbon.groupId, l]) + } + + try db.execute(sql: """ + UPDATE Ribbon \ + SET minLevel = ?, maxLevel = ?, currentLevel = ? WHERE groupId = ? + """, arguments: [newMin, newMax, newCurrent, ribbon.groupId]) + + + // upsert + + let ret = try Ribbon.fetchAll(db, sql: """ + SELECT * from Ribbon WHERE groupId = ? AND undoLevel = ? + """, arguments: [ribbon.groupId, ribbon.undoLevel]) + + if ret.count == 0 { + // insert + _ = try ribbon.inserted(db) + } else { + var updatedRibbon = ret[0] + updatedRibbon.minLevel = newMin + updatedRibbon.maxLevel = newMax + updatedRibbon.undoLevel = newCurrent + updatedRibbon.currentLevel = newCurrent + updatedRibbon.scrollId = ribbon.scrollId + updatedRibbon.scrollOffset = ribbon.scrollOffset + try updatedRibbon.update(db) + } + + print("ribbon inserted: \(ret)") + + + let ret2 = try Ribbon.fetchAll(db, sql: """ + SELECT * from Ribbon ORDER BY groupId + """) + for r in ret2 { + print("ribbon dump: \(r)") + } + } + + } catch { + print("Error info: \(error)") + } + } + func saveSelectedRibbon(_ selectedRibbon: inout SelectedRibbon) async throws { // if ribbon.name.isEmpty { // throw ValidationError.missingName @@ -254,7 +334,7 @@ extension AppDatabase { try importJson("john_export.json", db) try importJson("mark_export.json", db) _ = try Ribbon(id: 1, - group: 1, + groupId: 1, pos: 1, undoLevel: 1, currentLevel: 1, @@ -265,7 +345,7 @@ extension AppDatabase { scrollId: "1", scrollOffset: 0).inserted(db) _ = try Ribbon(id: 2, - group: 2, + groupId: 2, pos: 2, undoLevel: 1, currentLevel: 1, @@ -276,29 +356,44 @@ extension AppDatabase { scrollId: "1", scrollOffset: 300).inserted(db) + ///// + _ = try Ribbon(id: 3, - group: 3, + groupId: 3, pos: 3, undoLevel: 1, - currentLevel: 2, + currentLevel: 3, minLevel: 1, - maxLevel: 1, + maxLevel: 3, title: "bottom", book: "bible.john", scrollId: "1", scrollOffset: 0).inserted(db) _ = try Ribbon(id: 4, - group: 3, + groupId: 3, pos: 3, undoLevel: 2, - currentLevel: 2, + currentLevel: 3, minLevel: 1, - maxLevel: 2, + maxLevel: 3, title: "topp", book: "bible.john", scrollId: "1", scrollOffset: 0).inserted(db) + + _ = try Ribbon(id: 5, + groupId: 3, + pos: 3, + undoLevel: 3, + currentLevel: 3, + minLevel: 1, + maxLevel: 3, + title: "topp", + book: "bible.john", + scrollId: "1", + scrollOffset: 0).inserted(db) + _ = try SelectedRibbon(id: 1, ribbonId: 1).inserted(db) } } diff --git a/gloss/ContentView.swift b/gloss/ContentView.swift index 98c93fc..ca4f576 100644 --- a/gloss/ContentView.swift +++ b/gloss/ContentView.swift @@ -84,11 +84,17 @@ func goToRibbon(selectedRibbon: Ribbon, } } + // this gets run regardless of if we switch ribbons or not + // so if you click the same ribbon or a different ribbon if !loading { print("not loading") + + // updating the ribbon location updatedRibbon.scrollOffset = Int(floor(scrollOffsetToSave)) updatedRibbon.scrollId = scrollIdToSave - _ = try await appDatabase.saveRibbon(&updatedRibbon) + // updatedRibbon.undoLevel = updatedRibbon.undoLevel + 1 + // updatedRibbon.currentLevel = updatedRibbon.currentLevel + 1 + _ = try await appDatabase.bumpRibbon(&updatedRibbon) } else { print("loading") diff --git a/gloss/Ribbon.swift b/gloss/Ribbon.swift index dc859e0..eaa01d3 100644 --- a/gloss/Ribbon.swift +++ b/gloss/Ribbon.swift @@ -17,7 +17,7 @@ struct Ribbon: 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 group: Int + var groupId: Int var pos: Int var undoLevel: Int var currentLevel: Int @@ -32,7 +32,6 @@ struct Ribbon: Identifiable, Equatable { extension Ribbon { } -// MARK: - Persistence /// Make Line a Codable Record. ///