Skip to content

Commit

Permalink
szar
Browse files Browse the repository at this point in the history
  • Loading branch information
marci1175 committed Dec 22, 2023
1 parent da4b287 commit 723d7d6
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 25 deletions.
19 changes: 19 additions & 0 deletions src/app/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ use crate::app::input::Input;
#[derive(serde::Deserialize, serde::Serialize)]
#[serde(default)]
pub struct TemplateApp {
//request tracking
#[serde(skip)]
pub requests: RequestList,

//audio playback
#[serde(skip)]
pub audio_playback: AudioPlayback,
Expand Down Expand Up @@ -152,6 +156,9 @@ impl Default for TemplateApp {
let (itx, irx) = mpsc::channel::<String>();
let (audio_save_tx, audio_save_rx) = mpsc::channel::<String>();
Self {
//request tracking
requests: RequestList::default(),

//audio playback
audio_playback: AudioPlayback::default(),

Expand Down Expand Up @@ -726,3 +733,15 @@ impl Default for AudioPlayback {
}
}
}

//Request list to keep track of performance, one request per file
#[derive(Debug, Clone, Copy)]
pub struct RequestList {
pub audio: bool,
pub image: bool,
}
impl Default for RequestList {
fn default() -> Self {
Self { audio: true, image: true }
}
}
62 changes: 37 additions & 25 deletions src/app/ui/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,22 +291,39 @@ impl TemplateApp {
true => {
//if we already have the sound file :::

ui.with_layout(Layout::left_to_right(Align::Center), |ui|{
if ui.button("Play").clicked() {
ui.with_layout(Layout::top_down(Align::Center), |ui|{
match self.audio_playback.sink.as_mut() {
Some(sink) => {
match sink.is_paused() {
true => {
if ui.button("Play").clicked() {
sink.play();
};
},
false => {
if ui.button("Stop").clicked() {
sink.pause()
}
}
}
}
None => {
if ui.button("Play").clicked() {
let file = BufReader::new(File::open(PathBuf::from(format!("{}\\szeChat\\Client\\{}\\Audios\\{}", env!("APPDATA"), general_purpose::URL_SAFE_NO_PAD.encode(self.send_on_ip.clone()), audio.index))).unwrap());

let source = Decoder::new(file).unwrap();

self.audio_playback.sink = Some(Sink::try_new(&self.audio_playback.stream_handle).unwrap());

let file = BufReader::new(File::open(PathBuf::from(format!("{}\\szeChat\\Client\\{}\\Audios\\{}", env!("APPDATA"), general_purpose::URL_SAFE_NO_PAD.encode(self.send_on_ip.clone()), audio.index))).unwrap());

let source = Decoder::new(file).unwrap();

//let _play = self.stream_handle.play_raw(source.convert_samples());

//set ui

self.audio_playback.sink = Some(Sink::try_new(&self.audio_playback.stream_handle).unwrap());
let sink = self.audio_playback.sink.as_mut().unwrap();

self.audio_playback.sink.as_mut().unwrap().append(source);
sink.append(source);

};
sink.play();
};
}
}

});

ui.label(&audio.file_name);
Expand All @@ -318,20 +335,15 @@ impl TemplateApp {
Sink.pause();

}
let unpause = ui.button("Unpause");
if unpause.clicked() {
Sink.play();


}

}

},
false => {
//check if we already have sound file

//check if we are visible
if !ui.is_visible() {
if !ui.is_visible() || !self.requests.audio {
return;
}

Expand All @@ -344,7 +356,7 @@ impl TemplateApp {

let message = ClientMessage::construct_audio_request_msg(audio.index, passw, author, send_on_ip);

tokio::spawn(async move {
self.requests.audio = tokio::spawn(async move {
match client::send_msg(message).await {
Ok(ok) => {
match sender.send(ok) {
Expand All @@ -358,7 +370,7 @@ impl TemplateApp {
println!("{err} ln 264")
}
}
});
}).is_finished();

},
};
Expand Down Expand Up @@ -409,7 +421,7 @@ impl TemplateApp {
Err(_err) => {

//check if we are visible
if !ui.is_visible() {
if !ui.is_visible() || !self.requests.image {
return;
}

Expand All @@ -422,7 +434,7 @@ impl TemplateApp {

let message = ClientMessage::construct_image_request_msg(picture.index, passw, author, send_on_ip);

tokio::spawn(async move {
self.requests.image = tokio::spawn(async move {
match client::send_msg(message).await {
Ok(ok) => {
match sender.send(ok) {
Expand All @@ -436,7 +448,7 @@ impl TemplateApp {
println!("{err} ln 264")
}
}
});
}).is_finished();

},
};
Expand Down

0 comments on commit 723d7d6

Please sign in to comment.