| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="ProgresserBusyExtensions.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Threading; |
| | | 9 | | using System.Threading.Tasks; |
| | | 10 | | using MyNet.UI.Loading.Models; |
| | | 11 | | using MyNet.Utilities.Progress; |
| | | 12 | | |
| | | 13 | | #pragma warning disable IDE0130 |
| | | 14 | | namespace MyNet.UI.Loading; |
| | | 15 | | #pragma warning restore IDE0130 |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// Bridges the hierarchical progress API (<see cref="IProgresser"/>) onto the busy system: |
| | | 19 | | /// a <see cref="ProgressionBusy"/> scope is opened for the duration of the operation and kept in |
| | | 20 | | /// sync with every <see cref="ProgressReport{T}"/> emitted by the progresser (value, breadcrumb |
| | | 21 | | /// message, cancellation availability and cancel action). |
| | | 22 | | /// </summary> |
| | | 23 | | public static class ProgresserBusyExtensions |
| | | 24 | | { |
| | | 25 | | extension(IBusyService busyService) |
| | | 26 | | { |
| | | 27 | | /// <summary> |
| | | 28 | | /// Runs <paramref name="operation"/> inside a <see cref="ProgressionBusy"/> scope while mirroring |
| | | 29 | | /// the progress reported through <paramref name="progresser"/>. The operation receives the same |
| | | 30 | | /// <paramref name="progresser"/> so it can open steps with <c>Begin</c>/<c>StartStep</c>. |
| | | 31 | | /// </summary> |
| | | 32 | | /// <param name="progresser">The progresser whose reports drive the busy presentation.</param> |
| | | 33 | | /// <param name="operation">The asynchronous work to execute.</param> |
| | | 34 | | /// <param name="cancellationToken">A token to monitor for cancellation requests.</param> |
| | | 35 | | public Task RunWithProgressAsync( |
| | | 36 | | IProgresser progresser, |
| | | 37 | | Func<IProgresser, CancellationToken, Task> operation, |
| | | 38 | | CancellationToken cancellationToken = default) |
| | | 39 | | { |
| | 0 | 40 | | ArgumentNullException.ThrowIfNull(progresser); |
| | 0 | 41 | | ArgumentNullException.ThrowIfNull(operation); |
| | | 42 | | |
| | 0 | 43 | | return busyService.RunAsync<ProgressionBusy>( |
| | 0 | 44 | | (busy, token) => RunBridgedAsync(progresser, busy, () => operation(progresser, token), token), |
| | 0 | 45 | | cancellationToken); |
| | | 46 | | } |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// Runs <paramref name="operation"/> inside a <see cref="ProgressionBusy"/> scope, mirroring the |
| | | 50 | | /// progress reported through a freshly created <see cref="Progresser"/> handed to the operation. |
| | | 51 | | /// </summary> |
| | | 52 | | /// <param name="operation">The asynchronous work to execute.</param> |
| | | 53 | | /// <param name="cancellationToken">A token to monitor for cancellation requests.</param> |
| | | 54 | | public Task RunWithProgressAsync( |
| | | 55 | | Func<IProgresser, CancellationToken, Task> operation, |
| | | 56 | | CancellationToken cancellationToken = default) |
| | 0 | 57 | | => busyService.RunWithProgressAsync(new Progresser(), operation, cancellationToken); |
| | | 58 | | } |
| | | 59 | | |
| | | 60 | | private static async Task RunBridgedAsync( |
| | | 61 | | IProgresser progresser, |
| | | 62 | | ProgressionBusy busy, |
| | | 63 | | Func<Task> operation, |
| | | 64 | | CancellationToken token) |
| | | 65 | | { |
| | 0 | 66 | | ProgressReport<ProgressMessage>? lastReport = null; |
| | 0 | 67 | | string? lastMessage = null; |
| | | 68 | | |
| | | 69 | | // A cancel request coming from the busy UI (its CancellationToken) is forwarded to the |
| | | 70 | | // cancel action carried by the latest progress report, so the progress session stops too. |
| | 0 | 71 | | await using var registration = token.Register(() => lastReport?.CancelAction?.Invoke()).ConfigureAwait(false); |
| | | 72 | | |
| | | 73 | | // Progress<T> marshals callbacks to the synchronization context captured here, so the busy |
| | | 74 | | // model (and its bound UI) is always updated on the originating thread. |
| | 0 | 75 | | var sink = new Progress<ProgressReport<ProgressMessage>>(report => |
| | 0 | 76 | | { |
| | 0 | 77 | | lastReport = report; |
| | 0 | 78 | | busy.CanCancel = report.CanCancel; |
| | 0 | 79 | | busy.Value = report.Progress; |
| | 0 | 80 | | |
| | 0 | 81 | | var message = report.Messages.Count > 0 ? report.Messages[^1].ToString() : null; |
| | 0 | 82 | | if (string.IsNullOrWhiteSpace(message)) |
| | 0 | 83 | | return; |
| | 0 | 84 | | |
| | 0 | 85 | | busy.Title = message; |
| | 0 | 86 | | |
| | 0 | 87 | | // Only append a breadcrumb entry when the innermost message actually changes, |
| | 0 | 88 | | // otherwise repeated reports for the same step would flood the history. |
| | 0 | 89 | | if (!string.Equals(message, lastMessage, StringComparison.Ordinal)) |
| | 0 | 90 | | { |
| | 0 | 91 | | busy.Messages.Add(message); |
| | 0 | 92 | | lastMessage = message; |
| | 0 | 93 | | } |
| | 0 | 94 | | }); |
| | | 95 | | |
| | 0 | 96 | | progresser.Subscribe(sink); |
| | | 97 | | try |
| | | 98 | | { |
| | 0 | 99 | | await operation().ConfigureAwait(false); |
| | 0 | 100 | | } |
| | | 101 | | finally |
| | | 102 | | { |
| | 0 | 103 | | progresser.Unsubscribe(sink); |
| | | 104 | | } |
| | 0 | 105 | | } |
| | | 106 | | } |
| | | 107 | | |