diff options
Diffstat (limited to '')
| -rw-r--r-- | src/main.rs | 86 | 
1 files changed, 69 insertions, 17 deletions
diff --git a/src/main.rs b/src/main.rs index f79caf3..6b1b2fe 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,7 +15,7 @@ use iced::widget::button::Status;  use iced::widget::text::{Fragment, IntoFragment};  use iced::widget::{      button, center, checkbox, column, container, mouse_area, opaque, row, scrollable, stack, text, -    text_input, Column, Text, Toggler, +    text_input, toggler, Column, Text, Toggler,  };  use iced::Length::Fill;  use iced::{color, stream, Color, Element, Subscription, Task, Theme}; @@ -98,11 +98,55 @@ pub enum Account {      LoggedOut(LoginModal),  } +impl Account { +    pub fn is_connected(&self) -> bool { +        match self { +            Account::LoggedIn(client) => client.connection_state.is_connected(), +            Account::LoggedOut(login_modal) => false, +        } +    } + +    pub fn connection_status(&self) -> String { +        match self { +            Account::LoggedIn(client) => match client.connection_state { +                ConnectionState::Online => "online".to_string(), +                ConnectionState::Connecting => "connecting".to_string(), +                ConnectionState::Offline => "offline".to_string(), +            }, +            Account::LoggedOut(login_modal) => "no account".to_string(), +        } +    } +} +  #[derive(Clone, Debug)]  pub struct Client {      client: LuzHandle,      jid: JID, -    connection_status: Presence, +    status: Presence, +    connection_state: ConnectionState, +} + +impl Client { +    pub fn is_connected(&self) -> bool { +        self.connection_state.is_connected() +    } +} + +#[derive(Clone, Debug)] +pub enum ConnectionState { +    Online, +    Connecting, +    Offline, +} + +impl ConnectionState { +    pub fn is_connected(&self) -> bool { +        match self { +            ConnectionState::Online => true, +            ConnectionState::Connecting => false, +            ConnectionState::Offline => false, +        } +    }  }  impl DerefMut for Client { @@ -261,7 +305,9 @@ async fn main() -> iced::Result {                              client: luz_handle,                              // TODO:                              jid, -                            connection_status: Presence::Offline(Offline::default()), +                            // TODO: store cached status +                            status: Presence::Offline(Offline::default()), +                            connection_state: ConnectionState::Offline,                          }),                          cfg,                      ), @@ -349,7 +395,8 @@ impl Macaw {                  }                  UpdateMessage::Online(online, vec) => match &mut self.client {                      Account::LoggedIn(client) => { -                        client.connection_status = Presence::Online(online); +                        client.status = Presence::Online(online); +                        client.connection_state = ConnectionState::Online;                          let mut roster = HashMap::new();                          for contact in vec {                              roster.insert(contact.user_jid.clone(), contact); @@ -363,7 +410,8 @@ impl Macaw {                      // TODO: update all contacts' presences to unknown (offline)                      match &mut self.client {                          Account::LoggedIn(client) => { -                            client.connection_status = Presence::Offline(offline); +                            client.status = Presence::Offline(offline); +                            client.connection_state = ConnectionState::Offline;                              Task::none()                          }                          Account::LoggedOut(login_modal) => Task::none(), @@ -466,8 +514,9 @@ impl Macaw {                  self.roster = hash_map;                  Task::none()              } -            Message::Connect => match &self.client { +            Message::Connect => match &mut self.client {                  Account::LoggedIn(client) => { +                    client.connection_state = ConnectionState::Connecting;                      let client = client.client.clone();                      Task::future(async move {                          client.send(CommandMessage::Connect).await; @@ -530,9 +579,10 @@ impl Macaw {                                              Client {                                                  client: handle,                                                  jid, -                                                connection_status: Presence::Offline( +                                                status: Presence::Offline(                                                      Offline::default(),                                                  ), +                                                connection_state: ConnectionState::Offline,                                              },                                          )));                                          let stream = ReceiverStream::new(recv); @@ -698,23 +748,25 @@ impl Macaw {              }              let chats_list = scrollable(chats_list).height(Fill); -            let connection_status = match &self.client { -                Account::LoggedIn(client) => match &client.connection_status { -                    Presence::Online(_online) => "online", -                    Presence::Offline(_offline) => "disconnected", -                }, -                Account::LoggedOut(_) => "disconnected", -            }; +            let connection_status = self.client.connection_status();              let client_jid: Cow<'_, str> = match &self.client {                  Account::LoggedIn(client) => (&client.jid).into(),                  Account::LoggedOut(_) => Cow::from("no account"),                  // map(|client| (&client.jid).into());              }; +            let connected = self.client.is_connected(); +              let account_view = row![                  text(client_jid), -                text(connection_status), -                button("connect").on_press(Message::Connect), -                button("disconnect").on_press(Message::Disconnect) +                toggler(connected) +                    .on_toggle(|connect| { +                        if connect { +                            Message::Connect +                        } else { +                            Message::Disconnect +                        } +                    }) +                    .label(connection_status),              ];              let sidebar = column![chats_list, account_view].height(Fill);  | 
