69 lines
2.2 KiB
Swift
69 lines
2.2 KiB
Swift
|
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
|
||
|
var id: Int64!
|
||
|
|
||
|
// MARK: - Queryable Implementation
|
||
|
|
||
|
static var defaultValue: [SelectedRibbon] { [] }
|
||
|
|
||
|
func publisher(in appDatabase: AppDatabase) -> AnyPublisher<[SelectedRibbon], 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()
|
||
|
}
|
||
|
|
||
|
// This method is not required by Queryable, but it makes it easier
|
||
|
func fetchValue(_ db: Database) throws -> [SelectedRibbon] {
|
||
|
if (id == nil) {
|
||
|
return try SelectedRibbon.fetchAll(db)
|
||
|
} else {
|
||
|
return try SelectedRibbon.filter(idColumn == id).fetchAll(db)
|
||
|
}
|
||
|
|
||
|
// if book == "" {
|
||
|
// return try SelectedRibbon.filter(bookColumn == SelectedRibbon.randomBook()).fetchAll(db)
|
||
|
// } else {
|
||
|
// return try SelectedRibbon.filter(bookColumn == book).fetchAll(db)
|
||
|
// }
|
||
|
// switch ordering {
|
||
|
// case .byScore:
|
||
|
// return try SelectedRibbon.all().fetchAll(db)
|
||
|
// case .byName:
|
||
|
// // return try SelectedRibbon.all().orderedByName().fetchAll(db)
|
||
|
// return try SelectedRibbon.all().fetchAll(db)
|
||
|
// }
|
||
|
}
|
||
|
}
|