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::{
|
use iced::{
|
||||||
widget::{button, column, Column},
|
widget::{button, column, container, Container},
|
||||||
Command,
|
Command,
|
||||||
};
|
};
|
||||||
use tokio::sync::broadcast::{channel, Sender};
|
use tokio::sync::broadcast::{channel, Sender};
|
||||||
|
@ -19,6 +19,9 @@ pub struct Streamer {
|
||||||
sound_stream_producer: Sender<f32>,
|
sound_stream_producer: Sender<f32>,
|
||||||
stop_connection_producer: Sender<bool>,
|
stop_connection_producer: Sender<bool>,
|
||||||
stop_recording_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 {
|
impl Default for Streamer {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
|
@ -33,37 +36,76 @@ impl Streamer {
|
||||||
sound_stream_producer: channel(BUFFER_LENGTH).0,
|
sound_stream_producer: channel(BUFFER_LENGTH).0,
|
||||||
stop_connection_producer: channel(BUFFER_LENGTH).0,
|
stop_connection_producer: channel(BUFFER_LENGTH).0,
|
||||||
stop_recording_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) {
|
pub fn update(&mut self, message: Message) {
|
||||||
match message {
|
match message {
|
||||||
Message::StartStreaming => {
|
Message::StartStreaming => {
|
||||||
println!("Start Stream");
|
if !self.are_we_recovering && !self.are_we_streaming {
|
||||||
tokio::spawn(streaming::connect(
|
println!("Start Stream");
|
||||||
self.sound_stream_producer.subscribe(),
|
self.are_we_recovering = true;
|
||||||
self.config.clone().unwrap(),
|
self.are_we_streaming = true;
|
||||||
self.stop_connection_producer.subscribe(),
|
tokio::spawn(streaming::connect(
|
||||||
));
|
self.sound_stream_producer.subscribe(),
|
||||||
tokio::spawn(recording::record(
|
self.config.clone().unwrap(),
|
||||||
self.sound_stream_producer.clone(),
|
self.stop_connection_producer.subscribe(),
|
||||||
self.stop_recording_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 => {
|
Message::StopStreaming => {
|
||||||
println!("Stop Stream");
|
if !self.are_we_recovering && self.are_we_streaming {
|
||||||
self.stop_connection_producer.send(true).unwrap();
|
println!("Stop Stream");
|
||||||
self.stop_recording_producer.send(true).unwrap();
|
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) => {
|
Message::ConfigLoad(config) => {
|
||||||
self.config = Some(config);
|
self.config = Some(config);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn view(&self) -> Column<Message> {
|
pub fn view(&self) -> Container<Message> {
|
||||||
column![
|
let column = match self.are_we_streaming {
|
||||||
button("Start Streaming").on_press(Message::StartStreaming),
|
true => match self.are_we_recovering {
|
||||||
button("Stop Streaming").on_press(Message::StopStreaming),
|
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> {
|
pub fn load_config() -> Command<Message> {
|
||||||
Command::perform(get_config(), Message::ConfigLoad)
|
Command::perform(get_config(), Message::ConfigLoad)
|
||||||
|
|
|
@ -4,6 +4,8 @@ use streamer::gui::Streamer;
|
||||||
async fn main() -> iced::Result {
|
async fn main() -> iced::Result {
|
||||||
println!("Hello, world!");
|
println!("Hello, world!");
|
||||||
iced::program("Streamer GUI", Streamer::update, Streamer::view)
|
iced::program("Streamer GUI", Streamer::update, Streamer::view)
|
||||||
|
.centered()
|
||||||
|
.window_size((250.0, 250.0))
|
||||||
.load(Streamer::load_config)
|
.load(Streamer::load_config)
|
||||||
.run()
|
.run()
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,6 +16,7 @@ pub async fn connect(
|
||||||
sound_stream_consumer: Receiver<f32>,
|
sound_stream_consumer: Receiver<f32>,
|
||||||
streamer_config: Config,
|
streamer_config: Config,
|
||||||
mut stop_connection_consumer: Receiver<bool>,
|
mut stop_connection_consumer: Receiver<bool>,
|
||||||
|
connection_cleaning_status_producer: Sender<bool>,
|
||||||
) {
|
) {
|
||||||
let connect_addr = match streamer_config.tls {
|
let connect_addr = match streamer_config.tls {
|
||||||
true => format!("wss://{}", streamer_config.address),
|
true => format!("wss://{}", streamer_config.address),
|
||||||
|
@ -64,6 +65,7 @@ pub async fn connect(
|
||||||
message_organizer_task,
|
message_organizer_task,
|
||||||
stream_task,
|
stream_task,
|
||||||
stop_connection_consumer,
|
stop_connection_consumer,
|
||||||
|
connection_cleaning_status_producer,
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -168,11 +170,17 @@ async fn status_checker(
|
||||||
message_organizer_task: JoinHandle<()>,
|
message_organizer_task: JoinHandle<()>,
|
||||||
stream_task: JoinHandle<()>,
|
stream_task: JoinHandle<()>,
|
||||||
mut stop_connection_consumer: Receiver<bool>,
|
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() {
|
while let Err(_) = stop_connection_consumer.try_recv() {
|
||||||
tokio::time::sleep(Duration::from_secs(3)).await;
|
tokio::time::sleep(Duration::from_secs(3)).await;
|
||||||
}
|
}
|
||||||
stream_task.abort();
|
stream_task.abort();
|
||||||
message_organizer_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");
|
println!("Cleaning Done: Streamer Disconnected");
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue