dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
21.99k stars 1.72k forks source link

[Enhancement] ProgressBar should implement System.IProgress<float> #3360

Open vpenades opened 2 years ago

vpenades commented 2 years ago

Summary

ProgressBar reason of existence is for its progress to be updated as a task progresses. This is done by updating the double Progress property.

This means we have to pass the ProgressBar to the running task so the task can update the property, which means making the task by aware of the ProgressBar type.

API Changes

Simply let ProgressBar implement System.IProgress<double>

Optionally, it could also implement other types like System.IProgress<single> and System.IProgress<int>

e.g.

class ProgressBar : IProgress<double>, IProgress<float>, IProgress<int>
{
    void IProgress<double>.Progress(double progress) => SetProgressInternal(progress);
    void IProgress<float>.Progress(double progress) => SetProgressInternal(progress);
    void IProgress<int>.Progress(double progress) => SetProgressInternal(progress);

    void SetProgressInternal(double progress)
    {
       // not sure how UI threads work in MAUI, but it would be great this one would be
       // thread friendly, because it is expected to be called from running tasks...
       if ( !this.CheckAccess()) {  Dispatcher.Invoke( ()=> SetProgressInternal(progress) ); return; }

       this.Progress = progress;
    }
}

Intended Use Case

let's say we have an async method that reports the progress:

async Task SomeLongTask(IProgress<int> progressNotifier)
{
    for(int i=0; i < 100; ++i)
   {
       progressNotifier.Progress( i );
       await Task.Delay(100);
   }
}

// This async method could be called by passing a ProgressBar instance straight away.

var progressControl = new ProgressBar();

await SomeLongTask( progressControl );

Best of all, this could be done with async methods located in libraries that don't need to have knowledge of Maui types.

PD. Actually, it could have been great if Forms/WPF/Xamarin would also implement this in their respective controls...

ghost commented 2 years ago

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.