Skip to content

Commit

Permalink
cargo fmt cargo clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
marci1175 committed Jan 7, 2024
1 parent a6fa830 commit 42b772e
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 63 deletions.
2 changes: 1 addition & 1 deletion build_info.matthias_build
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2024.01.07. 14:41
2024.01.07. 14:43
2 changes: 1 addition & 1 deletion src/app/account_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ pub fn login(username: String, passw: String) -> Result<PathBuf> {
}

pub fn register(username: String, passw: String) -> Result<()> {
if username.contains(" ") || username.contains("@") || username.contains(" "){
if username.contains(' ') || username.contains('@') || username.contains(' ') {
return Err(anyhow::Error::msg("Cant use special characters in name"));
}

Expand Down
27 changes: 15 additions & 12 deletions src/app/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,7 @@ impl Default for TemplateApp {
let (itx, irx) = mpsc::channel::<String>();
let (audio_save_tx, audio_save_rx) = mpsc::channel::<String>();
Self {
audio_file:
Arc::new(Mutex::new(PathBuf::from(format!(
audio_file: Arc::new(Mutex::new(PathBuf::from(format!(
"{}\\Matthias\\Client\\voice_record.wav",
env!("APPDATA")
)))),
Expand Down Expand Up @@ -213,8 +212,6 @@ impl TemplateApp {
/// Client Ui
#[derive(serde::Deserialize, serde::Serialize)]
pub struct Client {


pub emoji_reaction_tray_rect: egui::Rect,

#[serde(skip)]
Expand All @@ -231,7 +228,6 @@ pub struct Client {
#[serde(skip)]
pub message_group_is_hovered: bool,


///audio playback
#[serde(skip)]
pub audio_playback: AudioPlayback,
Expand Down Expand Up @@ -355,8 +351,7 @@ impl Default for Client {
}

///Main, Global stuff
#[derive(serde::Deserialize, serde::Serialize)]
#[derive(Default)]
#[derive(serde::Deserialize, serde::Serialize, Default)]
pub struct Main {
///Checks if windwos needs to be set up
#[serde(skip)]
Expand Down Expand Up @@ -534,9 +529,17 @@ impl ClientMessage {
password: String,
ip: String,
) -> ClientMessage {
ClientMessage { replying_to: None, MessageType: ClientMessageType::ClientReaction(
ClientReaction { char: char, message_index: index }
), Password: password, Author: author, MessageDate: { Utc::now().format("%Y.%m.%d. %H:%M").to_string() }, Destination: ip }
ClientMessage {
replying_to: None,
MessageType: ClientMessageType::ClientReaction(ClientReaction {
char,
message_index: index,
}),
Password: password,
Author: author,
MessageDate: { Utc::now().format("%Y.%m.%d. %H:%M").to_string() },
Destination: ip,
}
}

///this is used for constructing a sync msg aka sending an empty packet, so server can reply
Expand Down Expand Up @@ -791,7 +794,7 @@ impl ServerOutput {
},
Author: normal_msg.Author,
MessageDate: normal_msg.MessageDate,
reactions: reactions,
reactions,
}
}
}
Expand Down Expand Up @@ -902,4 +905,4 @@ impl ScrollToMessage {
pub fn new(messages: Vec<egui::Response>, index: usize) -> ScrollToMessage {
ScrollToMessage { messages, index }
}
}
}
73 changes: 50 additions & 23 deletions src/app/server.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use std::{env, fs, io::Write, path::PathBuf};

use super::backend::{ServerMessageTypeDiscriminants::{Audio, Image, Normal, Upload}, MessageReaction, Reaction};
use super::backend::{
MessageReaction, Reaction,
ServerMessageTypeDiscriminants::{Audio, Image, Normal, Upload},
};
use rand::Rng;
use std::sync::Mutex;
use tonic::{transport::Server, Request, Response, Status};
Expand All @@ -26,11 +29,12 @@ use messages::{
use crate::app::backend::ServerMaster;
use crate::app::backend::{
ClientFileRequestType as ClientRequestTypeStruct, ClientFileUpload as ClientFileUploadStruct,
ClientMessage, ClientReaction as ClientReactionStruct,
ClientMessage,
ClientMessageType::{
ClientFileRequestType, ClientFileUpload, ClientNormalMessage, ClientSyncMessage, ClientReaction
ClientFileRequestType, ClientFileUpload, ClientNormalMessage, ClientReaction,
ClientSyncMessage,
},
ServerFileReply, ServerImageReply,
ClientReaction as ClientReactionStruct, ServerFileReply, ServerImageReply,
};

use super::backend::{ServerAudioReply, ServerOutput};
Expand Down Expand Up @@ -269,7 +273,12 @@ impl MessageService {
pub async fn NormalMessage(&self, req: ClientMessage) {
match self.messages.lock() {
Ok(mut ok) => {
ok.push(ServerOutput::convert_type_to_servermsg(req, -1, Normal, MessageReaction::default()));
ok.push(ServerOutput::convert_type_to_servermsg(
req,
-1,
Normal,
MessageReaction::default(),
));
}
Err(err) => {
println!("{err}")
Expand Down Expand Up @@ -355,7 +364,7 @@ impl MessageService {
request,
self.original_file_paths.lock().unwrap().len() as i32 - 1,
Upload,
MessageReaction::default()
MessageReaction::default(),
));
}
Err(err) => println!("{err}"),
Expand Down Expand Up @@ -404,7 +413,7 @@ impl MessageService {
req.clone(),
image_path_lenght as i32,
Image,
MessageReaction::default()
MessageReaction::default(),
));
}
Err(err) => println!("{err}"),
Expand Down Expand Up @@ -450,7 +459,7 @@ impl MessageService {
req.clone(),
audio_paths_lenght as i32,
Audio,
MessageReaction::default()
MessageReaction::default(),
));
}
Err(err) => println!("{err}"),
Expand Down Expand Up @@ -535,30 +544,48 @@ impl MessageService {
}

pub async fn handle_reaction(&self, reaction: &ClientReactionStruct) {

match &mut self.messages.try_lock() {
Ok(message_vec) => {
for (index, item) in message_vec[reaction.message_index].clone().reactions.message_reactions.iter().enumerate() {

for (index, item) in message_vec[reaction.message_index]
.clone()
.reactions
.message_reactions
.iter()
.enumerate()
{
//Check if it has already been reacted before
if item.char == reaction.char {
message_vec[reaction.message_index].reactions.message_reactions[index].times += 1;
}
else {
message_vec[reaction.message_index].reactions.message_reactions.push(
Reaction { char: reaction.char, times: 0 }
)
message_vec[reaction.message_index]
.reactions
.message_reactions[index]
.times += 1;
} else {
message_vec[reaction.message_index]
.reactions
.message_reactions
.push(Reaction {
char: reaction.char,
times: 0,
})
}

}

//First reaction
if message_vec[reaction.message_index].clone().reactions.message_reactions.is_empty() {
message_vec[reaction.message_index].reactions.message_reactions.push(
Reaction { char: reaction.char, times: 0 }
)
if message_vec[reaction.message_index]
.clone()
.reactions
.message_reactions
.is_empty()
{
message_vec[reaction.message_index]
.reactions
.message_reactions
.push(Reaction {
char: reaction.char,
times: 0,
})
}
},
}
Err(err) => println!("{err}"),
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/app/ui/client_ui/client_actions/audio_recording.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ pub fn audio_recroding(receiver: mpsc::Receiver<bool>, PATH: Arc<Mutex<PathBuf>>
}
.expect("failed to find input device");


let config = device
.default_input_config()
.expect("Failed to get default input config");
Expand All @@ -61,7 +60,7 @@ pub fn audio_recroding(receiver: mpsc::Receiver<bool>, PATH: Arc<Mutex<PathBuf>>
let spec = wav_spec_from_config(&config);

let PATH = PATH.lock().unwrap().to_string_lossy().to_string().clone();

let writer = hound::WavWriter::create(PATH, spec)?;

let writer = Arc::new(Mutex::new(Some(writer)));
Expand Down
1 change: 0 additions & 1 deletion src/app/ui/client_ui/message_instances/audio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ impl TemplateApp {
.speed,
);
}

/*
let pos = self.client_ui.audio_playback.settings_list[current_index_in_message_list].cursor_offset;
if let Some(cursor) = self.client_ui.audio_playback.settings_list[current_index_in_message_list].cursor.as_mut() {
Expand Down
19 changes: 4 additions & 15 deletions src/app/ui/client_ui/message_instances/message_main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use egui::{vec2, Align, Color32, Layout, Response, RichText};

//use crate::app::account_manager::write_file;
use crate::app::{backend::{AudioSettings, ScrollToMessage, ServerMessageType, TemplateApp, ClientMessage}, client};
use crate::app::{
backend::{AudioSettings, ClientMessage, ScrollToMessage, ServerMessageType, TemplateApp},
client,
};

impl TemplateApp {
pub fn client_ui_message_main(
Expand Down Expand Up @@ -47,7 +50,6 @@ impl TemplateApp {
//Define defaults, for speed and volume based on the same logic as above ^
self.client_ui.audio_playback.settings_list.push(AudioSettings::default());
}

let mut message_instances: Vec<Response> = Vec::new();

for (index, item) in self.client_ui.incoming_msg.clone().struct_list.iter().enumerate() {
Expand Down Expand Up @@ -85,26 +87,22 @@ impl TemplateApp {
).size(self.font_size / 1.5))
.frame(false))
.clicked() {

//implement scrolling to message
self.client_ui.scroll_to_message_index = Some(replied_to);
}
});
}

//Display author
ui.label(RichText::from(item.Author.to_string()).size(self.font_size / 1.3).color(Color32::WHITE));

//IMPORTANT: Each of these functions have logic inside them for displaying
self.markdown_text_display(i, ui);

self.audio_message_instance(item, ui, index);
self.file_message_instance(item, ui);
self.image_message_instance(item, ui, ctx);

//Display Message date
ui.label(RichText::from(item.MessageDate.to_string()).size(self.font_size / 1.5).color(Color32::DARK_GRAY));

// ui.allocate_ui(vec2(ui.available_width(), 30.), |ui|{
// if self.client_ui.message_group_is_hovered {
// ui.allocate_ui(vec2(20., 20.), |ui|{
Expand All @@ -125,7 +123,6 @@ impl TemplateApp {
// ui.allocate_space(vec2(1., 20.));
// }
// });

for item in &item.reactions.message_reactions {
ui.horizontal(|ui|{

Expand All @@ -134,7 +131,6 @@ impl TemplateApp {
});

ui.label(RichText::from(item.times.to_string()).size(self.font_size));

});
}
}
Expand Down Expand Up @@ -162,7 +158,6 @@ impl TemplateApp {
}),
)
.frame(false);

if ui.add(button).clicked() {
let message = ClientMessage::construct_reaction_msg(
chr, index, self.login_username.clone(), match self.client_ui.req_passw {
Expand All @@ -185,10 +180,6 @@ impl TemplateApp {
});
});





if ui.button("Reply").clicked() {
self.client_ui.replying_to = Some(index);
}
Expand All @@ -197,8 +188,6 @@ impl TemplateApp {
};
});



self.client_ui.message_group_is_hovered = message_group.hovered();

message_instances.push(message_group);
Expand Down
11 changes: 7 additions & 4 deletions src/app/ui/client_ui/message_instances/text.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use egui::{RichText, Color32};
use egui::{Color32, RichText};

use regex::Regex;

Expand All @@ -7,7 +7,6 @@ use crate::app::backend::TemplateApp;

impl TemplateApp {
pub fn markdown_text_display(&mut self, i: &String, ui: &mut egui::Ui) {

if (i.contains('[') && i.contains(']')) && (i.contains('(') && i.contains(')')) {
let re = Regex::new(r"\[(?P<link_text>[^\]]*)\]\((?P<link_target>[^)]+)\)").unwrap();
let mut captures: Vec<String> = Vec::new();
Expand All @@ -29,10 +28,14 @@ impl TemplateApp {
}
});
}
} else if let Some(index) = i.find("@") {
} else if let Some(index) = i.find('@') {
let result = i[index + 1..].split_whitespace().collect::<Vec<&str>>()[0];
if self.login_username == result {
ui.label(RichText::from(i).size(self.font_size).color(Color32::YELLOW));
ui.label(
RichText::from(i)
.size(self.font_size)
.color(Color32::YELLOW),
);
}
} else if i.contains('#') && i.rmatches('#').count() <= 5 {
let split_lines = i.rsplit_once('#').unwrap();
Expand Down
2 changes: 1 addition & 1 deletion src/app/ui/client_ui/widgets/emoji_tray/emoji.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ impl backend::TemplateApp {
.collect()
})
}

pub fn window_emoji(&mut self, ctx: &egui::Context) {
egui::Window::new("Character set")
.open(&mut self.main.emoji_mode)
Expand Down
2 changes: 1 addition & 1 deletion src/app/ui/client_ui/widgets/emoji_tray/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
pub mod emoji;
pub mod emoji;
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,6 @@ impl TemplateApp {
//Path to voice recording created by audio_recording.rs, Arc mutex to avoid data races
match self.audio_file.clone().try_lock() {
Ok(ok) => {

self.send_file(ok.to_path_buf());

let _ = fs::remove_file(ok.to_path_buf());
Expand All @@ -205,7 +204,6 @@ impl TemplateApp {
//Destroy state
self.atx = None;
}

});
} else if ui
.add(egui::ImageButton::new(egui::include_image!(
Expand Down

0 comments on commit 42b772e

Please sign in to comment.