]> gitweb.factorcode.org Git - factor.git/blob - extra/recipes/recipes.factor
recipes: add some terrible @2x icons, cleanup
[factor.git] / extra / recipes / recipes.factor
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 ;
6 IN: recipes
7
8 STORED-TUPLE: recipe
9     { title { VARCHAR 100 } }
10     { votes INTEGER }
11     { txt TEXT }
12     { genre { VARCHAR 100 } } ;
13
14 : <recipe> ( title genre text -- recipe )
15     recipe new swap >>txt swap >>genre swap >>title 0 >>votes ;
16
17 "recipes.db" temp-file <sqlite-db> recipe define-db
18
19 : top-recipes ( offset search -- recipes )
20     <query> T{ recipe } rot >>title >>tuple
21     "votes" >>order 30 >>limit swap >>offset get-tuples ;
22
23 : top-genres ( -- genres )
24     f f top-recipes [ genre>> ] map members 4 index-or-length head-slice ;
25
26 : interface ( -- book )
27     [
28         [
29             [ $ TOOLBAR $ ] <hbox> COLOR: AliceBlue <solid> >>interior ,
30             [ "Genres:" <label> , <spacer> $ ALL $ $ GENRES $ ] <hbox>
31             { 5 0 } >>gap COLOR: gray <solid> >>interior ,
32             $ RECIPES $
33         ] <vbox> ,
34         [
35             [ "Title:" <label> , $ TITLE $ "Genre:" <label> , $ GENRE $ ] <hbox> ,
36             $ BODY $
37             $ BUTTON $
38         ] <vbox> ,
39     ] <book*> { 350 245 } >>pref-dim ;
40
41 :: <recipe-gadget> ( -- gadget )
42     [
43         interface
44         <table*> :> tbl
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$>
55         4array merge
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
63         {
64             [ [ title>> ] fmap <model-field> TITLE ->% .5 ]
65             [ [ genre>> ] fmap <model-field> GENRE ->% .5 ]
66             [ [ txt>> ] fmap <model-editor> BODY ->% 1 ]
67         } cleave
68         [ <recipe> ] 3fmap
69         [ [ 1 ] <$ ]
70         [ quot ok updates #1 [ call( recipe -- ) 0 ] 2fmap ] bi
71         2merge 0 <basic> switch-models >>model
72     ] with-interface ;
73
74 MAIN-WINDOW: recipe-browser
75     { { title "Recipes" } }
76     <recipe-gadget> >>gadgets ;