cosmic_applet_avestruz/views/
notifications.rs1use 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
26const TAB_WIDTH: u16 = 76;
28
29impl AppModel {
30 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 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 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 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 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(¬ification.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 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}