⬆️ Update the components to take the dioxus 0.5 rework into account

This commit is contained in:
2024-03-31 23:26:10 +02:00
parent aad0064a0c
commit 9071b0073c
26 changed files with 537 additions and 534 deletions

View File

@@ -3,10 +3,13 @@
// (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;
use std::{collections::HashMap, sync::Arc};
use crate::matrix_interface::client::{Client, RoomEvent};
use crate::matrix_interface::requester::{Receivers, Requester};
use crate::matrix_interface::worker_tasks::LoginStyle;
use dioxus::prelude::*;
use fermi::*;
use matrix_sdk::{
room::{Room as MatrixRoom, RoomMember},
ruma::{OwnedRoomId, OwnedUserId},
@@ -15,9 +18,6 @@ use tokio::select;
use tracing::{debug, error, warn};
use crate::components::chats_window::interface::Interface as ChatsWinInterface;
use crate::matrix_interface::client::{Client, RoomEvent};
use crate::matrix_interface::requester::{Receivers, Requester};
use crate::matrix_interface::worker_tasks::LoginStyle;
// #[derive(Clone, Debug)]
// pub struct UserInfo {
@@ -140,11 +140,9 @@ impl AppSettings {
}
}
pub static APP_SETTINGS: AtomRef<AppSettings> = AtomRef(|_| AppSettings::new());
async fn on_room(room_id: OwnedRoomId, room: Room, rooms_ref: &UseAtomRef<ByIdRooms>) {
async fn on_room(room_id: OwnedRoomId, room: Room, by_id_rooms: &GlobalSignal<ByIdRooms>) {
// TODO: Update rooms
rooms_ref
by_id_rooms
.write()
.insert(room_id, RefCell::<Room>::new(room));
}
@@ -152,31 +150,36 @@ async fn on_room(room_id: OwnedRoomId, room: Room, rooms_ref: &UseAtomRef<ByIdRo
async fn on_joining_invitation(
room_id: OwnedRoomId,
room: Room,
rooms_ref: &UseAtomRef<ByIdRooms>,
by_id_rooms: &GlobalSignal<ByIdRooms>,
) {
debug!(
"You're invited to join the \"{}\" room",
room.name().unwrap()
);
// TODO: Update rooms
rooms_ref
by_id_rooms
.write()
.insert(room_id, RefCell::<Room>::new(room));
}
pub async fn on_room_topic(room_id: OwnedRoomId, topic: String, rooms_ref: &UseAtomRef<ByIdRooms>) {
if let Some(room_ref) = rooms_ref.read().get(&room_id) {
let mut room = room_ref.borrow_mut();
async fn on_room_topic(room_id: OwnedRoomId, topic: String, by_id_rooms: &GlobalSignal<ByIdRooms>) {
if let Some(room) = by_id_rooms.read().get(&room_id) {
let mut room = room.borrow_mut();
room.topic = Some(RefCell::new(topic));
} else {
warn!("No room found with the \"{}\" id", room_id);
}
}
pub async fn sync_messages(by_id_rooms: &GlobalSignal<ByIdRooms>, room_id: OwnedRoomId) {
error!("== sync_messages ==");
}
pub async fn sync_rooms(
mut rx: UnboundedReceiver<bool>,
receivers: Receivers,
rooms_ref: UseAtomRef<ByIdRooms>,
by_id_rooms: &GlobalSignal<ByIdRooms>,
) {
if let Some(_is_logged) = rx.next().await {
let mut rooms_receiver = receivers.room_receiver.borrow_mut();
@@ -187,9 +190,9 @@ pub async fn sync_rooms(
res = rooms_receiver.recv() => {
if let Ok(room_event) = res {
match room_event {
RoomEvent::MemberEvent(room_id, room) => on_room(room_id, room, &rooms_ref).await,
RoomEvent::InviteEvent(room_id, room) => on_joining_invitation(room_id, room, &rooms_ref).await,
RoomEvent::TopicEvent(room_id, topic) => on_room_topic(room_id, topic, &rooms_ref).await,
RoomEvent::MemberEvent(room_id, room) => on_room(room_id, room, &by_id_rooms).await,
RoomEvent::InviteEvent(room_id, room) => on_joining_invitation(room_id, room, &by_id_rooms).await,
RoomEvent::TopicEvent(room_id, topic) => on_room_topic(room_id, topic, &by_id_rooms).await,
};
}
},
@@ -200,14 +203,14 @@ pub async fn sync_rooms(
pub async fn login(
mut rx: UnboundedReceiver<bool>,
app_settings_ref: UseAtomRef<AppSettings>,
session_ref: UseAtomRef<Session>,
app_settings: &GlobalSignal<AppSettings>,
session: &GlobalSignal<Session>,
) {
while let Some(is_logged) = rx.next().await {
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();
let homeserver_url = session.read().homeserver_url.clone();
let username = session.read().username.clone();
let password = session.read().password.clone();
if homeserver_url.is_some() && username.is_some() && password.is_some() {
let client = Client::spawn(homeserver_url.unwrap()).await;
@@ -222,14 +225,14 @@ pub async fn login(
{
Ok(_) => {
debug!("successfully logged");
session_ref.write().is_logged = true;
session.write().is_logged = true;
}
Err(err) => {
error!("Error during login: {err}");
// invalid_login.modify(|_| true);
}
}
app_settings_ref.write().set_requester(RefCell::new(client));
app_settings.write().set_requester(RefCell::new(client));
} else {
warn!("At least one of the following values is/are invalid: homeserver, username or password");
}
@@ -247,7 +250,7 @@ pub struct Session {
pub is_logged: bool,
}
impl Session {
fn new() -> Self {
pub fn new() -> Self {
Self {
homeserver_url: None,
username: None,
@@ -267,6 +270,8 @@ impl Session {
}
}
pub static ROOMS: AtomRef<ByIdRooms> = AtomRef(|_| ByIdRooms::new());
pub static SESSION: AtomRef<Session> = AtomRef(|_| Session::new());
pub static CHATS_WIN_INTERFACE: AtomRef<ChatsWinInterface> = AtomRef(|_| ChatsWinInterface::new());
pub static APP_SETTINGS: GlobalSignal<AppSettings> = Signal::global(AppSettings::new);
pub static ROOMS: GlobalSignal<ByIdRooms> = Signal::global(ByIdRooms::new);
pub static SESSION: GlobalSignal<Session> = Signal::global(Session::new);
pub static CHATS_WIN_INTERFACE: GlobalSignal<ChatsWinInterface> =
Signal::global(ChatsWinInterface::new);