The power of Reactive Programming and CancellationTokens

public async Task<string> GetDataAsync(){if (Connectivity.NetworkAccess != NetworkAccess.Internet||Connectivity.ConnectionProfiles.All(x=>x!=ConnectionProfile.WiFi))return null;HttpResponseMessage response;using (var httpClient = new HttpClient()){response = await httpClient.GetAsync("https://medium.com/feed/@Medium");}response.EnsureSuccessStatusCode();return await response.Content.ReadAsStringAsync();}
Observable.FromEventPattern<Xamarin.Essentials.ConnectivityChangedEventArgs>(handler => Xamarin.Essentials.Connectivity.ConnectivityChanged += handler, handler => Xamarin.Essentials.Connectivity.ConnectivityChanged -= handler).FirstAsync(x=>x.EventArgs.NetworkAccess!=NetworkAccess.Internet)
public static async Task<string> GetDataAsync(CancellationToken cancellationToken){if (cancellationToken.IsCancellationRequested)return null;HttpResponseMessage response;using (var httpClient = new HttpClient()){response = await httpClient.GetAsync("https://medium.com/feed/@Medium", cancellationToken);}response.EnsureSuccessStatusCode();return await response.Content.ReadAsStringAsync();}
var cancellationTokenSource = Observable.FromEventPattern<ConnectivityChangedEventArgs>(handler => Connectivity.ConnectivityChanged += handler,handler => Connectivity.ConnectivityChanged -= handler).FirstAsync(x => x.EventArgs.NetworkAccess != NetworkAccess.Internet||Connectivity.ConnectionProfiles.All(connectionProfile => connectionProfile != ConnectionProfile.WiFi)).ToCancellationTokenSource();using (cancellationTokenSource){var result = await FullState.GetDataAsync(cancellationTokenSource.Token);}

Rx Bonus (combine events)

Func<EventPattern<ConnectivityChangedEventArgs>, bool> connectivityPredicate = x => x.EventArgs.NetworkAccess != NetworkAccess.Internet||Connectivity.ConnectionProfiles.All(connectionProfile => connectionProfile != ConnectionProfile.WiFi);Func<EventPattern<EnergySaverStatusChangedEventArgs>, bool> batteryPredicate = x => x.EventArgs.EnergySaverStatus == EnergySaverStatus.On;var connectivityObservable = Observable.FromEventPattern<ConnectivityChangedEventArgs>(handler => Connectivity.ConnectivityChanged += handler,handler => Connectivity.ConnectivityChanged -= handler);var batteryObservable = Observable.FromEventPattern<EnergySaverStatusChangedEventArgs>(handler => Battery.EnergySaverStatusChanged += handler,handler => Battery.EnergySaverStatusChanged -= handler);var cancellationTokenSource = connectivityObservable.CombineLatest(batteryObservable,(connectivityArgs, batteryArgs) => (connectivityArgs, batteryArgs)).FirstAsync(x=>connectivityPredicate(x.connectivityArgs)|| batteryPredicate(x.batteryArgs)).ToCancellationTokenSource();

--

--

--

Hello, I’m Ahmed. I’m a software engineer living in Vienna, Austria. I am a fan of technology, web development, and programming. I’m also interested in xamarin

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ahmed Fouad

Ahmed Fouad

Hello, I’m Ahmed. I’m a software engineer living in Vienna, Austria. I am a fan of technology, web development, and programming. I’m also interested in xamarin

More from Medium

Convert Enum To Object or Class in c#

Unit Testing in C#

C# Interface and why Interface

Is it possible to gain much more clean code with the Linq? /Linq ile daha temiz kod mümkün mü?)