From 7241d112b09aa0a9ba27ffa4e76024252e21d9af Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Fri, 15 Mar 2019 17:44:54 -0700 Subject: [PATCH] Add notification counts to page title. --- src/Main.elm | 12 +++++++++--- src/Scylla/Sync.elm | 26 ++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/src/Main.elm b/src/Main.elm index 4bc85d1..37a0901 100644 --- a/src/Main.elm +++ b/src/Main.elm @@ -60,9 +60,15 @@ init _ url key = view : Model -> Browser.Document Msg view m = - { title = "Scylla" - , body = viewFull m - } + let + notificationString = totalNotificationCountString m.sync + titleString = case notificationString of + Nothing -> "Scylla" + Just s -> s ++ " Scylla" + in + { title = titleString + , body = viewFull m + } update : Msg -> Model -> (Model, Cmd Msg) update msg model = case msg of diff --git a/src/Scylla/Sync.elm b/src/Scylla/Sync.elm index 2b9dbad..40a412b 100644 --- a/src/Scylla/Sync.elm +++ b/src/Scylla/Sync.elm @@ -493,6 +493,32 @@ joinedRoomsTimelineEvents s = <| Maybe.map (Dict.map (\k v -> Maybe.withDefault [] <| Maybe.andThen .events v.timeline)) <| Maybe.andThen .join s.rooms +totalNotificationCountString : SyncResponse -> Maybe String +totalNotificationCountString sr = + let + (h, n) = totalNotificationCounts sr + suffix = case h of + 0 -> "" + _ -> "!" + in + case n of + 0 -> Nothing + _ -> Just <| "(" ++ String.fromInt n ++ suffix ++ ")" + +totalNotificationCounts : SyncResponse -> (Int, Int) +totalNotificationCounts sr = + let + rooms = Maybe.withDefault [] + <| Maybe.map (Dict.values) + <| Maybe.andThen (.join) sr.rooms + zeroDefault = Maybe.withDefault 0 + getCounts = Maybe.map (\cs -> (zeroDefault cs.highlightCount, zeroDefault cs.notificationCount)) + << .unreadNotifications + sumCounts (h1, n1) (h2, n2) = (h1 + h2, n1 + n2) + in + List.foldl sumCounts (0, 0) + <| List.filterMap getCounts rooms + -- Business Logic: Room Info roomAccountData : JoinedRoom -> String -> Maybe Decode.Value roomAccountData jr et =