1module UI.Notifications exposing (Model, dismiss, show, showWithModel)
2
3import Notifications exposing (..)
4import Process
5import Task
6import UI.Types exposing (Msg(..))
7
8
9
10-- 🌳
11
12
13type alias Model =
14 List (Notification Msg)
15
16
17
18-- 📣
19
20
21dismiss : Model -> { id : Int } -> ( Model, Cmd Msg )
22dismiss collection { id } =
23 ( List.map
24 (\notification ->
25 if Notifications.id notification == id then
26 Notifications.dismiss notification
27
28 else
29 notification
30 )
31 collection
32 , Task.perform
33 (\_ -> RemoveNotification { id = id })
34 (Process.sleep 500)
35 )
36
37
38show : Notification Msg -> Model -> ( Model, Cmd Msg )
39show notification collection =
40 let
41 existingNotificationIds =
42 List.map Notifications.id collection
43 in
44 if List.member (Notifications.id notification) existingNotificationIds then
45 -- Don't show duplicate notifications
46 ( collection
47 , Cmd.none
48 )
49
50 else
51 ( notification :: collection
52 -- Hide notification after a certain amount of time,
53 -- unless it's a sticky notification.
54 , if (Notifications.options notification).sticky then
55 Cmd.none
56
57 else
58 Task.perform
59 (\_ -> DismissNotification { id = Notifications.id notification })
60 (Process.sleep 7500)
61 )
62
63
64showWithModel : Model -> Notification Msg -> ( Model, Cmd Msg )
65showWithModel model notification =
66 show notification model