public void Initialize <TState>(ReduxStore <TState> store) where TState : class, new()
        {
            if (store.TimeTravelEnabled)
            {
                // TODO : Cannot activate History component
            }

            // Observe UI events
            UndoButton.Events().Click
            .Subscribe(_ => store.Undo());
            RedoButton.Events().Click
            .Subscribe(_ => store.Redo());
            ResetButton.Events().Click
            .Subscribe(_ => store.Reset());

            PlayPauseButton.Events().Click
            .Subscribe(_ => _internalStore.Dispatch(new TogglePlayPauseAction()));

            Slider.Events().ValueChanged
            .Where(_ => Slider.FocusState != FocusState.Unfocused)
            .Subscribe(e =>
            {
                int newPosition = (int)e.NewValue;
                _internalStore.Dispatch(new MoveToPositionAction {
                    Position = newPosition
                });
            });

            // Observe changes on internal state
            _internalStore.Select(state => state.MaxPosition)
            .Subscribe(maxPosition =>
            {
                Slider.Maximum = maxPosition;
            });

            Observable.CombineLatest(
                _internalStore.Select(state => state.CurrentPosition),
                _internalStore.Select(state => state.PlaySessionActive),
                _internalStore.Select(state => state.MaxPosition),
                store.ObserveCanUndo(),
                store.ObserveCanRedo(),
                Tuple.Create
                )
            .ObserveOnDispatcher()
            .Subscribe(x =>
            {
                var(currentPosition, playSessionActive, maxPosition, canUndo, canRedo) = x;

                Slider.Value = currentPosition;

                if (playSessionActive)
                {
                    UndoButton.IsEnabled      = false;
                    RedoButton.IsEnabled      = false;
                    ResetButton.IsEnabled     = false;
                    PlayPauseButton.IsEnabled = true;

                    Slider.IsEnabled = false;

                    PlayPauseButton.Content = "\xE769";
                }
                else
                {
                    UndoButton.IsEnabled      = canUndo;
                    RedoButton.IsEnabled      = canRedo;
                    ResetButton.IsEnabled     = canUndo || canRedo;
                    PlayPauseButton.IsEnabled = canRedo;

                    Slider.IsEnabled = maxPosition > 0;

                    PlayPauseButton.Content = "\xE768";
                }
            });

            _internalStore.ObserveAction <MoveToPositionAction>()
            .Subscribe(a =>
            {
                if (a.Position < _internalStore.State.CurrentPosition)
                {
                    for (int i = 0; i < _internalStore.State.CurrentPosition - a.Position; i++)
                    {
                        store.Undo();
                    }
                }
                if (a.Position > _internalStore.State.CurrentPosition)
                {
                    for (int i = 0; i < a.Position - _internalStore.State.CurrentPosition; i++)
                    {
                        store.Redo();
                    }
                }
            });

            // Observe changes on listened state
            var goForwardNormalActionOrigin = store.ObserveAction()
                                              .Select(action => new { Action = action, BreaksTimeline = true });
            var goForwardRedoneActionOrigin = store.ObserveAction(ActionOriginFilter.Redone)
                                              .Select(action => new { Action = action, BreaksTimeline = false });

            goForwardNormalActionOrigin.Merge(goForwardRedoneActionOrigin)
            .ObserveOnDispatcher()
            .Subscribe(x =>
            {
                _internalStore.Dispatch(new GoForwardAction {
                    Action = x.Action, BreaksTimeline = x.BreaksTimeline
                });
                if (_internalStore.State.PlaySessionActive && !store.CanRedo)
                {
                    _internalStore.Dispatch(new TogglePlayPauseAction());
                }
            });

            store.ObserveUndoneAction()
            .ObserveOnDispatcher()
            .Subscribe(_ => _internalStore.Dispatch(new GoBackAction()));

            store.ObserveReset()
            .ObserveOnDispatcher()
            .Subscribe(_ => _internalStore.Dispatch(new ResetAction()));

            _internalStore.Select(state => state.PlaySessionActive)
            .Select(playSessionActive =>
                    playSessionActive ? Observable.Interval(TimeSpan.FromSeconds(1)) : Observable.Empty <long>()
                    )
            .Switch()
            .ObserveOnDispatcher()
            .Subscribe(_ =>
            {
                bool canRedo = store.Redo();
                if (!canRedo)
                {
                    _internalStore.Dispatch(new TogglePlayPauseAction());
                }
            });

            // Track redux actions
            _internalStore.ObserveAction()
            .Subscribe(action =>
            {
                TrackReduxAction(action);
            });
        }
