Reactive undo/redo framework for .NET.
Перейти к файлу
Wiesław Šoltés faec320775 Added BuildAvaloniaApp 2018-02-19 21:35:39 +01:00
build Updated Avalonia 2018-02-18 13:45:24 +01:00
docs Added docs project 2016-08-31 23:29:20 +02:00
samples Added BuildAvaloniaApp 2018-02-19 21:35:39 +01:00
src Updated build props and projects 2017-11-01 14:05:11 +01:00
tests/ReactiveHistory.UnitTests Update ReactiveHistory.UnitTests.csproj 2017-10-31 13:39:18 +01:00
.editorconfig Create .editorconfig 2016-09-05 12:48:13 +02:00
.gitattributes Updated gitattributes and gitignore 2017-11-14 18:40:09 +01:00
.gitignore Updated gitattributes and gitignore 2017-11-14 18:40:09 +01:00
.travis.yml Update .travis.yml 2017-10-31 07:38:37 +01:00
LICENSE.TXT Rename LICENSE to LICENSE.TXT 2016-08-31 15:15:11 +02:00
NuGet.Config Initial import 2016-08-31 23:15:05 +02:00
README.md Update README.md 2017-05-24 14:22:19 +02:00
ReactiveHistory.sln Updated build props and projects 2017-11-01 14:05:11 +01:00
_config.yml Set theme jekyll-theme-cayman 2017-04-07 15:40:38 +02:00
appveyor.yml Update appveyor.yml 2017-10-31 13:49:14 +01:00
build.cake Update build.cake 2017-11-16 10:26:19 +01:00
build.ps1 Added mono platform to validation set 2016-09-04 15:19:47 +02:00
build.sh Make build script executable 2016-08-31 23:24:24 +02:00
circle.yml Update circle.yml 2017-10-31 14:00:55 +01:00

README.md

ReactiveHistory

Gitter

Build status Build Status CircleCI

NuGet MyGet

CodeFactor

ReactiveHistory is an undo/redo framework for .NET.

About

The main design principle for ReactiveHistory is to provide easy to use undo functionality inside your view models and allow separation from data models when following the MVVM pattern for creation of modern and portable GUI applications.

The ReactiveHistory framework enables easy implementation of undo history for observable properties and collections. To enable quick conversion from standard .NET properties and collection helper extension methods are provided. For example the ObserveWithHistory extension method is intended to be used for IObservable<T> properties and the DeleteWithHistory for IList<T> collections.

Example Usage

For examples of ReactiveHistory usage please check the samples folder. Currently there are available samples for WPF and Avalonia frameworks. The samples follow MVVM pattern, the view models use ReactiveProperty framework to implement observable properties and commands. There are also available unit tests located in tests folder with all tests for each of the use cases.

MVVM

Model

public class Layer : BaseObject
{
    private ObservableCollection<LineShape> _shapes;

    public ObservableCollection<LineShape> Shapes
    {
        get { return _shapes; }
        set { Update(ref _shapes, value); }
    }

    public Layer(object owner = null, string name = null)
    {
        this.Owner = owner;
        this.Name = name;
        this.Shapes = new ObservableCollection<LineShape>();
    }
}

ViewModel

public class LayerViewModel : IDisposable
{
    private CompositeDisposable Disposable { get; set; }
    public ReactiveProperty<string> Name { get; set; }
    public ReadOnlyReactiveCollection<LineShapeViewModel> Shapes { get; set; }
    public ReactiveCommand UndoCommand { get; set; }
    public ReactiveCommand RedoCommand { get; set; }
    public ReactiveCommand ClearCommand { get; set; }

    public LayerViewModel(Layer layer, IHistory history)
    {
        Disposable = new CompositeDisposable();

        this.Name = layer.ToReactivePropertyAsSynchronized(l => l.Name)
            .SetValidateNotifyError(name => string.IsNullOrWhiteSpace(name) ? "Name can not be null or whitespace." : null)
            .AddTo(this.Disposable);

        this.Shapes = layer.Shapes
            .ToReadOnlyReactiveCollection(x => new LineShapeViewModel(x, history))
            .AddTo(this.Disposable);

        this.Name.ObserveWithHistory(name => layer.Name = name, layer.Name, history).AddTo(this.Disposable);
        
        UndoCommand = new ReactiveCommand(history.CanUndo, false);
        UndoCommand.Subscribe(_ => history.Undo()).AddTo(this.Disposable);

        RedoCommand = new ReactiveCommand(history.CanRedo, false);
        RedoCommand.Subscribe(_ => history.Redo()).AddTo(this.Disposable);

        ClearCommand = new ReactiveCommand(history.CanClear, false);
        ClearCommand.Subscribe(_ => history.Clear()).AddTo(this.Disposable);
    }

    public void Dispose()
    {
        this.Disposable.Dispose();
    }
}

Initialization

var layer1 = new Layer("layer1");

var line1 = new LineShape(layer1, "line1");
line1.Start = new PointShape(100, 100, line1, "start11");
line1.End = new PointShape(200, 100, line1, "end11");

var line2 = new LineShape(layer1, "line2");
line2.Start = new PointShape(100, 200, line2, "start21");
line2.End = new PointShape(200, 200, line2, "end21");

layer1.Shapes.Add(line1);
layer1.Shapes.Add(line2);

var history = new StackHistory();
var layerViewModel = new LayerViewModel(layer1, history).AddTo(disposable);

Building ReactiveHistory

First, clone the repository or download the latest zip.

git clone https://github.com/wieslawsoltes/ReactiveHistory.git
git submodule update --init --recursive

Build using IDE

Open ReactiveHistory.sln in selected IDE and run Build command.

Build on Windows using script

Open up a Powershell prompt and execute the bootstrapper script:

PS> .\build.ps1 -Target "Default" -Platform "Any CPU" -Configuration "Release"

Build on Linux/OSX using script

Open up a terminal prompt and execute the bootstrapper script:

$ ./build.sh --target "Default" --platform "Any CPU" --configuration "Release"

NuGet

ReactiveHistory is delivered as a NuGet package.

You can find the packages here NuGet or by using nightly build feed:

  • Add https://www.myget.org/F/reactivehistory-nightly/api/v2 to your package sources
  • Update your package using ReactiveHistory feed

You can install the package like this:

Install-Package ReactiveHistory -Pre

Package Dependencies

Package Sources

License

ReactiveHistory is licensed under the MIT license.