Skip to content

Commit 5a232a4

Browse files
committed
Testing next alarm
1 parent bbad1fc commit 5a232a4

File tree

8 files changed

+193
-17
lines changed

8 files changed

+193
-17
lines changed

src-tauri/Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src-tauri/Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,13 @@ thiserror = "1.0.15"
4141
anyhow = "1.0.28"
4242
derivative = "2.1.1"
4343
log4rs = "1.0"
44-
dirs = "3.0"
44+
dirs = { version = "^3.0", optional = true }
4545
once_cell = "1.8"
46+
maplit = "1.0"
4647

4748
[features]
49+
debug_logs = ["dirs"]
50+
4851
# this feature is used for production builds or when `devPath` points to the filesystem and the built-in dev server is disabled.
4952
# If you use cargo directly instead of tauri's cli you can use this feature flag to switch between tauri's `dev` and `build` modes.
5053
# DO NOT REMOVE!!

src-tauri/src/player.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
mod player {
22
use crate::utils::*;
33
//use log::debug;
4-
use std::thread;
5-
use rodio::{Decoder, OutputStream, source::{Source}};
4+
use rodio::{source::Source, Decoder, OutputStream};
65
use std::io::Cursor;
6+
use std::thread;
77

88
const BEEP_INTERVAL: u64 = 1000; //milliseconds
99
const PLAY_DURATION: u64 = 1000;

src-tauri/src/ui_handler/alarm_manager.rs

Lines changed: 139 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! Implements core timer functionality. Starts a thread which
1+
//! Implements core timer functionality. Starts a thread which
22
//! wakes up every minute and checks if an alarm is to be played.
33
//! CPU usage (on Mac) is not significant
44
//! The tread runs throughout the life of the app. No need to handle
@@ -62,6 +62,12 @@ mod alarm_manager {
6262
}
6363
}
6464
}
65+
66+
if let Some(alarm) =
67+
find_next_alarm(&self.alarms, current_weekday, current_hour, current_minute)
68+
{
69+
debug!("Next alarm at: {:?}", alarm);
70+
}
6571
}
6672

6773
/// Handles message from ui_handlers
@@ -87,6 +93,78 @@ mod alarm_manager {
8793
}
8894
}
8995

