Allow accepting/rejecting incoming requests via notification

This commit is contained in:
Antonio Scandurra 2022-05-11 15:25:33 +02:00
parent 97d3616ed9
commit c71b264786

View file

@ -7,10 +7,11 @@ use settings::Settings;
use std::sync::Arc; use std::sync::Arc;
use workspace::Notification; use workspace::Notification;
impl_internal_actions!(contact_notifications, [Dismiss]); impl_internal_actions!(contact_notifications, [Dismiss, RespondToContactRequest]);
pub fn init(cx: &mut MutableAppContext) { pub fn init(cx: &mut MutableAppContext) {
cx.add_action(IncomingRequestNotification::dismiss); cx.add_action(IncomingRequestNotification::dismiss);
cx.add_action(IncomingRequestNotification::respond_to_contact_request);
} }
pub struct IncomingRequestNotification { pub struct IncomingRequestNotification {
@ -21,6 +22,12 @@ pub struct IncomingRequestNotification {
#[derive(Clone)] #[derive(Clone)]
struct Dismiss(u64); struct Dismiss(u64);
#[derive(Clone)]
pub struct RespondToContactRequest {
pub user_id: u64,
pub accept: bool,
}
pub enum Event { pub enum Event {
Dismiss, Dismiss,
} }
@ -103,15 +110,43 @@ impl View for IncomingRequestNotification {
.with_child( .with_child(
Flex::row() Flex::row()
.with_child( .with_child(
Label::new("Decline".to_string(), theme.button.text.clone()) MouseEventHandler::new::<Reject, _, _>(
self.user.id as usize,
cx,
|_, _| {
Label::new("Reject".to_string(), theme.button.text.clone())
.contained() .contained()
.with_style(theme.button.container) .with_style(theme.button.container)
.boxed()
},
)
.with_cursor_style(CursorStyle::PointingHand)
.on_click(move |_, cx| {
cx.dispatch_action(RespondToContactRequest {
user_id,
accept: false,
});
})
.boxed(), .boxed(),
) )
.with_child( .with_child(
MouseEventHandler::new::<Accept, _, _>(
self.user.id as usize,
cx,
|_, _| {
Label::new("Accept".to_string(), theme.button.text.clone()) Label::new("Accept".to_string(), theme.button.text.clone())
.contained() .contained()
.with_style(theme.button.container) .with_style(theme.button.container)
.boxed()
},
)
.with_cursor_style(CursorStyle::PointingHand)
.on_click(move |_, cx| {
cx.dispatch_action(RespondToContactRequest {
user_id,
accept: true,
});
})
.boxed(), .boxed(),
) )
.aligned() .aligned()
@ -156,4 +191,16 @@ impl IncomingRequestNotification {
}); });
cx.emit(Event::Dismiss); cx.emit(Event::Dismiss);
} }
fn respond_to_contact_request(
&mut self,
action: &RespondToContactRequest,
cx: &mut ViewContext<Self>,
) {
self.user_store
.update(cx, |store, cx| {
store.respond_to_contact_request(action.user_id, action.accept, cx)
})
.detach();
}
} }