-#![allow(unused)]
-use std::{array, ops::Add, time::Duration};
-
-use anyhow::Result;
-use chrono::{Datelike, Local, NaiveDateTime, NaiveTime, TimeDelta, Weekday};
-use itertools::Itertools;
-use tokio::time::{Instant, sleep, sleep_until};
-use trayicon::{MenuBuilder, TrayIconBuilder};
-
-mod gui;
-
-#[derive(Debug, Clone)]
-pub struct Alarm {
- pub time: NaiveTime,
- ///if u use the 8th bit, i will be VERY ANGRY with u
- pub days: u8,
- pub label: String,
-}
-
-impl Alarm {
- pub fn new(time: NaiveTime, days: u8, label: String) -> Self {
- Self { time, days, label }
- }
-}
-
-#[derive(Clone, Eq, PartialEq, Debug)]
-enum TrayEvent {
- RightClickTrayIcon,
- LeftClickTrayIcon,
- DoubleClickTrayIcon,
- StatusActive,
- StatusNeedsAttention,
- StatusPassive,
- Exit,
-}
-
-pub const WEEKDAYS: u8 = 0b11111000;
-pub const WEEKENDS: u8 = 0b00000110;
-pub const ALL: u8 = 0b11111110;
-
-pub const MONDAY: u8 = 0b10000000;
-pub const TUESDAY: u8 = 0b01000000;
-pub const WEDNESDAY: u8 = 0b00100000;
-pub const THURSDAY: u8 = 0b00010000;
-pub const FRIDAY: u8 = 0b00001000;
-pub const SATURDAY: u8 = 0b00000100;
-pub const SUNDAY: u8 = 0b00000010;
-
-#[tokio::main]
-async fn main() -> Result<()> {
- let (tray_event_tx, tray_event_rx) = std::sync::mpsc::channel::<TrayEvent>();
-
- let icon = include_bytes!("../favicon.ico");
- let tray_icon = TrayIconBuilder::new()
- .sender(move |e: &TrayEvent| {
- let _ = tray_event_tx.send(e.clone());
- })
- .icon_from_buffer(icon)
- .title("Alarm that don't quit")
- .tooltip("tuff icon")
- .on_click(TrayEvent::LeftClickTrayIcon)
- .on_right_click(TrayEvent::RightClickTrayIcon)
- .on_double_click(TrayEvent::DoubleClickTrayIcon)
- .menu(MenuBuilder::new().item("Exit", TrayEvent::Exit))
- .build()
- .unwrap();
-
- let alarms: Vec<Alarm> = vec![
- Alarm::new(
- NaiveTime::from_hms_opt(17, 30, 0).unwrap(),
- WEEKDAYS,
- "test1".to_string(),
- ),
- Alarm::new(
- NaiveTime::from_hms_opt(12, 0, 0).unwrap(),
- WEEKDAYS,
- "test2".to_string(),
- ),
- Alarm::new(
- NaiveTime::from_hms_opt(19, 0, 0).unwrap(),
- WEEKDAYS,
- "test2".to_string(),
- ),
- Alarm::new(
- NaiveTime::from_hms_opt(17, 24, 0).unwrap(),
- ALL,
- "hi".to_string(),
- ),
- ];
-
- std::thread::spawn(move || {
- while let Ok(event) = tray_event_rx.recv() {
- match event {
- TrayEvent::Exit => std::process::exit(0),
- _ => (),
- }
- }
- });
-
- loop {
- let next = get_next_alarm(&alarms);
- wait_til_time(next.time).await;
- snooze_loop(&next).await?;
- }
-
- Ok(())
-}
-
-pub const SNOOZE_MINS: u8 = 1;
-
-fn alarm_gui(alarm: &Alarm) -> Result<bool> {
- Ok(gui::gui(alarm)?)
-}
-
-async fn snooze_loop(alarm: &Alarm) -> Result<()> {
- loop {
- let snoozed = alarm_gui(alarm)?;
- if snoozed {
- sleep(Duration::from_mins(SNOOZE_MINS as u64)).await;
- } else {
- return Ok(());
- }
- }
-}
-
-fn weekday() -> u8 {
- let day = Local::now().weekday();
- match day {
- chrono::Weekday::Mon => MONDAY,
- chrono::Weekday::Tue => TUESDAY,
- chrono::Weekday::Wed => WEDNESDAY,
- chrono::Weekday::Thu => THURSDAY,
- chrono::Weekday::Fri => FRIDAY,
- chrono::Weekday::Sat => SATURDAY,
- chrono::Weekday::Sun => SUNDAY,
- }
-}
-
-fn day_bitmask(day: Weekday) -> u8 {
- match day {
- Weekday::Mon => MONDAY,
- Weekday::Tue => TUESDAY,
- Weekday::Wed => WEDNESDAY,
- Weekday::Thu => THURSDAY,
- Weekday::Fri => FRIDAY,
- Weekday::Sat => SATURDAY,
- Weekday::Sun => SUNDAY,
- }
-}
-
-fn matches_day(days: u8, day: Weekday) -> bool {
- days & day_bitmask(day) != 0
-}
-
-fn days_until_alarm(alarm: &Alarm) -> i64 {
- let now = Local::now();
- let today = now.weekday().num_days_from_monday() as i64;
- let current_time = now.time();
-
- for offset in 0..7 {
- let day = (today + offset) % 7;
-
- let mask = match day {
- 0 => MONDAY,
- 1 => TUESDAY,
- 2 => WEDNESDAY,
- 3 => THURSDAY,
- 4 => FRIDAY,
- 5 => SATURDAY,
- 6 => SUNDAY,
- _ => unreachable!(),
- };
-
- if alarm.days & mask == 0 {
- continue;
- }
-
- // If it's today, the alarm must still be in the future.
- if offset == 0 && alarm.time <= current_time {
- continue;
- }
-
- return offset;
- }
-
- unreachable!("alarm has no enabled days");
-}
-
-fn get_next_alarm(alarms: &[Alarm]) -> &Alarm {
- alarms
- .iter()
- .min_by_key(|alarm| {
- let days = days_until_alarm(alarm);
-
- let delta = if days == 0 {
- (alarm.time - Local::now().time()).to_std().unwrap()
- } else {
- Duration::from_secs(days as u64 * 24 * 60 * 60)
- + alarm
- .time
- .signed_duration_since(NaiveTime::MIN)
- .to_std()
- .unwrap()
- };
-
- delta
- })
- .unwrap()
-}
-
-fn get_delta_for_alarm(alarm: &Alarm) -> Option<TimeDelta> {
- let now = Local::now().naive_local();
- let mut day = now.date().weekday();
-
- for offset in 0..8i64 {
- if matches_day(alarm.days, day) {
- let candidate: NaiveDateTime =
- (now.date() + TimeDelta::days(offset)).and_time(alarm.time);
- if candidate > now {
- return Some(candidate - now);
- }
- }
- day = day.succ();
- }
-
- // `days == 0` (no days set) falls through here.
- None
-}
-
-#[inline]
-fn time_until(delta: TimeDelta) -> Duration {
- delta
- .to_std()
- .unwrap_or_else(|_| Duration::from_hours(24) - delta.abs().to_std().unwrap())
-}
-
-#[inline]
-fn get_delta(time: NaiveTime) -> TimeDelta {
- time - Local::now().naive_local().time()
-}
-
-async fn wait_til_time(time: NaiveTime) {
- let delta = get_delta(time);
-
- let until = delta.to_std().map_or_else(
- |_| Instant::now() - delta.abs().to_std().unwrap() + Duration::from_hours(24),
- |k| Instant::now() + k,
- );
-
- sleep_until(until).await;
-}