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); }); }
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 } ); }
public void ConnectToStore <T>(ReduxStore <T> store) where T : class, IImmutable, new() { var rootSelector = Selectors.CreateSelector((T state) => state); var onHistory = store.ObserveHistory() .WithLatestFrom(store.Select(rootSelector), (history, state) => (history, state)) .Select(pair => _selectHistoryRecords(pair.history, pair.state)); var onLatestEnsureHistory = EnsureManager .GetHistory() .Select(history => history.ToImmutableDictionary(session => session.Action)); onLatestEnsureHistory .Subscribe(val => _ensureSessions = val) .DisposedBy(this); MvvmRx.ApplyOnCollection(onHistory, this, Records, factory: Resolver.Resolve <HistoryRecordVm>, syncer: (model, vm) => vm.ReadModel(model, _ensureSessions), onRemove: vm => vm.Dispose()); var onCurrentHistoryRecord = Observable.CombineLatest( onHistory, MvvmRx.ObservePropertyValues(this, x => x.SelectedItem), (records, index) => (records, index)) .Select(pair => _pure_getSelectedHistoryRecord(pair.records, pair.index)) .Where(record => record != null); onCurrentHistoryRecord .Select(record => _pure_createJsonHierarchy(record.Action)) .ApplyOnProperty(this, x => x.Action); onCurrentHistoryRecord .Select(record => _pure_createJsonHierarchy(record.NextState)) .ApplyOnProperty(this, x => x.State); onCurrentHistoryRecord .Subscribe(record => Differ.ReadModel(record.PreviousState.ToJson(), record.NextState.ToJson())) .DisposedBy(this); var onEnsureSession = Observable.CombineLatest(onLatestEnsureHistory, onCurrentHistoryRecord, (history, record) => _pure_getEnsureSessionHistory(record, history)); onEnsureSession .Select(session => session?.Items ?.Select(item => Resolver.Resolve <EnsureItemVm>().ReadModel(item)) ?.ToObservableCollection() ?? new ObservableCollection <EnsureItemVm>() ) .ApplyOnProperty(this, x => x.EnsureItems); var onEnsureItem = Observable.CombineLatest( onEnsureSession, MvvmRx.ObservePropertyValues(this, x => x.SelectedEnsureItem), (session, index) => (session, index)) .Select(pair => _pure_getSelectedEnsureItem(pair.session, pair.index)); onEnsureItem.Select(item => _pure_createJsonHierarchy(item?.Context.Entity)) .ApplyOnProperty(this, x => x.EnsureItemState); onEnsureItem .Subscribe(item => EnsureDiffer.ReadModel(item?.Before.ToJson(), item?.After.ToJson())) .DisposedBy(this); }
public MovieListViewModel( NavigationManager navigationManager, ILocalStorageService localStorageService, ReduxStore <MovieSearchState> movieSearchStore) { _navigationManager = navigationManager; _localStorageService = localStorageService; // Set initial value SearchText = movieSearchStore.State.SearchText; var source = new SourceCache <OmdbMovieSearchDto, string>(x => x.ImdbId) .DisposeWith(CleanUp); source.Connect() .Sort(SortExpressionComparer <OmdbMovieSearchDto> .Ascending(p => p.Title), SortOptimisations.ComparesImmutableValuesOnly) .Bind(Movies) .Subscribe(_ => UpdateState()) .DisposeWith(CleanUp); source.CountChanged .StartWith(0) .Select(x => x == 0) .ToPropertyEx(this, x => x.IsSourceEmpty) .DisposeWith(CleanUp); var searchTextObservable = this.WhenAnyValue(x => x.SearchText) .Skip(1) // Use throttle to prevent over requesting data .Throttle(TimeSpan.FromMilliseconds(250)) .Publish(); searchTextObservable .Where(string.IsNullOrEmpty) .Subscribe(_ => movieSearchStore.Dispatch(new ResetMovieSearchAction())) .DisposeWith(CleanUp); searchTextObservable .Where(x => !string.IsNullOrEmpty(x)) .Subscribe(x => movieSearchStore.Dispatch(new PerformMovieSearchAction(x))) .DisposeWith(CleanUp); searchTextObservable.Connect(); movieSearchStore .Select(MovieSearchSelectors.SelectIsSearching) .ToPropertyEx(this, x => x.IsSearching) .DisposeWith(CleanUp); movieSearchStore .Select(MovieSearchSelectors.SelectMovies) .Subscribe(x => source.Edit(list => { list.Clear(); list.AddOrUpdate(x); })) .DisposeWith(CleanUp); movieSearchStore .Select(MovieSearchSelectors.SelectSearchText) .Skip(1) .SelectMany(async x => { await SaveSearchTextsAsync(x) .ConfigureAwait(false); return(Unit.Default); }) .Subscribe() .DisposeWith(CleanUp); }