-
-
Save JoeM-RP/fa40a711a7e012e30ea6200dd3fda62e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.ObjectModel; | |
using System.Diagnostics; | |
using System.Reactive; | |
using System.Reactive.Disposables; | |
using System.Reactive.Linq; | |
using System.Threading.Tasks; | |
using ReactiveUI; | |
using RxUIForms.Models; | |
using Xamvvm; | |
namespace RxUIForms.ViewModels | |
{ | |
/* Omitted */ | |
public class HomePageViewModel : BasePageViewModel | |
{ | |
private async Task ShowAlert() | |
{ | |
// Natrually, this is silly, but it demonstrates how to use Merged Observables to | |
// handle errors elegantly. | |
try | |
{ | |
throw new NotImplementedException("This is a fake error to demonstrate the alert"); | |
} | |
catch(Exception ex) | |
{ | |
await Observable.Throw<Unit>(ex); | |
} | |
} | |
/* Omitted */ | |
private void Initialize() | |
{ | |
// Error Handling | |
Observable.Merge(this.ThrownExceptions, SelectItemCommand.ThrownExceptions, ShowAlertCommand.ThrownExceptions, ShowActionSheetCommand.ThrownExceptions) | |
.Throttle(TimeSpan.FromMilliseconds(250), RxApp.MainThreadScheduler) | |
.Do((obj) => Debug.WriteLine($"[{obj.Source}] Error = {obj}")) | |
.Subscribe(async ex => | |
{ | |
var response = await Interactions.Errors.Handle(ex); | |
if (response == ErrorRecoveryOption.Retry) | |
{ | |
// TODO: If response is true, the user wants to "retry" the action. | |
// Do something here to attempt the action again | |
} | |
else | |
{ | |
// TODO: If response is false, the user has canceled the attempted action | |
// So perform an action here that makes sense; we may want to naviagte to | |
// a previous page or just sit idle | |
} | |
}).DisposeWith(SubscriptionDisposables); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment