Advertisement
Guest User

Untitled

a guest
Aug 21st, 2014
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.31 KB | None | 0 0
  1. public class SearchViewModel : ViewModelBase
  2. {
  3. private string _searchTerm;
  4. private ReactiveList<string> _searchResults = new ReactiveList<string>();
  5.  
  6. public SearchViewModel()
  7. {
  8. ExecuteSearch = ReactiveCommand.CreateAsyncTask((o, ct) => DoSearchAsync(ct));
  9. this.ObservableForProperty(n => n.SearchTerm, (term) =>
  10. {
  11. return !String.IsNullOrWhiteSpace(term) && term.Length > 3;
  12. }).DistinctUntilChanged().Throttle(TimeSpan.FromMilliseconds(300),RxApp.MainThreadScheduler).Subscribe(
  13. b => ExecuteSearch.Execute(b));
  14. ExecuteSearch.Subscribe(list =>
  15. {
  16. _searchResults.Clear();
  17. _searchResults = list;
  18. NotifyOfPropertyChange(() => SearchResults);
  19. });
  20. }
  21.  
  22. public ReactiveList<string> SearchResults
  23. {
  24. get { return _searchResults; }
  25. }
  26.  
  27.  
  28. private async Task<ReactiveList<string>> DoSearchAsync(CancellationToken ct)
  29. {
  30. //throw new NotImplementedException();
  31. return new ReactiveList<string> { "a", "b", "c" };
  32. }
  33.  
  34.  
  35. public IReactiveCommand<ReactiveList<string>> ExecuteSearch { get; protected set; }
  36.  
  37. public string SearchTerm
  38. {
  39. get { return _searchTerm; }
  40. set { this.RaiseAndSetIfChanged(ref _searchTerm, value); }
  41. }
  42. }
  43.  
  44.  
  45.  
  46.  
  47. ///////////////TEST
  48. public class SearchViewModelTests :TestBase
  49. {
  50. //private SearchViewModel _viewModel;
  51.  
  52. //[TestInitialize]
  53. //public void Setup()
  54. //{
  55. // _viewModel = new SearchViewModel();
  56. //}
  57.  
  58. [TestMethod]
  59. public void SearchIs()
  60. {
  61. (new TestScheduler()).With(s =>
  62. {
  63. var _viewModel = getViewModel();
  64. _viewModel.SearchTerm = "q123wer";
  65. Assert.AreEqual(0, _viewModel.SearchResults.Count);
  66. s.AdvanceByMs(310);
  67. Assert.AreEqual(3, _viewModel.SearchResults.Count);
  68. });
  69. }
  70.  
  71. private SearchViewModel getViewModel()
  72. {
  73. return MinicabsterKernel.Instance.Get<SearchViewModel>();
  74. }
  75.  
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement