Encapsulate Room interaction within ActiveCall

Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
Antonio Scandurra 2022-09-29 17:39:53 +02:00
parent e0db62173a
commit 1898e813f5
9 changed files with 63 additions and 92 deletions

View file

@ -69,9 +69,8 @@ impl CollabTitlebarItem {
Some(_) => {}
None => {
if let Some(workspace) = self.workspace.upgrade(cx) {
let client = workspace.read(cx).client().clone();
let user_store = workspace.read(cx).user_store().clone();
let view = cx.add_view(|cx| ContactsPopover::new(client, user_store, cx));
let view = cx.add_view(|cx| ContactsPopover::new(user_store, cx));
cx.focus(&view);
cx.subscribe(&view, |this, _, event, cx| {
match event {

View file

@ -2,13 +2,12 @@ mod collab_titlebar_item;
mod contacts_popover;
mod incoming_call_notification;
use client::{Client, UserStore};
use client::UserStore;
pub use collab_titlebar_item::CollabTitlebarItem;
use gpui::{ModelHandle, MutableAppContext};
use std::sync::Arc;
pub fn init(client: Arc<Client>, user_store: ModelHandle<UserStore>, cx: &mut MutableAppContext) {
pub fn init(user_store: ModelHandle<UserStore>, cx: &mut MutableAppContext) {
contacts_popover::init(cx);
collab_titlebar_item::init(cx);
incoming_call_notification::init(client, user_store, cx);
incoming_call_notification::init(user_store, cx);
}

View file

@ -1,7 +1,7 @@
use std::sync::Arc;
use call::ActiveCall;
use client::{Client, Contact, User, UserStore};
use client::{Contact, User, UserStore};
use editor::{Cancel, Editor};
use fuzzy::{match_strings, StringMatchCandidate};
use gpui::{
@ -84,7 +84,6 @@ pub struct ContactsPopover {
entries: Vec<ContactEntry>,
match_candidates: Vec<StringMatchCandidate>,
list_state: ListState,
client: Arc<Client>,
user_store: ModelHandle<UserStore>,
filter_editor: ViewHandle<Editor>,
collapsed_sections: Vec<Section>,
@ -93,11 +92,7 @@ pub struct ContactsPopover {
}
impl ContactsPopover {
pub fn new(
client: Arc<Client>,
user_store: ModelHandle<UserStore>,
cx: &mut ViewContext<Self>,
) -> Self {
pub fn new(user_store: ModelHandle<UserStore>, cx: &mut ViewContext<Self>) -> Self {
let filter_editor = cx.add_view(|cx| {
let mut editor = Editor::single_line(
Some(|theme| theme.contacts_panel.user_query_editor.clone()),
@ -182,7 +177,6 @@ impl ContactsPopover {
match_candidates: Default::default(),
filter_editor,
_subscriptions: subscriptions,
client,
user_store,
};
this.update_entries(cx);
@ -633,17 +627,11 @@ impl ContactsPopover {
}
fn call(&mut self, action: &Call, cx: &mut ViewContext<Self>) {
let recipient_user_id = action.recipient_user_id;
let room = ActiveCall::global(cx).update(cx, |active_call, cx| {
active_call.get_or_create(&self.client, &self.user_store, cx)
});
cx.spawn_weak(|_, mut cx| async move {
let room = room.await?;
room.update(&mut cx, |room, cx| room.call(recipient_user_id, cx))
.await?;
anyhow::Ok(())
})
.detach();
ActiveCall::global(cx)
.update(cx, |active_call, cx| {
active_call.invite(action.recipient_user_id, cx)
})
.detach_and_log_err(cx);
}
}

View file

@ -1,7 +1,5 @@
use std::sync::Arc;
use call::ActiveCall;
use client::{incoming_call::IncomingCall, Client, UserStore};
use client::{incoming_call::IncomingCall, UserStore};
use futures::StreamExt;
use gpui::{
elements::*,
@ -14,7 +12,7 @@ use util::ResultExt;
impl_internal_actions!(incoming_call_notification, [RespondToCall]);
pub fn init(client: Arc<Client>, user_store: ModelHandle<UserStore>, cx: &mut MutableAppContext) {
pub fn init(user_store: ModelHandle<UserStore>, cx: &mut MutableAppContext) {
cx.add_action(IncomingCallNotification::respond_to_call);
let mut incoming_call = user_store.read(cx).incoming_call();
@ -34,13 +32,7 @@ pub fn init(client: Arc<Client>, user_store: ModelHandle<UserStore>, cx: &mut Mu
kind: WindowKind::PopUp,
is_movable: false,
},
|_| {
IncomingCallNotification::new(
incoming_call,
client.clone(),
user_store.clone(),
)
},
|_| IncomingCallNotification::new(incoming_call, user_store.clone()),
);
notification_window = Some(window_id);
}
@ -56,29 +48,18 @@ struct RespondToCall {
pub struct IncomingCallNotification {
call: IncomingCall,
client: Arc<Client>,
user_store: ModelHandle<UserStore>,
}
impl IncomingCallNotification {
pub fn new(
call: IncomingCall,
client: Arc<Client>,
user_store: ModelHandle<UserStore>,
) -> Self {
Self {
call,
client,
user_store,
}
pub fn new(call: IncomingCall, user_store: ModelHandle<UserStore>) -> Self {
Self { call, user_store }
}
fn respond_to_call(&mut self, action: &RespondToCall, cx: &mut ViewContext<Self>) {
if action.accept {
ActiveCall::global(cx)
.update(cx, |active_call, cx| {
active_call.join(&self.call, &self.client, &self.user_store, cx)
})
.update(cx, |active_call, cx| active_call.join(&self.call, cx))
.detach_and_log_err(cx);
} else {
self.user_store