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

Account for unread messages for jtb button visibility #142

Closed
Changes from 1 commit
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
51 changes: 50 additions & 1 deletion src/home/room_screen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -620,8 +620,31 @@ live_design! {
}
}
}
},

// Badge overlay for unread message count
unread_message_badge = <View> {
width: 20, height: 20,
align: {x: 1.0, y: -1.0}, // Position at the top-right of the button
margin: {top: -5.0, right: -5.0}, // Slightly overlap the button
draw_bg: {
instance background_color: #FF0000FF, // Red badge background
fn pixel(self) -> vec4 {
let sdf = Sdf2d::viewport(self.pos * self.rect_size);
let c = self.rect_size * 0.5;
sdf.circle(c.x, c.x, c.x);
sdf.fill_keep(self.background_color);
return sdf.result;
}
},

// Text inside the badge for unread message count
label = <Label> {
text: "0", // Default text, will be updated dynamically
draw_text: {color: #FFFFFF}, // White text color
align: {x: 0.5, y: 0.5}, // Center the text within the badge
}
}

}

IMG_SMILEY_FACE_BW = dep("crate://self/resources/img/smiley_face_bw.png")
Expand Down Expand Up @@ -853,6 +876,8 @@ struct RoomScreen {
room_name: String,
#[rust(None)]
replying_to: Option<RepliedToInfo>,
#[rust]
unread_message_count: usize,
smarizvi110 marked this conversation as resolved.
Show resolved Hide resolved
}

impl Widget for RoomScreen {
Expand Down Expand Up @@ -1004,6 +1029,28 @@ impl Widget for RoomScreen {
}
}
}

if let Some(timeline_update) = action.downcast_ref::<TimelineUpdate>() {
match timeline_update {
TimelineUpdate::NewItems {items, ..} => {
let portal_list = self.portal_list(id!(timeline.list));
if !portal_list.is_at_end() {
self.unread_message_count += items.len();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this logic doesn't quite seem correct; unfortunately, Matrix makes it quite difficult to correctly determine how many messages are unread.

I think you would want to determine the number of new items that were added beyond the last-known read marker of the current user. Doing this requires tracking the read marker (See #86), which we haven't yet finished.

So for now, you can just show a visual indicator of new messages existing at all, rather than attempting to count how many are considered "unread".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, sounds good!

Copy link
Contributor

@alanpoon alanpoon Oct 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

notification_count @kevinaboos @smarizvi110 @ZhangHanDong, The "unread" number got nothing to do with #86, it is based on backend data: unread_notifications' [unreadNotificationsCount](https://matrix-org.github.io/matrix-rust-sdk/matrix_sdk/sync/struct.UnreadNotificationsCount.html)

Without #86, unread notification count should be always increasing and not be able to reset to 0.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me look into this.

self.view(id!(jump_to_bottom_view)).set_visible(true);
let label_text = if self.unread_message_count > 0 {
format!("{} new messagers", self.unread_message_count)
smarizvi110 marked this conversation as resolved.
Show resolved Hide resolved
} else {
"".to_string()
};
self.label(id!(unread_message_badge.label)).set_text_and_redraw(cx, &label_text);
} else {
self.unread_message_count = 0;
self.view(id!(jump_to_bottom_view)).set_visible(false);
}
},
_ => {},
}
}
}

// Handle the cancel reply button being clicked.
Expand Down Expand Up @@ -1056,6 +1103,7 @@ impl Widget for RoomScreen {
SCROLL_TO_BOTTOM_SPEED,
);
jump_to_bottom_view.set_visible(false);
self.unread_message_count = 0;
self.redraw(cx);
}
}
Expand Down Expand Up @@ -1114,6 +1162,7 @@ pub enum TimelineAction {

/// A message that is sent from a background async task to a room's timeline view
/// for the purpose of update the Timeline UI contents or metadata.
#[derive(Debug)]
pub enum TimelineUpdate {
/// The content of a room's timeline was updated in the background.
NewItems {
Expand Down