private void DoToggleManifoldExpanded(ManifoldRecord mr)
 {
     if (mr.isExpanded)
     {
         CollapseManifold(mr);
     }
     else
     {
         ExpandManifold(mr);
     }
 }
        private void OnWorkbenchEvent(WorkbenchEvent e)
        {
            switch (e.type)
            {
            case WorkbenchEvent.EType.Added:
                if (!Contains(e.manifold))
                {
                    var i  = 0;
                    var mr = new ManifoldRecord(e.manifold);
                    if (e.index == 0)
                    {
                        records.Insert(i, mr);
                        NotifyItemRangeInserted(i, 2);
                    }
                    else
                    {
                        i  = AdapterIndexForManifold(workbench[e.index - 1]);
                        i += AdapterSizeForManifold(records[i] as ManifoldRecord);
                        records.Insert(i, mr);
                        NotifyItemRangeInserted(i, 2);
                    }

                    if (mr.manifold.sensorPropertyCount > 0)
                    {
                        ExpandManifold(mr);
                    }
                }
                break;                 // WorkbenchEvent.EType.Added

            case WorkbenchEvent.EType.ManifoldEvent: {
                OnManifoldEvent(e.manifoldEvent);
            } break;                     // WorkbenchEvent.EType.ManifoldEvent

            case WorkbenchEvent.EType.Removed: {
                var i = AdapterIndexForManifold(e.manifold);
                if (i != -1)
                {
                    var cnt = AdapterSizeForManifold(records[i] as ManifoldRecord);
                    records.RemoveRange(i, cnt);
                    NotifyItemRangeRemoved(i, cnt);
                }
            } break;                     // WorkbenchEvent.EType.Removed

            case WorkbenchEvent.EType.Swapped: {
                SwapRecords(AdapterIndexForManifold(workbench[e.index]), AdapterIndexForManifold(workbench[e.otherIndex]));
            } break;                     // WorkbenchEvent.EType.Swapped
            }
        }
        private void CollapseManifold(ManifoldRecord mr, bool refresh = true)
        {
            var i = AdapterIndexForManifold(mr.manifold) + 1;

            if (mr.isExpanded)
            {
                var cnt = mr.manifold.sensorPropertyCount;
                records.RemoveRange(i, cnt);
                NotifyItemRangeRemoved(i, cnt);
            }

            if (refresh)
            {
                NotifyItemChanged(i - 1);
            }
            mr.isExpanded = false;
        }
        public WorkbenchAdapter(Action onAddViewer, Workbench workbench, bool hideAdd)
        {
            this.onAddViewer = onAddViewer;
            this.workbench   = workbench;
            dragger          = new ItemTouchHelper(new Dragger(this));

            workbench.onWorkbenchEvent += OnWorkbenchEvent;
            foreach (var manifold in workbench.manifolds)
            {
                var mr = new ManifoldRecord(manifold);
                records.Add(mr);
                ExpandManifold(mr);
            }

            if (!hideAdd)
            {
                records.Add(new AddViewerRecord(onAddViewer));
            }
        }
        private void ExpandManifold(ManifoldRecord mr)
        {
            if (mr.isExpanded)
            {
                return;
            }

            var i = AdapterIndexForManifold(mr.manifold) + 1;

            if (!mr.isExpanded)
            {
                var cnt = mr.manifold.sensorPropertyCount;
                for (int j = cnt - 1; j >= 0; j--)
                {
                    records.Insert(i, CreateSensorPropertyRecord(mr.manifold, mr.manifold[j]));
                }
                NotifyItemRangeInserted(i, cnt);
            }

            NotifyItemChanged(i - 1);
            mr.isExpanded = true;
        }
 /// <summary>
 /// Queries the adapter size of the given manifold (this size of the manifold and all expanded subviews.
 /// </summary>
 /// <returns>The size for manifold.</returns>
 /// <param name="manifold">Manifold.</param>
 private int AdapterSizeForManifold(ManifoldRecord mr)
 {
     return(1 + (mr.isExpanded ? mr.manifold.sensorPropertyCount : 0));
 }