Store state in one key instead of a key per state field

This commit is contained in:
pjht 2023-05-30 15:10:12 -05:00
parent ecea11ad12
commit c14c499467
Signed by: pjht
GPG Key ID: CA239FC6934E6F3A
2 changed files with 62 additions and 3 deletions

View File

@ -8,6 +8,7 @@ use eframe::egui;
use new_roll::NewRollWindow;
use roll::Roll;
use roll_view::RollViewWindow;
use serde::{Deserialize, Serialize};
fn main() -> Result<(), eframe::Error> {
env_logger::init(); // Log to stderr (if you run with `RUST_LOG=debug`).
@ -22,6 +23,7 @@ fn main() -> Result<(), eframe::Error> {
)
}
#[derive(Serialize, Deserialize, Default)]
pub struct AppState {
pub rolls: Vec<Roll>,
}
@ -34,9 +36,8 @@ struct MyApp {
impl MyApp {
fn new(cc: &eframe::CreationContext<'_>) -> Self {
let rolls = eframe::get_value(cc.storage.unwrap(), "rolls").unwrap_or_default();
Self {
state: AppState { rolls },
state: eframe::get_value(cc.storage.unwrap(), "state").unwrap_or_default(),
roll_views: Vec::new(),
new_roll_window: None,
}
@ -45,7 +46,7 @@ impl MyApp {
impl eframe::App for MyApp {
fn save(&mut self, storage: &mut dyn eframe::Storage) {
eframe::set_value(storage, "rolls", &self.state.rolls);
eframe::set_value(storage, "state", &self.state);
}
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {

58
src/roll.rs Normal file
View File

@ -0,0 +1,58 @@
use chrono::NaiveDate;
use serde::{Serialize, Deserialize};
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Exposure {
pub num: u8,
#[serde(default)]
pub printed: bool,
#[serde(default)]
pub damaged: bool,
#[serde(default)]
pub paper: Option<String>,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Roll {
pub id: String,
pub name: String,
#[serde(default)]
pub desc: String,
pub date: NaiveDate,
pub exposures: Vec<Exposure>,
}
impl Roll {
pub fn new(id: String, name: String, desc: String, date: NaiveDate, num_exposures: u8) -> Self {
let mut exposures = Vec::new();
for num in 1..=num_exposures {
exposures.push(Exposure {
num,
printed: false,
damaged: false,
paper: None,
})
}
Self {
id,
name,
desc,
date,
exposures,
}
}
pub fn short_str(&self) -> String {
format!(
"{}{} on {}, {} exp",
self.id,
if self.name.is_empty() {
String::new()
} else {
format!(" - {}", self.name)
},
self.date.format("%m/%d/%Y"),
self.exposures.len()
)
}
}