on master help sign in
Got encoding working
0b9dcd7iko
backend/Bridge.purs+17−2
@@ -38,9 +38,24 @@ derive instance genericCmd :: Generic Cmd _
38derive instance eqCmd :: Eq Cmd
38derive instance eqCmd :: Eq Cmd
39derive instance ordCmd :: Ord Cmd
39derive instance ordCmd :: Ord Cmd
40
40
41+newtype Sub_Hello =
42+ Sub_Hello {
43+ first :: String
44+ , second :: String
45+ }
46+
47+instance encodeJsonSub_Hello :: EncodeJson Sub_Hello where
48+ encodeJson = genericEncodeAeson Argonaut.defaultOptions
49+instance decodeJsonSub_Hello :: DecodeJson Sub_Hello where
50+ decodeJson = genericDecodeAeson Argonaut.defaultOptions
51+derive instance genericSub_Hello :: Generic Sub_Hello _
52+derive instance eqSub_Hello :: Eq Sub_Hello
53+derive instance ordSub_Hello :: Ord Sub_Hello
54+
41data Sub =
55data Sub =
56+ Empty
42− GotEverything String
57+ | GotEverything String
43− | Hello
58+ | Hello Sub_Hello
44
59
45instance encodeJsonSub :: EncodeJson Sub where
60instance encodeJsonSub :: EncodeJson Sub where
46 encodeJson = genericEncodeAeson Argonaut.defaultOptions
61 encodeJson = genericEncodeAeson Argonaut.defaultOptions
backend/Main.purs+4−1
@@ -10,6 +10,7 @@ import Bridge as Bridge
10import Control.Monad.Reader (runReaderT)
10import Control.Monad.Reader (runReaderT)
11import Control.Promise as Promise
11import Control.Promise as Promise
12import Data.Either (Either(..))
12import Data.Either (Either(..))
13+import Data.Interval (second)
13import Data.Maybe (Maybe(..))
14import Data.Maybe (Maybe(..))
14import Data.Nullable (null, toNullable)
15import Data.Nullable (null, toNullable)
15import Effect (Effect)
16import Effect (Effect)
@@ -69,6 +70,8 @@ main = do
69 crypto : services.crypto
70 crypto : services.crypto
70 }
71 }
71
72
72− Elm.send app Hello
73+ Elm.send app (Bridge.Hello (Bridge.Sub_Hello {first: "f", second: "u"}))
74+
75+ Elm.send app (Bridge.Empty)
73
76
74 log "🍝"
77 log "🍝"
bridge.yaml+5−1
@@ -1,6 +1,10 @@
1Sub:
1Sub:
2−- Hello
2+- Hello:
3+ first: String
4+ second: String
5+
3- GotEverything: String
6- GotEverything: String
7+- Empty
4
8
5Cmd:
9Cmd:
6- Login:
10- Login:
frontend/Bridge.elm+28−5
@@ -45,15 +45,37 @@ jsonEncCmd (Login v1) =
45
45
46
46
47
47
48+type alias Sub_Hello =
49+ { first: String
50+ , second: String
51+ }
52+
53+jsonDecSub_Hello : Json.Decode.Decoder ( Sub_Hello )
54+jsonDecSub_Hello =
55+ Json.Decode.succeed (\pfirst psecond -> {first = pfirst, second = psecond})
56+ |> required "first" (Json.Decode.string)
57+ |> required "second" (Json.Decode.string)
58+
59+jsonEncSub_Hello : Sub_Hello -> Value
60+jsonEncSub_Hello val =
61+ Json.Encode.object
62+ [ ("first", Json.Encode.string val.first)
63+ , ("second", Json.Encode.string val.second)
64+ ]
65+
66+
67+
48type Sub =
68type Sub =
69+ Empty
49− GotEverything String
70+ | GotEverything String
50− | Hello
71+ | Hello Sub_Hello
51
72
52jsonDecSub : Json.Decode.Decoder ( Sub )
73jsonDecSub : Json.Decode.Decoder ( Sub )
53jsonDecSub =
74jsonDecSub =
54 let jsonDecDictSub = Dict.fromList
75 let jsonDecDictSub = Dict.fromList
55− [ ("GotEverything", Json.Decode.lazy (\_ -> Json.Decode.map GotEverything (Json.Decode.string)))
56− , ("Hello", Json.Decode.lazy (\_ -> Json.Decode.succeed Hello))
76+ [ ("Empty", Json.Decode.lazy (\_ -> Json.Decode.succeed Empty))
77+ , ("GotEverything", Json.Decode.lazy (\_ -> Json.Decode.map GotEverything (Json.Decode.string)))
78+ , ("Hello", Json.Decode.lazy (\_ -> Json.Decode.map Hello (jsonDecSub_Hello)))
57 ]
79 ]
58 jsonDecObjectSetSub = Set.fromList []
80 jsonDecObjectSetSub = Set.fromList []
59 in decodeSumTaggedObject "Sub" "tag" "contents" jsonDecDictSub jsonDecObjectSetSub
81 in decodeSumTaggedObject "Sub" "tag" "contents" jsonDecDictSub jsonDecObjectSetSub
@@ -61,7 +83,8 @@ jsonDecSub =
61jsonEncSub : Sub -> Value
83jsonEncSub : Sub -> Value
62jsonEncSub val =
84jsonEncSub val =
63 let keyval v = case v of
85 let keyval v = case v of
86+ Empty -> ("Empty", encodeValue (Json.Encode.list identity []))
64 GotEverything v1 -> ("GotEverything", encodeValue (Json.Encode.string v1))
87 GotEverything v1 -> ("GotEverything", encodeValue (Json.Encode.string v1))
65− Hello -> ("Hello", encodeValue (Json.Encode.list identity []))
88+ Hello v1 -> ("Hello", encodeValue (jsonEncSub_Hello v1))
66 in encodeSumTaggedObject "tag" "contents" keyval val
89 in encodeSumTaggedObject "tag" "contents" keyval val
67
90
frontend/FFI.elm+3−1
@@ -2,6 +2,7 @@ port module FFI exposing (getBridge, sendBridge)
2
2
3import Bridge
3import Bridge
4import Json.Decode as D
4import Json.Decode as D
5+import Json.Encode as E
5
6
6
7
7port bridgeSub : (D.Value -> msg) -> Sub msg
8port bridgeSub : (D.Value -> msg) -> Sub msg
@@ -13,7 +14,8 @@ port bridgeCmd : D.Value -> Cmd msg
13getBridge : (String -> msg) -> (Bridge.Sub -> msg) -> Sub msg
14getBridge : (String -> msg) -> (Bridge.Sub -> msg) -> Sub msg
14getBridge err f =
15getBridge err f =
15 bridgeSub
16 bridgeSub
17+ ((\v -> Debug.log (E.encode 1 v) v)
16− (D.decodeValue Bridge.jsonDecSub
18+ >> D.decodeValue Bridge.jsonDecSub
17 >> (\res ->
19 >> (\res ->
18 case res of
20 case res of
19 Ok a ->
21 Ok a ->
frontend/Main.elm+32−27
@@ -4,8 +4,6 @@ import Bridge
4import Browser
4import Browser
5import FFI exposing (getBridge)
5import FFI exposing (getBridge)
6import Html exposing (..)
6import Html exposing (..)
7−import Html.Attributes as Attr
8−import Html.Events as Ev
9import Notification
7import Notification
10import Pages.Loader exposing (loader)
8import Pages.Loader exposing (loader)
11import Pages.Login as Login
9import Pages.Login as Login
@@ -22,12 +20,16 @@ type Msg
22
20
23
21
24type alias Model =
22type alias Model =
25− { messages : List String
26− , notification : Maybe Notification.Config
23+ { notifications : List Notification.Config
27 , page : PageModel
24 , page : PageModel
28 }
25 }
29
26
30
27
28+appendNotification : Notification.Config -> Model -> Model
29+appendNotification cfg model =
30+ { model | notifications = cfg :: model.notifications }
31+
32+
31type PageModel
33type PageModel
32 = LoginModel Login.Model
34 = LoginModel Login.Model
33 | LoadingPage
35 | LoadingPage
@@ -58,19 +60,21 @@ main =
58 getBridge ShowError
60 getBridge ShowError
59 (\msg ->
61 (\msg ->
60 case msg of
62 case msg of
61− Bridge.Hello ->
63+ Bridge.Hello { first, second } ->
62− RecieveMessage "Got it!"
64+ RecieveMessage ("Got it! " ++ first ++ second)
63
65
64 Bridge.GotEverything everything ->
66 Bridge.GotEverything everything ->
65 RecieveMessage everything
67 RecieveMessage everything
68+
69+ Bridge.Empty ->
70+ RecieveMessage "Empty"
66 )
71 )
67 }
72 }
68
73
69
74
70init : Model
75init : Model
71init =
76init =
72− { messages = []
77+ { notifications = []
73− , notification = Nothing
74 , page = LoadingPage
78 , page = LoadingPage
75 }
79 }
76
80
@@ -84,7 +88,7 @@ view model =
84 div []
88 div []
85 [ Navigation.navigation { back = False, title = title }
89 [ Navigation.navigation { back = False, title = title }
86 , main_ []
90 , main_ []
87− (maybeList model.notification (Notification.notification CloseNotification)
91+ (maybeList (List.head model.notifications) (Notification.notification CloseNotification)
88 ++ body
92 ++ body
89 )
93 )
90 ]
94 ]
@@ -100,37 +104,38 @@ update msg model =
100 ( { model | page = liftModel mdl }, cmd )
104 ( { model | page = liftModel mdl }, cmd )
101
105
102 Err err ->
106 Err err ->
103− ( { model
107+ ( appendNotification
104− | notification =
105− Just
106− { title = "An error had occured"
108+ { title = "An error had occured"
107− , message = err
109+ , message = err
108− , severity = Notification.Error
110+ , severity = Notification.Error
109− }
111+ }
110− }
112+ model
111 , cmd
113 , cmd
112 )
114 )
113 in
115 in
114 case msg of
116 case msg of
115 RecieveMessage x ->
117 RecieveMessage x ->
116− ( { model | messages = x :: model.messages }
118+ ( appendNotification
119+ { title = "Got a message!"
120+ , message = x
121+ , severity = Notification.Info
122+ }
123+ model
117 , Cmd.none
124 , Cmd.none
118 )
125 )
119
126
120 ShowError err ->
127 ShowError err ->
121− ( { model
128+ ( appendNotification
122− | notification =
123− Just
124− { title = "Something went wrong"
129+ { title = "Something went wrong"
125− , message = err
130+ , message = err
126− , severity = Notification.Error
131+ , severity = Notification.Error
127− }
132+ }
128− }
133+ model
129 , Cmd.none
134 , Cmd.none
130 )
135 )
131
136
132 CloseNotification ->
137 CloseNotification ->
133− ( { model | notification = Nothing }
138+ ( { model | notifications = tailEmpty model.notifications }
134 , Cmd.none
139 , Cmd.none
135 )
140 )
136
141
frontend/Page.elm−2
@@ -1,9 +1,7 @@
1module Page exposing (..)
1module Page exposing (..)
2
2
3import Html exposing (..)
3import Html exposing (..)
4−import Html.Attributes as Attr exposing (..)
5import Html.Events exposing (..)
4import Html.Events exposing (..)
6−import Time
7
5
8
6
9type alias Env =
7type alias Env =
frontend/Utils.elm+10
@@ -71,3 +71,13 @@ alignRight inner =
71flexCenter : List (Html msg) -> Html msg
71flexCenter : List (Html msg) -> Html msg
72flexCenter =
72flexCenter =
73 div [ Attr.class "simple-flex-center" ]
73 div [ Attr.class "simple-flex-center" ]
74+
75+
76+tailEmpty : List a -> List a
77+tailEmpty x =
78+ case x of
79+ [] ->
80+ []
81+
82+ _ :: xs ->
83+ xs
spago.dhall+1
@@ -19,6 +19,7 @@ to generate this file without the comments in this block.
19 , "argonaut-codecs"
19 , "argonaut-codecs"
20 , "bw"
20 , "bw"
21 , "console"
21 , "console"
22+ , "datetime"
22 , "effect"
23 , "effect"
23 , "either"
24 , "either"
24 , "foreign"
25 , "foreign"