93 lines
2.8 KiB
Swift
93 lines
2.8 KiB
Swift
//
|
|
// Ribbon.swift
|
|
// gloss
|
|
//
|
|
// Created by Saint on 2/24/23.
|
|
//
|
|
|
|
import GRDB
|
|
/// The Line struct.
|
|
///
|
|
/// Identifiable conformance supports SwiftUI list animations, and type-safe
|
|
/// GRDB primary key methods.
|
|
/// Equatable conformance supports tests.
|
|
struct Ribbon: Identifiable, Equatable {
|
|
/// The player id.
|
|
///
|
|
/// 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 book: String
|
|
var scrollOffset: Int
|
|
}
|
|
|
|
extension Ribbon {
|
|
}
|
|
|
|
// MARK: - Persistence
|
|
|
|
/// Make Line a Codable Record.
|
|
///
|
|
/// See <https://github.com/groue/GRDB.swift/blob/master/README.md#records>
|
|
extension Ribbon: Codable, FetchableRecord, MutablePersistableRecord {
|
|
// Define database columns from CodingKeys
|
|
fileprivate enum Columns {
|
|
static let id = Column(CodingKeys.id)
|
|
static let book = Column(CodingKeys.book)
|
|
static let scrollOffset = Column(CodingKeys.scrollOffset)
|
|
}
|
|
|
|
/// Updates a player id after it has been inserted in the database.
|
|
mutating func didInsert(_ inserted: InsertionSuccess) {
|
|
id = inserted.rowID
|
|
}
|
|
}
|
|
|
|
// MARK: - Line Database Requests
|
|
|
|
/// Define some player requests used by the application.
|
|
///
|
|
/// See <https://github.com/groue/GRDB.swift/blob/master/README.md#requests>
|
|
/// See <https://github.com/groue/GRDB.swift/blob/master/Documentation/GoodPracticesForDesigningRecordTypes.md>
|
|
extension DerivableRequest<Line> {
|
|
/// A request of players ordered by name.
|
|
///
|
|
/// For example:
|
|
///
|
|
/// let players: [Line] = try dbWriter.read { db in
|
|
/// try Line.all().orderedByName().fetchAll(db)
|
|
/// }
|
|
//func orderedByName() -> Self {
|
|
// // Sort by name in a localized case insensitive fashion
|
|
// // See https://github.com/groue/GRDB.swift/blob/master/README.md#string-comparison
|
|
// order(Line.Columns.name.collating(.localizedCaseInsensitiveCompare))
|
|
//}
|
|
|
|
///// A request of players ordered by score.
|
|
/////
|
|
///// For example:
|
|
/////
|
|
///// let players: [Line] = try dbWriter.read { db in
|
|
///// try Line.all().orderedByScore().fetchAll(db)
|
|
///// }
|
|
///// let bestLine: Line? = try dbWriter.read { db in
|
|
///// try Line.all().orderedByScore().fetchOne(db)
|
|
///// }
|
|
//func orderedByScore() -> Self {
|
|
// // Sort by descending score, and then by name, in a
|
|
// // localized case insensitive fashion
|
|
// // See https://github.com/groue/GRDB.swift/blob/master/README.md#string-comparison
|
|
// order(
|
|
// Line.Columns.score.desc,
|
|
// Line.Columns.name.collating(.localizedCaseInsensitiveCompare))
|
|
//}
|
|
}
|
|
|
|
// Ribbon.swift
|
|
// gloss
|
|
//
|
|
// Created by Saint on 2/27/23.
|
|
//
|
|
|
|
import Foundation
|