gloss-ios/SelectedRibbonRequest.swift

55 lines
1.8 KiB
Swift
Raw Normal View History

2023-02-28 14:03:58 -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(SelectedRibbonRequest(ordering: .byName)) private var players: [SelectedRibbon]
///
/// var body: some View {
/// List(players) { player in ... )
/// }
/// }
struct SelectedRibbonRequest: Queryable {
// enum Ordering {
// case byScore
// case byName
// }
/// The ordering used by the player request.
// var ordering: Ordering
2023-03-01 10:47:34 -08:00
static var defaultValue: [Ribbon] { [] }
2023-02-28 14:03:58 -08:00
2023-03-01 10:47:34 -08:00
func publisher(in appDatabase: AppDatabase) -> AnyPublisher<[Ribbon], Error> {
2023-02-28 14:03:58 -08:00
// 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()
}
// This method is not required by Queryable, but it makes it easier
2023-03-01 10:47:34 -08:00
func fetchValue(_ db: Database) throws -> [Ribbon] {
2023-02-28 14:03:58 -08:00
2023-03-01 10:47:34 -08:00
print("FETCH SELECTED RIBBON")
var ret2 = try SelectedRibbon.fetchAll(db, sql: "SELECT * FROM selectedRibbon") // [Player]
print(ret2)
print("FETCH JOIN RIBBON")
var ret = try Ribbon.fetchAll(db, sql: "SELECT ribbon.* FROM selectedRibbon join ribbon on selectedRibbon.ribbonId = ribbon.rowid") // [Player]
print(ret)
return ret
2023-02-28 14:03:58 -08:00
}
}