fix(client): derive voice status from a single state machine
The status bar and voice page kept saying the room was being joined while the sidebar already reported a connected voice. Clicking the room you were already in set a free-text 'joining' message that nothing cleared, because the server does not answer a repeated join, and the voice page showed its joining label whenever the member list was empty. Joining is now an explicit voice state. The sidebar, the voice page and the status text all read one derived status; a repeated click only shows the room, and a server error cancels a pending join.
This commit is contained in:
parent
69d108b090
commit
7d488faf59
9 changed files with 153 additions and 14 deletions
|
|
@ -2,9 +2,11 @@ mod default;
|
|||
mod models;
|
||||
mod navigation;
|
||||
mod types;
|
||||
mod voice;
|
||||
|
||||
pub use models::{Channel, ChatMessage, DmConversation, FriendState, GuildState, NodeBookmark};
|
||||
pub use types::{ConnState, TrustPrompt, TrustPromptKind, UiState, VoiceUiState};
|
||||
pub use voice::VoiceStatus;
|
||||
|
||||
use models::bookmark_label_from_address;
|
||||
|
||||
|
|
|
|||
|
|
@ -5,6 +5,10 @@ use std::time::Instant;
|
|||
pub enum VoiceUiState {
|
||||
#[default]
|
||||
Off,
|
||||
/// A join was requested; audio is not running yet.
|
||||
Joining {
|
||||
channel: String,
|
||||
},
|
||||
Active {
|
||||
channel: String,
|
||||
},
|
||||
|
|
|
|||
126
src/ui/state/voice.rs
Normal file
126
src/ui/state/voice.rs
Normal file
|
|
@ -0,0 +1,126 @@
|
|||
//! Voice connection status: one state machine feeds the status bar, the sidebar and the
|
||||
//! voice page, so they can never disagree.
|
||||
use super::{ConnState, UiState, VoiceUiState};
|
||||
|
||||
/// What the interface shows about the voice connection.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum VoiceStatus {
|
||||
Off,
|
||||
/// A join was requested and audio is not running yet.
|
||||
Joining,
|
||||
/// Audio is running in the voice room.
|
||||
Connected,
|
||||
Failed(String),
|
||||
}
|
||||
|
||||
impl UiState {
|
||||
/// Derives the displayed voice status from the connection and voice state.
|
||||
pub fn voice_status(&self) -> VoiceStatus {
|
||||
if !matches!(self.conn, ConnState::Connected { .. }) {
|
||||
return VoiceStatus::Off;
|
||||
}
|
||||
match &self.voice {
|
||||
VoiceUiState::Off => VoiceStatus::Off,
|
||||
VoiceUiState::Joining { .. } => VoiceStatus::Joining,
|
||||
VoiceUiState::Active { .. } => VoiceStatus::Connected,
|
||||
VoiceUiState::Error(message) => VoiceStatus::Failed(message.clone()),
|
||||
}
|
||||
}
|
||||
|
||||
/// Handles a click on a voice channel. Returns true when a join request must be sent to
|
||||
/// the server. Clicking the room you are already in only shows it: the server does not
|
||||
/// answer a repeated join, so waiting for a reply would leave "joining" on screen forever.
|
||||
pub fn request_voice_join(&mut self, channel_id: &str) -> bool {
|
||||
self.showing_voice = true;
|
||||
let current = match &self.voice {
|
||||
VoiceUiState::Joining { channel } | VoiceUiState::Active { channel } => {
|
||||
Some(channel.as_str())
|
||||
}
|
||||
_ => None,
|
||||
};
|
||||
if current == Some(channel_id) {
|
||||
return false;
|
||||
}
|
||||
self.voice = VoiceUiState::Joining {
|
||||
channel: channel_id.to_string(),
|
||||
};
|
||||
true
|
||||
}
|
||||
|
||||
/// The server refused or lost a pending join: stop showing "joining".
|
||||
pub fn cancel_pending_voice_join(&mut self) {
|
||||
if matches!(self.voice, VoiceUiState::Joining { .. }) {
|
||||
self.voice = VoiceUiState::Off;
|
||||
self.voice_target = None;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn connected() -> UiState {
|
||||
UiState {
|
||||
conn: ConnState::Connected {
|
||||
session_id: "s".into(),
|
||||
node_name: "n".into(),
|
||||
},
|
||||
..UiState::default()
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn joining_becomes_connected_only_when_audio_is_active() {
|
||||
let mut ui = connected();
|
||||
assert_eq!(ui.voice_status(), VoiceStatus::Off);
|
||||
assert!(ui.request_voice_join("lounge"));
|
||||
assert_eq!(ui.voice_status(), VoiceStatus::Joining);
|
||||
ui.voice = VoiceUiState::Active {
|
||||
channel: "lounge".into(),
|
||||
};
|
||||
assert_eq!(ui.voice_status(), VoiceStatus::Connected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn clicking_the_current_room_never_restarts_joining() {
|
||||
let mut ui = connected();
|
||||
ui.voice = VoiceUiState::Active {
|
||||
channel: "lounge".into(),
|
||||
};
|
||||
ui.showing_voice = false;
|
||||
assert!(!ui.request_voice_join("lounge"));
|
||||
assert!(ui.showing_voice);
|
||||
assert_eq!(ui.voice_status(), VoiceStatus::Connected);
|
||||
// A different room is a real join.
|
||||
assert!(ui.request_voice_join("other"));
|
||||
assert_eq!(ui.voice_status(), VoiceStatus::Joining);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn retry_after_failure_and_cancel_of_a_refused_join() {
|
||||
let mut ui = connected();
|
||||
ui.voice = VoiceUiState::Error("no mic".into());
|
||||
assert_eq!(ui.voice_status(), VoiceStatus::Failed("no mic".into()));
|
||||
assert!(ui.request_voice_join("lounge"));
|
||||
ui.cancel_pending_voice_join();
|
||||
assert_eq!(ui.voice_status(), VoiceStatus::Off);
|
||||
// Cancelling does not touch an established connection.
|
||||
ui.voice = VoiceUiState::Active {
|
||||
channel: "lounge".into(),
|
||||
};
|
||||
ui.cancel_pending_voice_join();
|
||||
assert_eq!(ui.voice_status(), VoiceStatus::Connected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn status_is_off_while_the_control_connection_is_down() {
|
||||
let ui = UiState {
|
||||
voice: VoiceUiState::Active {
|
||||
channel: "lounge".into(),
|
||||
},
|
||||
..UiState::default()
|
||||
};
|
||||
assert_eq!(ui.voice_status(), VoiceStatus::Off);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue