Skip to content
This repository has been archived by the owner on Oct 15, 2022. It is now read-only.

Commit

Permalink
Error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Tofixrs committed Aug 20, 2022
1 parent 3833d1a commit 625a76d
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 33 deletions.
33 changes: 33 additions & 0 deletions src/error_bar.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use chrono::{DateTime, TimeZone, Utc};
use eframe::egui::{self, Color32, Context, Layout, RichText};

#[derive(Default)]
pub struct ErrorBar {
pub error: Option<String>,
time_til_end: Option<DateTime<Utc>>,
}

impl ErrorBar {
pub fn run(&mut self, ctx: &Context) {
if self.time_til_end.is_some() {
if let Some(error) = &self.error {
egui::TopBottomPanel::bottom("error_bar").show(ctx, |ui| {
ui.with_layout(Layout::right_to_left(), |ui| {
ui.label(
RichText::new("Error: ".to_string() + error).color(Color32::LIGHT_RED),
);
})
});
}
if self.time_til_end.unwrap().timestamp_nanos() < Utc::now().timestamp_nanos() {
self.time_til_end = None;
self.error = None;
}
}
}

pub fn new_error(&mut self, error: String) {
self.time_til_end = Some(Utc.timestamp(Utc::now().timestamp() + 3, 0));
self.error = Some(error);
}
}
112 changes: 79 additions & 33 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release

mod error_bar;
mod image;
mod menu_bar;
mod presence_button;
mod preset;
mod storage;
mod timestamp;

