Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update UI via a UI Signal upon background fetch of media or user profile #97

Merged
merged 1 commit into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/media_cache.rs
Original file line number Diff line number Diff line change
@@ -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}};

Expand Down Expand Up @@ -202,4 +202,5 @@ fn insert_into_cache<D: Into<Arc<[u8]>>>(
if let Some(sender) = update_sender {
let _ = sender.send(TimelineUpdate::MediaFetched);
}
SignalToUI::set_ui_signal();
}
34 changes: 20 additions & 14 deletions src/profile/user_profile.rs
Original file line number Diff line number Diff line change
@@ -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
};
Expand Down Expand Up @@ -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::*;
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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;
}
}
});
}

Expand Down