Exemplo n.º 1
0
        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 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);
        }