implement config with auto-connect option
This commit is contained in:
parent
a938d39dc5
commit
18e907386d
69
src/main.rs
69
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<Error> = None;
|
||||
let mut creds: Option<Creds> = 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<JID, (Chat, IndexMap<Uuid, ChatMessage>)> = 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<JID, (Chat, IndexMap<Uuid, ChatMessage>)> = 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<JID, (Chat, IndexMap<Uuid, ChatMessage>)> = chats
|
||||
// .into_iter()
|
||||
// .map(|chat| (chat.correspondent.clone(), (chat, IndexMap::new())))
|
||||
// .collect();
|
||||
Message::GotChats(chats)
|
||||
}),
|
||||
])
|
||||
}
|
||||
}
|
||||
Message::Roster(hash_map) => {
|
||||
self.roster = hash_map;
|
||||
|
|
Loading…
Reference in New Issue