Compare commits

...

3 commits

Author SHA1 Message Date
abc9d59810 Prettified code and added review scrolling 2025-09-10 14:50:05 +03:00
99af3eb2b8 Log loading 2025-09-10 10:00:18 +03:00
239484c3bc Event logging 2025-09-09 15:02:29 +03:00
8 changed files with 278 additions and 27 deletions

View file

@ -3,18 +3,26 @@ use serde::Deserialize;
#[derive(Deserialize)] #[derive(Deserialize)]
pub struct Config { pub struct Config {
/// directory, where config is located
#[serde(skip)]
pub conf_path: PathBuf,
pub log_path: PathBuf pub log_path: PathBuf
} }
impl Config { impl Config {
pub fn new() -> Self { pub fn new(conf_path: PathBuf) -> Self {
Config { log_path: PathBuf::from("./logs") } let conf_dir: PathBuf = conf_path.parent().unwrap().into();
Config { conf_path: conf_dir, log_path: PathBuf::from("./logs") }
} }
pub fn load(path: PathBuf) -> Self { pub fn load(path: PathBuf) -> Self {
if let Ok(toml_string) = std::fs::read_to_string(path) { if let Ok(toml_string) = std::fs::read_to_string(path.clone()) {
return toml::from_str(&toml_string).unwrap_or(Config::new()); let conf = toml::from_str::<Self>(&toml_string);
if let Ok(mut conf) = conf {
conf.conf_path = path.parent().unwrap().into();
return conf;
}
} }
Config::new() Config::new(path)
} }
} }

View file

@ -2,6 +2,7 @@ use config::Config;
use std::path::PathBuf; use std::path::PathBuf;
pub mod config; pub mod config;
pub mod log;
pub fn load_config() -> Config { pub fn load_config() -> Config {
if let Ok(path_str) = std::env::var("XDG_CONFIG_HOME") { if let Ok(path_str) = std::env::var("XDG_CONFIG_HOME") {
@ -10,5 +11,5 @@ pub fn load_config() -> Config {
path.push("config.toml"); path.push("config.toml");
return Config::load(path); return Config::load(path);
} }
Config::new() Config::new(PathBuf::from("./config.toml"))
} }

91
src/log.rs Normal file
View file

@ -0,0 +1,91 @@
use std::{
io::{Error, ErrorKind},
path::PathBuf
};
use serde::{Deserialize, Serialize};
use toml::value::{Date, Time};
use crate::config::Config;
#[derive(Serialize, Deserialize, Debug)]
pub struct Log {
pub date: Date,
pub events: Vec<Event>
}
impl Log {
pub fn new(date: Date) -> Self {
Log { date, events: Vec::new() }
}
pub fn load_from(config: &Config, date: Date) -> Self {
let path = Log::get_filepath(&date, config);
if let Ok(log_string) = std::fs::read_to_string(path) {
return toml::from_str(&log_string).unwrap_or(Log::new(date));
} else {
Log::new(date)
}
}
pub fn save(&self, config: &Config) -> std::io::Result<()> {
Log::try_create_log_dir(config)?;
let path = Log::get_filepath(&self.date, config);
match toml::to_string_pretty(self) {
Ok(toml_string) => std::fs::write(&path, toml_string),
Err(error) => {
return Err(Error::new(ErrorKind::Other, format!("{error}")));
}
}
}
fn get_filepath(date: &Date, config: &Config) -> PathBuf {
let mut path = Log::get_log_dir(&config);
let filename = format!("{}-{}-{}", date.day, date.month, date.year);
path.push(filename);
path.set_extension("toml");
path
}
fn get_log_dir(config: &Config) -> PathBuf {
if config.log_path.is_relative() {
let mut path = config.conf_path.clone();
path.push(&config.log_path);
return path;
} else {
return config.log_path.clone();
}
}
fn try_create_log_dir(config: &Config) -> std::io::Result<()> {
let path = Log::get_log_dir(config);
if !std::fs::exists(&path)? {
std::fs::create_dir_all(path)?
}
Ok(())
}
}
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct Event {
pub name: String,
pub start: Time,
pub end: Time,
pub finished: bool
}
impl Event {
pub fn new(name: String, start: i32, end: i32, finished: bool) -> Self {
let start = Time {
hour: (start / 3600) as u8,
minute: ((start / 3600) / 60) as u8,
second: (start % 60) as u8,
nanosecond: 0
};
let end = Time {
hour: (end / 3600) as u8,
minute: ((end % 3600) / 60) as u8,
second: (end % 60) as u8,
nanosecond: 0
};
Event { name, start, end, finished }
}
}

View file

@ -1,61 +1,149 @@
// 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. // 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")] #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
use std::error::Error; use std::{error::Error, rc::Rc, sync::{Arc, Mutex}};
use aliveline::{config::Config, load_config}; use aliveline::{config::Config, load_config, log::{Event, Log}};
use chrono::Timelike; use chrono::{Datelike, Timelike};
use slint::{Model, SharedString, VecModel}; use slint::{Model, ModelRc, SharedString, ToSharedString, VecModel};
use toml::value::{Date as TomlDate, Time};
slint::include_modules!(); 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 endsecs = event.start + event.duration;
let end = Time {
hour: (endsecs / 3600) as u8,
minute: ((endsecs % 3600) / 60) as u8,
second: (endsecs % 60) as u8,
nanosecond: 0
};
Event { start, end, name: event.label.to_string(), finished: event.finished }
}
}
fn main() -> Result<(), Box<dyn Error>> { fn main() -> Result<(), Box<dyn Error>> {
let ui = AppWindow::new()?; let ui = AppWindow::new()?;
let now = chrono::Local::now(); let now = chrono::Local::now();
let offset = now.hour() * 3600 + now.minute() * 60 + now.second(); 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)));
{
let ui_weak = ui.as_weak();
let log = writing_log.clone();
(move || {
let ui = ui_weak.unwrap();
let log_guard = log.lock().expect("Log shouldn't be used twice");
let events: Vec<TimelineEvent> = (*log_guard)
.events
.iter()
.map(|event| TimelineEvent::from((*event).clone()))
.collect();
let in_progress = events.iter().any(|event| !event.finished);
let model: ModelRc<TimelineEvent> = Rc::new(VecModel::from(events)).into();
ui.set_record_events(model);
ui.set_in_progress(in_progress);
})()
}
ui.invoke_update_record_offset(offset as i32); ui.invoke_update_record_offset(offset as i32);
ui.on_update_record_visible_time({ 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_visible_time({
let ui_weak = ui.as_weak(); let ui_weak = ui.as_weak();
move |hours_string: SharedString| { move |is_record: bool, hours_string: SharedString| {
let ui = ui_weak.unwrap(); let ui = ui_weak.unwrap();
let hours = hours_string.split(' ') let hours = hours_string.split(' ')
.next() .next()
.map(|h| h.parse::<i32>().unwrap()) .map(|h| h.parse::<i32>().unwrap())
.unwrap(); .unwrap();
ui.set_record_visible_time(hours * 3600); if is_record {
ui.set_record_visible_time(hours * 3600);
} else {
ui.set_review_visible_time(hours * 3600);
}
} }
}); });
ui.on_start_new_event({ ui.on_start_new_event({
let ui_weak = ui.as_weak(); let ui_weak = ui.as_weak();
let log = writing_log.clone();
move |event_name: SharedString| { move |event_name: SharedString| {
let ui = ui_weak.unwrap(); let ui = ui_weak.unwrap();
let events_rc = ui.get_record_events(); let events_rc = ui.get_record_events();
let events = events_rc.as_any() let events = events_rc.as_any()
.downcast_ref::<VecModel<TimelineEvent>>() .downcast_ref::<VecModel<TimelineEvent>>()
.unwrap(); .unwrap();
let offset = ui.get_record_offset(); let offset = ui.get_record_offset();
events.push(TimelineEvent {
let event = TimelineEvent {
duration: 0, duration: 0,
finished: false, finished: false,
label: event_name, label: event_name,
start: offset 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({ ui.on_stop_event({
let ui_weak = ui.as_weak(); let ui_weak = ui.as_weak();
let log = writing_log.clone();
move || { move || {
let ui = ui_weak.unwrap(); let ui = ui_weak.unwrap();
let events_rc = ui.get_record_events(); 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 offset = ui.get_record_offset();
let event_id = events.iter().position(|data| !data.finished).unwrap(); let event_id = events.iter()
let event = events.row_data(event_id).expect("stop-event called without unfinished events"); .position(|data| !data.finished)
.unwrap();
let event = events.row_data(event_id)
.expect("stop-event called without unfinished events");
let new_event = TimelineEvent { let new_event = TimelineEvent {
duration: offset - event.start, duration: offset - event.start,
finished: true, finished: true,
@ -63,6 +151,15 @@ fn main() -> Result<(), Box<dyn Error>> {
start: event.start 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); events.set_row_data(event_id, new_event);
} }
}); });
@ -76,8 +173,6 @@ fn main() -> Result<(), Box<dyn Error>> {
} }
}); });
let config: Config = load_config();
println!("logs path: {}", config.log_path.to_str().unwrap());
ui.run()?; ui.run()?;

View file

@ -4,11 +4,15 @@ import { ReviewWidget } from "review.slint";
import { TimelineEvent } from "timeline.slint"; import { TimelineEvent } from "timeline.slint";
export component AppWindow inherits Window { export component AppWindow inherits Window {
callback update-record-visible-time <=> record.update-visible-time;
callback start-new-event <=> record.start-new-event; callback start-new-event <=> record.start-new-event;
callback stop-event <=> record.stop-event; callback stop-event <=> record.stop-event;
callback chain-event <=> record.chain-event; callback chain-event <=> record.chain-event;
callback update-record-offset(int); callback update-record-offset(int);
callback save-log;
callback fetch-log <=> review.fetch-log;
callback update-visible-time(bool, string);
update-record-offset(new-offset) => { update-record-offset(new-offset) => {
record.offset = new-offset; record.offset = new-offset;
@ -16,7 +20,13 @@ export component AppWindow inherits Window {
in-out property record-events <=> record.events; in-out property record-events <=> record.events;
in-out property record-offset <=> record.offset; in-out property record-offset <=> record.offset;
in-out property<int> record-visible-time <=> record.visible-time; in-out property record-visible-time <=> record.visible-time;
in-out property in-progress <=> record.in-progress;
in-out property review-events <=> review.events;
in-out property review-offset <=> review.offset;
in-out property review-visible-time <=> review.visible-time;
property<[string]> combo-spans: ["1 Hour", "4 Hours", "8 Hours", "24 Hours"]; property<[string]> combo-spans: ["1 Hour", "4 Hours", "8 Hours", "24 Hours"];
title: "Aliveline"; title: "Aliveline";
@ -26,11 +36,19 @@ export component AppWindow inherits Window {
title: "Record"; title: "Record";
record := RecordWidget { record := RecordWidget {
combo-spans: combo-spans; combo-spans: combo-spans;
update-visible-time(time) => {
root.update-visible-time(true, time);
}
} }
} }
Tab { Tab {
title: "Review"; title: "Review";
review := ReviewWidget {} review := ReviewWidget {
combo-spans: combo-spans;
update-visible-time(time) => {
root.update-visible-time(false, time)
}
}
} }
} }
} }

View file

@ -11,7 +11,7 @@ export component RecordWidget inherits VerticalBox {
in-out property offset <=> tl.offset; in-out property offset <=> tl.offset;
in-out property events <=> tl.events; in-out property events <=> tl.events;
in property<[string]> combo-spans: []; in property<[string]> combo-spans: [];
property<bool> in-progress: false; in-out property<bool> in-progress: false;
property<string> event-name <=> le.text; property<string> event-name <=> le.text;
tl := Timeline { tl := Timeline {

View file

@ -1,13 +1,31 @@
import { VerticalBox, LineEdit, Button, DatePickerPopup } from "std-widgets.slint"; import { VerticalBox, LineEdit, Button, DatePickerPopup, ComboBox } from "std-widgets.slint";
import { Timeline } from "timeline.slint"; import { Timeline } from "timeline.slint";
export component ReviewWidget inherits VerticalBox { export component ReviewWidget inherits VerticalBox {
callback update-visible-time(string);
callback fetch-log(int, int, int);
property<int> max-offset: 24 * 3600;
property<int> current-year; property<int> current-year;
property<int> current-month; property<int> current-month;
property<int> current-day; property<int> current-day;
in property<[string]> combo-spans: [];
Timeline { in-out property visible-time <=> tl.visible-time;
in-out property offset <=> tl.offset;
in-out property events <=> tl.events;
tl := Timeline {
updating: false; updating: false;
TouchArea {
preferred-width: 100%;
preferred-height: 100%;
moved => {
if self.pressed {
root.offset -= (self.mouse-x - self.pressed-x) / 1px;
root.offset = clamp(root.offset, visible-time, max-offset);
}
}
}
} }
GridLayout { GridLayout {
spacing-vertical: 8px; spacing-vertical: 8px;
@ -16,12 +34,30 @@ export component ReviewWidget inherits VerticalBox {
text: "Day: \{current-day}/\{current-month}/\{current-year}"; text: "Day: \{current-day}/\{current-month}/\{current-year}";
font-size: 32px; font-size: 32px;
horizontal-alignment: right; horizontal-alignment: right;
row: 0;
} }
Button { Button {
text: "Select"; text: "Select";
clicked => { clicked => {
date-picker.show() date-picker.show()
} }
row: 0;
col: 1;
}
Text {
text: "Span: ";
font-size: 24px;
row: 1;
horizontal-alignment: right;
}
ComboBox {
model: combo-spans;
current-index: 0;
row: 1;
col: 1;
selected(current-value) => {
root.update-visible-time(current-value);
}
} }
} }
date-picker := DatePickerPopup { date-picker := DatePickerPopup {
@ -32,6 +68,7 @@ export component ReviewWidget inherits VerticalBox {
current-year = date.year; current-year = date.year;
current-month = date.month; current-month = date.month;
current-day = date.day; current-day = date.day;
root.fetch-log(current-year, current-month, current-day);
} }
} }
} }

View file

@ -85,4 +85,5 @@ export component Timeline inherits Rectangle {
visible: timeline-event.visible; visible: timeline-event.visible;
} }
} }
@children
} }