Exemplo n.º 1
0
        private IDisposable BindFromSource <TModel>(TModel model, Expression <Func <TModel, SelectionData> > propertyExpr, Func <IDisposable> startTransaction)
        {
            return(model.WhenAnyValue(propertyExpr)
                   .Buffer(2, 1)
                   .Where(b => b.Count == 2)
                   .Select(b =>
            {
                var previous = b[0].ObjectIds.ToList();
                var current = b[1].ObjectIds.ToList();
                var sections = Diff.CalculateSections(previous, current);
                var alignedElements = Diff
                                      .AlignElements(previous, current, sections,
                                                     new BasicInsertDeleteDiffElementAligner <SelectionData.ObjectId>())
                                      .ToList();
                var oldObjectIds = alignedElements
                                   .Where(e => e.Operation == DiffOperation.Delete || e.Operation == DiffOperation.Replace)
                                   .Select(e => e.ElementFromCollection1.Value);

                var newObjectIds = alignedElements
                                   .Where(e => e.Operation == DiffOperation.Insert || e.Operation == DiffOperation.Replace)
                                   .Select(e => e.ElementFromCollection2.Value);

                return new { oldSelectionData = b[0], oldObjectIds, newSelectionData = b[1], newObjectIds };
            })
                   .Subscribe(o =>
            {
                using (startTransaction())
                {
                    ModelDoc.ClearSelection(o.oldSelectionData.WithObjectIds(o.oldObjectIds));
                    ModelDoc.AddSelection(o.newSelectionData.WithObjectIds(o.newObjectIds));
                }
            }));
        }