| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="ProgressManager.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Collections.Generic; |
| | | 9 | | |
| | | 10 | | namespace MyNet.Utilities.Progress; |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// Static ambient-context facade that provides application-wide access to a single <see cref="IProgresser"/> instance. |
| | | 14 | | /// </summary> |
| | | 15 | | /// <remarks> |
| | | 16 | | /// <para> |
| | | 17 | | /// This class implements the <em>ambient context</em> pattern and should be used as a last resort |
| | | 18 | | /// when dependency injection is not available (e.g. in static helpers or deeply nested call stacks). |
| | | 19 | | /// In all other situations prefer injecting <see cref="IProgresser"/> directly and calling the |
| | | 20 | | /// extension methods provided by <see cref="ProgresserExtensions"/>. |
| | | 21 | | /// </para> |
| | | 22 | | /// <para> |
| | | 23 | | /// Call <see cref="Initialize"/> once at application startup before using any other member. |
| | | 24 | | /// All methods return <see langword="null"/> when no progresser has been registered or when |
| | | 25 | | /// StartStep variants are called outside an active session. |
| | | 26 | | /// </para> |
| | | 27 | | /// </remarks> |
| | | 28 | | public static class ProgressManager |
| | | 29 | | { |
| | | 30 | | private static IProgresser? _progresser; |
| | | 31 | | |
| | | 32 | | // ── Initialization ─────────────────────────────────────────────────────── |
| | | 33 | | |
| | | 34 | | /// <summary>Registers the global <see cref="IProgresser"/> instance to use for progress tracking.</summary> |
| | | 35 | | /// <param name="progresser">The progresser to register.</param> |
| | 12 | 36 | | public static void Initialize(IProgresser progresser) => _progresser = progresser; |
| | | 37 | | |
| | | 38 | | // ── Begin (root step) ──────────────────────────────────────────────────── |
| | | 39 | | |
| | | 40 | | /// <summary>Starts a new single-step session with an optional message.</summary> |
| | | 41 | | public static IProgressStep<ProgressMessage>? Begin(string message = "", params object[] parameters) |
| | 6 | 42 | | => _progresser?.Begin(message, parameters); |
| | | 43 | | |
| | | 44 | | /// <summary>Starts a new session divided into <paramref name="numberOfSteps"/> equal sub-steps.</summary> |
| | | 45 | | public static IProgressStep<ProgressMessage>? Begin(int numberOfSteps, string message = "", params object[] paramete |
| | 3 | 46 | | => _progresser?.Begin(numberOfSteps, message, parameters); |
| | | 47 | | |
| | | 48 | | /// <summary>Starts a new session with custom sub-step weightings.</summary> |
| | | 49 | | public static IProgressStep<ProgressMessage>? Begin(IEnumerable<double> subStepWeightings, string message = "", para |
| | 3 | 50 | | => _progresser?.Begin(subStepWeightings, message, parameters); |
| | | 51 | | |
| | | 52 | | /// <summary>Starts a new cancellable session with an optional message.</summary> |
| | | 53 | | public static IProgressStep<ProgressMessage>? Begin(Action cancelAction, string message = "", params object[] parame |
| | 3 | 54 | | => _progresser?.Begin(cancelAction, message, parameters); |
| | | 55 | | |
| | | 56 | | /// <summary>Starts a new cancellable session divided into <paramref name="numberOfSteps"/> equal sub-steps.</summar |
| | | 57 | | public static IProgressStep<ProgressMessage>? Begin(int numberOfSteps, Action cancelAction, string message = "", par |
| | 3 | 58 | | => _progresser?.Begin(numberOfSteps, cancelAction, message, parameters); |
| | | 59 | | |
| | | 60 | | /// <summary>Starts a new cancellable session with custom sub-step weightings.</summary> |
| | | 61 | | public static IProgressStep<ProgressMessage>? Begin(IEnumerable<double> subStepWeightings, Action cancelAction, stri |
| | 3 | 62 | | => _progresser?.Begin(subStepWeightings, cancelAction, message, parameters); |
| | | 63 | | |
| | | 64 | | // ── StartStep (child step) ─────────────────────────────────────────────── |
| | | 65 | | |
| | | 66 | | /// <summary> |
| | | 67 | | /// Pushes a cancellable child step onto the current session. |
| | | 68 | | /// Returns <see langword="null"/> when no progresser is registered or no session is active. |
| | | 69 | | /// </summary> |
| | | 70 | | public static IProgressStep<ProgressMessage>? StartStep(string message = "", params object[] parameters) |
| | 6 | 71 | | => TryStartStep(() => _progresser!.StartStep(message, parameters)); |
| | | 72 | | |
| | | 73 | | /// <summary> |
| | | 74 | | /// Pushes a cancellable child step divided into <paramref name="numberOfSteps"/> equal sub-steps. |
| | | 75 | | /// Returns <see langword="null"/> when no progresser is registered or no session is active. |
| | | 76 | | /// </summary> |
| | | 77 | | public static IProgressStep<ProgressMessage>? StartStep(int numberOfSteps, string message = "", params object[] para |
| | 0 | 78 | | => TryStartStep(() => _progresser!.StartStep(numberOfSteps, message, parameters)); |
| | | 79 | | |
| | | 80 | | /// <summary> |
| | | 81 | | /// Pushes a cancellable child step with custom sub-step weightings. |
| | | 82 | | /// Returns <see langword="null"/> when no progresser is registered or no session is active. |
| | | 83 | | /// </summary> |
| | | 84 | | public static IProgressStep<ProgressMessage>? StartStep(IEnumerable<double> subStepWeightings, string message = "", |
| | 0 | 85 | | => TryStartStep(() => _progresser!.StartStep(subStepWeightings, message, parameters)); |
| | | 86 | | |
| | | 87 | | /// <summary> |
| | | 88 | | /// Pushes a non-cancellable child step onto the current session. |
| | | 89 | | /// Returns <see langword="null"/> when no progresser is registered or no session is active. |
| | | 90 | | /// </summary> |
| | | 91 | | public static IProgressStep<ProgressMessage>? StartStepUncancellable(string message = "", params object[] parameters |
| | 3 | 92 | | => TryStartStep(() => _progresser!.StartStepUncancellable(message, parameters)); |
| | | 93 | | |
| | | 94 | | /// <summary> |
| | | 95 | | /// Pushes a non-cancellable child step divided into <paramref name="numberOfSteps"/> equal sub-steps. |
| | | 96 | | /// Returns <see langword="null"/> when no progresser is registered or no session is active. |
| | | 97 | | /// </summary> |
| | | 98 | | public static IProgressStep<ProgressMessage>? StartStepUncancellable(int numberOfSteps, string message = "", params |
| | 3 | 99 | | => TryStartStep(() => _progresser!.StartStepUncancellable(numberOfSteps, message, parameters)); |
| | | 100 | | |
| | | 101 | | /// <summary> |
| | | 102 | | /// Pushes a non-cancellable child step with custom sub-step weightings. |
| | | 103 | | /// Returns <see langword="null"/> when no progresser is registered or no session is active. |
| | | 104 | | /// </summary> |
| | | 105 | | public static IProgressStep<ProgressMessage>? StartStepUncancellable(IEnumerable<double> subStepWeightings, string m |
| | 3 | 106 | | => TryStartStep(() => _progresser!.StartStepUncancellable(subStepWeightings, message, parameters)); |
| | | 107 | | |
| | | 108 | | // ── Private helpers ─────────────────────────────────────────────────────── |
| | | 109 | | |
| | | 110 | | /// <summary> |
| | | 111 | | /// Invokes <paramref name="factory"/> and returns its result. |
| | | 112 | | /// Returns <see langword="null"/> when no progresser is registered or when the factory throws an |
| | | 113 | | /// <see cref="InvalidOperationException"/> (i.e. <c>StartStep</c> was called without a prior <c>Begin</c>). |
| | | 114 | | /// </summary> |
| | | 115 | | private static IProgressStep<ProgressMessage>? TryStartStep(Func<IProgressStep<ProgressMessage>> factory) |
| | | 116 | | { |
| | 15 | 117 | | if (_progresser is null) return null; |
| | | 118 | | try |
| | | 119 | | { |
| | 15 | 120 | | return factory(); |
| | | 121 | | } |
| | 3 | 122 | | catch (InvalidOperationException) |
| | | 123 | | { |
| | | 124 | | // No active session: StartStep was called without a prior Begin(). |
| | | 125 | | // This is accepted as a no-op in the static facade to simplify caller code. |
| | 3 | 126 | | return null; |
| | | 127 | | } |
| | 15 | 128 | | } |
| | | 129 | | } |
| | | 130 | | |