1 USING: accessors arrays colors combinators db.sqlite db.tuples
2 db.types io.files.temp kernel locals math models.combinators
3 monads persistency sequences sequences.extras sets ui
4 ui.gadgets.controls ui.gadgets.labels ui.gadgets.layout
5 ui.gadgets.scrollers ui.pens.solid ;
9 { title { VARCHAR 100 } }
12 { genre { VARCHAR 100 } } ;
14 : <recipe> ( title genre text -- recipe )
15 recipe new swap >>txt swap >>genre swap >>title 0 >>votes ;
17 "recipes.db" temp-file <sqlite-db> recipe define-db
19 : top-recipes ( offset search -- recipes )
20 <query> T{ recipe } rot >>title >>tuple
21 "votes" >>order 30 >>limit swap >>offset get-tuples ;
23 : top-genres ( -- genres )
24 f f top-recipes [ genre>> ] map members 4 index-or-length head-slice ;
26 : interface ( -- book )
29 [ $ TOOLBAR $ ] <hbox> COLOR: AliceBlue <solid> >>interior ,
30 [ "Genres:" <label> , <spacer> $ ALL $ $ GENRES $ ] <hbox>
31 { 5 0 } >>gap COLOR: gray <solid> >>interior ,
35 [ "Title:" <label> , $ TITLE $ "Genre:" <label> , $ GENRE $ ] <hbox> ,
39 ] <book*> { 350 245 } >>pref-dim ;
41 :: <recipe-gadget> ( -- gadget )
45 "okay" <model-border-btn> BUTTON -> :> ok
46 IMG-MODEL-BTN: submit [ store-tuple ] >>value TOOLBAR -> :> submit
47 IMG-MODEL-BTN: love 1 >>value TOOLBAR ->
48 IMG-MODEL-BTN: hate -1 >>value -> 2array merge :> votes
49 IMG-MODEL-BTN: back -> [ -30 ] <$
50 IMG-MODEL-BTN: more -> [ 30 ] <$ 2array merge :> viewed
51 <spacer> <model-field*> ->% 1 :> search
52 submit ok [ [ drop ] ] <$ 2array merge [ drop ] >>value :> quot
53 viewed 0 [ + ] fold search ok t <basic> "all" <model-btn> ALL ->
54 tbl selection>> votes [ [ + ] curry change-votes modify-tuple ] 2$>
56 [ drop [ f ] [ "%" dup surround <pattern> ] if-empty top-recipes ] 3fmap :> ups
57 ups [ top-genres [ <model-btn> GENRES -> ] map merge ] bind*
58 [ text>> T{ recipe } swap >>genre get-tuples ] fmap
59 tbl swap ups 2merge >>model
60 [ [ title>> ] [ genre>> ] bi 2array ] >>quot
61 { "Title" "Genre" } >>column-titles dup <scroller> RECIPES ,% 1 actions>>
62 submit [ "" dup dup <recipe> ] <$ 2array merge
64 [ [ title>> ] fmap <model-field> TITLE ->% .5 ]
65 [ [ genre>> ] fmap <model-field> GENRE ->% .5 ]
66 [ [ txt>> ] fmap <model-editor> BODY ->% 1 ]
70 [ quot ok updates #1 [ call( recipe -- ) 0 ] 2fmap ] bi
71 2merge 0 <basic> switch-models >>model
74 MAIN-WINDOW: recipe-browser
75 { { title "Recipes" } }
76 <recipe-gadget> >>gadgets ;