Skip to content

Commit

Permalink
More events that are top level
Browse files Browse the repository at this point in the history
  • Loading branch information
JR-Morgan committed Jun 20, 2024
1 parent cd956bd commit c5fe301
Show file tree
Hide file tree
Showing 10 changed files with 146 additions and 83 deletions.
1 change: 1 addition & 0 deletions All.sln.DotSettings
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,7 @@
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=QL/@EntryIndexedValue">QL</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=SQ/@EntryIndexedValue">SQ</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=UI/@EntryIndexedValue">UI</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=URI/@EntryIndexedValue">URI</s:String>
<s:Boolean x:Key="/Default/CodeStyle/Naming/CSharpNaming/ApplyAutoDetectedRules/@EntryValue">True</s:Boolean>
<s:String x:Key="/Default/CustomTools/CustomToolsData/@EntryValue">ExternalToolData|CSharpier|csharpier||csharpier|$FILE$</s:String>
<s:String x:Key="/Default/Environment/StructuredLogging/PropertyNamingType/@EntryValue">CamelCase</s:String>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public sealed class ArcGISSendBinding : ISendBinding
private readonly List<ISendFilter> _sendFilters;
private readonly CancellationManager _cancellationManager;
private readonly ISendConversionCache _sendConversionCache;
private readonly TopLevelExceptionHandler _topLevelExceptionHandler;

/// <summary>
/// Used internally to aggregate the changed objects' id.
Expand All @@ -45,32 +46,57 @@ public ArcGISSendBinding(
IEnumerable<ISendFilter> sendFilters,
IUnitOfWorkFactory unitOfWorkFactory,
CancellationManager cancellationManager,
ISendConversionCache sendConversionCache
ISendConversionCache sendConversionCache,
TopLevelExceptionHandler topLevelExceptionHandler
)
{
_store = store;
_unitOfWorkFactory = unitOfWorkFactory;
_sendFilters = sendFilters.ToList();
_cancellationManager = cancellationManager;
_sendConversionCache = sendConversionCache;
_topLevelExceptionHandler = topLevelExceptionHandler;
Parent = parent;
Commands = new SendBindingUICommands(parent);
SubscribeToArcGISEvents();
}

private void SubscribeToArcGISEvents()
{
LayersRemovedEvent.Subscribe(GetIdsForLayersRemovedEvent, true);
StandaloneTablesRemovedEvent.Subscribe(GetIdsForStandaloneTablesRemovedEvent, true);
MapPropertyChangedEvent.Subscribe(GetIdsForMapPropertyChangedEvent, true); // Map units, CRS etc.
MapMemberPropertiesChangedEvent.Subscribe(GetIdsForMapMemberPropertiesChangedEvent, true); // e.g. Layer name

ActiveMapViewChangedEvent.Subscribe(SubscribeToMapMembersDataSourceChange, true);
LayersAddedEvent.Subscribe(GetIdsForLayersAddedEvent, true);
StandaloneTablesAddedEvent.Subscribe(GetIdsForStandaloneTablesAddedEvent, true);
LayersRemovedEvent.Subscribe(
a => _topLevelExceptionHandler.CatchUnhandled(() => GetIdsForLayersRemovedEvent(a)),
true
);

StandaloneTablesRemovedEvent.Subscribe(
a => _topLevelExceptionHandler.CatchUnhandled(() => GetIdsForStandaloneTablesRemovedEvent(a)),
true
);

MapPropertyChangedEvent.Subscribe(
a => _topLevelExceptionHandler.CatchUnhandled(() => GetIdsForMapPropertyChangedEvent(a)),
true
); // Map units, CRS etc.

MapMemberPropertiesChangedEvent.Subscribe(
a => _topLevelExceptionHandler.CatchUnhandled(() => GetIdsForMapMemberPropertiesChangedEvent(a)),
true
); // e.g. Layer name

ActiveMapViewChangedEvent.Subscribe(
_ => _topLevelExceptionHandler.CatchUnhandled(SubscribeToMapMembersDataSourceChange),
true
);

LayersAddedEvent.Subscribe(a => _topLevelExceptionHandler.CatchUnhandled(() => GetIdsForLayersAddedEvent(a)), true);

StandaloneTablesAddedEvent.Subscribe(
a => _topLevelExceptionHandler.CatchUnhandled(() => GetIdsForStandaloneTablesAddedEvent(a)),
true
);
}

