69 lines
1.9 KiB
Rust
69 lines
1.9 KiB
Rust
use custom_consumption::app_config::AppConfig;
|
|
use custom_consumption::utils::time_millis;
|
|
|
|
fn main() {
|
|
env_logger::init();
|
|
|
|
let options = eframe::NativeOptions {
|
|
viewport: egui::ViewportBuilder::default().with_inner_size([220.0, 60.0]),
|
|
..Default::default()
|
|
};
|
|
eframe::run_native(
|
|
"Custom consumption",
|
|
options,
|
|
Box::new(|_cc| Box::<MyApp>::default()),
|
|
)
|
|
.unwrap()
|
|
}
|
|
|
|
struct MyApp {
|
|
consumption: i64,
|
|
last_saved_consumption: i64,
|
|
last_save_time: u128,
|
|
}
|
|
|
|
impl Default for MyApp {
|
|
fn default() -> Self {
|
|
let mut prev_consumption = 42;
|
|
|
|
if AppConfig::get().file_path().is_file() {
|
|
prev_consumption = std::fs::read_to_string(AppConfig::get().file_path())
|
|
.unwrap_or("43".to_string())
|
|
.parse()
|
|
.unwrap_or(44)
|
|
}
|
|
|
|
Self {
|
|
consumption: prev_consumption,
|
|
last_saved_consumption: prev_consumption,
|
|
last_save_time: time_millis(),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl eframe::App for MyApp {
|
|
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
|
|
if self.consumption != self.last_saved_consumption
|
|
&& self.last_save_time + AppConfig::get().min_save_interval < time_millis()
|
|
{
|
|
log::info!("Persist {}", self.consumption);
|
|
self.last_saved_consumption = self.consumption;
|
|
self.last_save_time = time_millis();
|
|
|
|
std::fs::write(AppConfig::get().file_path(), self.consumption.to_string())
|
|
.expect("Failed to save value!");
|
|
}
|
|
|
|
egui::CentralPanel::default().show(ctx, |ui| {
|
|
ui.heading("Custom consumption");
|
|
ui.add(
|
|
egui::Slider::new(
|
|
&mut self.consumption,
|
|
AppConfig::get().min..=AppConfig::get().max,
|
|
)
|
|
.text("W"),
|
|
);
|
|
});
|
|
}
|
|
}
|