From ed6c792f9f209fd2b03e3005684d539d129d605d Mon Sep 17 00:00:00 2001 From: Abdulbaqi-Alshareef Date: Sun, 17 May 2020 13:14:55 +0200 Subject: [PATCH 1/4] Implement Undo functionality --- src/toggl_api.h | 3 +- .../LibraryIntegrationTests.cs | 7 ++-- .../Services/UndoService/TimeEntrySnapshot.cs | 21 +++++++++++ .../TogglTimeEntryViewToSnapshotExtension.cs | 29 +++++++++++++++ .../UndoService/UndoDeletionService.cs | 35 +++++++++++++++++++ .../TogglDesktop/TogglDesktop/Toggl.cs | 27 ++++++++------ .../TogglDesktop/TogglDesktop/TogglApi.cs | 3 +- .../TogglDesktop/TogglDesktop.csproj | 4 +++ .../TogglDesktop/ui/ModelExtensions.cs | 4 +-- .../ui/Resources/TimeEntryCellStyles.xaml | 5 +++ .../ui/ViewModels/TimeEntryCellViewModel.cs | 3 +- .../ui/controls/TimeEntryCell.xaml.cs | 2 ++ .../TimeEntryCellContextMenuCommands.cs | 18 ++++++++++ .../ui/controls/TimeEntryList.xaml | 8 ++++- .../TogglDesktop/ui/views/EditView.xaml.cs | 5 +-- 15 files changed, 154 insertions(+), 20 deletions(-) create mode 100644 src/ui/windows/TogglDesktop/TogglDesktop/Services/UndoService/TimeEntrySnapshot.cs create mode 100644 src/ui/windows/TogglDesktop/TogglDesktop/Services/UndoService/TogglTimeEntryViewToSnapshotExtension.cs create mode 100644 src/ui/windows/TogglDesktop/TogglDesktop/Services/UndoService/UndoDeletionService.cs diff --git a/src/toggl_api.h b/src/toggl_api.h index 471d73836a..bf7672433d 100644 --- a/src/toggl_api.h +++ b/src/toggl_api.h @@ -986,7 +986,8 @@ extern "C" { const char_t *tags, const bool_t prevent_on_app, const uint64_t started, - const uint64_t ended); + const uint64_t ended, + const bool_t stop_current_running); // Create an Empty Time Entry without stopping the running TE TOGGL_EXPORT char_t *toggl_create_empty_time_entry( diff --git a/src/ui/windows/TogglDesktop/TogglDesktop.Tests/LibraryIntegrationTests.cs b/src/ui/windows/TogglDesktop/TogglDesktop.Tests/LibraryIntegrationTests.cs index 498431cbd3..397a3a129b 100644 --- a/src/ui/windows/TogglDesktop/TogglDesktop.Tests/LibraryIntegrationTests.cs +++ b/src/ui/windows/TogglDesktop/TogglDesktop.Tests/LibraryIntegrationTests.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using TogglDesktop.Services.UndoService; using Xunit; namespace TogglDesktop.Tests @@ -8,12 +9,14 @@ namespace TogglDesktop.Tests public class LibraryIntegrationTests : IClassFixture { private readonly LibraryFixture _state; + private readonly TimeEntrySnapshot _timeEntrySnapshot; private readonly string _firstTimeEntryGuid; public LibraryIntegrationTests(LibraryFixture libraryFixture) { _state = libraryFixture; Assert.True(Toggl.SetLoggedInUser(_state.MeJson)); + _timeEntrySnapshot = _state.TimeEntries[0].ToTimeEntrySnapshot(); _firstTimeEntryGuid = _state.TimeEntries[0].GUID; } @@ -148,9 +151,9 @@ public void ContinueLatestShouldStartTimeEntry() [Fact] public void DeleteTimeEntryShouldDeleteTimeEntry() { - Toggl.DeleteTimeEntry(_firstTimeEntryGuid); + Toggl.DeleteTimeEntry(_timeEntrySnapshot); - Assert.DoesNotContain(_state.TimeEntries, te => te.GUID == _firstTimeEntryGuid); + Assert.DoesNotContain(_state.TimeEntries, te => te.GUID == _timeEntrySnapshot.GUID); } [Fact] diff --git a/src/ui/windows/TogglDesktop/TogglDesktop/Services/UndoService/TimeEntrySnapshot.cs b/src/ui/windows/TogglDesktop/TogglDesktop/Services/UndoService/TimeEntrySnapshot.cs new file mode 100644 index 0000000000..c23c6b18dd --- /dev/null +++ b/src/ui/windows/TogglDesktop/TogglDesktop/Services/UndoService/TimeEntrySnapshot.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace TogglDesktop.Services.UndoService +{ + public class TimeEntrySnapshot + { + public string GUID { get; set; } + public string Description { get; set; } + public ulong ProjectId { get; set; } + public ulong TaskId { get; set; } + public string Tags { get; set; } + public bool Billable { get; set; } + public string Duration { get; set; } + public ulong Started { get; set; } + public ulong Ended { get; set; } + } +} diff --git a/src/ui/windows/TogglDesktop/TogglDesktop/Services/UndoService/TogglTimeEntryViewToSnapshotExtension.cs b/src/ui/windows/TogglDesktop/TogglDesktop/Services/UndoService/TogglTimeEntryViewToSnapshotExtension.cs new file mode 100644 index 0000000000..007e8c6948 --- /dev/null +++ b/src/ui/windows/TogglDesktop/TogglDesktop/Services/UndoService/TogglTimeEntryViewToSnapshotExtension.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using static TogglDesktop.Toggl; + +namespace TogglDesktop.Services.UndoService +{ + public static class TogglTimeEntryViewToSnapshotExtension + { + public static TimeEntrySnapshot ToTimeEntrySnapshot( + this TogglTimeEntryView timeEntryView) + { + return new TimeEntrySnapshot() + { + GUID = timeEntryView.GUID, + Description = timeEntryView.Description, + ProjectId = timeEntryView.PID, + TaskId = timeEntryView.TID, + Tags = timeEntryView.Tags, + Billable = timeEntryView.Billable, + Duration = timeEntryView.Duration, + Started = timeEntryView.Started, + Ended = timeEntryView.Ended + }; + } + } +} diff --git a/src/ui/windows/TogglDesktop/TogglDesktop/Services/UndoService/UndoDeletionService.cs b/src/ui/windows/TogglDesktop/TogglDesktop/Services/UndoService/UndoDeletionService.cs new file mode 100644 index 0000000000..bd49132fc0 --- /dev/null +++ b/src/ui/windows/TogglDesktop/TogglDesktop/Services/UndoService/UndoDeletionService.cs @@ -0,0 +1,35 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace TogglDesktop.Services.UndoService +{ + public class UndoDeletionService + { + private Stack undoStack; + public UndoDeletionService() + { + undoStack = new Stack(); + } + + public bool CanUndo => undoStack.Any(); + + public void SnapshotTimeEntry(TimeEntrySnapshot timeEntry) + { + undoStack.Push(timeEntry); + } + + public void UndoDeletion() + { + if (CanUndo) + { + var te = undoStack.Pop(); + Toggl.Start(te.Description, te.Duration, te.TaskId, te.ProjectId, "", te.Tags, true, te.Started, te.Ended, false); + if (te.Billable) + Toggl.SetTimeEntryBillable(te.GUID, te.Billable); + } + } + } +} diff --git a/src/ui/windows/TogglDesktop/TogglDesktop/Toggl.cs b/src/ui/windows/TogglDesktop/TogglDesktop/Toggl.cs index cb25cd95a8..b7ba2ae11f 100644 --- a/src/ui/windows/TogglDesktop/TogglDesktop/Toggl.cs +++ b/src/ui/windows/TogglDesktop/TogglDesktop/Toggl.cs @@ -7,6 +7,7 @@ using System.Windows.Input; using TogglDesktop.Diagnostics; using TogglDesktop.Services; +using TogglDesktop.Services.UndoService; // ReSharper disable InconsistentNaming @@ -14,8 +15,9 @@ namespace TogglDesktop { public static partial class Toggl { - #region constants and static fields + #region constants and static fields + public static UndoDeletionService UndoDeletionService = new UndoDeletionService(); public const string Project = "project"; public const string Duration = "duration"; public const string Description = "description"; @@ -293,14 +295,15 @@ public static bool ContinueLatest(bool preventOnApp = false) return toggl_continue_latest(ctx, preventOnApp); } - public static bool DeleteTimeEntry(string guid) + public static bool DeleteTimeEntry(TimeEntrySnapshot timeEntrySnapshot) { - return toggl_delete_time_entry(ctx, guid); + Toggl.UndoDeletionService.SnapshotTimeEntry(timeEntrySnapshot); + return toggl_delete_time_entry(ctx, timeEntrySnapshot.GUID); } - #region changing time entry + #region changing time entry - public static bool SetTimeEntryDuration(string guid, string value) + public static bool SetTimeEntryDuration(string guid, string value) { using (Performance.Measure("changing time entry duration")) { @@ -521,7 +524,10 @@ public static string Start( UInt64 project_id, string project_guid, string tags, - bool preventOnApp = false) + bool preventOnApp = false, + ulong started = 0, + ulong ended = 0, + bool stop_current_running = true) { OnUserTimeEntryStart(); @@ -533,8 +539,9 @@ public static string Start( project_guid, tags, preventOnApp, - 0, - 0); + started, + ended, + stop_current_running); } public static string AddProject( @@ -1422,14 +1429,14 @@ public static void ShowErrorAndNotify(string errmsg, Exception ex) BugsnagService.NotifyBugsnag(ex); } - public static bool AskToDeleteEntry(string guid) + public static bool AskToDeleteEntry(TimeEntrySnapshot timeEntrySnapshot) { var result = MessageBox.Show(mainWindow, "Deleted time entries cannot be restored.", "Delete time entry?", MessageBoxButton.OKCancel, "Delete entry"); if (result == MessageBoxResult.OK) { - return DeleteTimeEntry(guid); + return DeleteTimeEntry(timeEntrySnapshot); } return false; } diff --git a/src/ui/windows/TogglDesktop/TogglDesktop/TogglApi.cs b/src/ui/windows/TogglDesktop/TogglDesktop/TogglApi.cs index 65238f6110..eb41346417 100644 --- a/src/ui/windows/TogglDesktop/TogglDesktop/TogglApi.cs +++ b/src/ui/windows/TogglDesktop/TogglDesktop/TogglApi.cs @@ -1493,7 +1493,8 @@ private static extern string toggl_start( [MarshalAs(UnmanagedType.I1)] bool prevent_on_app, UInt64 started, - UInt64 ended); + UInt64 ended, + bool stop_current_running); // Create an Empty Time Entry without stopping the running TE [DllImport(dll, CharSet = charset, CallingConvention = convention)] diff --git a/src/ui/windows/TogglDesktop/TogglDesktop/TogglDesktop.csproj b/src/ui/windows/TogglDesktop/TogglDesktop/TogglDesktop.csproj index 819f68bad7..ee15445271 100644 --- a/src/ui/windows/TogglDesktop/TogglDesktop/TogglDesktop.csproj +++ b/src/ui/windows/TogglDesktop/TogglDesktop/TogglDesktop.csproj @@ -194,6 +194,9 @@ + + + @@ -783,6 +786,7 @@ + \ No newline at end of file diff --git a/src/ui/windows/TogglDesktop/TogglDesktop/ui/ModelExtensions.cs b/src/ui/windows/TogglDesktop/TogglDesktop/ui/ModelExtensions.cs index e3e8367beb..418e13cdc9 100644 --- a/src/ui/windows/TogglDesktop/TogglDesktop/ui/ModelExtensions.cs +++ b/src/ui/windows/TogglDesktop/TogglDesktop/ui/ModelExtensions.cs @@ -19,10 +19,10 @@ public static void DeleteTimeEntry(this TimeEntryCellViewModel cell) { if (cell.ConfirmlessDelete()) { - Toggl.DeleteTimeEntry(cell.Guid); + Toggl.DeleteTimeEntry(cell.TimeEntrySnapshot); return; } - Toggl.AskToDeleteEntry(cell.Guid); + Toggl.AskToDeleteEntry(cell.TimeEntrySnapshot); } private static bool IsDurationLessThan15Seconds(long durationInSeconds) diff --git a/src/ui/windows/TogglDesktop/TogglDesktop/ui/Resources/TimeEntryCellStyles.xaml b/src/ui/windows/TogglDesktop/TogglDesktop/ui/Resources/TimeEntryCellStyles.xaml index ab0bb38c75..5e7f6a0656 100644 --- a/src/ui/windows/TogglDesktop/TogglDesktop/ui/Resources/TimeEntryCellStyles.xaml +++ b/src/ui/windows/TogglDesktop/TogglDesktop/ui/Resources/TimeEntryCellStyles.xaml @@ -11,6 +11,8 @@ + @@ -19,6 +21,9 @@ + + - + + + + + Date: Tue, 19 May 2020 15:06:13 +0200 Subject: [PATCH 2/4] Remove Unneeded TimeEntrySnapshot class --- .../LibraryIntegrationTests.cs | 6 ++-- .../{UndoService => }/UndoDeletionService.cs | 17 +++++++---- .../Services/UndoService/TimeEntrySnapshot.cs | 21 -------------- .../TogglTimeEntryViewToSnapshotExtension.cs | 29 ------------------- .../TogglDesktop/TogglDesktop/Toggl.cs | 13 ++++----- .../TogglDesktop/TogglDesktop.csproj | 4 +-- .../ui/ViewModels/TimeEntryCellViewModel.cs | 4 +-- .../ui/controls/TimeEntryCell.xaml.cs | 3 +- .../TogglDesktop/ui/views/EditView.xaml.cs | 5 ++-- 9 files changed, 26 insertions(+), 76 deletions(-) rename src/ui/windows/TogglDesktop/TogglDesktop/Services/{UndoService => }/UndoDeletionService.cs (52%) delete mode 100644 src/ui/windows/TogglDesktop/TogglDesktop/Services/UndoService/TimeEntrySnapshot.cs delete mode 100644 src/ui/windows/TogglDesktop/TogglDesktop/Services/UndoService/TogglTimeEntryViewToSnapshotExtension.cs diff --git a/src/ui/windows/TogglDesktop/TogglDesktop.Tests/LibraryIntegrationTests.cs b/src/ui/windows/TogglDesktop/TogglDesktop.Tests/LibraryIntegrationTests.cs index 397a3a129b..0fe21c88c7 100644 --- a/src/ui/windows/TogglDesktop/TogglDesktop.Tests/LibraryIntegrationTests.cs +++ b/src/ui/windows/TogglDesktop/TogglDesktop.Tests/LibraryIntegrationTests.cs @@ -1,22 +1,22 @@ using System; using System.Collections.Generic; using System.Linq; -using TogglDesktop.Services.UndoService; using Xunit; +using static TogglDesktop.Toggl; namespace TogglDesktop.Tests { public class LibraryIntegrationTests : IClassFixture { private readonly LibraryFixture _state; - private readonly TimeEntrySnapshot _timeEntrySnapshot; + private readonly TogglTimeEntryView _timeEntrySnapshot; private readonly string _firstTimeEntryGuid; public LibraryIntegrationTests(LibraryFixture libraryFixture) { _state = libraryFixture; Assert.True(Toggl.SetLoggedInUser(_state.MeJson)); - _timeEntrySnapshot = _state.TimeEntries[0].ToTimeEntrySnapshot(); + _timeEntrySnapshot = _state.TimeEntries[0]; _firstTimeEntryGuid = _state.TimeEntries[0].GUID; } diff --git a/src/ui/windows/TogglDesktop/TogglDesktop/Services/UndoService/UndoDeletionService.cs b/src/ui/windows/TogglDesktop/TogglDesktop/Services/UndoDeletionService.cs similarity index 52% rename from src/ui/windows/TogglDesktop/TogglDesktop/Services/UndoService/UndoDeletionService.cs rename to src/ui/windows/TogglDesktop/TogglDesktop/Services/UndoDeletionService.cs index bd49132fc0..d2e689378c 100644 --- a/src/ui/windows/TogglDesktop/TogglDesktop/Services/UndoService/UndoDeletionService.cs +++ b/src/ui/windows/TogglDesktop/TogglDesktop/Services/UndoDeletionService.cs @@ -1,22 +1,27 @@ -using System; +using ReactiveUI; +using ReactiveUI.Fody.Helpers; +using System; using System.Collections.Generic; using System.Linq; +using System.Reactive.Linq; +using System.Reactive.Subjects; using System.Text; using System.Threading.Tasks; +using static TogglDesktop.Toggl; -namespace TogglDesktop.Services.UndoService +namespace TogglDesktop.Services { public class UndoDeletionService { - private Stack undoStack; + private Stack undoStack; public UndoDeletionService() { - undoStack = new Stack(); + undoStack = new Stack(); } public bool CanUndo => undoStack.Any(); - public void SnapshotTimeEntry(TimeEntrySnapshot timeEntry) + public void SnapshotTimeEntry(TogglTimeEntryView timeEntry) { undoStack.Push(timeEntry); } @@ -26,7 +31,7 @@ public void UndoDeletion() if (CanUndo) { var te = undoStack.Pop(); - Toggl.Start(te.Description, te.Duration, te.TaskId, te.ProjectId, "", te.Tags, true, te.Started, te.Ended, false); + Toggl.Start(te.Description, te.Duration, te.TID, te.PID, "", te.Tags, true, te.Started, te.Ended, false); if (te.Billable) Toggl.SetTimeEntryBillable(te.GUID, te.Billable); } diff --git a/src/ui/windows/TogglDesktop/TogglDesktop/Services/UndoService/TimeEntrySnapshot.cs b/src/ui/windows/TogglDesktop/TogglDesktop/Services/UndoService/TimeEntrySnapshot.cs deleted file mode 100644 index c23c6b18dd..0000000000 --- a/src/ui/windows/TogglDesktop/TogglDesktop/Services/UndoService/TimeEntrySnapshot.cs +++ /dev/null @@ -1,21 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace TogglDesktop.Services.UndoService -{ - public class TimeEntrySnapshot - { - public string GUID { get; set; } - public string Description { get; set; } - public ulong ProjectId { get; set; } - public ulong TaskId { get; set; } - public string Tags { get; set; } - public bool Billable { get; set; } - public string Duration { get; set; } - public ulong Started { get; set; } - public ulong Ended { get; set; } - } -} diff --git a/src/ui/windows/TogglDesktop/TogglDesktop/Services/UndoService/TogglTimeEntryViewToSnapshotExtension.cs b/src/ui/windows/TogglDesktop/TogglDesktop/Services/UndoService/TogglTimeEntryViewToSnapshotExtension.cs deleted file mode 100644 index 007e8c6948..0000000000 --- a/src/ui/windows/TogglDesktop/TogglDesktop/Services/UndoService/TogglTimeEntryViewToSnapshotExtension.cs +++ /dev/null @@ -1,29 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using static TogglDesktop.Toggl; - -namespace TogglDesktop.Services.UndoService -{ - public static class TogglTimeEntryViewToSnapshotExtension - { - public static TimeEntrySnapshot ToTimeEntrySnapshot( - this TogglTimeEntryView timeEntryView) - { - return new TimeEntrySnapshot() - { - GUID = timeEntryView.GUID, - Description = timeEntryView.Description, - ProjectId = timeEntryView.PID, - TaskId = timeEntryView.TID, - Tags = timeEntryView.Tags, - Billable = timeEntryView.Billable, - Duration = timeEntryView.Duration, - Started = timeEntryView.Started, - Ended = timeEntryView.Ended - }; - } - } -} diff --git a/src/ui/windows/TogglDesktop/TogglDesktop/Toggl.cs b/src/ui/windows/TogglDesktop/TogglDesktop/Toggl.cs index b7ba2ae11f..98e657581a 100644 --- a/src/ui/windows/TogglDesktop/TogglDesktop/Toggl.cs +++ b/src/ui/windows/TogglDesktop/TogglDesktop/Toggl.cs @@ -7,13 +7,12 @@ using System.Windows.Input; using TogglDesktop.Diagnostics; using TogglDesktop.Services; -using TogglDesktop.Services.UndoService; // ReSharper disable InconsistentNaming namespace TogglDesktop { -public static partial class Toggl + public static partial class Toggl { #region constants and static fields @@ -295,10 +294,10 @@ public static bool ContinueLatest(bool preventOnApp = false) return toggl_continue_latest(ctx, preventOnApp); } - public static bool DeleteTimeEntry(TimeEntrySnapshot timeEntrySnapshot) + public static bool DeleteTimeEntry(TogglTimeEntryView timeEntry) { - Toggl.UndoDeletionService.SnapshotTimeEntry(timeEntrySnapshot); - return toggl_delete_time_entry(ctx, timeEntrySnapshot.GUID); + Toggl.UndoDeletionService.SnapshotTimeEntry(timeEntry); + return toggl_delete_time_entry(ctx, timeEntry.GUID); } #region changing time entry @@ -1429,14 +1428,14 @@ public static void ShowErrorAndNotify(string errmsg, Exception ex) BugsnagService.NotifyBugsnag(ex); } - public static bool AskToDeleteEntry(TimeEntrySnapshot timeEntrySnapshot) + public static bool AskToDeleteEntry(TogglTimeEntryView timeEntry) { var result = MessageBox.Show(mainWindow, "Deleted time entries cannot be restored.", "Delete time entry?", MessageBoxButton.OKCancel, "Delete entry"); if (result == MessageBoxResult.OK) { - return DeleteTimeEntry(timeEntrySnapshot); + return DeleteTimeEntry(timeEntry); } return false; } diff --git a/src/ui/windows/TogglDesktop/TogglDesktop/TogglDesktop.csproj b/src/ui/windows/TogglDesktop/TogglDesktop/TogglDesktop.csproj index ee15445271..58692166d5 100644 --- a/src/ui/windows/TogglDesktop/TogglDesktop/TogglDesktop.csproj +++ b/src/ui/windows/TogglDesktop/TogglDesktop/TogglDesktop.csproj @@ -194,9 +194,7 @@ - - - + diff --git a/src/ui/windows/TogglDesktop/TogglDesktop/ui/ViewModels/TimeEntryCellViewModel.cs b/src/ui/windows/TogglDesktop/TogglDesktop/ui/ViewModels/TimeEntryCellViewModel.cs index 941aefc896..297ee7f78b 100644 --- a/src/ui/windows/TogglDesktop/TogglDesktop/ui/ViewModels/TimeEntryCellViewModel.cs +++ b/src/ui/windows/TogglDesktop/TogglDesktop/ui/ViewModels/TimeEntryCellViewModel.cs @@ -1,6 +1,6 @@ using ReactiveUI; using ReactiveUI.Fody.Helpers; -using TogglDesktop.Services.UndoService; +using static TogglDesktop.Toggl; namespace TogglDesktop.ViewModels { @@ -9,7 +9,7 @@ public class TimeEntryCellViewModel : ReactiveObject public TimeEntryCellViewModel() { } - public TimeEntrySnapshot TimeEntrySnapshot { get; set; } + public TogglTimeEntryView TimeEntrySnapshot { get; set; } [Reactive] public TimeEntryLabelViewModel TimeEntryLabel { get; set; } diff --git a/src/ui/windows/TogglDesktop/TogglDesktop/ui/controls/TimeEntryCell.xaml.cs b/src/ui/windows/TogglDesktop/TogglDesktop/ui/controls/TimeEntryCell.xaml.cs index 555e8db14d..1f8fd2c801 100644 --- a/src/ui/windows/TogglDesktop/TogglDesktop/ui/controls/TimeEntryCell.xaml.cs +++ b/src/ui/windows/TogglDesktop/TogglDesktop/ui/controls/TimeEntryCell.xaml.cs @@ -2,7 +2,6 @@ using System.Windows.Input; using ReactiveUI; using TogglDesktop.Diagnostics; -using TogglDesktop.Services.UndoService; using TogglDesktop.ViewModels; namespace TogglDesktop @@ -40,7 +39,7 @@ public TimeEntryCell() public void Display(Toggl.TogglTimeEntryView item) { - ViewModel.TimeEntrySnapshot = item.ToTimeEntrySnapshot(); + ViewModel.TimeEntrySnapshot = item; ViewModel.Guid = item.GUID; ViewModel.IsGroup = item.Group; if (ViewModel.IsGroup) diff --git a/src/ui/windows/TogglDesktop/TogglDesktop/ui/views/EditView.xaml.cs b/src/ui/windows/TogglDesktop/TogglDesktop/ui/views/EditView.xaml.cs index 6f86beb95c..a553db407f 100644 --- a/src/ui/windows/TogglDesktop/TogglDesktop/ui/views/EditView.xaml.cs +++ b/src/ui/windows/TogglDesktop/TogglDesktop/ui/views/EditView.xaml.cs @@ -9,7 +9,6 @@ using TogglDesktop.AutoCompletion; using TogglDesktop.AutoCompletion.Items; using TogglDesktop.Diagnostics; -using TogglDesktop.Services.UndoService; using TogglDesktop.ViewModels; using KeyEventArgs = System.Windows.Input.KeyEventArgs; using TextBox = System.Windows.Controls.TextBox; @@ -851,10 +850,10 @@ private void deleteButton_OnClick(object sender, RoutedEventArgs e) { if (this.timeEntry.ConfirmlessDelete()) { - Toggl.DeleteTimeEntry(this.timeEntry.ToTimeEntrySnapshot()); + Toggl.DeleteTimeEntry(this.timeEntry); return; } - Toggl.AskToDeleteEntry(this.timeEntry.ToTimeEntrySnapshot()); + Toggl.AskToDeleteEntry(this.timeEntry); } private void clearUndoHistory() From b18e3b330b96600eba77027e9bb4d280b5326bd7 Mon Sep 17 00:00:00 2001 From: Abdulbaqi-Alshareef Date: Tue, 19 May 2020 15:32:59 +0200 Subject: [PATCH 3/4] Add Ctrl+Z shortcut to Undo deletion --- .../TogglDesktop/ui/ViewModels/TimeEntryListViewModel.cs | 7 +++++++ .../TogglDesktop/ui/controls/TimeEntryList.xaml | 1 + 2 files changed, 8 insertions(+) diff --git a/src/ui/windows/TogglDesktop/TogglDesktop/ui/ViewModels/TimeEntryListViewModel.cs b/src/ui/windows/TogglDesktop/TogglDesktop/ui/ViewModels/TimeEntryListViewModel.cs index a0f45bf949..241061bcd8 100644 --- a/src/ui/windows/TogglDesktop/TogglDesktop/ui/ViewModels/TimeEntryListViewModel.cs +++ b/src/ui/windows/TogglDesktop/TogglDesktop/ui/ViewModels/TimeEntryListViewModel.cs @@ -1,4 +1,6 @@ +using System.Reactive; using System.Reactive.Linq; +using System.Windows.Input; using ReactiveUI; using ReactiveUI.Fody.Helpers; @@ -11,8 +13,13 @@ public TimeEntryListViewModel() this.WhenAnyValue(x => x.IsLoading) .Select(x => !x) .ToPropertyEx(this, x => x.IsViewEnabled); + + UndoCommand = ReactiveCommand.Create(()=>OnUndo()); } + public void OnUndo() => + Toggl.UndoDeletionService.UndoDeletion(); + public ReactiveCommand UndoCommand { get; set; } [Reactive] public bool ShowLoadMore { get; private set; } diff --git a/src/ui/windows/TogglDesktop/TogglDesktop/ui/controls/TimeEntryList.xaml b/src/ui/windows/TogglDesktop/TogglDesktop/ui/controls/TimeEntryList.xaml index c433b14723..1de12bbe36 100644 --- a/src/ui/windows/TogglDesktop/TogglDesktop/ui/controls/TimeEntryList.xaml +++ b/src/ui/windows/TogglDesktop/TogglDesktop/ui/controls/TimeEntryList.xaml @@ -60,6 +60,7 @@ + From f1f9bd81a1edb190cee2cb7eff2c26f0d9d24d64 Mon Sep 17 00:00:00 2001 From: Abdulbaqi-Alshareef Date: Thu, 18 Jun 2020 16:10:36 +0200 Subject: [PATCH 4/4] Refactor code after rebase --- src/toggl_api.h | 3 +- .../Services/UndoDeletionService.cs | 2 +- .../TogglDesktop/TogglDesktop/Toggl.cs | 39 +++++++++++++++---- .../TogglDesktop/TogglDesktop/TogglApi.cs | 21 +++++++++- .../ui/ViewModels/TimeEntryListViewModel.cs | 2 +- 5 files changed, 54 insertions(+), 13 deletions(-) diff --git a/src/toggl_api.h b/src/toggl_api.h index bf7672433d..471d73836a 100644 --- a/src/toggl_api.h +++ b/src/toggl_api.h @@ -986,8 +986,7 @@ extern "C" { const char_t *tags, const bool_t prevent_on_app, const uint64_t started, - const uint64_t ended, - const bool_t stop_current_running); + const uint64_t ended); // Create an Empty Time Entry without stopping the running TE TOGGL_EXPORT char_t *toggl_create_empty_time_entry( diff --git a/src/ui/windows/TogglDesktop/TogglDesktop/Services/UndoDeletionService.cs b/src/ui/windows/TogglDesktop/TogglDesktop/Services/UndoDeletionService.cs index d2e689378c..2992fa71a3 100644 --- a/src/ui/windows/TogglDesktop/TogglDesktop/Services/UndoDeletionService.cs +++ b/src/ui/windows/TogglDesktop/TogglDesktop/Services/UndoDeletionService.cs @@ -31,7 +31,7 @@ public void UndoDeletion() if (CanUndo) { var te = undoStack.Pop(); - Toggl.Start(te.Description, te.Duration, te.TID, te.PID, "", te.Tags, true, te.Started, te.Ended, false); + Toggl.StartWithCurrentRunning(te.Description, te.Duration, te.TID, te.PID, "", te.Tags, true, te.Started, te.Ended, false); if (te.Billable) Toggl.SetTimeEntryBillable(te.GUID, te.Billable); } diff --git a/src/ui/windows/TogglDesktop/TogglDesktop/Toggl.cs b/src/ui/windows/TogglDesktop/TogglDesktop/Toggl.cs index 98e657581a..c24d6a3b68 100644 --- a/src/ui/windows/TogglDesktop/TogglDesktop/Toggl.cs +++ b/src/ui/windows/TogglDesktop/TogglDesktop/Toggl.cs @@ -516,17 +516,41 @@ public static bool SetLoggedInUser(string json) { return testing_set_logged_in_user(ctx, json); } - public static string Start( + public static string StartWithCurrentRunning( + string description, + string duration, + UInt64 task_id, + UInt64 project_id, + string project_guid, + string tags, + bool preventOnApp, + ulong started, + ulong ended, + bool stop_current_running) + { + OnUserTimeEntryStart(); + + return toggl_start_with_current_running(ctx, + description, + duration, + task_id, + project_id, + project_guid, + tags, + preventOnApp, + started, + ended, + stop_current_running); + } + + public static string Start( string description, string duration, UInt64 task_id, UInt64 project_id, string project_guid, string tags, - bool preventOnApp = false, - ulong started = 0, - ulong ended = 0, - bool stop_current_running = true) + bool preventOnApp = false) { OnUserTimeEntryStart(); @@ -538,9 +562,8 @@ public static string Start( project_guid, tags, preventOnApp, - started, - ended, - stop_current_running); + 0, + 0); } public static string AddProject( diff --git a/src/ui/windows/TogglDesktop/TogglDesktop/TogglApi.cs b/src/ui/windows/TogglDesktop/TogglDesktop/TogglApi.cs index eb41346417..3ec59da9ea 100644 --- a/src/ui/windows/TogglDesktop/TogglDesktop/TogglApi.cs +++ b/src/ui/windows/TogglDesktop/TogglDesktop/TogglApi.cs @@ -1478,7 +1478,7 @@ private static extern bool toggl_clear_cache( // returns GUID of the started time entry. you must free() the result [DllImport(dll, CharSet = charset, CallingConvention = convention)] - private static extern string toggl_start( + private static extern string toggl_start_with_current_running( IntPtr context, [MarshalAs(UnmanagedType.LPWStr)] string description, @@ -1496,6 +1496,25 @@ private static extern string toggl_start( UInt64 ended, bool stop_current_running); + // returns GUID of the started time entry. you must free() the result + [DllImport(dll, CharSet = charset, CallingConvention = convention)] + private static extern string toggl_start( + IntPtr context, + [MarshalAs(UnmanagedType.LPWStr)] + string description, + [MarshalAs(UnmanagedType.LPWStr)] + string duration, + UInt64 task_id, + UInt64 project_id, + [MarshalAs(UnmanagedType.LPWStr)] + string project_guid, + [MarshalAs(UnmanagedType.LPWStr)] + string tags, + [MarshalAs(UnmanagedType.I1)] + bool prevent_on_app, + UInt64 started, + UInt64 ended); + // Create an Empty Time Entry without stopping the running TE [DllImport(dll, CharSet = charset, CallingConvention = convention)] private static extern string toggl_create_empty_time_entry( diff --git a/src/ui/windows/TogglDesktop/TogglDesktop/ui/ViewModels/TimeEntryListViewModel.cs b/src/ui/windows/TogglDesktop/TogglDesktop/ui/ViewModels/TimeEntryListViewModel.cs index 241061bcd8..5abd515615 100644 --- a/src/ui/windows/TogglDesktop/TogglDesktop/ui/ViewModels/TimeEntryListViewModel.cs +++ b/src/ui/windows/TogglDesktop/TogglDesktop/ui/ViewModels/TimeEntryListViewModel.cs @@ -14,7 +14,7 @@ public TimeEntryListViewModel() .Select(x => !x) .ToPropertyEx(this, x => x.IsViewEnabled); - UndoCommand = ReactiveCommand.Create(()=>OnUndo()); + UndoCommand = ReactiveCommand.Create(OnUndo); } public void OnUndo() =>