use error_bar::ErrorBar;
use image::Image;
use presence_button::PresenceButton;
use preset::Preset;
use storage::Storage;
use timestamp::{Timestamp, TimestampEnum};
Expand Down Expand Up @@ -42,6 +46,7 @@ fn main() {
);
}
pub struct App {
error_bar: ErrorBar,
menu_bar: menu_bar::MenuBar,
first_btn: presence_button::PresenceButton,
second_btn: presence_button::PresenceButton,
Expand All @@ -62,11 +67,12 @@ pub struct App {
impl Default for App {
fn default() -> Self {
Self {
error_bar: ErrorBar::default(),
menu_bar: menu_bar::MenuBar::default(),
first_btn: presence_button::PresenceButton::default(),
second_btn: presence_button::PresenceButton::default(),
first_img: image::Image::default(),
second_img: image::Image::default(),
first_btn: PresenceButton::default(),
second_btn: PresenceButton::default(),
first_img: Image::default(),
second_img: Image::default(),
id: String::new(),
details: String::new(),
party: 0,
Expand Down Expand Up @@ -111,11 +117,15 @@ impl App {
true => cc.egui_ctx.set_visuals(egui::Visuals::dark()),
false => cc.egui_ctx.set_visuals(egui::Visuals::light()),
}
let mut client = DiscordIpcClient::new(storage.id)
.expect("Failed to create client while loading storage");
if storage.autoconnect {
client.connect().expect("Failed to autoconnect on startup");
}
let mut client =
DiscordIpcClient::new(storage.id).expect("No reason for this to failed either");
let error = match storage.autoconnect {
true => match client.connect() {
Ok(_) => "",
Err(_) => "Failed to connect. (AutoConnect)",
},
false => "",
};
let mut app = App {
id: storage.id.to_owned(),
details: storage.details.to_owned(),
Expand Down Expand Up @@ -150,7 +160,10 @@ impl App {
client,
..Default::default()
};
if storage.autoconnect {
if !error.is_empty() {
app.error_bar.new_error(error.to_string());
}
if storage.autoconnect && error.is_empty() {
app.set_presence();
app.connected = true;
}
Expand Down Expand Up @@ -181,10 +194,15 @@ impl eframe::App for App {
autoconnect: self.menu_bar.autoconnect,
darkmode: self.menu_bar.darkmode,
};
storage.set_string(
"settings",
to_string(&save).expect("Failed to parse save struct"),
);
let storage_string = match to_string(&save) {
Ok(save) => save,
Err(_) => "".to_string(),
};
if storage_string.is_empty() {
self.error_bar.new_error("Failed to save".to_string());
return;
}
storage.set_string("settings", storage_string);
}
fn auto_save_interval(&self) -> std::time::Duration {
Duration::from_secs(5)
Expand All @@ -205,18 +223,28 @@ impl eframe::App for App {
.clicked()
&& !self.id.is_empty()
{
self.client = DiscordIpcClient::new(&self.id).expect("sus");
self.client.connect().expect("Failed to connect to discord");
self.last_update = Utc::now();
self.set_presence();
self.connected = true;
self.client = DiscordIpcClient::new(&self.id)
.expect("Theres no reason for this function to fail bruh");
let error = match self.client.connect() {
Ok(_) => "",
Err(_) => "Failed to connect to discord",
};
if error.is_empty() {
self.last_update = Utc::now();
self.set_presence();
self.connected = true;
} else {
self.error_bar.new_error(error.to_string());
}
}
ui.add_space(10.);
if ui
.add_enabled(self.connected, egui::Button::new("Disconnect"))
.clicked()
{
self.client.close().expect("Failed to disconnect");
self.client
.close()
.expect("Theres no reason for this function to fail bruh");
self.connected = false;
}
});
Expand Down Expand Up @@ -272,22 +300,31 @@ impl eframe::App for App {
.show(ctx, |ui| {
ui.with_layout(Layout::top_down(Align::Center), |ui| {
ui.heading("Discord Presence");
ui.label("Version v0.3-beta");
ui.label("Version v0.5-beta");
});
});

//preset stuff
self.load_preset();
self.save_preset();

//Error bar
self.error_bar.run(ctx);
}
}
impl App {
fn set_presence(&mut self) {
if self.id != self.client.client_id {
self.client
.close()
.expect("Failed to disconnect while updating application");
self.client = DiscordIpcClient::new(&self.id)
.expect("Failed to create client while updating application id");
self.client.connect().expect("Failed to connect to discord");
self.client.close().expect("No Reason for this to fail...");
self.client = DiscordIpcClient::new(&self.id).expect("No Reason for this to fail...");
let error = match self.client.connect() {
Ok(_) => "",
Err(_) => "Failed to connect to discord",
};
if !error.is_empty() {
self.error_bar.new_error(error.to_string());
return;
}
}
let first_btn = Button::new(&self.first_btn.label, &self.first_btn.url);
let second_btn = Button::new(&self.second_btn.label, &self.second_btn.url);
Expand Down Expand Up @@ -370,10 +407,15 @@ impl App {
true => activity.party(Party::new().size([self.party_of as i32, self.party as i32])),
false => activity,
};
self.client
.set_activity(activity)
.expect("Failed to set activity");
let error = match self.client.set_activity(activity) {
Ok(_) => "",
Err(_) => "Failed to set activity",
};
if !error.is_empty() {
self.error_bar.new_error(error.to_string());
}
}

fn load_preset(&mut self) {
if self.menu_bar.loaded_preset.is_some() {
let preset = self.menu_bar.loaded_preset.as_ref().unwrap();
Expand Down Expand Up @@ -429,11 +471,15 @@ impl App {
fn save_preset(&mut self) {
if self.menu_bar.preset_save_location.is_some() {
let preset = Preset::from_app(self);
fs::write(
match fs::write(
self.menu_bar.preset_save_location.as_ref().unwrap(),
preset.to_xml(),
)
.expect("Failed to save preset");
) {
Ok(_) => (),
Err(_) => self
.error_bar
.new_error("Failed to save preset".to_string()),
}
self.menu_bar.preset_save_location = None;
}
}
Expand Down
1 change: 1 addition & 0 deletions src/menu_bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ impl MenuBar {
if let Some(file) = file {
let file = fs::read_to_string(file).unwrap();
let xml: Preset = serde_xml_rs::from_str(&file).unwrap();
println!("{:?}", xml);
self.loaded_preset = Some(xml);
}
}
Expand Down

0 comments on commit 625a76d

Please sign in to comment.