private void SubscribeToMapMembersDataSourceChange(ActiveMapViewChangedEventArgs args)
private void SubscribeToMapMembersDataSourceChange()
{
var task = QueuedTask.Run(() =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class ArcGISDocumentStore : DocumentModelStore
public ArcGISDocumentStore(JsonSerializerSettings serializerOption, TopLevelExceptionHandler topLevelExceptionHandler)
: base(serializerOption, true)
{
ActiveMapViewChangedEvent.Subscribe(OnMapViewChanged);
ActiveMapViewChangedEvent.Subscribe(a => topLevelExceptionHandler.CatchUnhandled(() => OnMapViewChanged(a)));
ProjectSavingEvent.Subscribe(_ =>
{
topLevelExceptionHandler.CatchUnhandled(OnProjectSaving);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public sealed class AutocadSendBinding : ISendBinding
private readonly IUnitOfWorkFactory _unitOfWorkFactory;
private readonly AutocadSettings _autocadSettings;
private readonly ISendConversionCache _sendConversionCache;
private readonly TopLevelExceptionHandler _topLevelExceptionHandler;

/// <summary>
/// Used internally to aggregate the changed objects' id.
Expand All @@ -43,7 +44,8 @@ public AutocadSendBinding(
CancellationManager cancellationManager,
AutocadSettings autocadSettings,
IUnitOfWorkFactory unitOfWorkFactory,
ISendConversionCache sendConversionCache
ISendConversionCache sendConversionCache,
TopLevelExceptionHandler topLevelExceptionHandler
)
{
_store = store;
Expand All @@ -53,10 +55,13 @@ ISendConversionCache sendConversionCache
_cancellationManager = cancellationManager;
_sendFilters = sendFilters.ToList();
_sendConversionCache = sendConversionCache;
_topLevelExceptionHandler = topLevelExceptionHandler;
Parent = parent;
Commands = new SendBindingUICommands(parent);

Application.DocumentManager.DocumentActivated += (sender, args) => SubscribeToObjectChanges(args.Document);
Application.DocumentManager.DocumentActivated += (_, args) =>
topLevelExceptionHandler.CatchUnhandled(() => SubscribeToObjectChanges(args.Document));

if (Application.DocumentManager.CurrentDocument != null)
{
// catches the case when autocad just opens up with a blank new doc
Expand All @@ -74,9 +79,14 @@ private void SubscribeToObjectChanges(Document doc)
}

_docSubsTracker.Add(doc.Name);
doc.Database.ObjectAppended += (_, e) => OnChangeChangedObjectIds(e.DBObject);
doc.Database.ObjectErased += (_, e) => OnChangeChangedObjectIds(e.DBObject);
doc.Database.ObjectModified += (_, e) => OnChangeChangedObjectIds(e.DBObject);
doc.Database.ObjectAppended += (_, e) => OnObjectChanged(e.DBObject);
doc.Database.ObjectErased += (_, e) => OnObjectChanged(e.DBObject);
doc.Database.ObjectModified += (_, e) => OnObjectChanged(e.DBObject);
}

void OnObjectChanged(DBObject dbObject)
{
_topLevelExceptionHandler.CatchUnhandled(() => OnChangeChangedObjectIds(dbObject));
}

private void OnChangeChangedObjectIds(DBObject dBObject)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ internal sealed class RevitSendBinding : RevitBaseBinding, ISendBinding
private readonly CancellationManager _cancellationManager;
private readonly IUnitOfWorkFactory _unitOfWorkFactory;
private readonly ISendConversionCache _sendConversionCache;
private readonly TopLevelExceptionHandler _topLevelExceptionHandler;

public RevitSendBinding(
IRevitIdleManager idleManager,
Expand All @@ -36,7 +37,8 @@ public RevitSendBinding(
IBridge bridge,
IUnitOfWorkFactory unitOfWorkFactory,
RevitSettings revitSettings,
ISendConversionCache sendConversionCache
ISendConversionCache sendConversionCache,
TopLevelExceptionHandler topLevelExceptionHandler
)
: base("sendBinding", store, bridge, revitContext)
{
Expand All @@ -45,12 +47,15 @@ ISendConversionCache sendConversionCache
_unitOfWorkFactory = unitOfWorkFactory;
_revitSettings = revitSettings;
_sendConversionCache = sendConversionCache;
_topLevelExceptionHandler = topLevelExceptionHandler;

Commands = new SendBindingUICommands(bridge);
// TODO expiry events
// TODO filters need refresh events
revitContext.UIApplication.NotNull().Application.DocumentChanged += (_, e) => DocChangeHandler(e);
Store.DocumentChanged += (_, _) => OnDocumentChanged();
revitContext.UIApplication.NotNull().Application.DocumentChanged += (_, e) =>
_topLevelExceptionHandler.CatchUnhandled(() => DocChangeHandler(e));

Store.DocumentChanged += (_, _) => _topLevelExceptionHandler.CatchUnhandled(OnDocumentChanged);
}

public List<ISendFilter> GetSendFilters()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,23 @@ namespace Speckle.Connectors.Revit.Bindings;
internal sealed class SelectionBinding : RevitBaseBinding, ISelectionBinding
{
private readonly IRevitIdleManager _revitIdleManager;
private readonly TopLevelExceptionHandler _topLevelExceptionHandler;

public SelectionBinding(
RevitContext revitContext,
DocumentModelStore store,
IRevitIdleManager idleManager,
IBridge bridge
IBridge bridge,
TopLevelExceptionHandler topLevelExceptionHandler
)
: base("selectionBinding", store, bridge, revitContext)
{
_revitIdleManager = idleManager;
_topLevelExceptionHandler = topLevelExceptionHandler;
// POC: we can inject the solution here
// TODO: Need to figure it out equivalent of SelectionChanged for Revit2020
RevitContext.UIApplication.NotNull().SelectionChanged += (_, _) =>
_revitIdleManager.SubscribeToIdle(OnSelectionChanged);
topLevelExceptionHandler.CatchUnhandled(() => _revitIdleManager.SubscribeToIdle(OnSelectionChanged));
}

private void OnSelectionChanged()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Events;
using Revit.Async;
using Speckle.Connectors.DUI.Bridge;
using Speckle.Connectors.DUI.Models;
using Speckle.Connectors.Revit.Plugin;
using Speckle.Connectors.RevitShared.Helpers;
Expand All @@ -29,7 +30,8 @@ public RevitDocumentStore(
RevitContext revitContext,
JsonSerializerSettings serializerSettings,
DocumentModelStorageSchema documentModelStorageSchema,
IdStorageSchema idStorageSchema
IdStorageSchema idStorageSchema,
TopLevelExceptionHandler topLevelExceptionHandler
)
: base(serializerSettings, true)
{
Expand All @@ -40,12 +42,15 @@ IdStorageSchema idStorageSchema

UIApplication uiApplication = _revitContext.UIApplication.NotNull();

uiApplication.ViewActivated += OnViewActivated;
uiApplication.ViewActivated += (s, e) => topLevelExceptionHandler.CatchUnhandled(() => OnViewActivated(s, e));

uiApplication.Application.DocumentOpening += (_, _) => IsDocumentInit = false;
uiApplication.Application.DocumentOpened += (_, _) => IsDocumentInit = false;
uiApplication.Application.DocumentOpening += (_, _) =>
topLevelExceptionHandler.CatchUnhandled(() => IsDocumentInit = false);

Models.CollectionChanged += (_, _) => WriteToFile();
uiApplication.Application.DocumentOpened += (_, _) =>
topLevelExceptionHandler.CatchUnhandled(() => IsDocumentInit = false);

Models.CollectionChanged += (_, _) => topLevelExceptionHandler.CatchUnhandled(WriteToFile);

// There is no event that we can hook here for double-click file open...
// It is kind of harmless since we create this object as "SingleInstance".
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections.Concurrent;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Events;
using Speckle.Connectors.DUI.Bridge;
using Speckle.Connectors.RevitShared.Helpers;

namespace Speckle.Connectors.Revit.Plugin;
Expand All @@ -9,15 +10,17 @@ namespace Speckle.Connectors.Revit.Plugin;
// is probably misnamed, perhaps OnIdleCallbackManager
internal sealed class RevitIdleManager : IRevitIdleManager
{
private readonly TopLevelExceptionHandler _topLevelExceptionHandler;
private readonly UIApplication _uiApplication;

private readonly ConcurrentDictionary<string, Action> _calls = new();

// POC: still not thread safe
private volatile bool _hasSubscribed;

public RevitIdleManager(RevitContext revitContext)
public RevitIdleManager(RevitContext revitContext, TopLevelExceptionHandler topLevelExceptionHandler)
{
_topLevelExceptionHandler = topLevelExceptionHandler;
_uiApplication = revitContext.UIApplication!;
}

Expand Down Expand Up @@ -46,15 +49,18 @@ public void SubscribeToIdle(Action action)

private void RevitAppOnIdle(object sender, IdlingEventArgs e)
{
foreach (KeyValuePair<string, Action> kvp in _calls)
_topLevelExceptionHandler.CatchUnhandled(() =>
{
kvp.Value();
}
foreach (KeyValuePair<string, Action> kvp in _calls)
{
kvp.Value.Invoke();
}

_calls.Clear();
_uiApplication.Idling -= RevitAppOnIdle;
_calls.Clear();
_uiApplication.Idling -= RevitAppOnIdle;

// setting last will delay ntering re-subscritption
_hasSubscribed = false;
// setting last will delay ntering re-subscritption
_hasSubscribed = false;
});
}
}
Loading

0 comments on commit c5fe301

Please sign in to comment.