winget-cli

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

ApplyConfigurationSetProgressOutput.cs (3554B)


      1 // -----------------------------------------------------------------------------
      2 // <copyright file="ApplyConfigurationSetProgressOutput.cs" company="Microsoft Corporation">
      3 //     Copyright (c) Microsoft Corporation. Licensed under the MIT License.
      4 // </copyright>
      5 // -----------------------------------------------------------------------------
      6 
      7 namespace Microsoft.WinGet.Configuration.Engine.Helpers
      8 {
      9     using Microsoft.Management.Configuration;
     10     using Microsoft.WinGet.Common.Command;
     11     using Windows.Foundation;
     12 
     13     /// <summary>
     14     /// Helper to handle progress callbacks from ApplySetAsync.
     15     /// </summary>
     16     internal class ApplyConfigurationSetProgressOutput : ConfigurationSetProgressOutputBase<ApplyConfigurationSetResult, ConfigurationSetChangeData>
     17     {
     18         private bool isFirstProgress = true;
     19 
     20         /// <summary>
     21         /// Initializes a new instance of the <see cref="ApplyConfigurationSetProgressOutput"/> class.
     22         /// </summary>
     23         /// <param name="cmd">Command that outputs the messages.</param>
     24         /// <param name="activityId">The activity id of the progress bar.</param>
     25         /// <param name="activity">The activity.</param>
     26         /// <param name="inProgressMessage">The message in the progress bar.</param>
     27         /// <param name="completeMessage">The activity complete message.</param>
     28         /// <param name="totalUnitsExpected">Total of units expected.</param>
     29         public ApplyConfigurationSetProgressOutput(PowerShellCmdlet cmd, int activityId, string activity, string inProgressMessage, string completeMessage, int totalUnitsExpected)
     30             : base(cmd, activityId, activity, inProgressMessage, completeMessage, totalUnitsExpected)
     31         {
     32         }
     33 
     34         /// <inheritdoc/>
     35         public override void Progress(IAsyncOperationWithProgress<ApplyConfigurationSetResult, ConfigurationSetChangeData> operation, ConfigurationSetChangeData data)
     36         {
     37             if (this.isFirstProgress)
     38             {
     39                 this.HandleProgress(operation.GetResults());
     40             }
     41 
     42             switch (data.Change)
     43             {
     44                 case ConfigurationSetChangeEventType.UnitStateChanged:
     45                     this.HandleUnitProgress(data.Unit, data.UnitState);
     46                     break;
     47             }
     48         }
     49 
     50         /// <inheritdoc/>
     51         public override void HandleProgress(ApplyConfigurationSetResult result)
     52         {
     53             if (!this.isFirstProgress)
     54             {
     55                 this.isFirstProgress = false;
     56                 foreach (var unitResult in result.UnitResults)
     57                 {
     58                     this.HandleUnitProgress(unitResult.Unit, unitResult.State);
     59                 }
     60             }
     61         }
     62 
     63         private void HandleUnitProgress(ConfigurationUnit unit, ConfigurationUnitState state)
     64         {
     65             if (this.UnitsCompleted.Contains(unit.InstanceIdentifier))
     66             {
     67                 return;
     68             }
     69 
     70             switch (state)
     71             {
     72                 case ConfigurationUnitState.Pending:
     73                     // The unreported progress handler may send pending units, just ignore them
     74                     break;
     75                 case ConfigurationUnitState.InProgress:
     76                     break;
     77                 case ConfigurationUnitState.Completed:
     78                 case ConfigurationUnitState.Skipped:
     79                     this.CompleteUnit(unit);
     80                     break;
     81             }
     82         }
     83     }
     84 }