@@ -40,8 +40,8 @@ module App =
4040 type Page =
4141 | Setup
4242 | Conversations
43− | Chat of pubkey: string * compose: string
44− | AddContact of pubkey: string * nickname: string
43+ | Chat of pubkey: string
44+ | AddContact
4545 | Settings
4646
4747 type Session = { Url: string; Token: string; Identity: Identity }
@@ -75,7 +75,10 @@ module App =
7575 PollStatus: PollStatus
7676 Messages: Map<string, ChatMessage list>
7777 Error: string option
78− PollCursor: int64 }
78+ PollCursor: int64
79+ Compose: string
80+ ContactPubkey: string
81+ ContactNickname: string }
7982
8083 // ── Msg ──
8184
@@ -173,6 +176,11 @@ module App =
173176 let private saveCmdMsg model =
174177 CmdSaveData { Contacts = model.Contacts; Messages = model.Messages; ServerUrl = Some model.ServerUrl; PollCursor = model.PollCursor }
175178
179+ let private connStatusLabel = function
180+ | NoIdentity | Identified(_, Offline) -> "OFFLINE"
181+ | Identified(_, Connecting) -> "CONNECTING"
182+ | Identified(_, Online _) -> "ONLINE"
183+
176184 // ── Init ──
177185
178186 let init () =
@@ -183,31 +191,25 @@ module App =
183191 Contacts = []
184192 Messages = Map.empty
185193 Error = None
186− PollCursor = 0L },
194+ PollCursor = 0L
195+ Compose = ""
196+ ContactPubkey = ""
197+ ContactNickname = "" },
187198 [ CmdLoadState ]
188199
189200 // ── Update ──
190201
191202 let update msg model =
192203 match msg with
193− | SetPage(Chat(pk, _)) ->
194− { model with Page = Chat(pk, "") }, []
195− | SetPage(AddContact _) ->
196− { model with Page = AddContact("", "") }, []
204+ | SetPage(Chat pk) ->
205+ { model with Page = Chat pk; Compose = "" }, []
206+ | SetPage AddContact ->
207+ { model with Page = AddContact; ContactPubkey = ""; ContactNickname = "" }, []
197208 | SetPage page -> { model with Page = page }, []
198209
199− | SetCompose text ->
200− match model.Page with
201− | Chat(pk, _) -> { model with Page = Chat(pk, text) }, []
202− | _ -> model, []
203− | SetContactPubkey pk ->
204− match model.Page with
205− | AddContact(_, nn) -> { model with Page = AddContact(pk, nn) }, []
206− | _ -> model, []
207− | SetContactNickname nn ->
208− match model.Page with
209− | AddContact(pk, _) -> { model with Page = AddContact(pk, nn) }, []
210− | _ -> model, []
210+ | SetCompose text -> { model with Compose = text }, []
211+ | SetContactPubkey pk -> { model with ContactPubkey = pk }, []
212+ | SetContactNickname nn -> { model with ContactNickname = nn }, []
211213
212214 | GenIdentity -> model, [ CmdGenIdentity ]
213215
@@ -237,20 +239,20 @@ module App =
237239
238240 | DoSend ->
239241 match model.Page, trySession model with
240− | Chat(pk, compose), Some session when compose <> "" ->
242+ | Chat pk, Some session when model.Compose <> "" ->
241243 let id = Guid.NewGuid().ToString()
242244 let outMsg =
243245 { Id = id
244− Body = compose
246+ Body = model.Compose
245247 Timestamp = DateTimeOffset.UtcNow
246248 IsOutgoing = true }
247249
248250 let model' =
249251 { model with
250− Page = Chat(pk, "")
252+ Compose = ""
251253 Messages = model.Messages |> appendMessage pk outMsg }
252254
253− model', [ CmdSend(session, pk, compose, id); saveCmdMsg model' ]
255+ model', [ CmdSend(session, pk, model.Compose, id); saveCmdMsg model' ]
254256 | _ -> model, []
255257
256258 | SendFailed err -> { model with Error = Some err }, []
@@ -272,11 +274,10 @@ module App =
272274 if not incoming.IsEmpty then saveCmdMsg model' ]
273275
274276 | DoSaveContact ->
275− match model.Page with
276− | AddContact(pk, nn) when pk <> "" && nn <> "" ->
277− match tryParsePubkey pk with
277+ if model.ContactPubkey <> "" && model.ContactNickname <> "" then
278+ match tryParsePubkey model.ContactPubkey with
278279 | Some bytes ->
279− let contact: Contact = { Pubkey = Crypto.toHex bytes; Nickname = nn }
280+ let contact: Contact = { Pubkey = Crypto.toHex bytes; Nickname = model.ContactNickname }
280281
281282 let model' =
282283 { model with
@@ -284,8 +285,9 @@ module App =
284285 Page = Conversations }
285286
286287 model', [ saveCmdMsg model' ]
287− | _ -> { model with Error = Some "Invalid public key" }, []
288− | _ -> model, []
288+ | None -> { model with Error = Some "Invalid public key" }, []
289+ else
290+ model, []
289291
290292 | CopyPubKey ->
291293 match model.Auth with
@@ -501,12 +503,6 @@ module App =
501503 )
502504
503505 let private viewConversations model =
504− let connStr =
505− match model.Auth with
506− | NoIdentity | Identified(_, Offline) -> "OFFLINE"
507− | Identified(_, Connecting) -> "CONNECTING"
508− | Identified(_, Online _) -> "ONLINE"
509−
510506 ContentPage(
511507 ScrollView(
512508 (VStack(spacing = 8.) {
@@ -514,11 +510,11 @@ module App =
514510 Label("Conversations")
515511 .font(size = 24.)
516512
517− Button("+", SetPage(AddContact("", "")))
513+ Button("+", SetPage AddContact)
518514 Button("Settings", SetPage Settings)
519515 }
520516
521− Label($"{connStr} | {formatPollStatus model.PollStatus}")
517+ Label($"{connStatusLabel model.Auth} | {formatPollStatus model.PollStatus}")
522518 .font(size = 10.)
523519 .textColor(Colors.DimGray)
524520
@@ -536,13 +532,13 @@ module App =
536532 |> Option.map (fun m -> if m.Body.Length > 40 then m.Body.[..39] + "..." else m.Body)
537533 |> Option.defaultValue ""
538534
539− Button($"{c.Nickname}\n{preview}", SetPage(Chat(c.Pubkey, "")))
535+ Button($"{c.Nickname}\n{preview}", SetPage(Chat c.Pubkey))
540536 })
541537 .padding(20.)
542538 )
543539 )
544540
545− let private viewChat model pk compose =
541+ let private viewChat model pk =
546542 let name = contactName model.Contacts pk
547543 let msgs = messagesFor pk model.Messages
548544
@@ -569,7 +565,7 @@ module App =
569565 .font(size = 14.)
570566
571567 HStack(spacing = 8.) {
572− Entry(compose, SetCompose)
568+ Entry(model.Compose, SetCompose)
573569 .placeholder("Message...")
574570
575571 Button("Send", DoSend)
@@ -578,7 +574,7 @@ module App =
578574 .padding(12.)
579575 )
580576
581− let private viewAddContact model pubkey nickname =
577+ let private viewAddContact model =
582578 ContentPage(
583579 (VStack(spacing = 16.) {
584580 HStack(spacing = 8.) {
@@ -591,11 +587,11 @@ module App =
591587 viewErrorBanner model.Error
592588
593589 Label("Public Key:")
594− Entry(pubkey, SetContactPubkey)
590+ Entry(model.ContactPubkey, SetContactPubkey)
595591 .placeholder("sampel-palnet-...")
596592
597593 Label("Nickname:")
598− Entry(nickname, SetContactNickname)
594+ Entry(model.ContactNickname, SetContactNickname)
599595
600596 Button("Save Contact", DoSaveContact)
601597 .centerHorizontal()
@@ -609,8 +605,8 @@ module App =
609605 | Setup -> viewSetup ()
610606 | Settings -> viewSettings model
611607 | Conversations -> viewConversations model
612− | Chat(pk, compose) -> viewChat model pk compose
613− | AddContact(pubkey, nickname) -> viewAddContact model pubkey nickname
608+ | Chat pk -> viewChat model pk
609+ | AddContact -> viewAddContact model
614610
615611 Application() { Window(page) }
616612