Esempio n. 2
0
        internal void Initialize <TState>(ReduxStore <TState> store) where TState : class, new()
        {
            // Observe UI events
            UndoButton.Events().Click
            .Subscribe(_ => store.Undo());
            RedoButton.Events().Click
            .Subscribe(_ => store.Redo());
            ResetButton.Events().Click
            .Subscribe(_ => store.Reset());

            PlayPauseButton.Events().Click
            .Subscribe(_ => _devToolsStore.Dispatch(new TogglePlayPauseAction()));

            Slider.Events().ValueChanged
            .Where(_ => Slider.FocusState != Windows.UI.Xaml.FocusState.Unfocused)
            .Select(e => (int)e.NewValue)
            .DistinctUntilChanged()
            .Subscribe(newPosition =>
            {
                _devToolsStore.Dispatch(new MoveToPositionAction {
                    Position = newPosition
                });
            });

            ReduxActionInfosListView.Events().ItemClick
            .Subscribe(e =>
            {
                int index = ReduxActionInfosListView.Items.IndexOf(e.ClickedItem);
                _devToolsStore.Dispatch(new SelectPositionAction {
                    Position = index
                });
            });

            // Observe changes on DevTools state
            Observable.CombineLatest(
                _devToolsStore.Select(SelectCurrentPosition),
                _devToolsStore.Select(SelectPlaySessionActive),
                _devToolsStore.Select(SelectMaxPosition),
                store.ObserveCanUndo(),
                store.ObserveCanRedo(),
                Tuple.Create
                )
            .Subscribe(x =>
            {
                var(currentPosition, playSessionActive, maxPosition, canUndo, canRedo) = x;

                Slider.Value   = currentPosition;
                Slider.Maximum = maxPosition;

                if (playSessionActive)
                {
                    UndoButton.IsEnabled      = false;
                    RedoButton.IsEnabled      = false;
                    ResetButton.IsEnabled     = false;
                    PlayPauseButton.IsEnabled = true;
                    Slider.IsEnabled          = false;
                    PlayPauseButton.Content   = "\xE769";
                }
                else
                {
                    UndoButton.IsEnabled      = canUndo;
                    RedoButton.IsEnabled      = canRedo;
                    ResetButton.IsEnabled     = canUndo || canRedo;
                    PlayPauseButton.IsEnabled = canRedo;
                    Slider.IsEnabled          = maxPosition > 0;
                    PlayPauseButton.Content   = "\xE768";
                }
            });

            _devToolsStore.Select(
                CombineSelectors(SelectCurrentActions, SelectSelectedActionPosition)
                )
            .Subscribe(x =>
            {
                var(actions, selectedPosition) = x;

                ReduxActionInfosListView.ItemsSource   = actions;
                ReduxActionInfosListView.SelectedIndex = Math.Clamp(selectedPosition, -1, actions.Count - 1);
            });

            _devToolsStore.Select(SelectSelectedReduxAction)
            .Subscribe(reduxActionOption =>
            {
                reduxActionOption.Match()
                .Some().Do(reduxAction =>
                {
                    var serializerSettings = new JsonSerializerSettings
                    {
                        ContractResolver      = SuccinctContractResolver.Instance,
                        ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
                        Formatting            = Formatting.Indented
                    };

                    SelectedReduxActionDataTextBlock.Text = JsonConvert.SerializeObject(
                        reduxAction.Data,
                        serializerSettings
                        );
                    SelectedStateTextBlock.Text = JsonConvert.SerializeObject(
                        reduxAction.NextState,
                        serializerSettings
                        );
                    SelectedDiffStateTextBlock.Text = "This feature will be available soon...";
                })
                .None().Do(() =>
                {
                    SelectedReduxActionDataTextBlock.Text = string.Empty;
                    SelectedStateTextBlock.Text           = string.Empty;
                    SelectedDiffStateTextBlock.Text       = string.Empty;
                })
                .Exec();
            });

            _devToolsStore.ObserveAction <MoveToPositionAction>()
            .WithLatestFrom(
                _devToolsStore.Select(SelectCurrentPosition),
                Tuple.Create
                )
            .Subscribe(x =>
            {
                var(action, currentPosition) = x;

                if (action.Position < currentPosition)
                {
                    for (int i = 0; i < currentPosition - action.Position; i++)
                    {
                        store.Undo();
                    }
                }
                if (action.Position > currentPosition)
                {
                    for (int i = 0; i < action.Position - currentPosition; i++)
                    {
                        store.Redo();
                    }
                }
            });

            _devToolsStore.Select(SelectPlaySessionActive)
            .Select(playSessionActive =>
                    playSessionActive ? Observable.Interval(TimeSpan.FromSeconds(1)) : Observable.Empty <long>()
                    )
            .Switch()
            .ObserveOnDispatcher()
            .Subscribe(_ =>
            {
                bool canRedo = store.Redo();
                if (!canRedo)
                {
                    _devToolsStore.Dispatch(new TogglePlayPauseAction());
                }
            });

            // Observe changes on listened state
            var storeHistoryAtInitialization = store.GetHistory();

            store.ObserveHistory()
            .StartWith(storeHistoryAtInitialization)
            .Subscribe(historyInfos =>
            {
                var mementosOrderedByDate = historyInfos.PreviousStates
                                            .OrderBy(reduxMemento => reduxMemento.Date)
                                            .ToList();

                // Set list of current actions
                // Set list of future (undone) actions
                _devToolsStore.Dispatch(new HistoryUpdated
                {
                    CurrentActions = mementosOrderedByDate
                                     .Select((reduxMemento, index) =>
                    {
                        int nextIndex = index + 1;
                        var nextState = nextIndex < mementosOrderedByDate.Count
                                    ? mementosOrderedByDate[nextIndex].PreviousState
                                    : store.State;

                        return(new ReduxActionInfo
                        {
                            Date = reduxMemento.Date,
                            Type = reduxMemento.Action.GetType(),
                            Data = reduxMemento.Action,
                            PreviousState = reduxMemento.PreviousState,
                            NextState = nextState
                        });
                    })
                                     .ToImmutableList(),
                    FutureActions = historyInfos.FutureActions
                                    .Select(action =>
                    {
                        return(new ReduxActionInfo
                        {
                            Type = action.GetType(),
                            Data = action
                        });
                    })
                                    .ToImmutableList()
                });
            });

            _devToolsStore.Dispatch(
                new SelectPositionAction {
                Position = storeHistoryAtInitialization.PreviousStates.Count - 1
            }
                );
        }