135 lines
2.9 KiB
Elm
135 lines
2.9 KiB
Elm
module Main exposing (main)
|
|
|
|
import Element exposing (Element)
|
|
import GameList exposing (Game, GameList)
|
|
import Json.Decode as D
|
|
import Layout
|
|
import Material.Icons as Icons
|
|
import Program
|
|
import Widget.Icon
|
|
|
|
|
|
main =
|
|
Program.document
|
|
{ flagsDecoder = D.string
|
|
, headers = headers
|
|
, init = init
|
|
, subscriptions = subscriptions
|
|
, title = always "Coolio!" -- TODO
|
|
, update = update
|
|
, view = view
|
|
}
|
|
|
|
|
|
|
|
-- MODEL
|
|
|
|
|
|
type alias Model =
|
|
{ baseUrl : String
|
|
, games : GameList
|
|
, screen : Screen
|
|
}
|
|
|
|
|
|
type Screen
|
|
= ViewCreateGame
|
|
| ViewGameSelectionMenu
|
|
| ViewGame Game
|
|
|
|
|
|
type Msg
|
|
= OnGameList GameList.Msg
|
|
| OnScreen Screen
|
|
|
|
|
|
init : Result D.Error String -> ( Model, Cmd Msg )
|
|
init baseUrl =
|
|
let
|
|
( gmdl, gmsg ) =
|
|
GameList.init {}
|
|
in
|
|
( { baseUrl =
|
|
case baseUrl of
|
|
Ok s ->
|
|
s
|
|
|
|
Err _ ->
|
|
"http://localhost:5000"
|
|
, games = gmdl
|
|
, screen = ViewGameSelectionMenu
|
|
}
|
|
, Cmd.map OnGameList gmsg
|
|
)
|
|
|
|
|
|
|
|
-- UPDATE
|
|
|
|
|
|
update : Msg -> Model -> ( Model, Cmd Msg )
|
|
update msg model =
|
|
case msg of
|
|
OnGameList m ->
|
|
case GameList.update m model.games of
|
|
( newMdl, newM ) ->
|
|
( { model | games = newMdl }, Cmd.map OnGameList newM )
|
|
|
|
OnScreen screen ->
|
|
( { model | screen = screen }, Cmd.none )
|
|
|
|
|
|
|
|
-- SUBSCRIPTIONS
|
|
|
|
|
|
subscriptions : Model -> Sub Msg
|
|
subscriptions model =
|
|
Sub.map OnGameList (GameList.subscriptions model.games)
|
|
|
|
|
|
|
|
-- VIEW
|
|
|
|
|
|
headers : Model -> List { icon : Widget.Icon.Icon Msg, onPress : Msg }
|
|
headers model =
|
|
case model.screen of
|
|
ViewCreateGame ->
|
|
[ { icon = Layout.iconAsIcon Icons.arrow_back, onPress = OnScreen ViewGameSelectionMenu }
|
|
]
|
|
|
|
ViewGameSelectionMenu ->
|
|
[]
|
|
|
|
ViewGame _ ->
|
|
[ { icon = Layout.iconAsIcon Icons.arrow_back, onPress = OnScreen ViewGameSelectionMenu }
|
|
]
|
|
|
|
|
|
view : Program.ViewBox Model -> Element Msg
|
|
view data =
|
|
case data.model.screen of
|
|
ViewCreateGame ->
|
|
Element.text "Create game menu!"
|
|
|
|
ViewGameSelectionMenu ->
|
|
GameList.viewSelection
|
|
{ flavor = data.flavor
|
|
, height = data.size.height
|
|
, model = data.model.games
|
|
, onCreateGame = OnScreen ViewCreateGame
|
|
, onNavigateToGame = OnScreen << ViewGame
|
|
, width = data.size.width
|
|
}
|
|
|
|
ViewGame game ->
|
|
GameList.viewGame
|
|
{ flavor = data.flavor
|
|
, game = game
|
|
, height = data.size.height
|
|
, onNavigateBack = OnScreen ViewGameSelectionMenu
|
|
, toMsg = OnGameList
|
|
, width = data.size.width
|
|
}
|