Skip to content

Commit

Permalink
Merge pull request #30 from Toumash/feature/sum-hours-since
Browse files Browse the repository at this point in the history
Feature/sum hours since
  • Loading branch information
Toumash authored Oct 26, 2019
2 parents ca098c0 + 217002e commit e7d65f8
Show file tree
Hide file tree
Showing 12 changed files with 183 additions and 9 deletions.
11 changes: 10 additions & 1 deletion DailyStatus.Common/BLL/WorkDaysCalculator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@ namespace DailyStatus.Common.BLL
public class WorkDaysCalculator
{
public TimeSpan MonthExpectedHours(DateTime todayWithHours, TimeSpan workDayStartHour, double numberOfWorkingHoursPerDay, params DateTime[] holidays)
{
return TimeExpectedHours(new DateTime(todayWithHours.Year, todayWithHours.Month, 1), todayWithHours, workDayStartHour, numberOfWorkingHoursPerDay, holidays);
}
public TimeSpan TimeExpectedHours(DateTime since, DateTime todayWithHours, TimeSpan workDayStartHour, double numberOfWorkingHoursPerDay, params DateTime[] holidays)
{
var today = todayWithHours.Date;
var first = new DateTime(today.Year, today.Month, 1);
var first = since;
var workDayStart = today + workDayStartHour;

var worktime = TimeSpan.FromHours(first.BusinessDaysUntil(today, holidays) * numberOfWorkingHoursPerDay);
Expand All @@ -32,6 +36,11 @@ public TimeSpan ExpectedWorkedDays(TimeSpan workDayStartHour, double numberOfWor
{
return MonthExpectedHours(DateTime.Now, workDayStartHour, numberOfWorkingHoursPerDay, holidaysDuringWeek);
}

internal TimeSpan ExpectedWorkedDaysSince(DateTime since, TimeSpan workDayStartHour, double numberOfWorkingHoursPerDay, params DateTime[] holidaysDuringWeek)
{
return TimeExpectedHours(since, DateTime.Now, workDayStartHour, numberOfWorkingHoursPerDay, holidaysDuringWeek);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ public class DailyStatusConfiguration
public int HoursADay { get; set; }
public long WorkspaceId { get; set; }
public List<DateTime> Holidays { get; set; }
public DateTime SumSince { get; set; }
}
}
18 changes: 14 additions & 4 deletions DailyStatus.Common/TogglProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ public TimeSpan GetExpectedWorkingTime(WorkDay dayConfig)
dayConfig.NumberOfWorkingHoursPerDay);
}

public TimeSpan GetExpectedWorkingTime(WorkDay dayConfig, DateTime since)
{
return new WorkDaysCalculator()
.ExpectedWorkedDaysSince(since,TimeSpan.FromHours(dayConfig.WorkDayStartHour),
dayConfig.NumberOfWorkingHoursPerDay);
}

public TimeSpan GetDifference(TimeSpan expected, TimeSpan sum)
{
var diff = sum - expected;
Expand Down Expand Up @@ -67,15 +74,18 @@ public class TogglStatus
}

public async Task<TogglStatus> GetStatus()
{
return await GetStatus(new DateTimeOffset(new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1)));
}

public async Task<TogglStatus> GetStatus(DateTimeOffset since)
{
try
{
var workspace = await GetWorkspace();
var today = DateTime.Today;
var offset = new DateTimeOffset(new DateTime(today.Year, today.Month, 1));
var entries = await _togglApi.TimeEntries.GetAllSince(offset)
var entries = await _togglApi.TimeEntries.GetAllSince(since)
.SelectMany(e => e)
.Where(e => !e.ServerDeletedAt.HasValue && e.Start > offset)
.Where(e => !e.ServerDeletedAt.HasValue && e.Start > since)
.Where(e => e.WorkspaceId == workspace.Id)
.ToList();
var sumSeconds = entries.Where(e => e.Duration.HasValue)
Expand Down
2 changes: 1 addition & 1 deletion DailyStatus.CommonTests/BLL/WorkDaysCalculatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public void Given12_11_2019_ShouldReturn6Days_BecauseThereIsOneHolidayOn11_11()
todayWithHours: new DateTime(2019, 11, 12),
workDayStartHour: TimeSpan.FromHours(10),
numberOfWorkingHoursPerDay: 8,
new DateTime(2019, 11, 11));
new DateTime(2019, 11, 11));
var expected = TimeSpan.FromHours(6 * 8);
Assert.AreEqual(expected, actual);
}
Expand Down
3 changes: 3 additions & 0 deletions DailyStatus.UI/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@
<setting name="holidays" serializeAs="String">
<value>1/01/2019,01/06/2019,04/21/2019,04/22/2019,05/01/2019,05/03/2019,06/09/2019,06/20/2019,08/15/2019,11/01/2019,11/11/2019,12/25/2019,12/26/2019</value>
</setting>
<setting name="sum_since" serializeAs="String">
<value>2019-09-01</value>
</setting>
</DailyStatus.UI.Properties.Settings>
</userSettings>
</configuration>
7 changes: 7 additions & 0 deletions DailyStatus.UI/DailyStatus.UI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@
<DependentUpon>App.xaml</DependentUpon>
<SubType>Code</SubType>
</Compile>
<Compile Include="View\DateTimeSincePrompt.xaml.cs">
<DependentUpon>DateTimeSincePrompt.xaml</DependentUpon>
</Compile>
<Compile Include="View\HourRatePrompt.xaml.cs">
<DependentUpon>HourRatePrompt.xaml</DependentUpon>
</Compile>
Expand Down Expand Up @@ -168,6 +171,10 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="View\DateTimeSincePrompt.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
<Page Include="View\HourRatePrompt.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
Expand Down
12 changes: 12 additions & 0 deletions DailyStatus.UI/Properties/Settings.Designer.cs

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

3 changes: 3 additions & 0 deletions DailyStatus.UI/Properties/Settings.settings
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,8 @@
<Setting Name="holidays" Type="System.String" Scope="User">
<Value Profile="(Default)">1/01/2019,01/06/2019,04/21/2019,04/22/2019,05/01/2019,05/03/2019,06/09/2019,06/20/2019,08/15/2019,11/01/2019,11/11/2019,12/25/2019,12/26/2019</Value>
</Setting>
<Setting Name="sum_since" Type="System.DateTime" Scope="User">
<Value Profile="(Default)">2019-09-01</Value>
</Setting>
</Settings>
</SettingsFile>
2 changes: 2 additions & 0 deletions DailyStatus.UI/SettingsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public static DailyStatusConfiguration LoadSettings()
cfg.HoursADay = stg.hours_a_day;
cfg.WorkspaceId = stg.api_workspaceid;
cfg.Holidays = stg.holidays.Split(',').Select(str => DateTime.Parse(str, CultureInfo.InvariantCulture)).ToList();
cfg.SumSince = stg.sum_since;
return cfg;
}

Expand All @@ -34,6 +35,7 @@ public static void SaveSettings(DailyStatusConfiguration cfg)
stg.start_hour = cfg.WorkDayStartHour;
stg.hours_a_day = cfg.HoursADay;
stg.holidays = string.Join(",", cfg.Holidays.Select(d => d.ToString(CultureInfo.InvariantCulture)));
stg.sum_since = cfg.SumSince;
stg.Save();
}
}
Expand Down
23 changes: 23 additions & 0 deletions DailyStatus.UI/View/DateTimeSincePrompt.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Window x:Class="DailyStatus.UI.View.DateTimeSincePrompt"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:DailyStatus.UI.View"
mc:Ignorable="d"
Title="{Binding WindowTitle}" Height="100" Width="468.22">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="33*"/>
<ColumnDefinition Width="59*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="50*"/>
<RowDefinition Height="50*"/>
</Grid.RowDefinitions>
<TextBlock Text="{Binding WindowPrompt}" Grid.RowSpan="1" Grid.ColumnSpan="1" Grid.Column="0" FontSize="20"/>
<DatePicker Name="DatePicker" HorizontalAlignment="Left" Margin="10,0,0,0" Grid.Row="0" Grid.Column="1" Width="275" />
<Button Content="OK" Click="OKButton_Click" Grid.RowSpan="1" Grid.Row="1" MaxHeight="30" Grid.ColumnSpan="2"/>

</Grid>
</Window>
71 changes: 71 additions & 0 deletions DailyStatus.UI/View/DateTimeSincePrompt.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace DailyStatus.UI.View
{
public partial class DateTimeSincePrompt : Window, INotifyPropertyChanged
{
private string _windowTitle;
private string _windowPrompt;

public DateTimeSincePrompt()
{
InitializeComponent();
DataContext = this;
}

public string WindowTitle
{
get { return _windowTitle; }
set
{
_windowTitle = value;
NotifyPropertyChanged(nameof(WindowTitle));
}
}
public string WindowPrompt
{
get { return _windowPrompt; }
set
{
_windowPrompt = value;
NotifyPropertyChanged(nameof(WindowPrompt));
}
}

public DateTime? Date
{
get
{
return DatePicker.SelectedDate;
}
set
{
DatePicker.SelectedDate = value;
}
}

private void OKButton_Click(object sender, RoutedEventArgs e)
{
DialogResult = true;
}

public event PropertyChangedEventHandler PropertyChanged;
void NotifyPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
39 changes: 36 additions & 3 deletions DailyStatus.UI/ViewModel/StatusViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,21 @@ public int HoursADay
}
}

public DateTime SumSince
{
get
{
return cfg.SumSince;
}
set
{
cfg.SumSince = value;
NotifyPropertyChanged(nameof(SumSince));
NotifyPropertyChanged(nameof(ContextMenu));
SaveSettings();
}
}

public List<MenuItem> ContextMenu
{
get
Expand Down Expand Up @@ -334,6 +349,23 @@ public List<MenuItem> ContextMenu
WorkDayStartHour = (int)newDayStartHour;
})
});
items.Add(new MenuItem()
{
Header = "Sum since: " + cfg.SumSince.ToString("dd.MM.yyyy"),
Command = new RelayCommand((_) =>
{
var prompt = new DateTimeSincePrompt
{
Owner = this.Window,
Date = cfg.SumSince,
WindowTitle = "Sum hours since",
WindowPrompt = "Sum since: "
};

prompt.ShowDialog();
SumSince = prompt.Date ?? new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1);
})
});
items.Add(new MenuItem() { Header = "Minimize", Command = new RelayCommand((_) => WindowState = WindowState.Minimized) });
items.Add(new MenuItem() { Header = "Close", Command = CloseCommand });
return items;
Expand Down Expand Up @@ -391,12 +423,13 @@ async Task RefreshData()
NotifyPropertyChanged(nameof(ContextMenu));
}
_togglClient.SetWorkspace(SelectedWorkspace);
var actual = (await _togglClient.GetStatus());
var since = SumSince;
var actual = (await _togglClient.GetStatus(new DateTimeOffset(since)));
TodayHours = actual.TodaysHours;
IsTimerActive = actual.IsTimerActive;

var workday = new WorkDay(cfg.HoursADay, cfg.WorkDayStartHour);
var expected = _togglClient.GetExpectedWorkingTime(workday);
var workday = new WorkDay(HoursADay, WorkDayStartHour);
var expected = _togglClient.GetExpectedWorkingTime(workday, since);
Diff = _togglClient.GetDifference(expected: expected, sum: actual.TimeInMonth);
LastUpdateTime = DateTime.Now;
OfflineMode = false;
Expand Down

0 comments on commit e7d65f8

Please sign in to comment.