Skip to content

Commit

Permalink
[Sample App] Add Microsoft.Extensions.Http.Resilience (#1643)
Browse files Browse the repository at this point in the history
Add Microsoft.Extensions.Http.Resilience
  • Loading branch information
TheCodeTraveler authored Jan 14, 2024
1 parent 02896d3 commit 11e7122
Show file tree
Hide file tree
Showing 10 changed files with 25 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@

<PackageReference Include="CommunityToolkit.Maui.Markup" Version="4.0.0" />
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.2" />
<PackageReference Include="Microsoft.Extensions.Http.Polly" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Http.Resilience" Version="8.1.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="8.0.0" />
</ItemGroup>

Expand Down
16 changes: 13 additions & 3 deletions samples/CommunityToolkit.Maui.Sample/MauiProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
using CommunityToolkit.Maui.Sample.ViewModels.Views.AvatarView;
using CommunityToolkit.Maui.Sample.Views.Popups;
using CommunityToolkit.Maui.Storage;
using Microsoft.Extensions.Http.Resilience;
using Microsoft.Extensions.Logging;
using Polly;

Expand Down Expand Up @@ -57,7 +58,7 @@ public static MauiApp CreateMauiApp()
});

builder.Services.AddHttpClient<ByteArrayToImageSourceConverterViewModel>()
.AddTransientHttpErrorPolicy(policyBuilder => policyBuilder.WaitAndRetryAsync(3, SleepDurationProvider));
.AddStandardResilienceHandler(options => options.Retry = new MobileHttpRetryStrategyOptions());

builder.Services.AddSingleton<PopupSizeConstants>();

Expand All @@ -69,8 +70,6 @@ public static MauiApp CreateMauiApp()
#endif

return builder.Build();

static TimeSpan SleepDurationProvider(int attemptNumber) => TimeSpan.FromSeconds(Math.Pow(2, attemptNumber));
}

static void RegisterViewsAndViewModels(in IServiceCollection services)
Expand Down Expand Up @@ -216,4 +215,15 @@ static IServiceCollection AddTransientWithShellRoute<TPage, TViewModel>(this ISe
{
return services.AddTransientWithShellRoute<TPage, TViewModel>(AppShell.GetPageRoute<TViewModel>());
}

sealed class MobileHttpRetryStrategyOptions : HttpRetryStrategyOptions
{
public MobileHttpRetryStrategyOptions()
{
BackoffType = DelayBackoffType.Exponential;
MaxRetryAttempts = 3;
UseJitter = true;
Delay = TimeSpan.FromSeconds(2);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public partial class IconTintColorBehaviorViewModel : BaseViewModel

static readonly IEnumerator<Color?> toggleableColorsEnumerator = new List<Color?> { Colors.Red, Colors.Green, null }.GetEnumerator();

[ObservableProperty]
[ObservableProperty]
string toggleableImageSource = shieldImageFileName;

[ObservableProperty]
Expand All @@ -36,6 +36,6 @@ void ChangeColor()
toggleableColorsEnumerator.MoveNext();
}

ToggleableIconTintColor = toggleableColorsEnumerator.Current;
ToggleableIconTintColor = toggleableColorsEnumerator.Current;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Task InternalStartListeningAsync(CultureInfo culture, CancellationToken cancella

audioEngine = new AVAudioEngine();
liveSpeechRequest = new SFSpeechAudioBufferRecognitionRequest();

InitializeAvAudioSession(out _);

var node = audioEngine.InputNode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Task InternalStartListeningAsync(CultureInfo culture, CancellationToken cancella

audioEngine = new AVAudioEngine();
liveSpeechRequest = new SFSpeechAudioBufferRecognitionRequest();

InitializeAvAudioSession(out var audioSession);

var mode = audioSession.AvailableModes.Contains(AVAudioSession.ModeMeasurement)
Expand Down Expand Up @@ -52,7 +52,7 @@ Task InternalStartListeningAsync(CultureInfo culture, CancellationToken cancella
{
throw new Exception(error.LocalizedDescription);
}

cancellationToken.ThrowIfCancellationRequested();

var currentIndex = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public static ValueTask<Stream> GetImageStream(IList<PointF> points, Size imageS
{
throw new InvalidOperationException("Unable to generate image. No Lines Found");
}

return GetUIImage(points, (context, offset) =>
{
foreach (var line in lines)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public override void ViewDidDisappear(bool animated)
overlayView.RemoveFromSuperview();
overlayView.Dispose();
}

base.ViewDidDisappear(animated);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ or PlaybackStateCompat.StateSkippingToQueueItem
if (playbackState is IPlayer.StateReady)
{
MediaElement.Duration = TimeSpan.FromMilliseconds(Player.Duration < 0 ? 0 : Player.Duration);
MediaElement.Position = TimeSpan.FromMilliseconds(Player.CurrentPosition < 0 ? 0: Player.CurrentPosition);
MediaElement.Position = TimeSpan.FromMilliseconds(Player.CurrentPosition < 0 ? 0 : Player.CurrentPosition);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public class AnimationBehavior : EventToCommandBehavior
/// Gets the Command that allows the triggering of the animation.
/// </summary>
/// <remarks>
/// <see cref="AnimateCommand"/> has a <see cref="Type"/> of Command&lt;CancellationToken&gt; which requires a <see cref="CancellationToken"/> as a CommandParameter. See <see cref="Command{CancellationToken}"/> and <see cref="System.Windows.Input.ICommand.Execute(object)"/> for more information on passing a <see cref="CancellationToken"/> into <see cref="Command{T}"/> as a CommandParameter"
/// </remarks>
/// <see cref="AnimateCommand"/> has a <see cref="Type"/> of Command&lt;CancellationToken&gt; which requires a <see cref="CancellationToken"/> as a CommandParameter. See <see cref="Command{CancellationToken}"/> and <see cref="System.Windows.Input.ICommand.Execute(object)"/> for more information on passing a <see cref="CancellationToken"/> into <see cref="Command{T}"/> as a CommandParameter"
/// </remarks>
public Command<CancellationToken> AnimateCommand => (Command<CancellationToken>)GetValue(AnimateCommandProperty);

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/CommunityToolkit.Maui/Views/Popup/Popup.shared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ public IList<string> StyleClass

/// <inheritdoc/>
TaskCompletionSource IAsynchronousHandler.HandlerCompleteTCS => popupDismissedTaskCompletionSource;

/// <inheritdoc/>
bool IResourcesProvider.IsResourcesCreated => resources is not null;

Expand Down

0 comments on commit 11e7122

Please sign in to comment.