-
Notifications
You must be signed in to change notification settings - Fork 0
/
Draft.elm
754 lines (567 loc) · 16.1 KB
/
Draft.elm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
module Main exposing (main)
import Browser
import Dict exposing (Dict)
import Element
exposing
( Attribute
, Element
, alignLeft
, alignRight
, centerX
, centerY
, clip
, column
, el
, fill
, height
, image
, padding
, paddingXY
, transparent
, wrappedRow
, pointer
, px
, rgb255
, rgba255
, row
, spacing
, text
, width
)
import Element.Background as Background
import Element.Border as Border
import Element.Events exposing (onClick)
import Element.Font as Font
import Element.Input as Input
import Html exposing (Html)
-- Model
type alias ID =
String
type alias URL =
String
type alias MessageID =
String
type Container
= Horizontal (List Container)
| Vertical (List Container)
| Node ID Chat
type alias LoginData =
{ login : Maybe String
, password : Maybe String
}
type alias LoggedData =
{ contacts : Dict ID Contact
, profile : Contact
, chats : Container
}
type Message
= Incoming String
| Sending String
type MessageBuilder
= Text String MessageBuilder
| Emote String MessageBuilder
| Link String MessageBuilder
| MessageStart
type Status
= Online
| Away
| Busy
| Offline
type alias Contact =
{ nickname : String
, realName : String
, avatar : URL
, status : Status
}
type alias Chat =
{ contactId : ID
, messages : Dict MessageID Message
, textArea : MessageBuilder
}
type Model
= LoginPage LoginData
| LoggedPage LoggedData
init : () -> UpdateResponse msg
init _ =
( LoggedPage loggedDataDummy, Cmd.none )
stringToMaybeString : String -> Maybe String
stringToMaybeString str =
if str == "" then
Nothing
else
Just str
statusToString : Status -> String
statusToString status =
case status of
Online ->
"Online"
Away ->
"Away"
Busy ->
"Busy"
Offline ->
"Offline"
statusToColor : Status -> Attribute Msg
statusToColor status =
case status of
Online ->
Font.color <| rgb255 36 199 71
Away ->
Font.color <| rgb255 255 165 0
Busy ->
Font.color <| rgb255 185 0 0
Offline ->
Font.color <| rgb255 128 128 128
-- Msg
type Msg
= OpenChat ID
| SendMsg ID Message
| ReceiveMsg ID Message
| Login String String -- Username and Password
| ChangeStatus Status
| ContactStatusChange ID Status
| ContactAdd ID String -- Contact's ID and Nickname
| ContactRemove ID -- Contact's ID
| UpdateLoginField String
| UpdatePasswordField String
-- Update
type alias UpdateResponse msg =
( Model, Cmd msg )
-- Login
contactDummy =
{ nickname = "Max Hero"
, realName = "Marcelo Amancio"
, avatar = "https://avatarfiles.alphacoders.com/202/202499.png"
, status = Online
}
contactListDummy : Dict ID Contact
contactListDummy =
Dict.fromList
[ ( "0", contactDummy )
, ( "1", contactDummy )
, ( "2", contactDummy )
, ( "3", contactDummy )
, ( "4", contactDummy )
, ( "5", contactDummy )
, ( "6", contactDummy )
]
messagesDummy =
Dict.fromList
[ ("0", Incoming "Uma monad é apenas uma monoid na categoria dos endofunctors")
, ("1", Sending "kek")
]
chatDummy =
{ contactId = ""
, messages = messagesDummy
, textArea = MessageStart
}
loggedDataDummy =
{ contacts = contactListDummy
, profile = contactDummy
, chats = Vertical [ Node "0" chatDummy, Node "1" chatDummy]
}
doLogin : String -> String -> Model -> UpdateResponse msg
doLogin username password model =
( LoggedPage loggedDataDummy, Cmd.none )
doUpdateLoginField : String -> Model -> UpdateResponse msg
doUpdateLoginField username model =
case model of
LoginPage data ->
( LoginPage { data | login = stringToMaybeString username }, Cmd.none )
_ ->
( model, Cmd.none )
doUpdatePasswordField : String -> Model -> UpdateResponse msg
doUpdatePasswordField password model =
case model of
LoginPage data ->
( LoginPage { data | password = stringToMaybeString password }, Cmd.none )
_ ->
( model, Cmd.none )
loginPageUpdate : Msg -> LoginData -> Model -> UpdateResponse msg
loginPageUpdate msg data model =
case msg of
Login username password ->
doLogin username password model
UpdateLoginField username ->
doUpdateLoginField username model
UpdatePasswordField password ->
doUpdatePasswordField password model
_ ->
( model, Cmd.none )
-- Logged
doOpenChat : ID -> LoggedData -> UpdateResponse msg
doOpenChat contactId data =
( LoggedPage data, Cmd.none )
doSendMessage : ID -> Message -> LoggedData -> UpdateResponse msg
doSendMessage contactId message data =
( LoggedPage data, Cmd.none )
handleReceiveMessage : ID -> Message -> LoggedData -> UpdateResponse msg
handleReceiveMessage contactId message data =
( LoggedPage data, Cmd.none )
doChangeStatus : Status -> LoggedData -> UpdateResponse msg
doChangeStatus status data =
( LoggedPage data, Cmd.none )
handleContactStatusChange : ID -> Status -> LoggedData -> UpdateResponse msg
handleContactStatusChange contactId status data =
( LoggedPage data, Cmd.none )
doContactAdd : ID -> String -> LoggedData -> UpdateResponse msg
doContactAdd contactId nickname data =
( LoggedPage data, Cmd.none )
doContactRemove : ID -> LoggedData -> UpdateResponse msg
doContactRemove contactId data =
( LoggedPage data, Cmd.none )
loggedPageUpdate : Msg -> LoggedData -> Model -> UpdateResponse msg
loggedPageUpdate msg data model =
case msg of
OpenChat contactId ->
doOpenChat contactId data
SendMsg contactId message ->
doSendMessage contactId message data
ReceiveMsg contactId message ->
handleReceiveMessage contactId message data
ChangeStatus status ->
doChangeStatus status data
ContactStatusChange contactId status ->
handleContactStatusChange contactId status data
ContactAdd contactId nickname ->
doContactAdd contactId nickname data
ContactRemove contactId ->
doContactRemove contactId data
_ ->
( model, Cmd.none )
update : Msg -> Model -> UpdateResponse msg
update msg model =
case model of
LoginPage data ->
loginPageUpdate msg data model
LoggedPage data ->
loggedPageUpdate msg data model
-- Login View
loginFieldInputProps onChange label content =
{ onChange = onChange
, text = Maybe.withDefault "" content
, placeholder = Nothing
, label = Input.labelAbove [] (text label)
}
loginFieldInput : Maybe String -> String -> (String -> Msg) -> Element Msg
loginFieldInput content label onChange =
content
|> loginFieldInputProps onChange label
|> Input.text []
usernameInput : Maybe String -> Element Msg
usernameInput login =
loginFieldInput login "Username" UpdateLoginField
passwordInputProps password =
{ onChange = UpdatePasswordField
, text = Maybe.withDefault "" password
, placeholder = Nothing
, label = Input.labelAbove [] (text "Password")
, show = False
}
passwordInput : Maybe String -> Element Msg
passwordInput password =
password
|> passwordInputProps
|> Input.newPassword []
loginMessage : LoginData -> Maybe Msg
loginMessage { login, password } =
case ( login, password ) of
( Just actualLogin, Just actualPassword ) ->
Just (Login actualLogin actualPassword)
_ ->
Nothing
shouldDisableLogin : LoginData -> Bool
shouldDisableLogin { login, password } =
case ( login, password ) of
( Just actualLogin, Just actualPassword ) ->
True
_ ->
False
loginButton : LoginData -> Element Msg
loginButton data =
Input.button
[ Background.color (rgb255 32 32 32)
, Font.color (rgb255 200 200 200)
, alignRight
, paddingXY 8 8
, Border.rounded 8
]
{ onPress = loginMessage data
, label = text "Login"
}
viewLoginStyle : List (Attribute Msg)
viewLoginStyle =
[ centerX
, centerY
]
viewLoginForm : LoginData -> Element Msg
viewLoginForm data =
column [ spacing 8 ]
[ usernameInput data.login
, passwordInput data.password
, loginButton data
]
viewLogin : LoginData -> Element Msg
viewLogin data =
el viewLoginStyle (viewLoginForm data)
-- Logged View
userProfile : LoggedData -> Element Msg
userProfile data =
row [ paddingXY 8 8
, spacing 8
]
[ el
[ Border.rounded 24
, clip
, width (px 128)
, height (px 128)
]
(image
[ width fill, height fill ]
{ src = data.profile.avatar
, description = "User Avatar"
}
)
, column [ spacing 8 ]
[ el
[ Font.size 24
, Font.color (rgb255 196 196 196)
]
(text data.profile.nickname)
, el
[ Font.size 18
, statusToColor data.profile.status
]
(text <| statusToString data.profile.status)
]
]
contactListMember : ID -> Contact -> Element Msg
contactListMember id contact =
row
[ paddingXY 8 8
, spacing 8
, Background.color (rgb255 56 56 56)
, Border.rounded 8
, width (px 328)
, onClick (OpenChat id)
, pointer
]
[ el
[ Border.rounded 8
, clip
, width (px 64)
, height (px 64)
]
(image
[ width fill, height fill ]
{ src = contact.avatar, description = "Contact Avatar" }
)
, column
[ spacing 4 ]
[ el [ Font.size 18 ] (text contact.nickname)
, el
[ Font.size 16, statusToColor contact.status ]
(text <| statusToString contact.status)
]
]
contactReducer : ID -> Contact -> List (Element Msg) -> List (Element Msg)
contactReducer id contact acc =
contactListMember id contact :: acc
contactListStyles : List (Attribute Msg)
contactListStyles =
[ paddingXY 8 8
, Background.color (rgb255 71 71 71)
, Border.rounded 8
, spacing 8
, width fill
, height fill
]
contactList : LoggedData -> Element Msg
contactList data =
data
|> .contacts
|> Dict.foldl contactReducer []
|> column contactListStyles
sidebarStyles : List (Attribute Msg)
sidebarStyles =
[ Background.color (rgb255 64 64 64)
, width (px 360)
, height fill
, paddingXY 8 8
]
sidebar : LoggedData -> Element Msg
sidebar data =
data
|> contactList
|> List.singleton
|> (::) (userProfile data)
|> column sidebarStyles
incomingMessage : MessageID -> String -> Element Msg
incomingMessage id str =
wrappedRow
[ Border.roundEach
{ topLeft = 0
, topRight = 8
, bottomLeft = 8
, bottomRight = 8
}
, padding 8
, alignLeft
, Background.color (rgb255 71 71 71)
, Font.size 16
]
[text str]
sendingMessage : MessageID -> String -> Element Msg
sendingMessage id str =
wrappedRow
[ Border.roundEach
{ topLeft = 8
, topRight = 0
, bottomLeft = 8
, bottomRight = 8
}
, padding 8
, alignRight
, Background.color (rgb255 104 104 104)
, Font.size 16
]
[text str]
messageView : MessageID -> Message -> List (Element Msg) -> List (Element Msg)
messageView id message acc =
case message of
Incoming str ->
incomingMessage id str :: acc
Sending str ->
sendingMessage id str :: acc
chatHeader : Chat -> LoggedData -> Element Msg
chatHeader chat =
.contacts
>> Dict.get chat.contactId
>> Maybe.withDefault contactDummy
>> contactListMember chat.contactId
chatMessages : Dict ID Message -> Element Msg
chatMessages messages =
column
[ Background.color (rgb255 40 40 40)
, Border.rounded 8
, spacing 8
, padding 8
, width fill
, height fill
, Border.solid
, Border.color (rgb255 196 196 196)
]
(Dict.foldr messageView [] messages)
chatMessageBox : ID -> Chat -> LoggedData -> Element Msg
chatMessageBox id chat data =
el
[ padding 8
, width fill
, Border.rounded 8
, Background.color (rgb255 40 40 40)
]
(column
[ width fill
, spacing 8
]
[ Input.text
[ Font.color (rgb255 39 39 39)
, Background.color (rgba255 0 0 0 0)
, Border.color (rgba255 0 0 0 0)
, Font.size 16
]
{ onChange = UpdatePasswordField
, text = ""
, placeholder = Just <| Input.placeholder [] <| text "Digite uma mensagem para enviar"
, label = Input.labelHidden "Digite uma mensagem para enviar"
}
, row
[ width fill, spacing 8 ]
[ Input.button
[ Border.rounded 8
, width (px 32)
, height (px 32)
, Background.color (rgb255 71 71 71)
, alignRight
]
{ onPress = Nothing
, label = text ""
}
, Input.button
[ Border.rounded 8
, width (px 32)
, height (px 32)
, Background.color (rgb255 71 71 71)
, alignRight
]
{ onPress = Nothing
, label = text ""
}
]
])
currentChatMapper : LoggedData -> Container -> Element Msg
currentChatMapper data container =
case container of
Horizontal chats ->
row [ spacing 8, width fill, height fill ]
(List.map (currentChatMapper data) chats)
Vertical chats ->
column [ spacing 8, width fill, height fill ]
(List.map (currentChatMapper data) chats)
Node id chat ->
column
[ Border.rounded 8
, Background.color (rgb255 56 56 56)
, width fill
, height fill
, padding 8
, spacing 8
]
[ chatHeader chat data
, chatMessages chat.messages
, chatMessageBox id chat data
]
currentChatsStyles : List (Attribute Msg)
currentChatsStyles =
[ width fill
, height fill
, padding 8
]
currentChats : LoggedData -> Element Msg
currentChats data =
data
|> .chats
|> currentChatMapper data
|> el currentChatsStyles
viewLogged : LoggedData -> Element Msg
viewLogged data =
row
[ Font.color (rgb255 196 196 196)
, Background.color (rgb255 39 39 39)
, width fill
, height fill
]
[ sidebar data
, currentChats data
]
-- View
view : Model -> Element Msg
view model =
case model of
LoginPage data ->
viewLogin data
LoggedPage data ->
viewLogged data
-- Subs
subs : Model -> Sub msg
subs _ =
Sub.none
-- Main
main : Program () Model Msg
main =
Browser.element
{ init = init
, view = view >> Element.layout []
, update = update
, subscriptions = subs
}