Last active
December 8, 2015 22:44
-
-
Save chrisschreiner/f7a01a713f4a33c90cc2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Lifting (main) where | |
import Json.Decode as J | |
import Window | |
import Html | |
import Http | |
import Task | |
import Graphics.Element | |
import Color exposing (grayscale) | |
import Signal exposing(map) | |
import Graphics.Element exposing (show, flow, right, Element) | |
import Graphics.Input exposing (button) | |
import Signal exposing (message, Mailbox, mailbox, map, map2) | |
import Text exposing (fromString, Text) | |
import Effects exposing (Never, Effects) | |
import Mouse exposing (position, x, clicks) | |
-- Shortcut to use in dict-like lists, | |
-- like [key => value] vs [(key, value)] | |
(=>) : a -> b -> (a, b) | |
(=>) = | |
(,) | |
-- lift is no longer available, using map instead | |
main : Signal Element | |
main = | |
Signal.map header widthMousexRation | |
-- | |
sampleTextLabel : Float -> Element | |
sampleTextLabel windowWidth = | |
Graphics.Element.centered | |
<| Text.bold | |
<| Text.color (grayscale windowWidth) | |
<| fromString "This is a fading text" | |
-- action states / or what this program can do | |
type Action | |
= Top | |
| Next | |
| Prev | |
| Last | |
| NoOp | |
| NewGif (Maybe String) | |
-- * | |
update : Action -> () | |
update action = | |
case action of | |
Top -> | |
() | |
_ -> | |
() | |
-- | |
action : Mailbox Action | |
action = | |
mailbox NoOp | |
-- | |
header : Float -> Element | |
header windowWidth = | |
flow right | |
[ button (message action.address Top) "Top" | |
, button (message action.address Next) "Next" | |
, button (message action.address Prev) "Previous" | |
, button (message action.address Last) "Last" | |
, sampleTextLabel windowWidth | |
] | |
-- | |
output : a -> Html.Html | |
output a = | |
toString a | |
|> Html.text | |
-- | |
content : List String | |
content = | |
[ "test" | |
, "po" | |
] | |
-- | |
integerDiv : Int -> Int -> Float | |
integerDiv a b = | |
toFloat a / toFloat b | |
-- Left side yield 0, right side yields 1 and | |
-- everything between is yielded as a signal between 0..1 | |
widthMousexRation : Signal Float | |
widthMousexRation = | |
let | |
-- Gives a signal of a (changing) height and width of window/div | |
windowDimensionSignal : Signal (Int,Int) | |
windowDimensionSignal = map2 (,) Window.width Window.height | |
in | |
map fst windowDimensionSignal | |
|> map2 integerDiv x | |
-- equivalent: clickPosition = Signal.sampleOn Mouse.clicks Mouse.position | |
clickPosition : Signal (Int,Int) | |
clickPosition = | |
position | |
|> Signal.sampleOn clicks | |
-- | |
allUpdates : Signal () | |
allUpdates = | |
let | |
-- | |
updates : Signal a -> Signal () | |
updates = always () |> map | |
in | |
Signal.merge (updates position) (updates clicks) | |
-- The first line there created an HTTP GET request. It tries to | |
-- get some JSON at `randomUrl topic` and decodes the result | |
-- with `decodeImageUrl`. Both are defined below! | |
-- | |
-- Next we use `Task.toMaybe` to capture any potential failures and | |
-- apply the `NewGif` tag to turn the result into a `Action`. | |
-- Finally we turn it into an `Effects` value that can be used in our | |
-- `init` or `update` functions. | |
getRandomGif : String -> Effects Action | |
getRandomGif topic = | |
Http.get decodeImageUrl (randomUrl topic) | |
|> Task.toMaybe | |
|> Task.map NewGif | |
|> Effects.task | |
-- Given a topic, construct a URL for the giphy API. | |
randomUrl : String -> String | |
randomUrl topic = | |
Http.url "http://api.giphy.com/v1/gifs/random" | |
[ "api_key" => "dc6zaTOxFJmzC" | |
, "tag" => topic | |
] | |
-- A JSON decoder that takes a big chunk of JSON spit out by | |
-- giphy and extracts the string at `json.data.image_url` | |
decodeImageUrl : J.Decoder String | |
decodeImageUrl = | |
J.at ["data", "image_url"] J.string |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment