Skip to content
This repository has been archived by the owner on Sep 3, 2024. It is now read-only.

Commit

Permalink
Merge pull request #57 from julien-wff/fix-pause
Browse files Browse the repository at this point in the history
fix: pause
  • Loading branch information
julien-wff authored Dec 15, 2023
2 parents f90cd67 + fcd5fd3 commit a774d4d
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 10 deletions.
6 changes: 6 additions & 0 deletions EasyGUI/Controls/Buttons/BaseButton.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@
CornerRadius="6">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Opacity" Value="0.5" />
<Setter Property="Cursor" Value="No" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Button.Template>

Expand Down
16 changes: 16 additions & 0 deletions EasyGUI/Controls/JobDisplay.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName
UpdateJobProgress();
UpdateBreadcrumbs();
UpdateButtons();
SetButtonsState(true);
}

if (propertyName == nameof(SelectedJobs) && !_selectedJobsLocked)
Expand Down Expand Up @@ -138,6 +139,16 @@ private void UpdateButtons()
SetElementVisibility(PauseButton, Job.State != JobState.End && Job.CurrentlyRunning);
}

private void SetButtonsState(bool enabled)
{
EditButton.IsEnabled = enabled;
StartButton.IsEnabled = enabled;
DeleteButton.IsEnabled = enabled;
ResumeButton.IsEnabled = enabled;
DiscardButton.IsEnabled = enabled;
PauseButton.IsEnabled = enabled;
}

private void UpdateJobProgress()
{
if (Job.State == JobState.End)
Expand Down Expand Up @@ -168,6 +179,7 @@ private static void SetElementVisibility(UIElement element, bool visible)

private void StartButton_OnClick(object sender, RoutedEventArgs e)
{
SetButtonsState(false);
JobStarted?.Invoke(this, new JobEventArgs(Job));
}

Expand Down Expand Up @@ -207,21 +219,25 @@ private void EditButton_OnClick(object sender, RoutedEventArgs e)

private void ResumeButton_OnClick(object sender, RoutedEventArgs e)
{
SetButtonsState(false);
JobResumed?.Invoke(this, new JobEventArgs(Job));
}

private void DeleteButton_OnClick(object sender, RoutedEventArgs e)
{
SetButtonsState(false);
JobDeleted?.Invoke(this, new JobEventArgs(Job));
}

private void DiscardButton_OnClick(object sender, RoutedEventArgs e)
{
SetButtonsState(false);
JobDiscarded?.Invoke(this, new JobEventArgs(Job));
}

private void PauseButton_OnClick(object sender, RoutedEventArgs e)
{
SetButtonsState(false);
JobPaused?.Invoke(this, new JobEventArgs(Job));
}

Expand Down
45 changes: 35 additions & 10 deletions EasyLib/Job/Job.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public Job(JsonJob job) : this(job.name, job.source_folder, job.destination_fold
public string CurrentFileSource { get; set; } = string.Empty;
public string CurrentFileDestination { get; set; } = string.Empty;
public bool CurrentlyRunning { get; private set; }
public CancellationTokenSource CancellationToken { get; } = new();
public CancellationTokenSource CancellationToken { get; private set; } = new();

public void Subscribe(IJobStatusSubscriber subscriber)
{
Expand Down Expand Up @@ -182,16 +182,41 @@ private void JobSteps(TransferManager transferManager, List<List<string>> folder
{
transferManager.Subscribe(this);
CurrentlyRunning = true;
_setJobState(JobState.SourceScan);
transferManager.ScanSource();
_setJobState(JobState.DifferenceCalculation);
transferManager.ComputeDifference(folders);
_setJobState(JobState.DestinationStructureCreation);
transferManager.CreateDestinationStructure();
_setJobState(JobState.Copy);
transferManager.TransferFiles();

if (!CancellationToken.IsCancellationRequested)
{
_setJobState(JobState.SourceScan);
transferManager.ScanSource();
}

if (!CancellationToken.IsCancellationRequested)
{
_setJobState(JobState.DifferenceCalculation);
transferManager.ComputeDifference(folders);
}

if (!CancellationToken.IsCancellationRequested)
{
_setJobState(JobState.DestinationStructureCreation);
transferManager.CreateDestinationStructure();
}

if (!CancellationToken.IsCancellationRequested)
{
_setJobState(JobState.Copy);
transferManager.TransferFiles();
}

CurrentlyRunning = false;
_setJobState(JobState.End);

// If the job is cancelled, re-send the state to the subscribers so they can update their UI
// Otherwise, set the state to "End"
_setJobState(!CancellationToken.IsCancellationRequested ? JobState.End : State);

// Make the cancellation token available for the next job
CancellationToken.Dispose();
CancellationToken = new CancellationTokenSource();

transferManager.Unsubscribe(this);
}

Expand Down

0 comments on commit a774d4d

Please sign in to comment.