Skip to content

Commit

Permalink
Added the possibility to set work hours during the night (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
lpeyr committed Jul 31, 2023
1 parent cbbfcd8 commit 97e5f4b
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 18 deletions.
55 changes: 41 additions & 14 deletions DayBar/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public MainWindow()
InitializeComponent();

Global.MainWindow = this;
InitTimer(Global.Settings.StartHour * 3600, Global.Settings.EndHour * 3600);
InitTimer(new(Global.Settings.StartHour, 0, 0), new(Global.Settings.EndHour, 0, 0));
InitUI();
Hide();
}
Expand All @@ -68,33 +68,60 @@ private async void InitUI()

DispatcherTimer dispatcherTimer = new();
bool halfShown = false;
internal void InitTimer(int startHour, int endHour)
internal void InitTimer(TimeSpan startWorkHour, TimeSpan endWorkHour)
{
dispatcherTimer.Stop();
int c = CalculatePercentage(startHour, endHour);

int c = CalculatePercentage(DateTime.Now, startWorkHour, endWorkHour);

dispatcherTimer.Interval = TimeSpan.FromMinutes(1);
dispatcherTimer.Tick += (o, e) =>
{
c = CalculatePercentage(startHour, endHour);
c = CalculatePercentage(DateTime.Now, startWorkHour, endWorkHour);
SetNotifyIcon(ref c);
};

SetNotifyIcon(ref c);
dispatcherTimer.Start();
}

private int CalculatePercentage(int startHour, int endHour)
private int CalculatePercentage(DateTime currentTime, TimeSpan startWorkHour, TimeSpan endWorkHour)
{
// Get the current time
DateTime now = DateTime.Now;
// Get the start of the day
DateTime startOfDay = new(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, startHour / 3600, 0, 0);
// Get the difference as a TimeSpan
TimeSpan elapsed = now - startOfDay;
// Get the total number of seconds
int seconds = (int)elapsed.TotalSeconds;
return seconds * 100 / (endHour - startHour);
// Get the current date and time
DateTime currentDate = DateTime.Now;

// Create DateTime objects for the start and end work hours
DateTime startDateTime = currentDate.Date.Add(startWorkHour);
DateTime endDateTime = currentDate.Date.Add(endWorkHour);

// If end work hour is before the start work hour, adjust it to the next day
if (endDateTime < startDateTime)
{
endDateTime = endDateTime.AddDays(1);
}

// Check if the current time is within the range of the previous day
if (currentTime < startDateTime)
{
startDateTime = startDateTime.AddDays(-1);
endDateTime = endDateTime.AddDays(-1);
}

// Calculate the total number of minutes between start and end work hours
TimeSpan totalWorkHours = endDateTime - startDateTime;
int totalWorkMinutes = (int)totalWorkHours.TotalMinutes;

// Calculate the number of minutes passed since the start of the workday
TimeSpan timePassed = currentTime - startDateTime;
int minutesPassed = (int)timePassed.TotalMinutes;

// Calculate the percentage of time passed (rounded down to the nearest integer)
int percentagePassed = (int)((double)minutesPassed / totalWorkMinutes * 100);

// Make sure the percentage is within the range of 0 to 100
percentagePassed = Math.Max(0, Math.Min(100, percentagePassed));

return percentagePassed;
}

private void SetNotifyIcon(ref int progress)
Expand Down
2 changes: 1 addition & 1 deletion DayBar/Pages/FirstRun/CustomizePage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ private void RadioButton_Checked(object sender, RoutedEventArgs e)
try
{
Global.Settings.UseDarkThemeSystemTray = DarkRadio.IsChecked ?? false;
Global.MainWindow.InitTimer(Global.Settings.StartHour * 3600, Global.Settings.EndHour * 3600);
Global.MainWindow.InitTimer(new(Global.Settings.StartHour, 0, 0), new(Global.Settings.EndHour, 0, 0));
SettingsManager.Save();
}
catch { }
Expand Down
4 changes: 2 additions & 2 deletions DayBar/Pages/HomePage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ private void ValidateTxt_Click(object sender, RoutedEventArgs e)
int start = int.Parse(FromTxt.Text);
int end = int.Parse(ToTxt.Text);

if (start < end && start >= 0 && end <= 24)
if (start >= 0)
{
Global.Settings.StartHour = start;
Global.Settings.EndHour = end;
SettingsManager.Save();
Global.MainWindow.InitTimer(start * 3600, end * 3600);
Global.MainWindow.InitTimer(new(start, 0, 0), new(end, 0, 0));

return;
}
Expand Down
2 changes: 1 addition & 1 deletion DayBar/Pages/ThemePage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ private void RadioButton_Checked(object sender, RoutedEventArgs e)
try
{
Global.Settings.UseDarkThemeSystemTray = DarkRadio.IsChecked ?? false;
Global.MainWindow.InitTimer(Global.Settings.StartHour * 3600, Global.Settings.EndHour * 3600);
Global.MainWindow.InitTimer(new(Global.Settings.StartHour, 0, 0), new(Global.Settings.EndHour, 0, 0));
SettingsManager.Save();
}
catch { }
Expand Down

0 comments on commit 97e5f4b

Please sign in to comment.