From 18e907386d629966236d0b6a30501f492133d2f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?cel=20=F0=9F=8C=B8?= Date: Sat, 1 Mar 2025 07:03:22 +0000 Subject: [PATCH] implement config with auto-connect option --- src/main.rs | 69 +++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 49 insertions(+), 20 deletions(-) diff --git a/src/main.rs b/src/main.rs index 8e38e7b..558c608 100644 --- a/src/main.rs +++ b/src/main.rs @@ -113,7 +113,7 @@ impl Deref for Client { async fn main() -> iced::Result { tracing_subscriber::fmt::init(); - let cfg = confy::load("macaw", None).unwrap(); + let cfg: Config = confy::load("macaw", None).unwrap(); let entry = Entry::new("macaw", "macaw"); let mut client_creation_error: Option = None; let mut creds: Option = None; @@ -167,6 +167,13 @@ async fn main() -> iced::Result { if let Some((jid, luz_handle, update_recv)) = client { let stream = ReceiverStream::new(update_recv); let stream = stream.map(|message| Message::Luz(message)); + let task = { + if cfg.auto_connect { + Task::batch([Task::stream(stream), Task::done(Message::Connect)]) + } else { + Task::stream(stream) + } + }; iced::application("Macaw", Macaw::update, Macaw::view).run_with(|| { ( Macaw::new( @@ -179,7 +186,7 @@ async fn main() -> iced::Result { cfg, ), // TODO: autoconnect config - Task::stream(stream), + task, ) }) } else { @@ -322,24 +329,46 @@ impl Macaw { self.client = Account::LoggedIn(client.clone()); let client1 = client.clone(); let client2 = client.clone(); - Task::batch([ - Task::perform(async move { client1.client.get_roster().await }, |result| { - let roster = result.unwrap(); - let mut macaw_roster = HashMap::new(); - for contact in roster { - macaw_roster.insert(contact.user_jid.clone(), contact); - } - Message::Roster(macaw_roster) - }), - Task::perform(async move { client2.client.get_chats().await }, |chats| { - let chats = chats.unwrap(); - // let chats: HashMap)> = chats - // .into_iter() - // .map(|chat| (chat.correspondent.clone(), (chat, IndexMap::new()))) - // .collect(); - Message::GotChats(chats) - }), - ]) + if self.config.auto_connect { + Task::batch([ + Task::perform(async move { client1.client.get_roster().await }, |result| { + let roster = result.unwrap(); + let mut macaw_roster = HashMap::new(); + for contact in roster { + macaw_roster.insert(contact.user_jid.clone(), contact); + } + Message::Roster(macaw_roster) + }), + Task::perform(async move { client2.client.get_chats().await }, |chats| { + let chats = chats.unwrap(); + // let chats: HashMap)> = chats + // .into_iter() + // .map(|chat| (chat.correspondent.clone(), (chat, IndexMap::new()))) + // .collect(); + Message::GotChats(chats) + }), + Task::done(Message::Connect), + ]) + } else { + Task::batch([ + Task::perform(async move { client1.client.get_roster().await }, |result| { + let roster = result.unwrap(); + let mut macaw_roster = HashMap::new(); + for contact in roster { + macaw_roster.insert(contact.user_jid.clone(), contact); + } + Message::Roster(macaw_roster) + }), + Task::perform(async move { client2.client.get_chats().await }, |chats| { + let chats = chats.unwrap(); + // let chats: HashMap)> = chats + // .into_iter() + // .map(|chat| (chat.correspondent.clone(), (chat, IndexMap::new()))) + // .collect(); + Message::GotChats(chats) + }), + ]) + } } Message::Roster(hash_map) => { self.roster = hash_map;