From 2a3b63117552ea863d0d5e8f53346aa61180fc3e Mon Sep 17 00:00:00 2001 From: Kevin Boos Date: Wed, 24 Jul 2024 15:54:51 -0700 Subject: [PATCH] Update UI via a UI Signal upon background fetch of media or a user profile --- src/media_cache.rs | 3 ++- src/profile/user_profile.rs | 34 ++++++++++++++++++++-------------- 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/src/media_cache.rs b/src/media_cache.rs index 56877f7b..f1a6bd26 100644 --- a/src/media_cache.rs +++ b/src/media_cache.rs @@ -1,5 +1,5 @@ use std::{sync::{Mutex, Arc}, collections::{BTreeMap, btree_map::Entry}, time::SystemTime, ops::{Deref, DerefMut}}; -use makepad_widgets::{error, log}; +use makepad_widgets::{error, log, SignalToUI}; use matrix_sdk::{ruma::{OwnedMxcUri, events::room::MediaSource}, media::{MediaRequest, MediaFormat}}; use crate::{home::room_screen::TimelineUpdate, sliding_sync::{self, MatrixRequest}, utils::{MediaFormatConst, MEDIA_THUMBNAIL_FORMAT}}; @@ -202,4 +202,5 @@ fn insert_into_cache>>( if let Some(sender) = update_sender { let _ = sender.send(TimelineUpdate::MediaFetched); } + SignalToUI::set_ui_signal(); } diff --git a/src/profile/user_profile.rs b/src/profile/user_profile.rs index 882c9b79..8e88a922 100644 --- a/src/profile/user_profile.rs +++ b/src/profile/user_profile.rs @@ -1,7 +1,7 @@ -use std::{borrow::Cow, cell::RefCell, collections::{btree_map::Entry, BTreeMap}, ops::Deref, sync::Arc}; +use std::{borrow::Cow, cell::RefCell, collections::{btree_map::Entry, BTreeMap}, ops::{Deref, DerefMut}, sync::Arc}; use crossbeam_queue::SegQueue; use makepad_widgets::*; -use matrix_sdk::{room::RoomMember, ruma::{events::room::member::MembershipState, OwnedRoomId, OwnedUserId, UserId}}; +use matrix_sdk::{room::RoomMember, ruma::events::room::member::MembershipState, OwnedRoomId, OwnedUserId, UserId}; use crate::{ shared::avatar::AvatarWidgetExt, sliding_sync::{get_client, submit_async_request, MatrixRequest}, utils }; @@ -190,7 +190,11 @@ impl Deref for AvatarInfo { &self.user_profile } } - +impl DerefMut for AvatarInfo { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.user_profile + } +} live_design! { import makepad_draw::shader::std::*; @@ -551,6 +555,11 @@ impl Deref for UserProfilePaneInfo { &self.avatar_info } } +impl DerefMut for UserProfilePaneInfo { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.avatar_info + } +} impl UserProfilePaneInfo { fn membership_title(&self) -> String { if self.room_name.is_empty() { @@ -624,21 +633,18 @@ impl Widget for UserProfileSlidingPane { if let Event::Signal = event { USER_PROFILE_CACHE.with_borrow_mut(|cache| { while let Some(update) = PENDING_USER_PROFILE_UPDATES.pop() { - // Apply this update to the current user profile pane (if relevant). - if let Some(our_info) = self.info.as_mut() { - let val = update.apply_to_current_pane(our_info); - redraw_this_pane |= val; - } // Insert the updated info into the cache update.apply_to_cache(cache); } - // TODO: it's probably better to re-fetch the user profile info from the cache here - // just once, after all updates have been processed, instead of doing it - // repeatedly for each update. - // That also has the side benefit of avoiding a timing issue where a UI Signal - // does not happend at the same time as a user profile element being fetched, - // such as an avatar coming in later after the Signal has already been handled. + // Apply this update to the current user profile pane (if relevant). + if let Some(our_info) = self.info.as_mut() { + if let Some(new_info) = cache.get(&our_info.user_id) { + our_info.user_profile = new_info.user_profile.clone(); + our_info.room_member = new_info.room_members.get(&our_info.room_id).cloned(); + redraw_this_pane = true; + } + } }); }