Skip to main content

cosmic_applet_avestruz/views/
notifications.rs

1// SPDX-License-Identifier: MIT
2
3//! The notifications view for Avestruz: what others did that concerns the
4//! account, newest first, in a scroller.
5//!
6//! Above the list, a control chooses between all of it and one kind:
7//! what was written to the account, what was made of its notes, what it
8//! was zapped. What is new since the notifications were last looked at
9//! is picked out, for as long as this visit lasts (see
10//! [`crate::handlers::notifications`]).
11
12use cosmic::Element;
13use cosmic::iced::{Alignment, Length};
14use cosmic::widget::{self, button, container, text};
15use nostr_sdk::prelude::EventId;
16
17use crate::fl;
18use crate::messages::Message;
19use crate::nostr::notifications::{Notifications, Tab};
20use crate::state::{AppModel, NotificationsPage};
21use crate::views::components::{
22    VirtualList, estimated_notification_height, list_scroller, marking, notification_row, status_line,
23};
24use crate::views::feed::LOAD_OLDER_WITHIN;
25
26/// How wide each choice of which notifications show is, at the least.
27const TAB_WIDTH: u16 = 76;
28
29impl AppModel {
30    /// The line above the notifications: the way back, and what this is.
31    fn view_notifications_header(&self) -> Element<'_, Message> {
32        let back = button::icon(widget::icon::from_name("go-previous-symbolic"))
33            .tooltip(fl!("notifications-back"))
34            .on_press(Message::ClosePage);
35        let title = container(text(fl!("notifications-title")).size(16).font(cosmic::font::bold())).width(Length::Fill);
36        widget::Row::new().push(back).push(title).spacing(8).align_y(Alignment::Center).padding([4, 12]).into()
37    }
38
39    /// The choice of which notifications show, in the middle of its line.
40    fn view_notification_tabs(&self) -> Element<'_, Message> {
41        let tabs = widget::segmented_control::horizontal(&self.notification_tabs)
42            .on_activate(Message::SelectNotificationsTab)
43            .minimum_button_width(TAB_WIDTH)
44            .width(Length::Shrink);
45        container(tabs).center_x(Length::Fill).padding([0, 12, 6, 12]).into()
46    }
47
48    /// What is said in place of the list when the tab on show has
49    /// nothing: that it is being looked for, that it could not be, or
50    /// that there is nothing.
51    fn empty_notifications_text(&self, tab: Tab) -> String {
52        let state = &self.notifications;
53        if state.failed {
54            fl!("notifications-failed")
55        } else if !state.settled {
56            fl!("notifications-loading")
57        } else {
58            match tab {
59                Tab::All => fl!("notifications-empty"),
60                Tab::Mentions => fl!("notifications-empty-mentions"),
61                Tab::Reactions => fl!("notifications-empty-reactions"),
62                Tab::Zaps => fl!("notifications-empty-zaps"),
63            }
64        }
65    }
66
67    /// The notifications the tab on show has, in the scroller, and under
68    /// them what is going on at their end.
69    fn view_notifications_list<'a>(&'a self, page: &'a NotificationsPage) -> Element<'a, Message> {
70        let state = &self.notifications;
71        let tab = self.notifications_tab();
72        let listed = state.list.listed(tab);
73        if listed.is_empty() {
74            return status_line(self.empty_notifications_text(tab));
75        }
76
77        let context = self.row_context(&page.expanded, page.marked);
78        let sizes = context.sizes;
79        let (told, seen_before) = (&state.list, state.seen_before);
80        let list: VirtualList<'_, usize, _, Message, cosmic::Theme, cosmic::Renderer> = VirtualList::new(
81            listed,
82            &page.heights,
83            // A place that holds nothing has no row, and is nobody's.
84            move |place: &usize| told.get(*place).map_or(EventId::from_byte_array([0; 32]), |notification| notification.id),
85            move |place, width| {
86                told.get(*place).map_or(0.0, |notification| {
87                    estimated_notification_height(notification, sizes, width, context.expanded.has(&notification.id))
88                })
89            },
90            move |_index, place| match told.get(*place) {
91                Some(notification) => marking(
92                    notification_row(notification, &context, Notifications::is_new(notification, seen_before)),
93                    notification.id,
94                    &context,
95                ),
96                None => widget::Space::new().into(),
97            },
98        )
99        .on_near_end(LOAD_OLDER_WITHIN, Message::LoadOlderNotifications);
100
101        let end = if state.loading_older {
102            Some(status_line(fl!("notifications-loading-older")))
103        } else if state.end_reached {
104            Some(status_line(fl!("notifications-end")))
105        } else {
106            None
107        };
108        list_scroller(&page.position, widget::Column::new().push(list).push_maybe(end))
109    }
110
111    /// Render the notifications view, of the page `page`.
112    pub fn view_notifications<'a>(&'a self, page: &'a NotificationsPage) -> Element<'a, Message> {
113        widget::Column::new()
114            .push(self.view_notifications_header())
115            .push(self.view_notification_tabs())
116            .push(widget::divider::horizontal::default())
117            .push(self.view_notifications_list(page))
118            .into()
119    }
120}