feat: 💄 two button turned into one button
This commit is contained in:
parent
6eb3e9b419
commit
c5df598338
3 changed files with 71 additions and 19 deletions
|
@ -1,5 +1,5 @@
|
|||
use iced::{
|
||||
widget::{button, column, Column},
|
||||
widget::{button, column, container, Container},
|
||||
Command,
|
||||
};
|
||||
use tokio::sync::broadcast::{channel, Sender};
|
||||
|
@ -19,6 +19,9 @@ pub struct Streamer {
|
|||
sound_stream_producer: Sender<f32>,
|
||||
stop_connection_producer: Sender<bool>,
|
||||
stop_recording_producer: Sender<bool>,
|
||||
connection_cleaning_status_producer: Sender<bool>,
|
||||
are_we_streaming: bool,
|
||||
are_we_recovering: bool,
|
||||
}
|
||||
impl Default for Streamer {
|
||||
fn default() -> Self {
|
||||
|
@ -33,37 +36,76 @@ impl Streamer {
|
|||
sound_stream_producer: channel(BUFFER_LENGTH).0,
|
||||
stop_connection_producer: channel(BUFFER_LENGTH).0,
|
||||
stop_recording_producer: channel(BUFFER_LENGTH).0,
|
||||
connection_cleaning_status_producer: channel(BUFFER_LENGTH).0,
|
||||
are_we_streaming: false,
|
||||
are_we_recovering: false,
|
||||
}
|
||||
}
|
||||
pub fn update(&mut self, message: Message) {
|
||||
match message {
|
||||
Message::StartStreaming => {
|
||||
println!("Start Stream");
|
||||
tokio::spawn(streaming::connect(
|
||||
self.sound_stream_producer.subscribe(),
|
||||
self.config.clone().unwrap(),
|
||||
self.stop_connection_producer.subscribe(),
|
||||
));
|
||||
tokio::spawn(recording::record(
|
||||
self.sound_stream_producer.clone(),
|
||||
self.stop_recording_producer.subscribe(),
|
||||
));
|
||||
if !self.are_we_recovering && !self.are_we_streaming {
|
||||
println!("Start Stream");
|
||||
self.are_we_recovering = true;
|
||||
self.are_we_streaming = true;
|
||||
tokio::spawn(streaming::connect(
|
||||
self.sound_stream_producer.subscribe(),
|
||||
self.config.clone().unwrap(),
|
||||
self.stop_connection_producer.subscribe(),
|
||||
self.connection_cleaning_status_producer.clone(),
|
||||
));
|
||||
tokio::spawn(recording::record(
|
||||
self.sound_stream_producer.clone(),
|
||||
self.stop_recording_producer.subscribe(),
|
||||
));
|
||||
self.are_we_recovering = false;
|
||||
}
|
||||
}
|
||||
Message::StopStreaming => {
|
||||
println!("Stop Stream");
|
||||
self.stop_connection_producer.send(true).unwrap();
|
||||
self.stop_recording_producer.send(true).unwrap();
|
||||
if !self.are_we_recovering && self.are_we_streaming {
|
||||
println!("Stop Stream");
|
||||
self.are_we_recovering = true;
|
||||
self.are_we_streaming = false;
|
||||
let _ = self.connection_cleaning_status_producer.send(true);
|
||||
let _ = self.stop_connection_producer.send(true);
|
||||
let _ = self.stop_recording_producer.send(true);
|
||||
while !self.connection_cleaning_status_producer.is_empty() {}
|
||||
self.are_we_recovering = false;
|
||||
}
|
||||
}
|
||||
Message::ConfigLoad(config) => {
|
||||
self.config = Some(config);
|
||||
}
|
||||
}
|
||||
}
|
||||
pub fn view(&self) -> Column<Message> {
|
||||
column![
|
||||
button("Start Streaming").on_press(Message::StartStreaming),
|
||||
button("Stop Streaming").on_press(Message::StopStreaming),
|
||||
]
|
||||
pub fn view(&self) -> Container<Message> {
|
||||
let column = match self.are_we_streaming {
|
||||
true => match self.are_we_recovering {
|
||||
true => {
|
||||
column![button("Stop Streaming").width(100),]
|
||||
}
|
||||
false => {
|
||||
column![button("Stop Streaming")
|
||||
.width(100)
|
||||
.on_press(Message::StopStreaming),]
|
||||
}
|
||||
},
|
||||
false => match self.are_we_recovering {
|
||||
true => {
|
||||
column![button("Start Streaming").width(100),]
|
||||
}
|
||||
false => {
|
||||
column![button("Start Streaming")
|
||||
.width(100)
|
||||
.on_press(Message::StartStreaming),]
|
||||
}
|
||||
},
|
||||
};
|
||||
container(column)
|
||||
.width(200)
|
||||
.height(200)
|
||||
.center_x()
|
||||
.center_y()
|
||||
}
|
||||
pub fn load_config() -> Command<Message> {
|
||||
Command::perform(get_config(), Message::ConfigLoad)
|
||||
|
|
|
@ -4,6 +4,8 @@ use streamer::gui::Streamer;
|
|||
async fn main() -> iced::Result {
|
||||
println!("Hello, world!");
|
||||
iced::program("Streamer GUI", Streamer::update, Streamer::view)
|
||||
.centered()
|
||||
.window_size((250.0, 250.0))
|
||||
.load(Streamer::load_config)
|
||||
.run()
|
||||
}
|
||||
|
|
|
@ -16,6 +16,7 @@ pub async fn connect(
|
|||
sound_stream_consumer: Receiver<f32>,
|
||||
streamer_config: Config,
|
||||
mut stop_connection_consumer: Receiver<bool>,
|
||||
connection_cleaning_status_producer: Sender<bool>,
|
||||
) {
|
||||
let connect_addr = match streamer_config.tls {
|
||||
true => format!("wss://{}", streamer_config.address),
|
||||
|
@ -64,6 +65,7 @@ pub async fn connect(
|
|||
message_organizer_task,
|
||||
stream_task,
|
||||
stop_connection_consumer,
|
||||
connection_cleaning_status_producer,
|
||||
));
|
||||
}
|
||||
}
|
||||
|
@ -168,11 +170,17 @@ async fn status_checker(
|
|||
message_organizer_task: JoinHandle<()>,
|
||||
stream_task: JoinHandle<()>,
|
||||
mut stop_connection_consumer: Receiver<bool>,
|
||||
connection_cleaning_status_producer: Sender<bool>,
|
||||
) {
|
||||
let mut connection_cleaning_status_consumer = connection_cleaning_status_producer.subscribe();
|
||||
connection_cleaning_status_producer.send(true).unwrap();
|
||||
while let Err(_) = stop_connection_consumer.try_recv() {
|
||||
tokio::time::sleep(Duration::from_secs(3)).await;
|
||||
}
|
||||
stream_task.abort();
|
||||
message_organizer_task.abort();
|
||||
while let Ok(_) = connection_cleaning_status_consumer.try_recv() {}
|
||||
drop(connection_cleaning_status_consumer);
|
||||
drop(connection_cleaning_status_producer);
|
||||
println!("Cleaning Done: Streamer Disconnected");
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue