on master help sign in
Simplify code and improve type safety: replace nested option in StateLoaded with dedicated DU, remove pubKeyHex helper, restructure viewSettings to single auth match
8dcf190iko
client/App.fs+36−30
@@ -77,6 +77,11 @@ module App =
77
77
78 // ── Msg ──
78 // ── Msg ──
79
79
80+ type StateLoadResult =
81+ | IdentityAndData of Identity * Data
82+ | IdentityOnly of Identity
83+ | NothingSaved
84+
80 type Msg =
85 type Msg =
81 | SetPage of Page
86 | SetPage of Page
82 | SetCompose of string
87 | SetCompose of string
@@ -95,7 +100,7 @@ module App =
95 | PollResult of messages: (string * ChatMessage) list * status: string * cursor: int64
100 | PollResult of messages: (string * ChatMessage) list * status: string * cursor: int64
96 | CopyPubKey
101 | CopyPubKey
97 | DismissError
102 | DismissError
98− | StateLoaded of (Identity * Data option) option
103+ | StateLoaded of StateLoadResult
99
104
100 // ── CmdMsg ──
105 // ── CmdMsg ──
101
106
@@ -157,11 +162,6 @@ module App =
157 | Some i -> contacts |> List.updateAt i contact
162 | Some i -> contacts |> List.updateAt i contact
158 | None -> contacts @ [ contact ]
163 | None -> contacts @ [ contact ]
159
164
160− let private pubKeyHex model =
161− match model.Auth with
162− | Identified(id, _) -> id.PubKeyHex
163− | NoIdentity -> ""
164−
165 let private trySession model =
165 let private trySession model =
166 match model.Auth with
166 match model.Auth with
167 | Identified(id, Online token) -> Some { Url = model.ServerUrl; Token = token; Identity = id }
167 | Identified(id, Online token) -> Some { Url = model.ServerUrl; Token = token; Identity = id }
@@ -293,7 +293,7 @@ module App =
293
293
294 | DismissError -> { model with Error = None }, []
294 | DismissError -> { model with Error = None }, []
295
295
296− | StateLoaded(Some(identity, Some data)) ->
296+ | StateLoaded(IdentityAndData(identity, data)) ->
297 let model' =
297 let model' =
298 { model with
298 { model with
299 Auth = Identified(identity, Connecting)
299 Auth = Identified(identity, Connecting)
@@ -303,9 +303,9 @@ module App =
303 PollCursor = data.PollCursor
303 PollCursor = data.PollCursor
304 Page = Conversations }
304 Page = Conversations }
305 model', [ CmdConnect(model'.ServerUrl, identity) ]
305 model', [ CmdConnect(model'.ServerUrl, identity) ]
306− | StateLoaded(Some(identity, None)) ->
306+ | StateLoaded(IdentityOnly identity) ->
307 { model with Auth = Identified(identity, Offline); Page = Settings }, []
307 { model with Auth = Identified(identity, Offline); Page = Settings }, []
308− | StateLoaded None -> model, []
308+ | StateLoaded NothingSaved -> model, []
309
309
310 // ── mapCmd ──
310 // ── mapCmd ──
311
311
@@ -411,8 +411,12 @@ module App =
411 | CmdLoadState ->
411 | CmdLoadState ->
412 asyncCmd (async {
412 asyncCmd (async {
413 match! Store.loadIdentity () with
413 match! Store.loadIdentity () with
414+ | Some id ->
414− | Some id -> return StateLoaded(Some(id, Store.loadData ()))
415+ return StateLoaded(
416+ match Store.loadData () with
417+ | Some data -> IdentityAndData(id, data)
418+ | None -> IdentityOnly id)
415− | None -> return StateLoaded None
419+ | None -> return StateLoaded NothingSaved
416 })
420 })
417
421
418 | CmdSaveIdentity identity ->
422 | CmdSaveIdentity identity ->
@@ -449,32 +453,34 @@ module App =
449 .font(size = 24.)
453 .font(size = 24.)
450 .centerTextHorizontal()
454 .centerTextHorizontal()
451
455
456+ match model.Auth with
457+ | Identified(id, conn) ->
452− Label("Your Public Key:")
458+ Label("Your Public Key:")
453
459
454− Label(truncOb (hexToOb (pubKeyHex model)))
460+ Label(truncOb (hexToOb id.PubKeyHex))
455− .font(size = 14.)
461+ .font(size = 14.)
456
462
457− Button("Copy Public Key", CopyPubKey)
463+ Button("Copy Public Key", CopyPubKey)
458
464
459− Label("Server:")
465+ Label("Server:")
460
466
461− Entry(model.ServerUrl, SetServerUrl)
467+ Entry(model.ServerUrl, SetServerUrl)
462
468
463− match model.Error with
469+ match model.Error with
464− | Some err ->
470+ | Some err ->
465− Label(err)
471+ Label(err)
466− .textColor(Colors.Red)
472+ .textColor(Colors.Red)
467− .font(size = 12.)
473+ .font(size = 12.)
468
474
469− Button("Dismiss", DismissError)
475+ Button("Dismiss", DismissError)
470− | None -> ()
476+ | None -> ()
471
477
472− match model.Auth with
478+ match conn with
473− | Identified(_, Offline) -> Button("Connect", DoConnect)
479+ | Offline -> Button("Connect", DoConnect)
474− | Identified(_, Connecting) -> Label("Connecting...").centerTextHorizontal()
480+ | Connecting -> Label("Connecting...").centerTextHorizontal()
475− | Identified(_, Online _) ->
481+ | Online _ ->
476− Button("Disconnect", DoDisconnect)
482+ Button("Disconnect", DoDisconnect)
477− Button("Go to Conversations", SetPage Conversations)
483+ Button("Go to Conversations", SetPage Conversations)
478 | NoIdentity -> ()
484 | NoIdentity -> ()
479 })
485 })
480 .padding(20.)
486 .padding(20.)