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

Commit

Permalink
saving presets :D
Browse files Browse the repository at this point in the history
  • Loading branch information
Tofixrs committed Aug 19, 2022
1 parent 3705b87 commit 12ba8c7
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "discord_presence"
version = "0.1.0"
version = "0.3.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
30 changes: 24 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ mod preset;
mod storage;
mod timestamp;

use preset::Preset;
use storage::Storage;
use timestamp::{Timestamp, TimestampEnum};

use std::time::Duration;
use std::vec;
use std::{fs, vec};

use serde_json::{from_str, to_string};

Expand Down Expand Up @@ -40,7 +41,7 @@ fn main() {
Box::new(|cc| Box::new(App::new(cc))),
);
}
struct App {
pub struct App {
menu_bar: menu_bar::MenuBar,
first_btn: presence_button::PresenceButton,
second_btn: presence_button::PresenceButton,
Expand Down Expand Up @@ -255,10 +256,11 @@ 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.2.1-beta");
ui.label("Version v0.3-beta");
});
});
self.load_preset()
self.load_preset();
self.save_preset();
}
}
impl App {
Expand Down Expand Up @@ -375,7 +377,7 @@ impl App {
self.party_of = size;
}

self.timestamp.timestamp = preset.timestamp();
self.timestamp.timestamp = preset.timestamp_from_num();
if let Some(key) = preset.LargeKey.as_ref() {
self.first_img.key = key.to_string();
}
Expand All @@ -400,7 +402,23 @@ impl App {
if let Some(url) = preset.Button1URL.as_ref() {
self.first_btn.url = url.to_string();
}
self.menu_bar.loaded_preset = None
self.menu_bar.loaded_preset = None;

if self.connected {
self.set_presence()
}
}
}

fn save_preset(&mut self) {
if self.menu_bar.preset_save_location.is_some() {
let preset = Preset::from_app(self);
fs::write(
self.menu_bar.preset_save_location.as_ref().unwrap(),
preset.to_xml(),
)
.expect("Failed to save preset");
self.menu_bar.preset_save_location = None;
}
}
}
27 changes: 19 additions & 8 deletions src/menu_bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub struct MenuBar {
pub darkmode: bool,
pub about_me: bool,
pub loaded_preset: Option<Preset>,
pub preset_save_location: Option<PathBuf>,
}

impl Default for MenuBar {
Expand All @@ -23,6 +24,7 @@ impl Default for MenuBar {
darkmode: true,
about_me: false,
loaded_preset: None,
preset_save_location: None,
}
}
}
Expand All @@ -41,7 +43,9 @@ impl MenuBar {
if ui.button("Load Preset | Ctrl + O").clicked() {
self.load_preset();
}
if ui.button("Save Preset | Ctrl + S").clicked() {}
if ui.button("Save Preset | Ctrl + S").clicked() {
self.save_preset();
}
if ui.button("Upload Assets | Ctrl + U").clicked() {}
if ui.button("Exit | Alt + F4").clicked() {
exit(0)
Expand All @@ -66,20 +70,27 @@ impl MenuBar {
}
});
}

fn load_preset(&mut self) {
let file = match FileDialog::new()
let file = FileDialog::new()
.add_filter("Preset", &["crp"])
.set_directory("/")
.set_title("Load preset")
.pick_file()
{
None => PathBuf::new(),
Some(path) => path,
};
if file.to_str().unwrap() != "" {
.pick_file();
if let Some(file) = file {
let file = fs::read_to_string(file).unwrap();
let xml: Preset = serde_xml_rs::from_str(&file).unwrap();
self.loaded_preset = Some(xml);
}
}

fn save_preset(&mut self) {
let file = FileDialog::new()
.add_filter("Preset", &["crp"])
.set_directory("/")
.set_title("Save Preset")
.set_file_name("Preset")
.save_file();
self.preset_save_location = file;
}
}
33 changes: 31 additions & 2 deletions src/preset.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![allow(non_snake_case)]

use crate::TimestampEnum;
use crate::{App, TimestampEnum};
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
Expand All @@ -23,7 +23,7 @@ pub struct Preset {
}

impl Preset {
pub fn timestamp(&self) -> TimestampEnum {
pub fn timestamp_from_num(&self) -> TimestampEnum {
if self.Timestamps == None {
return TimestampEnum::None;
}
Expand All @@ -37,4 +37,33 @@ impl Preset {
_ => TimestampEnum::None,
}
}

pub fn from_app(app: &App) -> Self {
Self {
ID: Some(app.id.clone()),
Details: Some(app.details.clone()),
State: Some(app.state.clone()),
PartySize: Some(app.party),
PartyMax: Some(app.party_of),
Timestamps: Some(app.timestamp.timestamp.to_num()),
CustomTimestamp: None,
LargeKey: Some(app.first_img.key.clone()),
LargeText: Some(app.first_img.text.clone()),
SmallKey: Some(app.second_img.key.clone()),
SmallText: Some(app.second_img.text.clone()),
Button1Text: Some(app.first_btn.label.clone()),
Button1URL: Some(app.first_btn.url.clone()),
Button2Text: Some(app.second_btn.label.clone()),
Button2URL: Some(app.second_btn.url.clone()),
}
}

pub fn to_xml(&self) -> String {
let file = serde_xml_rs::to_string(self).unwrap();
let (_, xml) = file.split_at(8);
let xml = r#"<?xml version="1.0"?><Preset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">"#.to_string()
+ xml;

xml.replace("<CustomTimestamp></CustomTimestamp>", "")
}
}
13 changes: 13 additions & 0 deletions src/timestamp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,19 @@ pub enum TimestampEnum {
LocalTime,
CustomTimeStamp,
}

impl TimestampEnum {
pub fn to_num(self) -> u8 {
match self {
TimestampEnum::None => 0,
TimestampEnum::SinceStart => 1,
TimestampEnum::SinceLastUpdate => 4,
TimestampEnum::LocalTime => 2,
TimestampEnum::CustomTimeStamp => 3,
}
}
}

pub struct Timestamp {
pub timestamp: TimestampEnum,
pub date: Date<Utc>,
Expand Down

0 comments on commit 12ba8c7

Please sign in to comment.