2023-02-27 14:27:15 -08:00
|
|
|
import Combine
|
|
|
|
import GRDB
|
|
|
|
import GRDBQuery
|
|
|
|
|
|
|
|
/// A player request can be used with the `@Query` property wrapper in order to
|
|
|
|
/// feed a view with a list of players.
|
|
|
|
///
|
|
|
|
/// For example:
|
|
|
|
///
|
|
|
|
/// struct MyView: View {
|
|
|
|
/// @Query(RibbonRequest(ordering: .byName)) private var players: [Ribbon]
|
|
|
|
///
|
|
|
|
/// var body: some View {
|
|
|
|
/// List(players) { player in ... )
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
|
2023-02-28 14:03:58 -08:00
|
|
|
|
|
|
|
var idColumn = Column("id")
|
2023-02-27 14:27:15 -08:00
|
|
|
struct RibbonRequest: Queryable {
|
|
|
|
// enum Ordering {
|
|
|
|
// case byScore
|
|
|
|
// case byName
|
|
|
|
// }
|
|
|
|
|
|
|
|
/// The ordering used by the player request.
|
|
|
|
// var ordering: Ordering
|
2023-02-28 14:03:58 -08:00
|
|
|
var id: Int64!
|
2023-02-27 14:27:15 -08:00
|
|
|
|
2024-05-27 21:13:54 -07:00
|
|
|
|
2023-02-27 14:27:15 -08:00
|
|
|
// MARK: - Queryable Implementation
|
2024-05-27 21:13:54 -07:00
|
|
|
|
2023-02-27 14:27:15 -08:00
|
|
|
static var defaultValue: [Ribbon] { [] }
|
2024-05-27 21:13:54 -07:00
|
|
|
|
2023-02-27 14:27:15 -08:00
|
|
|
func publisher(in appDatabase: AppDatabase) -> AnyPublisher<[Ribbon], Error> {
|
|
|
|
// Build the publisher from the general-purpose read-only access
|
|
|
|
// granted by `appDatabase.reader`.
|
|
|
|
// Some apps will prefer to call a dedicated method of `appDatabase`.
|
|
|
|
ValueObservation
|
|
|
|
.tracking(fetchValue(_:))
|
|
|
|
.publisher(
|
|
|
|
in: appDatabase.reader,
|
|
|
|
// The `.immediate` scheduling feeds the view right on
|
|
|
|
// subscription, and avoids an undesired animation when the
|
|
|
|
// application starts.
|
|
|
|
scheduling: .immediate)
|
|
|
|
.eraseToAnyPublisher()
|
|
|
|
}
|
2024-05-27 21:13:54 -07:00
|
|
|
|
2023-02-27 14:27:15 -08:00
|
|
|
// This method is not required by Queryable, but it makes it easier
|
|
|
|
func fetchValue(_ db: Database) throws -> [Ribbon] {
|
2024-05-27 21:13:54 -07:00
|
|
|
|
|
|
|
var sql = """
|
|
|
|
select distinct r1.* from Ribbon r1 join Ribbon r2 ON \
|
2024-05-28 13:04:21 -07:00
|
|
|
r1.undoLevel = r2.currentLevel AND r1.id = r2.id ORDER BY pos ASC
|
2024-05-27 21:13:54 -07:00
|
|
|
"""
|
|
|
|
|
|
|
|
do {
|
|
|
|
var ret = try Ribbon.fetchAll(db, sql: sql)
|
|
|
|
print("xxxxx fetching ribbons")
|
|
|
|
print(ret)
|
|
|
|
return ret
|
|
|
|
|
|
|
|
} catch {
|
|
|
|
print(error.localizedDescription)
|
|
|
|
print(error)
|
|
|
|
print("Error")
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
|
|
|
|
if id == nil {
|
2023-08-02 16:26:17 -07:00
|
|
|
return try Ribbon.order(Column("pos")).fetchAll(db)
|
2023-02-28 14:03:58 -08:00
|
|
|
} else {
|
|
|
|
return try Ribbon.filter(idColumn == id).fetchAll(db)
|
|
|
|
}
|
2023-02-27 14:27:15 -08:00
|
|
|
|
|
|
|
// if book == "" {
|
|
|
|
// return try Ribbon.filter(bookColumn == Ribbon.randomBook()).fetchAll(db)
|
|
|
|
// } else {
|
|
|
|
// return try Ribbon.filter(bookColumn == book).fetchAll(db)
|
|
|
|
// }
|
|
|
|
// switch ordering {
|
|
|
|
// case .byScore:
|
|
|
|
// return try Ribbon.all().fetchAll(db)
|
|
|
|
// case .byName:
|
|
|
|
// // return try Ribbon.all().orderedByName().fetchAll(db)
|
|
|
|
// return try Ribbon.all().fetchAll(db)
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
}
|