Created
December 3, 2014 22:20
-
-
Save ghuntley/7a82e198acfc508ea1f0 to your computer and use it in GitHub Desktop.
reactiveui
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
public SearchViewModel(ISearchService searchService = null) : ReactiveObject, IRoutableViewHost | |
{ | |
SearchService = searchService ?? Locator.Current.GetService<ISearchService>(); | |
// Here we're describing here, in a *declarative way*, the conditions in | |
// which the Search command is enabled. Now our Command IsEnabled is | |
// perfectly efficient, because we're only updating the UI in the scenario | |
// when it should change. | |
var canSearch = this.WhenAny(x => x.SearchQuery, x => !String.IsNullOrWhiteSpace(x.Value)); | |
// ReactiveCommand has built-in support for background operations and | |
// guarantees that this block will only run exactly once at a time, and | |
// that the CanExecute will auto-disable and that property IsExecuting will | |
// be set according whilst it is running. | |
Subscribe = ReactiveCommand.CreateAsyncTask(canSearch, async _ => { | |
return await searchService.Search(this.SearchQuery); | |
}); | |
// ReactiveCommands are themselves IObservables, whose value are the results | |
// from the async method, guaranteed to arrive on the UI thread. We're going | |
// to take the list of search results that the background operation loaded, | |
// and them into our SearchResults. | |
Search.Subscribe(results => { | |
SearchResults.Clear(); | |
SearchResults.AddRange(results); | |
}); | |
// ThrownExceptions is any exception thrown from the CreateAsyncTask piped | |
// to this Observable. Subscribing to this allows you to handle errors on | |
// the UI thread. | |
Subscribe.ThrownExceptions | |
.Subscribe(ex => { | |
UserError.Throw("Potential Network Connectivity Error", ex); | |
}); | |
// Whenever the Search query changes, we're going to wait for one second | |
// of "dead airtime", then automatically invoke the subscribe command. | |
this.WhenAnyValue(x => x.SearchQuery) | |
.Throttle(TimeSpan.FromSeconds(1), RxApp.MainThreadScheduler) | |
.InvokeCommand(this, x => x.SearchService); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment