Skip to content

Commit

Permalink
Fix starting of new activity
Browse files Browse the repository at this point in the history
  • Loading branch information
ShadoySV committed Feb 3, 2024
1 parent 8d83f4a commit 0bf2e99
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 26 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.5.1] - 2024-02-03

## Changed

- Fixed starting of new activity from the first time when tracking stops on work day start

## [0.5.0] - 2024-01-27

### Changed
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "work-break"
version = "0.5.0"
version = "0.5.1"
edition = "2021"
description = "Work-break balancer for Windows / MacOS / Linux desktops"
license = "MIT"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,6 @@ work-break reload

Look at [how to restore PowerShell notifications](https://answers.microsoft.com/en-us/windows/forum/all/powershell-toast-notifcations/a8a83b7a-0a4c-4f3d-b541-7e11403955fb)

# Similar projects
## Similar projects

[Bartib](https://github.com/nikolassv/bartib) is a simple time tracker for the command line. It saves a log of all tracked activities as a plaintext file and allows you to create flexible reports.
2 changes: 1 addition & 1 deletion extra/osx/Work-break.app/Contents/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>0.5.0</string>
<string>0.5.1</string>
<key>CFBundleSupportedPlatforms</key>
<array>
<string>MacOSX</string>
Expand Down
14 changes: 7 additions & 7 deletions src/activities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl Activities {
pub fn summary(
&self,
formula: &Formula,
now: SystemTime,
time: SystemTime,
) -> (Option<SystemTime>, Duration, Duration) {
let (end, strain, total_work) =
self.list
Expand All @@ -41,7 +41,7 @@ impl Activities {
total_work,
);
};
let work = end.unwrap_or(now).duration_since(*start).unwrap();
let work = end.unwrap_or(time).duration_since(*start).unwrap();
strain += work;
total_work += work;

Expand All @@ -53,7 +53,7 @@ impl Activities {
formula.compute_strain(
formula
.compute_break(strain, total_work)
.saturating_sub(now.duration_since(end).unwrap()),
.saturating_sub(time.duration_since(end).unwrap()),
total_work,
)
} else {
Expand All @@ -63,19 +63,19 @@ impl Activities {
)
}

pub fn switch(&mut self, now: SystemTime) {
pub fn switch(&mut self, time: SystemTime) {
if let Some(last) = self.list.last_mut() {
if last.end.is_some() {
self.list.push(Activity {
start: now,
start: time,
end: None,
})
} else {
last.end = Some(now)
last.end = Some(time)
}
} else {
self.list.push(Activity {
start: now,
start: time,
end: None,
})
}
Expand Down
35 changes: 21 additions & 14 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,27 +136,14 @@ impl App {
})
}

pub fn status(&mut self) -> Result<(Duration, Duration, String), Box<dyn std::error::Error>> {
pub fn status(&self) -> Result<(Duration, Duration, String), Box<dyn std::error::Error>> {
let formula = Formula::new(
&self.config.coefficient_a,
&self.config.coefficient_b,
&self.config.coefficient_c,
&self.config.coefficient_d,
);
let now = SystemTime::now();
let now_ch: DateTime<Local> = DateTime::from(now);
let morning = now_ch
.date_naive()
.and_hms_opt(self.config.work_days_start_at.into(), 0, 0)
.unwrap();
let mut morning_local = morning.and_local_timezone(Local).unwrap();
if now_ch < morning_local {
morning_local = morning_local
.checked_sub_days(chrono::Days::new(1))
.unwrap();
}
let truncate_point = UNIX_EPOCH + Duration::from_secs(morning_local.timestamp() as u64);
self.state.activities.truncate_until(truncate_point);

let (end, strain, work) = self.state.activities.summary(&formula, now);

Expand Down Expand Up @@ -219,13 +206,33 @@ impl App {
}

pub fn switch(&mut self) -> Result<(), Box<dyn std::error::Error>> {
self.trancate_activities();

let now = SystemTime::now();
self.state.activities.switch(now);
confy::store(APP_NAME, STATE_NAME, &self.state)?;
self.notify(true)?;
Ok(())
}

pub fn trancate_activities(&mut self) -> &mut Self {
let now = SystemTime::now();
let now_ch: DateTime<Local> = DateTime::from(now);
let morning = now_ch
.date_naive()
.and_hms_opt(self.config.work_days_start_at.into(), 0, 0)
.unwrap();
let mut morning_local = morning.and_local_timezone(Local).unwrap();
if now_ch < morning_local {
morning_local = morning_local
.checked_sub_days(chrono::Days::new(1))
.unwrap();
}
let truncate_point = UNIX_EPOCH + Duration::from_secs(morning_local.timestamp() as u64);
self.state.activities.truncate_until(truncate_point);
self
}

pub fn start(&self, switch: bool) -> Result<(), Box<dyn std::error::Error>> {
let socket = socket_name();

Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
}
}
Some(Commands::Autorun) => {
App::new()?.start(false)?;
App::new()?.trancate_activities().start(false)?;
}
Some(Commands::Reload) => {
let stream = LocalSocketStream::connect(&*socket).map_err(|_| "App is not running")?;
Expand Down

0 comments on commit 0bf2e99

Please sign in to comment.