Event logging

This commit is contained in:
Alexey 2025-09-09 15:02:29 +03:00
commit 239484c3bc
5 changed files with 187 additions and 17 deletions

View file

@ -1,21 +1,65 @@
// Prevent console window in addition to Slint window in Windows release builds when, e.g., starting the app via file manager. Ignored on other platforms.
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
use std::error::Error;
use std::{error::Error, sync::{Arc, Mutex}};
use aliveline::{config::Config, load_config};
use chrono::Timelike;
use slint::{Model, SharedString, VecModel};
use aliveline::{config::Config, load_config, log::{Event, Log}};
use chrono::{Datelike, Timelike};
use slint::{Model, SharedString, ToSharedString, VecModel};
use toml::value::{Date as TomlDate, Time};
slint::include_modules!();
impl From<Event> for TimelineEvent {
fn from(event: Event) -> Self {
let start = (event.start.hour as i32) * 3600 + (event.start.minute as i32) * 60 + (event.start.second as i32);
let end = (event.end.hour as i32) * 3600 + (event.end.minute as i32) * 60 + (event.end.second as i32);
TimelineEvent { start, duration: end - start, label: event.name.to_shared_string(), finished: event.finished }
}
}
impl From<TimelineEvent> for Event {
fn from(event: TimelineEvent) -> Self {
let start = Time {
hour: (event.start / 3600) as u8,
minute: ((event.start % 3600) / 60) as u8,
second: (event.start % 60) as u8,
nanosecond: 0
};
let end = Time {
hour: start.hour + (event.duration / 3600) as u8,
minute: start.minute + ((event.duration % 3600) / 60) as u8,
second: start.second + (event.duration % 60) as u8,
nanosecond: 0
};
Event { start, end, name: event.label.to_string(), finished: event.finished }
}
}
fn main() -> Result<(), Box<dyn Error>> {
let ui = AppWindow::new()?;
let now = chrono::Local::now();
let offset = now.hour() * 3600 + now.minute() * 60 + now.second();
let date: TomlDate = TomlDate { day: now.day() as u8, month: now.month() as u8, year: now.year() as u16 };
let config: Arc<Config> = Arc::new(load_config());
let writing_log: Arc<Mutex<Log>> = Arc::new(Mutex::new(Log::load_from(&config, date)));
ui.invoke_update_record_offset(offset as i32);
ui.on_save_log({
let config = config.clone();
let log = writing_log.clone();
move || {
let log_guard = log.lock().expect("Log shouldn't be used twice");
if let Err(error) = (*log_guard).save(&config) {
eprintln!("Error occured while saving log: {error}");
}
}
});
ui.on_update_record_visible_time({
let ui_weak = ui.as_weak();
move |hours_string: SharedString| {
@ -30,32 +74,50 @@ fn main() -> Result<(), Box<dyn Error>> {
ui.on_start_new_event({
let ui_weak = ui.as_weak();
let log = writing_log.clone();
move |event_name: SharedString| {
let ui = ui_weak.unwrap();
let events_rc = ui.get_record_events();
let events = events_rc.as_any()
.downcast_ref::<VecModel<TimelineEvent>>()
.unwrap();
let offset = ui.get_record_offset();
events.push(TimelineEvent {
let event = TimelineEvent {
duration: 0,
finished: false,
label: event_name,
start: offset
});
};
{
let mut log_guard = log.lock().expect("Log shouldn't be used twice");
(*log_guard).events.push(Event::from(event.clone()));
}
ui.invoke_save_log();
events.push(event);
}
});
ui.on_stop_event({
let ui_weak = ui.as_weak();
let log = writing_log.clone();
move || {
let ui = ui_weak.unwrap();
let events_rc = ui.get_record_events();
let events = events_rc.as_any().downcast_ref::<VecModel<TimelineEvent>>().unwrap();
let events = events_rc.as_any()
.downcast_ref::<VecModel<TimelineEvent>>().unwrap();
let offset = ui.get_record_offset();
let event_id = events.iter().position(|data| !data.finished).unwrap();
let event = events.row_data(event_id).expect("stop-event called without unfinished events");
let event_id = events.iter()
.position(|data| !data.finished)
.unwrap();
let event = events.row_data(event_id)
.expect("stop-event called without unfinished events");
let new_event = TimelineEvent {
duration: offset - event.start,
finished: true,
@ -63,6 +125,15 @@ fn main() -> Result<(), Box<dyn Error>> {
start: event.start
};
{
let mut log_guard = log.lock().expect("Log shouldn't be used twice");
(*log_guard).events.push(Event::from(new_event.clone()));
let index = (*log_guard).events.iter().position(|data| !data.finished).unwrap();
(*log_guard).events.swap_remove(index);
}
ui.invoke_save_log();
events.set_row_data(event_id, new_event);
}
});
@ -76,8 +147,6 @@ fn main() -> Result<(), Box<dyn Error>> {
}
});
let config: Config = load_config();
println!("logs path: {}", config.log_path.to_str().unwrap());
ui.run()?;