Saving work before I start a notable re-factor
Going to make major changes to how messages are routed/handled.
This commit is contained in:
parent
d4745bc9d8
commit
bdaee51130
|
@ -2,6 +2,13 @@ use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
pub struct Landing {
|
pub enum DownMsg {
|
||||||
pub time : String
|
Greeting(String),
|
||||||
|
TimeUpdate(String),
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#[derive(Serialize)]
|
||||||
|
pub enum UpMsg {
|
||||||
|
RequestGreet(String)
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,22 +9,9 @@ use chrono::Local;
|
||||||
|
|
||||||
mod landing;
|
mod landing;
|
||||||
|
|
||||||
|
|
||||||
/// Greeting API structures
|
|
||||||
#[derive(Serialize, Deserialize)]
|
|
||||||
struct GreetingRequest {
|
|
||||||
name: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
struct GreetingResponse {
|
enum DownMsg {
|
||||||
message: String,
|
Landing(landing::DownMsg),
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
#[derive(Serialize)]
|
|
||||||
enum ToFrontend {
|
|
||||||
Landing(landing::Landing)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// WebSocket actor
|
/// WebSocket actor
|
||||||
|
@ -37,7 +24,7 @@ impl Actor for MyWebSocket {
|
||||||
info!("WebSocket actor started");
|
info!("WebSocket actor started");
|
||||||
ctx.run_interval(Duration::from_secs(1), |_, ctx| {
|
ctx.run_interval(Duration::from_secs(1), |_, ctx| {
|
||||||
let current_time = Local::now().format("%H:%M:%S").to_string();
|
let current_time = Local::now().format("%H:%M:%S").to_string();
|
||||||
let message = ToFrontend::Landing(landing::Landing { time: current_time });
|
let message = DownMsg::Landing(landing::DownMsg::TimeUpdate(current_time));
|
||||||
|
|
||||||
if let Ok(json_message) = serde_json::to_string(&message) {
|
if let Ok(json_message) = serde_json::to_string(&message) {
|
||||||
ctx.text(json_message);
|
ctx.text(json_message);
|
||||||
|
|
|
@ -10,36 +10,52 @@ import Element exposing (Element)
|
||||||
import Websockets
|
import Websockets
|
||||||
import Ports
|
import Ports
|
||||||
import Json.Decode as Decode
|
import Json.Decode as Decode
|
||||||
|
import Json.Encode as Encode exposing (Value)
|
||||||
import Html.Attributes exposing (placeholder)
|
import Html.Attributes exposing (placeholder)
|
||||||
import Element.Input
|
import Element.Input
|
||||||
|
import Element.Background
|
||||||
|
|
||||||
type alias Model = {
|
type alias Model = {
|
||||||
time : String
|
time : String,
|
||||||
}
|
greetWidgetText : String
|
||||||
type alias Landing = {
|
|
||||||
time : String
|
|
||||||
}
|
}
|
||||||
|
type DownMsgLanding
|
||||||
|
= Greeting String
|
||||||
|
| TimeUpdate String
|
||||||
|
|
||||||
|
decodeDownMsgLanding : Decode.Decoder DownMsgLanding
|
||||||
|
decodeDownMsgLanding =
|
||||||
|
Decode.oneOf
|
||||||
|
[ Decode.map Greeting (Decode.field "Greeting" Decode.string)
|
||||||
|
, Decode.map TimeUpdate (Decode.field "TimeUpdate" Decode.string)
|
||||||
|
]
|
||||||
|
|
||||||
|
decodeDownMsg : Decode.Decoder Msg
|
||||||
|
decodeDownMsg =
|
||||||
|
Decode.map DownMsg decodeDownMsgLanding
|
||||||
|
|
||||||
|
type UpMsgLanding = RequestGreet String
|
||||||
|
|
||||||
|
encodeUpMsgLanding : UpMsgLanding -> Value
|
||||||
|
encodeUpMsgLanding msg =
|
||||||
|
case msg of
|
||||||
|
RequestGreet name ->
|
||||||
|
Encode.object
|
||||||
|
[ ( "RequestGreet", Encode.string name ) ]
|
||||||
|
|
||||||
type Msg
|
type Msg
|
||||||
= ToFrontend Landing
|
= DownMsg DownMsgLanding
|
||||||
|
| UpMsg UpMsgLanding
|
||||||
| DecodeError Decode.Error
|
| DecodeError Decode.Error
|
||||||
| GreetWidgetText String
|
| GreetWidgetText String
|
||||||
| NoOp
|
| NoOp
|
||||||
|
|
||||||
init : () -> Model
|
init : () -> Model
|
||||||
init flags = {
|
init flags = {
|
||||||
time = "time not yet set"
|
time = "time not yet set",
|
||||||
|
greetWidgetText = ""
|
||||||
}
|
}
|
||||||
|
|
||||||
decodeToFrontend message =
|
|
||||||
let
|
|
||||||
decodeTime = Decode.field "time" Decode.string |> Decode.map Landing
|
|
||||||
decodeLanding = Decode.field "Landing" decodeTime |> Decode.map ToFrontend
|
|
||||||
decodedMessage = Decode.decodeString decodeLanding message
|
|
||||||
in
|
|
||||||
case decodedMessage of
|
|
||||||
Ok decoded -> decoded
|
|
||||||
Err err -> DecodeError err
|
|
||||||
|
|
||||||
subscriptions : Model -> Sub Msg
|
subscriptions : Model -> Sub Msg
|
||||||
subscriptions model =
|
subscriptions model =
|
||||||
Ports.socketOnEvent
|
Ports.socketOnEvent
|
||||||
|
@ -47,23 +63,42 @@ subscriptions model =
|
||||||
(\_ -> NoOp)
|
(\_ -> NoOp)
|
||||||
(\_ -> NoOp)
|
(\_ -> NoOp)
|
||||||
(\_ -> NoOp)
|
(\_ -> NoOp)
|
||||||
(\message -> (message.data |> decodeToFrontend))
|
(\message ->
|
||||||
|
case Decode.decodeString decodeDownMsg message.data of
|
||||||
|
Ok msg -> msg
|
||||||
|
Err err -> DecodeError err
|
||||||
|
)
|
||||||
(\_ -> NoOp)
|
(\_ -> NoOp)
|
||||||
)
|
)
|
||||||
|
|
||||||
update : Msg -> Model -> (Model, Cmd Msg)
|
update : Msg -> Model -> (Model, Cmd Msg)
|
||||||
update msg model =
|
update msg model =
|
||||||
case msg of
|
case msg of
|
||||||
ToFrontend landing -> ( {model | time = landing.time}, Cmd.none )
|
DownMsg (TimeUpdate time) -> ( {model | time = time}, Cmd.none )
|
||||||
|
GreetWidgetText text -> ( {model | greetWidgetText = text}, Cmd.none )
|
||||||
_ -> (model, Cmd.none)
|
_ -> (model, Cmd.none)
|
||||||
|
|
||||||
greetWidget =
|
greetWidget : Model -> Element Msg
|
||||||
|
greetWidget model =
|
||||||
let
|
let
|
||||||
|
idleBlue = Element.Background.color <| Element.rgb255 18 147 217
|
||||||
|
focusBlue = Element.Background.color <| Element.rgb255 18 125 184
|
||||||
|
|
||||||
|
myButton = Element.Input.button
|
||||||
|
[ idleBlue
|
||||||
|
, Element.focused [ focusBlue ]
|
||||||
|
]
|
||||||
|
{ onPress = Just <| DownMsg <| Greeting model.greetWidgetText
|
||||||
|
, label = Element.text "My Button"
|
||||||
|
}
|
||||||
|
placeholder = case model.greetWidgetText of
|
||||||
|
"" -> Just <| Element.Input.placeholder [] <| Element.text "Type Your Name"
|
||||||
|
_ -> Just <| Element.Input.placeholder [] <| Element.text ""
|
||||||
textInput =
|
textInput =
|
||||||
Element.Input.text []
|
Element.Input.text []
|
||||||
{ onChange = GreetWidgetText
|
{ onChange = GreetWidgetText
|
||||||
, text = "text"
|
, text = model.greetWidgetText
|
||||||
, placeholder = Nothing
|
, placeholder = placeholder
|
||||||
, label = Element.Input.labelHidden "Greet"
|
, label = Element.Input.labelHidden "Greet"
|
||||||
}
|
}
|
||||||
in
|
in
|
||||||
|
@ -75,5 +110,5 @@ view model =
|
||||||
Element.column []
|
Element.column []
|
||||||
[ Element.text "Landing"
|
[ Element.text "Landing"
|
||||||
, Element.text <| "Current time is : " ++ model.time
|
, Element.text <| "Current time is : " ++ model.time
|
||||||
, greetWidget
|
, greetWidget model
|
||||||
]
|
]
|
||||||
|
|
Loading…
Reference in a new issue