Add enum for room status

This way, it is far easier to understand what the different values mean
This commit is contained in:
Joscha 2022-08-21 00:35:17 +02:00
parent 28899965c7
commit 19d75a1d15
2 changed files with 34 additions and 26 deletions

View file

@ -38,6 +38,14 @@ enum State {
ChooseNick(EditorState), ChooseNick(EditorState),
} }
#[allow(clippy::large_enum_variant)]
pub enum RoomStatus {
NoRoom,
Stopped,
Connecting,
Connected(Status),
}
pub struct EuphRoom { pub struct EuphRoom {
ui_event_tx: mpsc::UnboundedSender<UiEvent>, ui_event_tx: mpsc::UnboundedSender<UiEvent>,
@ -102,11 +110,14 @@ impl EuphRoom {
self.room = None; self.room = None;
} }
pub async fn status(&self) -> Option<Option<Status>> { pub async fn status(&self) -> RoomStatus {
if let Some(room) = &self.room { match &self.room {
room.status().await.ok() Some(room) => match room.status().await {
} else { Ok(Some(status)) => RoomStatus::Connected(status),
None Ok(None) => RoomStatus::Connecting,
Err(_) => RoomStatus::Stopped,
},
None => RoomStatus::NoRoom,
} }
} }
@ -146,9 +157,10 @@ impl EuphRoom {
self.stabilize_pseudo_msg().await; self.stabilize_pseudo_msg().await;
let status = self.status().await; let status = self.status().await;
let chat = match &status { let chat = if let RoomStatus::Connected(Status::Joined(joined)) = &status {
Some(Some(Status::Joined(joined))) => self.widget_with_nick_list(&status, joined).await, self.widget_with_nick_list(&status, joined).await
_ => self.widget_without_nick_list(&status).await, } else {
self.widget_without_nick_list(&status).await
}; };
let mut layers = vec![chat]; let mut layers = vec![chat];
@ -175,7 +187,7 @@ impl EuphRoom {
.build() .build()
} }
async fn widget_without_nick_list(&self, status: &Option<Option<Status>>) -> BoxedWidget { async fn widget_without_nick_list(&self, status: &RoomStatus) -> BoxedWidget {
VJoin::new(vec![ VJoin::new(vec![
Segment::new(Border::new( Segment::new(Border::new(
Padding::new(self.status_widget(status).await).horizontal(1), Padding::new(self.status_widget(status).await).horizontal(1),
@ -186,11 +198,7 @@ impl EuphRoom {
.into() .into()
} }
async fn widget_with_nick_list( async fn widget_with_nick_list(&self, status: &RoomStatus, joined: &Joined) -> BoxedWidget {
&self,
status: &Option<Option<Status>>,
joined: &Joined,
) -> BoxedWidget {
HJoin::new(vec![ HJoin::new(vec![
Segment::new(VJoin::new(vec![ Segment::new(VJoin::new(vec![
Segment::new(Border::new( Segment::new(Border::new(
@ -206,20 +214,20 @@ impl EuphRoom {
.into() .into()
} }
async fn status_widget(&self, status: &Option<Option<Status>>) -> BoxedWidget { async fn status_widget(&self, status: &RoomStatus) -> BoxedWidget {
// TODO Include unread message count // TODO Include unread message count
let room = self.chat.store().room(); let room = self.chat.store().room();
let room_style = ContentStyle::default().bold().blue(); let room_style = ContentStyle::default().bold().blue();
let mut info = Styled::new(format!("&{room}"), room_style); let mut info = Styled::new(format!("&{room}"), room_style);
info = match status { info = match status {
None => info.then_plain(", archive"), RoomStatus::NoRoom | RoomStatus::Stopped => info.then_plain(", archive"),
Some(None) => info.then_plain(", connecting..."), RoomStatus::Connecting => info.then_plain(", connecting..."),
Some(Some(Status::Joining(j))) if j.bounce.is_some() => { RoomStatus::Connected(Status::Joining(j)) if j.bounce.is_some() => {
info.then_plain(", auth required") info.then_plain(", auth required")
} }
Some(Some(Status::Joining(_))) => info.then_plain(", joining..."), RoomStatus::Connected(Status::Joining(_)) => info.then_plain(", joining..."),
Some(Some(Status::Joined(j))) => { RoomStatus::Connected(Status::Joined(j)) => {
let nick = &j.session.name; let nick = &j.session.name;
if nick.is_empty() { if nick.is_empty() {
info.then_plain(", present without nick") info.then_plain(", present without nick")

View file

@ -14,7 +14,7 @@ use toss::terminal::Terminal;
use crate::euph::EuphRoomEvent; use crate::euph::EuphRoomEvent;
use crate::vault::Vault; use crate::vault::Vault;
use super::euph::room::EuphRoom; use super::euph::room::{EuphRoom, RoomStatus};
use super::input::{key, InputEvent, KeyBindingsList, KeyEvent}; use super::input::{key, InputEvent, KeyBindingsList, KeyEvent};
use super::widgets::editor::EditorState; use super::widgets::editor::EditorState;
use super::widgets::join::{HJoin, Segment, VJoin}; use super::widgets::join::{HJoin, Segment, VJoin};
@ -159,13 +159,13 @@ impl Rooms {
async fn format_status(room: &EuphRoom) -> Option<String> { async fn format_status(room: &EuphRoom) -> Option<String> {
match room.status().await { match room.status().await {
None => None, RoomStatus::NoRoom | RoomStatus::Stopped => None,
Some(None) => Some("connecting".to_string()), RoomStatus::Connecting => Some("connecting".to_string()),
Some(Some(Status::Joining(j))) if j.bounce.is_some() => { RoomStatus::Connected(Status::Joining(j)) if j.bounce.is_some() => {
Some("auth required".to_string()) Some("auth required".to_string())
} }
Some(Some(Status::Joining(_))) => Some("joining".to_string()), RoomStatus::Connected(Status::Joining(_)) => Some("joining".to_string()),
Some(Some(Status::Joined(joined))) => Some(Self::format_pbln(&joined)), RoomStatus::Connected(Status::Joined(joined)) => Some(Self::format_pbln(&joined)),
} }
} }