Guest User

ViewModel

a guest
May 12th, 2017
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.99 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using NoContact.Interfaces;
  7. using NoContact.Helpers;
  8. using System.Windows.Input;
  9. using NoContact.ViewModels.Base;
  10. using NoContact.Views;
  11. using System.Collections.ObjectModel;
  12. using System.Windows;
  13. using System.Windows.Controls;
  14. using GalaSoft.MvvmLight.Command;
  15. using System.Reactive.Linq;
  16. using Reactive.Bindings;
  17.  
  18. namespace NoContact.ViewModels
  19. {
  20. public class PhoneWindowViewModel : BindableBase, IPhoneWindowViewModel
  21. {
  22. private IWindowManager _windowManager;
  23. private IViewFactory _viewFactory;
  24.  
  25. private ObservableCollection<IReminderBoxViewModel> _reminders;
  26.  
  27. public PhoneWindowViewModel(IWindowManager windowManager, IViewFactory viewFactory)
  28. {
  29. _windowManager = windowManager;
  30. _viewFactory = viewFactory;
  31.  
  32. _reminders = new ObservableCollection<IReminderBoxViewModel>();
  33.  
  34. SearchText = new ReactiveProperty<string>();
  35. ItemsSource = new ReactiveCollection<ReminderBoxViewModel>();
  36.  
  37. IObservable<IEnumerable<ReminderBoxViewModel>> searchedStrings = SearchText.
  38. Where(x => !string.IsNullOrEmpty(x)).
  39. Throttle(TimeSpan.FromSeconds(1)).
  40. DistinctUntilChanged().
  41. Select(SearchForText).
  42. Switch();
  43.  
  44. searchedStrings.Subscribe(x => UpdateUI(x));
  45.  
  46. SearchText.Value = "momma";
  47. }
  48.  
  49. public ICommand ReminderBoxCloseCommand { get { return new RelayCommand<ReminderBoxViewModel>(CloseReminderBox); } }
  50. public ICommand AddReminderCommand { get { return new RelayCommand(AddReminder, () => true); } }
  51.  
  52. private ReactiveCollection<ReminderBoxViewModel> _itemsSource;
  53. public ReactiveCollection<ReminderBoxViewModel> ItemsSource
  54. {
  55. get { return _itemsSource; }
  56. set
  57. {
  58. _itemsSource = value;
  59. OnPropertyChanged();
  60. }
  61. }
  62.  
  63. private ReactiveProperty<string> _searchText;
  64. public ReactiveProperty<string> SearchText
  65. {
  66. get { return _searchText; }
  67. set
  68. {
  69. _searchText = value;
  70. OnPropertyChanged();
  71. MessageBox.Show(_searchText.Value);
  72. }
  73. }
  74.  
  75. public ObservableCollection<IReminderBoxViewModel> Reminders
  76. {
  77. get { return _reminders; }
  78. set
  79. {
  80. _reminders = value;
  81. OnPropertyChanged();
  82. }
  83. }
  84.  
  85. /// <summary>
  86. /// Delegates closing the window, associated with
  87. /// this ViewModel instance.
  88. /// </summary>
  89. private void CloseWindow()
  90. {
  91. _windowManager.CloseWindow(this);
  92. }
  93.  
  94. /// <summary>
  95. /// "Closes" the Reminder Box by deleting it from the Collection
  96. /// </summary>
  97. private void CloseReminderBox(IReminderBoxViewModel viewModel)
  98. {
  99. var index = Reminders.IndexOf(viewModel);
  100. Reminders.Remove(viewModel);
  101. }
  102.  
  103. /// <summary>
  104. /// Adds the reminder to the Collection by resolving it in the
  105. /// abstract typed factory facility.
  106. /// </summary>
  107. private void AddReminder()
  108. {
  109. var viewModel = _viewFactory.Create<IReminderBoxViewModel>();
  110. Reminders.Add(viewModel);
  111.  
  112. _viewFactory.Release(viewModel);
  113. }
  114.  
  115. private async Task<IEnumerable<ReminderBoxViewModel>> SearchForText(string searchedText)
  116. {
  117. return await Task.FromResult(Enumerable.Range(1, 100).Select(x => new ReminderBoxViewModel()));
  118. }
  119.  
  120. private void UpdateUI(IEnumerable<ReminderBoxViewModel> viewModelCollection)
  121. {
  122. ItemsSource.AddRangeOnScheduler(viewModelCollection);
  123. }
  124. }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment