Show unseen message count in room list

This commit is contained in:
Joscha 2022-08-08 23:14:58 +02:00
parent e00ce4ebba
commit 888870b779
5 changed files with 92 additions and 18 deletions

View file

@ -11,6 +11,7 @@ use toss::terminal::Terminal;
use crate::euph::api::{SessionType, SessionView, Snowflake};
use crate::euph::{self, Joined, Status};
use crate::store::MsgStore;
use crate::vault::EuphVault;
use super::chat::{ChatState, Reaction};
@ -36,6 +37,7 @@ enum State {
pub struct EuphRoom {
ui_event_tx: mpsc::UnboundedSender<UiEvent>,
vault: EuphVault,
room: Option<euph::Room>,
state: State,
@ -50,6 +52,7 @@ impl EuphRoom {
pub fn new(vault: EuphVault, ui_event_tx: mpsc::UnboundedSender<UiEvent>) -> Self {
Self {
ui_event_tx,
vault: vault.clone(),
room: None,
state: State::Normal,
chat: ChatState::new(vault),
@ -91,6 +94,10 @@ impl EuphRoom {
}
}
pub async fn unseen_msgs_count(&self) -> usize {
self.vault.unseen_msgs_count().await
}
async fn stabilize_pseudo_msg(&mut self) {
if let Some(id_rx) = &mut self.last_msg_sent {
match id_rx.try_recv() {
@ -169,6 +176,7 @@ impl EuphRoom {
}
fn status_widget(&self, status: &Option<Option<Status>>) -> BoxedWidget {
// TODO Include unread message count
let room = self.chat.store().room();
let room_style = ContentStyle::default().bold().blue();
let mut info = Styled::new(format!("&{room}"), room_style);

View file

@ -154,12 +154,44 @@ impl Rooms {
result.join(" ")
}
fn format_status(status: &Option<Status>) -> String {
match status {
None => " (connecting)".to_string(),
Some(Status::Joining(j)) if j.bounce.is_some() => " (auth required)".to_string(),
Some(Status::Joining(_)) => " (joining)".to_string(),
Some(Status::Joined(j)) => format!(" ({})", Self::format_pbln(j)),
async fn format_status(room: &EuphRoom) -> Option<String> {
match room.status().await {
None => None,
Some(None) => Some("connecting".to_string()),
Some(Some(Status::Joining(j))) if j.bounce.is_some() => {
Some("auth required".to_string())
}
Some(Some(Status::Joining(_))) => Some("joining".to_string()),
Some(Some(Status::Joined(joined))) => Some(Self::format_pbln(&joined)),
}
}
async fn format_unseen_msgs(room: &EuphRoom) -> Option<String> {
let unseen = room.unseen_msgs_count().await;
if unseen == 0 {
None
} else {
Some(format!("{unseen}"))
}
}
async fn format_room_info(room: &EuphRoom) -> Styled {
let unseen_style = ContentStyle::default().bold().green();
let status = Self::format_status(room).await;
let unseen = Self::format_unseen_msgs(room).await;
match (status, unseen) {
(None, None) => Styled::default(),
(None, Some(u)) => Styled::new_plain(" (")
.then(&u, unseen_style)
.then_plain(")"),
(Some(s), None) => Styled::new_plain(" (").then_plain(&s).then_plain(")"),
(Some(s), Some(u)) => Styled::new_plain(" (")
.then_plain(&s)
.then_plain(", ")
.then(&u, unseen_style)
.then_plain(")"),
}
}
@ -176,26 +208,18 @@ impl Rooms {
}
for room in rooms {
let bg_style = ContentStyle::default();
let bg_sel_style = ContentStyle::default().black().on_white();
let room_style = ContentStyle::default().bold().blue();
let room_sel_style = ContentStyle::default().bold().black().on_white();
let mut normal = Styled::new(format!("&{room}"), room_style);
let mut selected = Styled::new(format!("&{room}"), room_sel_style);
if let Some(room) = self.euph_rooms.get(&room) {
if let Some(status) = room.status().await {
let status = Self::format_status(&status);
normal = normal.then(status.clone(), bg_style);
selected = selected.then(status, bg_sel_style);
}
let info = Self::format_room_info(room).await;
normal = normal.and_then(info.clone());
selected = selected.and_then(info);
};
list.add_sel(
room,
Text::new(normal),
Background::new(Text::new(selected)).style(bg_sel_style),
);
list.add_sel(room, Text::new(normal), Text::new(selected));
}
}