From 3e38684c17f210bed5111ced50a68d6deea14114 Mon Sep 17 00:00:00 2001 From: FliegendeWurst <2012gdwu+github@posteo.de> Date: Thu, 5 Oct 2023 13:23:01 +0200 Subject: [PATCH] Fix schedules --- src/bin/main_loop.rs | 7 ++++++- src/bin/schedule/mod.rs | 8 ++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/bin/main_loop.rs b/src/bin/main_loop.rs index 0ed564a..591e898 100644 --- a/src/bin/main_loop.rs +++ b/src/bin/main_loop.rs @@ -41,6 +41,8 @@ fn main() { pub trait Context { fn do_action(&self, action: Action); + + fn active_count(&self) -> usize; } struct ContextDefault> { @@ -101,6 +103,10 @@ impl> Context for ContextDefault { }, } } + + fn active_count(&self) -> usize { + self.active.borrow().len() + } } #[cfg(not(feature = "pc"))] @@ -117,7 +123,6 @@ fn pc_main() { window::WindowBuilder, }; - use crate::screensaver::{Screensaver, DUOLINGO}; use raspi_oled::FrameOutput; let args: Vec<_> = env::args().map(|x| x.to_string()).collect(); diff --git a/src/bin/schedule/mod.rs b/src/bin/schedule/mod.rs index 918e772..d2413b8 100644 --- a/src/bin/schedule/mod.rs +++ b/src/bin/schedule/mod.rs @@ -6,12 +6,12 @@ use crate::{action::Action, Context}; /// Guaranteed to be checked at least once every minute. pub trait Schedule { fn check_and_do(&self, ctx: &dyn Context, time: OffsetDateTime) { - if self.check(time) { + if self.check(ctx, time) { self.execute(ctx, time); } } - fn check(&self, time: OffsetDateTime) -> bool; + fn check(&self, ctx: &dyn Context, time: OffsetDateTime) -> bool; fn execute(&self, ctx: &dyn Context, time: OffsetDateTime); } @@ -23,8 +23,8 @@ pub struct Reminder { } impl Schedule for Reminder { - fn check(&self, time: OffsetDateTime) -> bool { - time.hour() == self.hour && time.minute() == self.minute + fn check(&self, ctx: &dyn Context, time: OffsetDateTime) -> bool { + time.hour() == self.hour && time.minute() == self.minute && ctx.active_count() == 1 } fn execute(&self, ctx: &dyn Context, _time: OffsetDateTime) {