96+
fn find_next_alarm(
97+
alarm_schedule: &HashMap<Weekday, HashMap<usize, Vec<usize>>>,
98+
current_day: Weekday,
99+
current_hour: usize,
100+
current_minute: usize,
101+
) -> Option<(Weekday, usize, usize)> {
102+
let mut current_day_to_check;
103+
let mut next_alarm: Option<(Weekday, usize, usize)> = None;
104+
105+
// Start with current_day, check if alarms are scheduled after current time.
106+
// Repeat for successive days until we wrap around to today
107+
108+
if let Some(hour_map) = alarm_schedule.get(&current_day) {
109+
//hashmaps are not sorted. So first get sorted hours
110+
let mut sorted_keys: Vec<usize> = hour_map.keys().cloned().collect();
111+
sorted_keys.sort();
112+
113+
'hour_loop:for hour in &sorted_keys {
114+
if let Some(mins) = hour_map.get(hour) {
115+
for min in mins.iter() {
116+
if *hour > current_hour {
117+
//mins are sorted. just pick up the first
118+
next_alarm = Some((current_day, *hour, *min));
119+
break 'hour_loop;
120+
} else if *hour == current_hour {
121+
if *min > current_minute {
122+
next_alarm = Some((current_day, *hour, *min));
123+
break 'hour_loop;
124+
}
125+
}
126+
}
127+
}
128+
}
129+
}
130+
131+
if next_alarm.is_some() {
132+
//we found our next alarm on the same day
133+
return next_alarm;
134+
}
135+
136+
//try searching other days
137+
current_day_to_check = current_day.succ();
138+
loop {
139+
if let Some(hour_map) = alarm_schedule.get(&current_day_to_check) {
140+
let mut sorted_keys: Vec<usize> = hour_map.keys().cloned().collect();
141+
sorted_keys.sort();
142+
143+
for hour in &sorted_keys {
144+
if let Some(mins) = hour_map.get(hour) {
145+
for min in mins.iter() {
146+
//mins are sorted. just pick up the first
147+
return Some((current_day_to_check, *hour, *min));
148+
}
149+
}
150+
}
151+
}
152+
153+
current_day_to_check = current_day_to_check.succ();
154+
155+
if current_day_to_check == current_day {
156+
debug!("breaking");
157+
break;
158+
}
159+
}
160+
161+
return next_alarm;
162+
}
163+
164+
//fn find_next_for_today(hour_map: &HashMap<usize, Vec<usize>>) -> (Weekday, usize, usize) {
165+
//return
166+
//}
167+
90168
fn get_hours(s: usize, e: usize) -> Vec<usize> {
91169
let mut hrs = Vec::new();
92170

@@ -99,7 +177,7 @@ mod alarm_manager {
99177
hrs
100178
}
101179

102-
/// For a set of rules, finds the hours and minutes for each day at which
180+
/// For a set of rules, finds the hours and minutes for each day at which
103181
/// alarm should be played. Assumes that rules are not overlapping
104182
fn get_alarms(rules: &[Rule]) -> HashMap<Weekday, HashMap<usize, Vec<usize>>> {
105183
let mut alarms: HashMap<Weekday, HashMap<usize, Vec<usize>>> = HashMap::new();
@@ -172,6 +250,7 @@ mod alarm_manager {
172250
mod tests {
173251
use super::*;
174252
use crate::utils::*;
253+
use maplit::hashmap;
175254

176255
#[test]
177256
fn test_get_alarms() {
@@ -198,6 +277,64 @@ mod alarm_manager {
198277
assert!(alarms.contains_key(&Weekday::Tue));
199278
assert!(alarms.contains_key(&Weekday::Wed));
200279
}
280+
281+
#[test]
282+
fn test_next_alarm() {
283+
setup_logger();
284+
let rule1 = Rule {
285+
serial: 1,
286+
days: vec!["Fri".to_string()],
287+
interval: 30,
288+
from: 17,
289+
to: 18,
290+
};
291+
292+
let rule2 = Rule {
293+
serial: 2,
294+
days: vec!["Sun".to_string()],
295+
interval: 30,
296+
from: 19,
297+
to: 20,
298+
};
299+
300+
let rules = vec![rule1, rule2];
301+
let alarms = get_alarms(&rules);
302+
303+
let expected = hashmap! {
304+
Weekday::Fri => hashmap! {
305+
17 => vec![30],
306+
18 => vec![0],
307+
},
308+
// Add more weekdays and alarms as needed
309+
Weekday::Sun => hashmap! {
310+
19 => vec![30],
311+
20 => vec![0],
312+
},
313+
};
314+
315+
assert_eq!(alarms, expected);
316+
317+
let mut next = find_next_alarm(&alarms, Weekday::Fri, 17, 58);
318+
assert_eq!(next, Some((Weekday::Fri, 18, 0)));
319+
320+
next = find_next_alarm(&alarms, Weekday::Sat, 18, 0);
321+
assert_eq!(next, Some((Weekday::Sun, 19, 30)));
322+
323+
next = find_next_alarm(&alarms, Weekday::Sun, 18, 0);
324+
assert_eq!(next, Some((Weekday::Sun, 19, 30)));
325+
326+
next = find_next_alarm(&alarms, Weekday::Sun, 19, 0);
327+
assert_eq!(next, Some((Weekday::Sun, 19, 30)));
328+
329+
next = find_next_alarm(&alarms, Weekday::Sun, 19, 20);
330+
assert_eq!(next, Some((Weekday::Sun, 19, 30)));
331+
332+
next = find_next_alarm(&alarms, Weekday::Sun, 19, 31);
333+
assert_eq!(next, Some((Weekday::Sun, 20, 0)));
334+
335+
next = find_next_alarm(&alarms, Weekday::Sun, 20, 31);
336+
assert_eq!(next, Some((Weekday::Fri, 17, 30)));
337+
}
201338
}
202339
}
203340

src-tauri/src/utils.rs

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ mod utils {
55
use std::io::Cursor;
66
use std::process::Command;
77

8+
#[cfg(any(feature = "debug-logs", feature = "release-logs"))]
89
use dirs;
910
use log::LevelFilter;
1011
use log4rs::append::rolling_file::policy::compound::roll::fixed_window::FixedWindowRoller;
@@ -83,6 +84,7 @@ mod utils {
8384
pub fn setup_logger() {
8485
//setup rotation
8586
let file_name = get_log_file_name();
87+
println!("file_name: {}", file_name);
8688
let window_size = 1; // log0, log1, log2
8789
let fixed_window_roller = FixedWindowRoller::builder()
8890
.build(&(file_name.clone() + "{}"), window_size)
@@ -129,16 +131,43 @@ mod utils {
129131
return "".to_string();
130132
}
131133

134+
//fn get_app_dir() -> Option<PathBuf> {
135+
//if let Some(home_dir) = dirs::home_dir() {
136+
//let mut path = PathBuf::new();
137+
//path.push(home_dir);
138+
//path.push("Library");
139+
//path.push("com.68kilo.tab");
140+
//return Some(path);
141+
//}
142+
//
143+
//return None;
144+
//}
145+
132146
fn get_app_dir() -> Option<PathBuf> {
133-
if let Some(home_dir) = dirs::home_dir() {
134-
let mut path = PathBuf::new();
135-
path.push(home_dir);
136-
path.push("Library");
137-
path.push("com.68kilo.tab");
138-
return Some(path);
147+
#[cfg(feature = "debug_logs")]
148+
{
149+
if let Some(home_dir) = dirs::home_dir() {
150+
let mut path = PathBuf::new();
151+
path.push(home_dir);
152+
path.push("Library");
153+
path.push("com.68kilo.tab_debug"); // Debug-specific path
154+
return Some(path);
155+
}
156+
}
157+
158+
#[cfg(feature = "release_logs")]
159+
{
160+
if let Some(home_dir) = dirs::home_dir() {
161+
let mut path = PathBuf::new();
162+
path.push(home_dir);
163+
path.push("Library");
164+
path.push("com.68kilo.tab"); // Release-specific path
165+
return Some(path);
166+
}
139167
}
140168

141-
return None;
169+
// Default path if no features are enabled
170+
None
142171
}
143172
}
144173

src-tauri/tauri.conf.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
{
5858
"fullscreen": false,
5959
"height": 600,
60-
"resizable": false,
60+
"resizable": true,
6161
"title": "",
6262
"width": 600
6363
}

0 commit comments

Comments
 (0)