Skip to content

Instantly share code, notes, and snippets.

@Qata
Created March 8, 2016 05:23
Show Gist options
  • Save Qata/1433285542c4f3f0569e to your computer and use it in GitHub Desktop.
Save Qata/1433285542c4f3f0569e to your computer and use it in GitHub Desktop.
import Signal
import Graphics.Element exposing (..)
import Graphics.Input exposing (..)
import Graphics.Input.Field exposing (..)
import List
import String
import Window
import Task exposing (Task)
import Text
import Keyboard
type alias Item = Float
type alias Model = { items : List Item
, receipt : String
, field : Content
, windowSize : (Int, Int)
}
type Action = Reset
| ScanItem
| UndoScan
| UpdateField Content
| UpdateWindowSize (Int, Int)
model : Signal Model
model =
Signal.foldp
update
emptyModel
(Signal.merge actions.signal <| Signal.filterMap (\x -> if x then Just ScanItem else Nothing) Reset Keyboard.enter)
actions : Signal.Mailbox Action
actions = Signal.mailbox Reset
main =
Signal.map view model
view : Model -> Element
view model =
let windowWidth = fst model.windowSize
windowHeight = snd model.windowSize
centeredContainer = \value -> container windowWidth (heightOf value) middle value
visualElements =
[ "You're buying $" ++ toString (List.sum model.items) ++ " worth of junk, " ++ toString (List.length model.items) ++ " items in total"
|> Text.fromString
|> centered
|> centeredContainer
, field defaultStyle (UpdateField >> Signal.message actions.address) "Item Cost" model.field
|> centeredContainer
, button (Signal.message actions.address ScanItem) "Add Item"
|> height 20
|> centeredContainer
]
|> List.intersperse (spacer 20 20)
in
visualElements
++
[ button (Signal.message actions.address UndoScan) "Undo Last Scan"
|> height 20
|> centeredContainer
]
|> flow down
emptyModel : Model
emptyModel = { items = []
, receipt = ""
, field = noContent
, windowSize = (0,0)
}
update : Action -> Model -> Model
update action model =
case action of
Reset ->
{ emptyModel
| windowSize = model.windowSize
}
ScanItem ->
case String.toFloat model.field.string of
Ok item -> { model
| items = model.items ++ [item]
, field = noContent
}
Err _ -> { model
| field = noContent
}
UndoScan ->
{ model
| items = List.reverse <| List.drop 1 <| List.reverse model.items
}
UpdateField f ->
{ model
| field = f
}
UpdateWindowSize s ->
{ model
| windowSize = s
}
port windowSizeUpdate : Signal (Task x ())
port windowSizeUpdate =
Signal.map UpdateWindowSize Window.dimensions
|> Signal.map (Signal.send actions.address)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment