🏗️ Remove data handling from components

The data sent by matrix_interface senders is now handled by the App.
This commit is contained in:
2023-12-21 22:07:08 +01:00
parent 513b05ddb3
commit c9292fd613
7 changed files with 177 additions and 71 deletions

View File

@@ -1,13 +1,24 @@
use futures::select;
// Cf. https://dioxuslabs.com/learn/0.4/reference/use_coroutine
// In order to use/run the rx.next().await statement you will need to extend the [Stream] trait
// (used by [UnboundedReceiver]) by adding 'futures_util' as a dependency to your project
// and adding the use futures_util::stream::StreamExt;
use futures_util::stream::StreamExt;
use std::{cell::RefCell, collections::HashMap, sync::Arc};
use dioxus::prelude::*;
use fermi::*;
use matrix_sdk::room::Room as MatrixRoom;
use matrix_sdk::{
room::RoomMember,
ruma::{OwnedMxcUri, OwnedRoomId, OwnedUserId},
};
use tracing::{debug, error, warn};
use crate::matrix_interface::client::Client;
use crate::matrix_interface::requester::Requester;
use crate::matrix_interface::worker_tasks::LoginStyle;
#[derive(Clone, Debug)]
pub struct UserInfo {
@@ -107,7 +118,7 @@ impl PartialEq for Store {
impl Eq for Store {}
pub struct AppSettings {
pub requester: Option<Requester>,
requester: Option<Box<Requester>>,
pub store: Store,
}
@@ -118,7 +129,116 @@ impl AppSettings {
store: Store::new(),
}
}
pub fn set_requester(&mut self, requester: Box<Requester>) {
self.requester = Some(requester);
}
}
pub static APP_SETTINGS: AtomRef<AppSettings> = AtomRef(|_| AppSettings::new());
async fn on_room(room_option: Option<Room>, rooms_ref: &UseAtomRef<ByIdRooms>) {
if let Some(room) = room_option {
let room_id = room.id();
// TODO: Update rooms
rooms_ref
.write()
.insert(room_id, RefCell::<Room>::new(room));
}
}
pub async fn sync_rooms(
mut rx: UnboundedReceiver<bool>,
app_settings_ref: UseAtomRef<AppSettings>,
rooms_ref: UseAtomRef<ByIdRooms>,
) {
error!("=== SYNC ROOMS BEG ===");
while let Some(_is_logged) = rx.next().await {
let app_settings_ref = app_settings_ref.read();
let requester = &app_settings_ref.requester;
if requester.is_some() {
let rooms_receiver = &requester.as_ref().unwrap().rooms_receiver;
let mut room_stream = rooms_receiver.stream();
loop {
select! {
room = room_stream.next() => on_room(room, &rooms_ref).await,
}
}
}
}
}
pub async fn login(
mut rx: UnboundedReceiver<bool>,
app_settings_ref: UseAtomRef<AppSettings>,
session_ref: UseAtomRef<Session>,
) {
error!("=== LOGIN BEG ===");
while let Some(is_logged) = rx.next().await {
error!("State updated");
if !is_logged {
let homeserver_url = session_ref.read().homeserver_url.clone();
let username = session_ref.read().username.clone();
let password = session_ref.read().password.clone();
if homeserver_url.is_some() && username.is_some() && password.is_some() {
let client = Client::spawn(homeserver_url.unwrap()).await;
client.init();
match client.login(LoginStyle::Password(username.unwrap(), password.unwrap())) {
Ok(_) => {
debug!("successfully logged");
session_ref.write().is_logged = true;
}
Err(err) => {
error!("Error during login: {err}");
// invalid_login.modify(|_| true);
}
}
app_settings_ref.write().set_requester(Box::new(client));
} else {
warn!("At least one of the following values is/are invalid: homeserver, username or password");
}
} else {
warn!("already logged... skip login");
}
}
error!("=== LOGIN END ===");
}
pub static ROOMS: AtomRef<ByIdRooms> = AtomRef(|_| ByIdRooms::new());
pub struct Session {
pub homeserver_url: Option<String>,
pub username: Option<String>,
pub password: Option<String>,
pub is_logged: bool,
}
impl Session {
fn new() -> Self {
Self {
homeserver_url: None,
username: None,
password: None,
is_logged: false,
}
}
pub fn update(
&mut self,
homeserver_url: Option<String>,
username: Option<String>,
password: Option<String>,
) {
self.homeserver_url = homeserver_url;
self.username = username;
self.password = password;
}
}
pub static SESSION: AtomRef<Session> = AtomRef(|_| Session::new());