Load more notifications when scrolling down

This commit is contained in:
Max Brunsfeld 2023-10-22 17:10:21 +02:00
parent 23400a5a70
commit 59adcc1744
6 changed files with 97 additions and 30 deletions

View File

@ -3184,7 +3184,10 @@ async fn get_notifications(
.map(|id| db::NotificationId::from_proto(id)), .map(|id| db::NotificationId::from_proto(id)),
) )
.await?; .await?;
response.send(proto::GetNotificationsResponse { notifications })?; response.send(proto::GetNotificationsResponse {
done: notifications.len() < NOTIFICATION_COUNT_PER_PAGE,
notifications,
})?;
Ok(()) Ok(())
} }

View File

@ -134,7 +134,7 @@ impl ChatPanel {
ListState::<Self>::new(0, Orientation::Bottom, 1000., move |this, ix, cx| { ListState::<Self>::new(0, Orientation::Bottom, 1000., move |this, ix, cx| {
this.render_message(ix, cx) this.render_message(ix, cx)
}); });
message_list.set_scroll_handler(|visible_range, this, cx| { message_list.set_scroll_handler(|visible_range, _, this, cx| {
if visible_range.start < MESSAGE_LOADING_THRESHOLD { if visible_range.start < MESSAGE_LOADING_THRESHOLD {
this.load_more_messages(&LoadMoreMessages, cx); this.load_more_messages(&LoadMoreMessages, cx);
} }

View File

@ -1,7 +1,4 @@
use crate::{ use crate::{chat_panel::ChatPanel, format_timestamp, render_avatar, NotificationPanelSettings};
chat_panel::ChatPanel, format_timestamp, is_channels_feature_enabled, render_avatar,
NotificationPanelSettings,
};
use anyhow::Result; use anyhow::Result;
use channel::ChannelStore; use channel::ChannelStore;
use client::{Client, Notification, User, UserStore}; use client::{Client, Notification, User, UserStore};
@ -29,6 +26,7 @@ use workspace::{
Workspace, Workspace,
}; };
const LOADING_THRESHOLD: usize = 30;
const MARK_AS_READ_DELAY: Duration = Duration::from_secs(1); const MARK_AS_READ_DELAY: Duration = Duration::from_secs(1);
const TOAST_DURATION: Duration = Duration::from_secs(5); const TOAST_DURATION: Duration = Duration::from_secs(5);
const NOTIFICATION_PANEL_KEY: &'static str = "NotificationPanel"; const NOTIFICATION_PANEL_KEY: &'static str = "NotificationPanel";
@ -98,11 +96,21 @@ impl NotificationPanel {
}) })
.detach(); .detach();
let notification_list = let mut notification_list =
ListState::<Self>::new(0, Orientation::Top, 1000., move |this, ix, cx| { ListState::<Self>::new(0, Orientation::Top, 1000., move |this, ix, cx| {
this.render_notification(ix, cx) this.render_notification(ix, cx)
.unwrap_or_else(|| Empty::new().into_any()) .unwrap_or_else(|| Empty::new().into_any())
}); });
notification_list.set_scroll_handler(|visible_range, count, this, cx| {
if count.saturating_sub(visible_range.end) < LOADING_THRESHOLD {
if let Some(task) = this
.notification_store
.update(cx, |store, cx| store.load_more_notifications(false, cx))
{
task.detach();
}
}
});
let mut this = Self { let mut this = Self {
fs, fs,
@ -653,15 +661,14 @@ impl Panel for NotificationPanel {
fn set_active(&mut self, active: bool, cx: &mut ViewContext<Self>) { fn set_active(&mut self, active: bool, cx: &mut ViewContext<Self>) {
self.active = active; self.active = active;
if active { if self.notification_store.read(cx).notification_count() == 0 {
if !is_channels_feature_enabled(cx) { cx.emit(Event::Dismissed);
cx.emit(Event::Dismissed);
}
} }
} }
fn icon_path(&self, cx: &gpui::WindowContext) -> Option<&'static str> { fn icon_path(&self, cx: &gpui::WindowContext) -> Option<&'static str> {
(settings::get::<NotificationPanelSettings>(cx).button && is_channels_feature_enabled(cx)) (settings::get::<NotificationPanelSettings>(cx).button
&& self.notification_store.read(cx).notification_count() > 0)
.then(|| "icons/bell.svg") .then(|| "icons/bell.svg")
} }

View File

@ -30,7 +30,7 @@ struct StateInner<V> {
orientation: Orientation, orientation: Orientation,
overdraw: f32, overdraw: f32,
#[allow(clippy::type_complexity)] #[allow(clippy::type_complexity)]
scroll_handler: Option<Box<dyn FnMut(Range<usize>, &mut V, &mut ViewContext<V>)>>, scroll_handler: Option<Box<dyn FnMut(Range<usize>, usize, &mut V, &mut ViewContext<V>)>>,
} }
#[derive(Clone, Copy, Debug, Default, PartialEq)] #[derive(Clone, Copy, Debug, Default, PartialEq)]
@ -420,7 +420,7 @@ impl<V: 'static> ListState<V> {
pub fn set_scroll_handler( pub fn set_scroll_handler(
&mut self, &mut self,
handler: impl FnMut(Range<usize>, &mut V, &mut ViewContext<V>) + 'static, handler: impl FnMut(Range<usize>, usize, &mut V, &mut ViewContext<V>) + 'static,
) { ) {
self.0.borrow_mut().scroll_handler = Some(Box::new(handler)) self.0.borrow_mut().scroll_handler = Some(Box::new(handler))
} }
@ -533,7 +533,12 @@ impl<V: 'static> StateInner<V> {
if self.scroll_handler.is_some() { if self.scroll_handler.is_some() {
let visible_range = self.visible_range(height, scroll_top); let visible_range = self.visible_range(height, scroll_top);
self.scroll_handler.as_mut().unwrap()(visible_range, view, cx); self.scroll_handler.as_mut().unwrap()(
visible_range,
self.items.summary().count,
view,
cx,
);
} }
cx.notify(); cx.notify();

View File

@ -21,6 +21,7 @@ pub struct NotificationStore {
channel_messages: HashMap<u64, ChannelMessage>, channel_messages: HashMap<u64, ChannelMessage>,
channel_store: ModelHandle<ChannelStore>, channel_store: ModelHandle<ChannelStore>,
notifications: SumTree<NotificationEntry>, notifications: SumTree<NotificationEntry>,
loaded_all_notifications: bool,
_watch_connection_status: Task<Option<()>>, _watch_connection_status: Task<Option<()>>,
_subscriptions: Vec<client::Subscription>, _subscriptions: Vec<client::Subscription>,
} }
@ -83,9 +84,10 @@ impl NotificationStore {
let this = this.upgrade(&cx)?; let this = this.upgrade(&cx)?;
match status { match status {
client::Status::Connected { .. } => { client::Status::Connected { .. } => {
this.update(&mut cx, |this, cx| this.handle_connect(cx)) if let Some(task) = this.update(&mut cx, |this, cx| this.handle_connect(cx))
.await {
.log_err()?; task.await.log_err()?;
}
} }
_ => this.update(&mut cx, |this, cx| this.handle_disconnect(cx)), _ => this.update(&mut cx, |this, cx| this.handle_disconnect(cx)),
} }
@ -96,6 +98,7 @@ impl NotificationStore {
Self { Self {
channel_store: ChannelStore::global(cx), channel_store: ChannelStore::global(cx),
notifications: Default::default(), notifications: Default::default(),
loaded_all_notifications: false,
channel_messages: Default::default(), channel_messages: Default::default(),
_watch_connection_status: watch_connection_status, _watch_connection_status: watch_connection_status,
_subscriptions: vec![ _subscriptions: vec![
@ -142,22 +145,46 @@ impl NotificationStore {
None None
} }
pub fn load_more_notifications(&self, cx: &mut ModelContext<Self>) -> Task<Result<()>> { pub fn load_more_notifications(
let request = self &self,
.client clear_old: bool,
.request(proto::GetNotifications { before_id: None }); cx: &mut ModelContext<Self>,
cx.spawn(|this, cx| async move { ) -> Option<Task<Result<()>>> {
if self.loaded_all_notifications && !clear_old {
return None;
}
let before_id = if clear_old {
None
} else {
self.notifications.first().map(|entry| entry.id)
};
let request = self.client.request(proto::GetNotifications { before_id });
Some(cx.spawn(|this, mut cx| async move {
let response = request.await?; let response = request.await?;
Self::add_notifications(this, false, response.notifications, cx).await?; this.update(&mut cx, |this, _| {
this.loaded_all_notifications = response.done
});
Self::add_notifications(
this,
response.notifications,
AddNotificationsOptions {
is_new: false,
clear_old,
includes_first: response.done,
},
cx,
)
.await?;
Ok(()) Ok(())
}) }))
} }
fn handle_connect(&mut self, cx: &mut ModelContext<Self>) -> Task<Result<()>> { fn handle_connect(&mut self, cx: &mut ModelContext<Self>) -> Option<Task<Result<()>>> {
self.notifications = Default::default(); self.notifications = Default::default();
self.channel_messages = Default::default(); self.channel_messages = Default::default();
cx.notify(); cx.notify();
self.load_more_notifications(cx) self.load_more_notifications(true, cx)
} }
fn handle_disconnect(&mut self, cx: &mut ModelContext<Self>) { fn handle_disconnect(&mut self, cx: &mut ModelContext<Self>) {
@ -172,8 +199,12 @@ impl NotificationStore {
) -> Result<()> { ) -> Result<()> {
Self::add_notifications( Self::add_notifications(
this, this,
true,
envelope.payload.notification.into_iter().collect(), envelope.payload.notification.into_iter().collect(),
AddNotificationsOptions {
is_new: true,
clear_old: false,
includes_first: false,
},
cx, cx,
) )
.await .await
@ -193,8 +224,8 @@ impl NotificationStore {
async fn add_notifications( async fn add_notifications(
this: ModelHandle<Self>, this: ModelHandle<Self>,
is_new: bool,
notifications: Vec<proto::Notification>, notifications: Vec<proto::Notification>,
options: AddNotificationsOptions,
mut cx: AsyncAppContext, mut cx: AsyncAppContext,
) -> Result<()> { ) -> Result<()> {
let mut user_ids = Vec::new(); let mut user_ids = Vec::new();
@ -256,6 +287,20 @@ impl NotificationStore {
}) })
.await?; .await?;
this.update(&mut cx, |this, cx| { this.update(&mut cx, |this, cx| {
if options.clear_old {
cx.emit(NotificationEvent::NotificationsUpdated {
old_range: 0..this.notifications.summary().count,
new_count: 0,
});
this.notifications = SumTree::default();
this.channel_messages.clear();
this.loaded_all_notifications = false;
}
if options.includes_first {
this.loaded_all_notifications = true;
}
this.channel_messages this.channel_messages
.extend(messages.into_iter().filter_map(|message| { .extend(messages.into_iter().filter_map(|message| {
if let ChannelMessageId::Saved(id) = message.id { if let ChannelMessageId::Saved(id) = message.id {
@ -269,7 +314,7 @@ impl NotificationStore {
notifications notifications
.into_iter() .into_iter()
.map(|notification| (notification.id, Some(notification))), .map(|notification| (notification.id, Some(notification))),
is_new, options.is_new,
cx, cx,
); );
}); });
@ -406,3 +451,9 @@ impl<'a> sum_tree::Dimension<'a, NotificationSummary> for UnreadCount {
self.0 += summary.unread_count; self.0 += summary.unread_count;
} }
} }
struct AddNotificationsOptions {
is_new: bool,
clear_old: bool,
includes_first: bool,
}

View File

@ -1616,6 +1616,7 @@ message AddNotification {
message GetNotificationsResponse { message GetNotificationsResponse {
repeated Notification notifications = 1; repeated Notification notifications = 1;
bool done = 2;
} }
message DeleteNotification { message DeleteNotification {