-
-
Save evancz/88bf5ef55583f180750f to your computer and use it in GitHub Desktop.
This file contains 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
-- WIRING - how does this feel? | |
main : Task x (Signal Html) | |
main = | |
Stream.mailbox | |
`andThen` \queryMailbox -> | |
Task.runStream (Stream.sample getImage Window.dimensions queryMailbox.stream) | |
`andThen` \results -> | |
Signal.map3 (view queryMailbox.address) | |
Window.height | |
(Stream.toSignal "" queryMailbox.stream) | |
(Stream.toSignal "waiting.gif" <| Stream.filterMap Result.toMaybe results) | |
-- FLICKR - how does getImage feel? | |
flickrUrl : String -> List (String, String) -> String | |
getImage : (Int,Int) -> String -> Task Http.Error String | |
getImage dimensions tag = | |
let | |
args = [ ("sort", "random"), ("per_page", "10"), ("tags", tag) ] | |
getPhotoList = | |
Http.get decodePhotoList (flickrUrl "search" args) | |
choosePhoto photoList = | |
Task.fromMaybe photoErr (List.head photoList) | |
getSizeList photo = | |
Http.get decodeSizeList (flickrUrl "getSizes" [ ("photo_id", photo.id) ]) | |
chooseSize sizeList = | |
sizeList | |
|> List.sortBy (sizeRating dimensions) | |
|> List.head | |
|> Task.fromMaybe sizeErr | |
in | |
getPhotoList | |
`andThen` choosePhoto | |
`andThen` getSizeList | |
`andThen` chooseSize | |
-- OTHER STUFF - same in all versions | |
view : Address String -> Int -> String -> String -> Html | |
photoErr : Http.Error | |
sizeErr : Http.Error | |
type alias Photo = { id : String, title : String } | |
type alias Size = { source : String, width : Int, height : Int } | |
decodePhotoList : JS.Decoder (List Photo) | |
decodeSizeList : JS.Decoder (List Size) | |
sizeRating : (Int,Int) -> Size -> Int |
This file contains 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
-- WIRING - how does this feel? | |
query := Stream.mailbox | |
results : Stream (Result Http.Error String) | |
results := | |
Task.runStream (Stream.sample getImage Window.dimensions query.stream) | |
main : Signal Html | |
main = | |
Signal.map3 (view query.address) | |
Window.height | |
(Stream.toSignal "" query.stream) | |
(Stream.toSignal "waiting.gif" <| Stream.filterMap Result.toMaybe results) | |
-- FLICKR - how does getImage feel? | |
getImage : (Int,Int) -> String -> Task Http.Error String | |
getImage dimensions tag = | |
tasklet | |
args = [ ("sort", "random"), ("per_page", "10"), ("tags", tag) ] | |
photoList := | |
Http.get decodePhotoList (flickrUrl "search" args) | |
photo := | |
Task.fromMaybe photoErr (List.head photoList) | |
sizeList := | |
Http.get decodeSizeList (flickrUrl "getSizes" [ ("photo_id", photo.id) ]) | |
in | |
sizeList | |
|> List.sortBy (sizeRating dimensions) | |
|> List.head | |
|> Task.fromMaybe sizeErr | |
-- OTHER STUFF - same in all versions | |
view : Address String -> Int -> String -> String -> Html | |
flickrUrl : String -> List (String, String) -> String | |
photoErr : Http.Error | |
sizeErr : Http.Error | |
type alias Photo = { id : String, title : String } | |
type alias Size = { source : String, width : Int, height : Int } | |
decodePhotoList : JS.Decoder (List Photo) | |
decodeSizeList : JS.Decoder (List Size) | |
sizeRating : (Int,Int) -> Size -> Int |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment