public AsynchronousViewModel() { // Notifier of network connecitng status/count var connect = new CountNotifier(); // Notifier of network progress report var progress = new ScheduledNotifier<Tuple<long, long>>(); // current, total // skip initialValue on subscribe SearchTerm = new ReactiveProperty<string>(mode: ReactivePropertyMode.DistinctUntilChanged); // Search asynchronous & result direct bind // if network error, use OnErroRetry // that catch exception and do action and resubscript. SearchResults = SearchTerm .Select(async term => { using (connect.Increment()) // network open { return await WikipediaModel.SearchTermAsync(term, progress); } }) .Switch() // flatten .OnErrorRetry((HttpRequestException ex) => ProgressStatus.Value = "error occured") .ToReactiveProperty(); // CountChangedStatus : Increment(network open), Decrement(network close), Empty(all complete) SearchingStatus = connect .Select(x => (x != CountChangedStatus.Empty) ? "loading..." : "complete") .ToReactiveProperty(); ProgressStatus = progress .Select(x => string.Format("{0}/{1} {2}%", x.Item1, x.Item2, ((double)x.Item1 / x.Item2) * 100)) .ToReactiveProperty(); }
public AsynchronousViewModel() { // Notifier of network connecitng status/count var connect = new CountNotifier(); // Notifier of network progress report var progress = new ScheduledNotifier <Tuple <long, long> >(); // current, total // skip initialValue on subscribe SearchTerm = new ReactiveProperty <string>(mode: ReactivePropertyMode.DistinctUntilChanged); // Search asynchronous & result direct bind // if network error, use OnErroRetry // that catch exception and do action and resubscript. SearchResults = SearchTerm .Select(async term => { using (connect.Increment()) // network open { return(await WikipediaModel.SearchTermAsync(term, progress)); } }) .Switch() // flatten .OnErrorRetry((HttpRequestException ex) => ProgressStatus.Value = "error occured") .ToReactiveProperty(); // CountChangedStatus : Increment(network open), Decrement(network close), Empty(all complete) SearchingStatus = connect .Select(x => (x != CountChangedStatus.Empty) ? "loading..." : "complete") .ToReactiveProperty(); ProgressStatus = progress .Select(x => string.Format("{0}/{1} {2}%", x.Item1, x.Item2, ((double)x.Item1 / x.Item2) * 100)) .ToReactiveProperty(); }
public BooleanNotifierViewModel() { ToggleCommand.Subscribe(BooleanNotifier.SwitchValue).AddTo(DisposeCollection); ONCommand.Subscribe(BooleanNotifier.TurnOn).AddTo(DisposeCollection); OFFCommand.Subscribe(BooleanNotifier.TurnOff).AddTo(DisposeCollection); BooleanNotifier.Subscribe(_ => CountNotifier.Increment()).AddTo(DisposeCollection); }
public void Test() { var notifier = new CountNotifier(10); var recorder = new TestScheduler().CreateObserver <CountChangedStatus>(); notifier.Subscribe(recorder); notifier.Max.Is(10); notifier.Count.Is(0); notifier.Increment(3); notifier.Increment(3); notifier.Increment(3); notifier.Increment(); notifier.Decrement(5); notifier.Decrement(5); recorder.Messages.Is( OnNext(0, CountChangedStatus.Increment), OnNext(0, CountChangedStatus.Increment), OnNext(0, CountChangedStatus.Increment), OnNext(0, CountChangedStatus.Increment), OnNext(0, CountChangedStatus.Max), OnNext(0, CountChangedStatus.Decrement), OnNext(0, CountChangedStatus.Decrement), OnNext(0, CountChangedStatus.Empty)); // over max notifier.Increment(10); notifier.Count.Is(10); recorder.Messages.Clear(); notifier.Increment(); notifier.Count.Is(10); recorder.Messages.Count.Is(0); // under zero notifier.Decrement(10); notifier.Count.Is(0); recorder.Messages.Clear(); notifier.Decrement(); notifier.Count.Is(0); recorder.Messages.Count.Is(0); // over inc recorder.Messages.Clear(); notifier.Increment(5); notifier.Increment(15); notifier.Count.Is(10); recorder.Messages.Is( OnNext(0, CountChangedStatus.Increment), OnNext(0, CountChangedStatus.Increment), OnNext(0, CountChangedStatus.Max)); // over dec recorder.Messages.Clear(); notifier.Decrement(5); notifier.Decrement(15); notifier.Count.Is(0); recorder.Messages.Is( OnNext(0, CountChangedStatus.Decrement), OnNext(0, CountChangedStatus.Decrement), OnNext(0, CountChangedStatus.Empty)); AssertEx.Throws <ArgumentException>(() => notifier.Increment(-1)); AssertEx.Throws <ArgumentException>(() => notifier.Decrement(-1)); AssertEx.Throws <ArgumentException>(() => new CountNotifier(0)); AssertEx.DoesNotThrow(() => new CountNotifier(1)); }
public void Test() { var notifier = new CountNotifier(10); var recorder = new TestScheduler().CreateObserver<CountChangedStatus>(); notifier.Subscribe(recorder); notifier.Max.Is(10); notifier.Count.Is(0); notifier.Increment(3); notifier.Increment(3); notifier.Increment(3); notifier.Increment(); notifier.Decrement(5); notifier.Decrement(5); recorder.Messages.Is( OnNext(0, CountChangedStatus.Increment), OnNext(0, CountChangedStatus.Increment), OnNext(0, CountChangedStatus.Increment), OnNext(0, CountChangedStatus.Increment), OnNext(0, CountChangedStatus.Max), OnNext(0, CountChangedStatus.Decrement), OnNext(0, CountChangedStatus.Decrement), OnNext(0, CountChangedStatus.Empty)); // over max notifier.Increment(10); notifier.Count.Is(10); recorder.Messages.Clear(); notifier.Increment(); notifier.Count.Is(10); recorder.Messages.Count.Is(0); // under zero notifier.Decrement(10); notifier.Count.Is(0); recorder.Messages.Clear(); notifier.Decrement(); notifier.Count.Is(0); recorder.Messages.Count.Is(0); // over inc recorder.Messages.Clear(); notifier.Increment(5); notifier.Increment(15); notifier.Count.Is(10); recorder.Messages.Is( OnNext(0, CountChangedStatus.Increment), OnNext(0, CountChangedStatus.Increment), OnNext(0, CountChangedStatus.Max)); // over dec recorder.Messages.Clear(); notifier.Decrement(5); notifier.Decrement(15); notifier.Count.Is(0); recorder.Messages.Is( OnNext(0, CountChangedStatus.Decrement), OnNext(0, CountChangedStatus.Decrement), OnNext(0, CountChangedStatus.Empty)); AssertEx.Throws<ArgumentException>(() => notifier.Increment(-1)); AssertEx.Throws<ArgumentException>(() => notifier.Decrement(-1)); AssertEx.Throws<ArgumentException>(() => new CountNotifier(0)); AssertEx.DoesNotThrow(() => new CountNotifier(1)); }
public CountNotifierViewModel() { CountNotifierStatus = CountNotifier.Select(item => Enum.GetName(typeof(CountChangedStatus), item)).ToReactiveProperty().AddTo(DisposeCollection); CountNotifierCount = CountNotifier.Select(item => CountNotifier.Count).ToReactiveProperty().AddTo(DisposeCollection); CountNotifier .ObserveOnUIDispatcher() .Where(item => CountChangedStatus.Max.Equals(item)) .Where(_ => MessageBoxResult.Yes.Equals(MessageBox.Show("Maxになりましたが元に戻しますか?", "確認", MessageBoxButton.YesNo))) .Subscribe(_ => beforeOperation.Dispose()) .AddTo(DisposeCollection); Decrement1Command.ObserveOnUIDispatcher().Subscribe(_ => CountNotifier.Decrement(1)).AddTo(DisposeCollection); Decrement10Command.ObserveOnUIDispatcher().Subscribe(_ => CountNotifier.Decrement(10)).AddTo(DisposeCollection); DecrementNCommand.ObserveOnUIDispatcher().Subscribe(_ => CountNotifier.Decrement(DecrementN.Value)).AddTo(DisposeCollection); Increment1Command.ObserveOnUIDispatcher().Subscribe(_ => beforeOperation = CountNotifier.Increment(1)).AddTo(DisposeCollection); Increment10Command.ObserveOnUIDispatcher().Subscribe(_ => beforeOperation = CountNotifier.Increment(10)).AddTo(DisposeCollection); IncrementNCommand.ObserveOnUIDispatcher().Subscribe(_ => beforeOperation = CountNotifier.Increment(IncrementN.Value)).AddTo(DisposeCollection); MaxCommand.ObserveOnUIDispatcher().Subscribe(_ => beforeOperation = CountNotifier.Increment(CountNotifier.Max)).AddTo(DisposeCollection); EmptyCommand.ObserveOnUIDispatcher().Subscribe(_ => CountNotifier.Decrement(CountNotifier.Max)).AddTo(DisposeCollection); }