Skip to content

Instantly share code, notes, and snippets.

@JoeM-RP
Last active August 15, 2018 04:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoeM-RP/fa40a711a7e012e30ea6200dd3fda62e to your computer and use it in GitHub Desktop.
Save JoeM-RP/fa40a711a7e012e30ea6200dd3fda62e to your computer and use it in GitHub Desktop.
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