2023-02-28 14:03:58 -08:00
import Combine
import GRDB
import GRDBQuery
// / A p l a y e r r e q u e s t c a n b e u s e d w i t h t h e ` @ Q u e r y ` p r o p e r t y w r a p p e r i n o r d e r t o
// / f e e d a v i e w w i t h a l i s t o f p l a y e r s .
// /
// / F o r e x a m p l e :
// /
// / s t r u c t M y V i e w : V i e w {
// / @ Q u e r y ( S e l e c t e d R i b b o n R e q u e s t ( o r d e r i n g : . b y N a m e ) ) p r i v a t e v a r p l a y e r s : [ S e l e c t e d R i b b o n ]
// /
// / v a r b o d y : s o m e V i e w {
// / L i s t ( p l a y e r s ) { p l a y e r i n . . . )
// / }
// / }
struct SelectedRibbonRequest : Queryable {
// e n u m O r d e r i n g {
// c a s e b y S c o r e
// c a s e b y N a m e
// }
// / T h e o r d e r i n g u s e d b y t h e p l a y e r r e q u e s t .
// v a r o r d e r i n g : O r d e r i n g
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
// B u i l d t h e p u b l i s h e r f r o m t h e g e n e r a l - p u r p o s e r e a d - o n l y a c c e s s
// g r a n t e d b y ` a p p D a t a b a s e . r e a d e r ` .
// S o m e a p p s w i l l p r e f e r t o c a l l a d e d i c a t e d m e t h o d o f ` a p p D a t a b a s e ` .
ValueObservation
. tracking ( fetchValue ( _ : ) )
. publisher (
in : appDatabase . reader ,
// T h e ` . i m m e d i a t e ` s c h e d u l i n g f e e d s t h e v i e w r i g h t o n
// s u b s c r i p t i o n , a n d a v o i d s a n u n d e s i r e d a n i m a t i o n w h e n t h e
// a p p l i c a t i o n s t a r t s .
scheduling : . immediate )
. eraseToAnyPublisher ( )
}
// T h i s m e t h o d i s n o t r e q u i r e d b y Q u e r y a b l e , b u t i t m a k e s i t e a s i e r
2023-03-01 10:47:34 -08:00
func fetchValue ( _ db : Database ) throws -> [ Ribbon ] {
2023-02-28 14:03:58 -08:00
2023-03-11 20:31:48 -08:00
// p r i n t ( " F E T C H S E L E C T E D R I B B O N " )
// v a r r e t 2 = t r y S e l e c t e d R i b b o n . f e t c h A l l ( d b , s q l : " S E L E C T * F R O M s e l e c t e d R i b b o n " ) / / [ P l a y e r ]
// p r i n t ( r e t 2 )
// p r i n t ( " F E T C H R I B B O N " )
// v a r r e t 3 = t r y R i b b o n . f e t c h A l l ( d b , s q l : " S E L E C T * F R O M R i b b o n " ) / / [ P l a y e r ]
// p r i n t ( r e t 3 )
// p r i n t ( " F E T C H J O I N R I B B O N " )
var ret = try Ribbon . fetchAll ( db , sql : " SELECT Ribbon.* FROM SelectedRibbon join Ribbon on SelectedRibbon.ribbonId = ribbon.rowId WHERE SelectedRibbon.rowId = 1 " ) // [ P l a y e r ]
// p r i n t ( r e t )
2023-03-01 10:47:34 -08:00
return ret
2023-02-28 14:03:58 -08:00
}
}