Skip to content

Commit

Permalink
Added UnReadMessageCount
Browse files Browse the repository at this point in the history
  • Loading branch information
alanpoon committed Dec 30, 2024
1 parent 1cb9524 commit 05e0889
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 41 deletions.
8 changes: 5 additions & 3 deletions src/home/room_screen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use crate::{
user_profile::{AvatarState, ShowUserProfileAction, UserProfile, UserProfileAndRoomId, UserProfilePaneInfo, UserProfileSlidingPaneRef, UserProfileSlidingPaneWidgetExt},
user_profile_cache,
}, shared::{
avatar::{AvatarRef, AvatarWidgetRefExt}, html_or_plaintext::{HtmlOrPlaintextRef, HtmlOrPlaintextWidgetRefExt}, jump_to_bottom_button::JumpToBottomButtonWidgetExt, text_or_image::{TextOrImageRef, TextOrImageWidgetRefExt}, typing_animation::TypingAnimationWidgetExt
avatar::{AvatarRef, AvatarWidgetRefExt}, html_or_plaintext::{HtmlOrPlaintextRef, HtmlOrPlaintextWidgetRefExt}, jump_to_bottom_button::{JumpToBottomButtonWidgetExt, UnReadMessageCount}, text_or_image::{TextOrImageRef, TextOrImageWidgetRefExt}, typing_animation::TypingAnimationWidgetExt
}, sliding_sync::{self, get_client, get_fully_read_event, submit_async_request, take_timeline_endpoints, BackwardsPaginateUntilEventRequest, MatrixRequest, PaginationDirection, TimelineRequestSender}, utils::{self, unix_time_millis_to_datetime, ImageFormat, MediaFormatConst}
};
use rangemap::RangeSet;
Expand Down Expand Up @@ -1521,6 +1521,8 @@ impl RoomScreen {
// If new items were appended to the end of the timeline, show an unread messages badge on the jump to bottom button.
if is_append && !portal_list.is_at_end() {
if let Some(room_id) = &self.room_id {
// Display empty green badge just in case request for the number of unread messages fails
jump_to_bottom.show_unread_message_badge(cx, UnReadMessageCount::Unknown);
// Set the number of unread messages to unread_notification_badge by async request to avoid locking in the Main UI thread
submit_async_request(MatrixRequest::GetNumberUnreadMessages{ room_id: room_id.clone() });
}
Expand Down Expand Up @@ -2208,7 +2210,7 @@ pub enum TimelineUpdate {
clear_cache: bool,
},
/// The updated number of unread messages in the room.
NewUnreadMessagesCount(u64),
NewUnreadMessagesCount(UnReadMessageCount),
/// The target event ID was found at the given `index` in the timeline items vector.
///
/// This means that the RoomScreen widget can scroll the timeline up to this event,
Expand Down Expand Up @@ -2335,7 +2337,7 @@ struct TimelineUiState {
/// at which point we submit a backwards pagination request to fetch more events.
last_scrolled_index: usize,

/// The index of the first item in the timeline that is currently visible
/// The previous first index of the portallist before timeline receives new item, scroll changes or timeline view jumps
prev_first_index: Option<usize>,

/// Boolean to indicate if the user scrolled pass the read marker
Expand Down
83 changes: 51 additions & 32 deletions src/shared/jump_to_bottom_button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,40 +123,50 @@ impl JumpToBottomButton {
///
/// This does not automatically redraw any views.
/// If unread_message_count is `0`, the unread message badge is hidden.
pub fn show_unread_message_badge(&mut self, cx: &mut Cx, unread_message_count: u64) {
if unread_message_count > 0 {
self.visible = true;
self.view(id!(unread_message_badge)).set_visible(true);
self.label(id!(unread_messages_count)).set_text(&format!(
"{}{}",
std::cmp::min(unread_message_count, 99),
if unread_message_count > 99 { "+" } else { "" }
));
if unread_message_count > 99 {
self.view(id!(unread_message_badge.green_rounded_label)).apply_over(cx, live!{
draw_bg: {
border_width: 0.0
}
});
} else if unread_message_count > 9 {
self.view(id!(unread_message_badge.green_rounded_label)).apply_over(cx, live!{
draw_bg: {
border_width: 1.0
}
});
} else {
self.view(id!(unread_message_badge.green_rounded_label)).apply_over(cx, live!{
draw_bg: {
border_width: 2.0
pub fn show_unread_message_badge(&mut self, cx: &mut Cx, unread_message_count: UnReadMessageCount) {
match unread_message_count {
UnReadMessageCount::Unknown => {
self.visible = true;
self.view(id!(unread_message_badge)).set_visible(true);
self.label(id!(unread_messages_count)).set_text("");
}
UnReadMessageCount::Known(unread_message_count) => {
if unread_message_count > 0 {
self.visible = true;
self.view(id!(unread_message_badge)).set_visible(true);
self.label(id!(unread_messages_count)).set_text(&format!(
"{}{}",
std::cmp::min(unread_message_count, 99),
if unread_message_count > 99 { "+" } else { "" }
));
if unread_message_count > 99 {
self.view(id!(unread_message_badge.green_rounded_label)).apply_over(cx, live!{
draw_bg: {
border_width: 0.0
}
});
} else if unread_message_count > 9 {
self.view(id!(unread_message_badge.green_rounded_label)).apply_over(cx, live!{
draw_bg: {
border_width: 1.0
}
});
} else {
self.view(id!(unread_message_badge.green_rounded_label)).apply_over(cx, live!{
draw_bg: {
border_width: 2.0
}
});
}
});
} else {
self.visible = false;
self.view(id!(unread_message_badge)).set_visible(false);
self.label(id!(unread_messages_count)).set_text("");
}
}
} else {
self.visible = false;
self.view(id!(unread_message_badge)).set_visible(false);
self.label(id!(unread_messages_count))
.set_text("");

}

}

/// Updates the visibility of the jump to bottom button and the unread message badge
Expand Down Expand Up @@ -204,7 +214,7 @@ impl JumpToBottomButtonRef {
}

/// See [`JumpToBottomButton::show_unread_message_badge()`].
pub fn show_unread_message_badge(&self, cx: &mut Cx, unread_message_count: u64) {
pub fn show_unread_message_badge(&self, cx: &mut Cx, unread_message_count: UnReadMessageCount) {
if let Some(mut inner) = self.borrow_mut() {
inner.show_unread_message_badge(cx, unread_message_count);
}
Expand All @@ -222,3 +232,12 @@ impl JumpToBottomButtonRef {
}
}
}

/// The number of unread messages in the room
#[derive(Clone, Debug)]
pub enum UnReadMessageCount {
/// Display green badge without text
Unknown,
/// Display green badge and text for the number of unread messages
Known(u64)
}
12 changes: 6 additions & 6 deletions src/sliding_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use crate::{
}, login::login_screen::LoginAction, media_cache::MediaCacheEntry, persistent_state::{self, ClientSessionPersisted}, profile::{
user_profile::{AvatarState, UserProfile},
user_profile_cache::{enqueue_user_profile_update, UserProfileUpdate},
}, utils::MEDIA_THUMBNAIL_FORMAT, verification::add_verification_event_handlers_and_sync_client
}, shared::jump_to_bottom_button::UnReadMessageCount, utils::MEDIA_THUMBNAIL_FORMAT, verification::add_verification_event_handlers_and_sync_client
};

#[derive(Parser, Debug, Default)]
Expand Down Expand Up @@ -416,7 +416,7 @@ async fn async_worker(
login_sender: Sender<LoginRequest>,
) -> Result<()> {
log!("Started async_worker task.");
let subscribe_to_owned_user_read_receipt_changed: std::sync::Arc<tokio::sync::Mutex<BTreeMap<OwnedRoomId, bool>>> = Arc::new(tokio::sync::Mutex::new(BTreeMap::new()));
let subscribe_to_current_user_read_receipt_changed: std::sync::Arc<tokio::sync::Mutex<BTreeMap<OwnedRoomId, bool>>> = Arc::new(tokio::sync::Mutex::new(BTreeMap::new()));
while let Some(request) = request_receiver.recv().await {
match request {
MatrixRequest::Login(login_request) => {
Expand Down Expand Up @@ -631,7 +631,7 @@ async fn async_worker(
if let Some(unread_messages_count) = get_client()
.and_then(|c| c.get_room(&room_id)).map(|room| room.num_unread_messages())
{
if let Err(e) = sender.send(TimelineUpdate::NewUnreadMessagesCount(unread_messages_count)) {
if let Err(e) = sender.send(TimelineUpdate::NewUnreadMessagesCount(UnReadMessageCount::Known(unread_messages_count))) {
log!("Failed to send timeline update: {e:?} for NumOfUnReadMessages request for room {room_id}");
} else {
SignalToUI::set_ui_signal();
Expand Down Expand Up @@ -768,11 +768,11 @@ async fn async_worker(
};
room_info.timeline.clone()
};
let subscribe_to_owned_user_read_receipt_changed = subscribe_to_owned_user_read_receipt_changed.clone();
let subscribe_to_current_user_read_receipt_changed = subscribe_to_current_user_read_receipt_changed.clone();

let _to_updates_task = Handle::current().spawn(async move {
let update_receiver = timeline.subscribe_own_user_read_receipts_changed().await;
let read_receipt_change_mutex = subscribe_to_owned_user_read_receipt_changed.clone();
let read_receipt_change_mutex = subscribe_to_current_user_read_receipt_changed.clone();
let mut read_receipt_change_mutex_guard = read_receipt_change_mutex.lock().await;
if let Some(subscription) = read_receipt_change_mutex_guard.get(&room_id) {
if *subscription && subscribe {
Expand All @@ -784,7 +784,7 @@ async fn async_worker(
pin_mut!(update_receiver);
if let Some(client_user_id) = current_user_id() {
while (update_receiver.next().await).is_some() {
let read_receipt_change = subscribe_to_owned_user_read_receipt_changed.clone();
let read_receipt_change = subscribe_to_current_user_read_receipt_changed.clone();
let read_receipt_change = read_receipt_change.lock().await;
let Some(subscribed_to_user_read_receipt) = read_receipt_change.get(&room_id) else { continue; };
if !subscribed_to_user_read_receipt {
Expand Down

0 comments on commit 05e0889

Please sign in to comment.