-
-
Notifications
You must be signed in to change notification settings - Fork 220
MAUI CommunityToolkit MVVM auto instrumentation of async commands #4125
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+1,921
−38
Merged
Changes from 5 commits
Commits
Show all changes
94 commits
Select commit
Hold shift + click to select a range
228deef
Create MauiGestureRecognizerEventsBinder.cs
aritchie e66308c
Wireup
aritchie d2cb84f
Format code
getsentry-bot 2eec158
WIP
aritchie 24248da
Cleanup and fix code due stupid bot
aritchie 98a9789
I'm not an animal - I can save mem
aritchie 35e769c
Format code
getsentry-bot 017f7f1
Command instrumentation
aritchie 006abec
Format code
getsentry-bot f76f132
Fixes from live usage in our sample app
aritchie 1943f22
Format code
getsentry-bot 6687937
Update according to code review
aritchie 751abe2
Merge branch 'main' into maui_ctmvvm
aritchie 8059884
Update CHANGELOG.md
aritchie 6586050
Update MauiGestureRecognizerEventsBinder.cs
aritchie d1d5089
Create MauiEventsBinderTests.GestureRecognizers.cs
aritchie efcd673
Update MauiEventsBinderTests.GestureRecognizers.cs
aritchie 08a82bc
Merge branch 'main' into maui_gestures_breadcrumbs
aritchie 4226806
Initial Unit Tests and samples
aritchie 5752506
Merge branch 'main' into maui_ctmvvm
aritchie 054041f
Merge branch 'main' into maui_gestures_breadcrumbs
aritchie 1fba474
Update MauiEventsBinderTests.cs
aritchie 118b627
Format code
getsentry-bot 8ef9aca
Update CHANGELOG.md
aritchie 559c8e0
Format code
getsentry-bot a71375a
WIP
aritchie ba54096
Merge branch 'maui_gestures_breadcrumbs' of https://github.com/getsen…
aritchie 1a74296
add visual runner so I can test
aritchie 90cbf1d
Merge branch 'main' into maui_ctmvvm
aritchie 3af183a
Trying to add visual runner
aritchie e14e5c2
Merge branch 'main' into maui_gestures_breadcrumbs
aritchie 66adf29
Update CHANGELOG.md
aritchie c55a364
Shelve so I can reset branch
aritchie 86b25b9
Passing tests
aritchie 587c137
generate slnf
aritchie c76ddec
Format code
getsentry-bot 4e57733
Merge branch 'main' into maui_ctmvvm
aritchie 6d8f86f
Finally fixed all build and test issues
aritchie 7ed7f5c
Update CHANGELOG.md
aritchie 490ad7b
Comment out visual runner for now
aritchie 5b15d4d
Update Sentry.Maui.Device.TestApp.csproj
aritchie 18c6e1f
Format code
getsentry-bot fe3fb27
Update device-test.ps1
aritchie afc5ea4
Merge branch 'maui_ctmvvm' of https://github.com/getsentry/sentry-dot…
aritchie dda6299
Update CtMvvmViewModel.cs
aritchie 1a1f3b2
Update samples/Sentry.Samples.Maui/MauiProgram.cs
aritchie 86079a3
Updates from PR review
aritchie 394d12a
Merge branch 'maui_ctmvvm' of https://github.com/getsentry/sentry-dot…
aritchie 12760df
Merge branch 'main' into maui_ctmvvm
aritchie 23a9a00
Update CHANGELOG.md
aritchie 67dde5f
Updated generated filters - added crashable app - some tests depend o…
aritchie f8dfca3
Format code
getsentry-bot b0393ac
Fixes
aritchie e7a5a9b
Merge branch 'main' into maui_gestures_breadcrumbs
aritchie d0c58f4
Bring over device tests to improve setup, fix gesture tests
aritchie 039e35b
Merge branch 'maui_gestures_breadcrumbs' into maui_ctmvvm
aritchie 0a08817
Update .generated.NoMobile.sln
aritchie 4961411
Format code
getsentry-bot 56e6d12
These run every time in the visual runner
aritchie ed4bb20
If working strictly from slnf - these don't get built and are depende…
aritchie a79dc8f
Finally got these to generate
aritchie ff6c52b
Format code
getsentry-bot c8d545c
Update MauiEventsBinderTests.GestureRecognizers.cs
aritchie b4f77b1
Merge branch 'maui_gestures_breadcrumbs' of https://github.com/getsen…
aritchie c10f2f0
Improve testing setup
aritchie 19166b5
Merge branch 'maui_gestures_breadcrumbs' into maui_ctmvvm
aritchie 8fa09e9
Update MauiEventsBinderTests.cs
aritchie 4c1b584
Update api approval tests... again
aritchie 28a4993
Update src/Sentry.Maui/Internal/MauiGestureRecognizerEventsBinder.cs
aritchie 8e32be7
Merge branch 'main' into maui_gestures_breadcrumbs
aritchie 28c2205
Update MauiGestureRecognizerEventsBinder.cs
aritchie 44f30f8
Merge branch 'main' into maui_ctmvvm
aritchie dd29c1e
Merge branch 'maui_gestures_breadcrumbs' into maui_ctmvvm
aritchie bf8888a
Update src/Sentry.Maui.CommunityToolkit.Mvvm/MauiAppBuilderExtensions.cs
aritchie e73aa26
Renaming things
aritchie 5115f0d
Merge branch 'main' into maui_ctmvvm
aritchie fd4c0f7
Update Directory.Build.props
aritchie e621044
Update MauiEventsBinderTests.cs
aritchie 0cc439d
Simplified initialisation of the integration
jamescrosswell 1488ad6
Merge branch 'main' into maui_ctmvvm
jamescrosswell 62b56c2
Update CHANGELOG.md
jamescrosswell 6d78d81
Format code
getsentry-bot 78a934f
Update CtMvvmViewModel.cs
jamescrosswell 931fb95
Update Sentry.csproj
jamescrosswell ecbab28
Update SentryMauiAppBuilderExtensions.cs
jamescrosswell 52ef07a
Addressed thread safety concerns
jamescrosswell a2f3de9
Merge branch 'maui_ctmvvm' of github.com:getsentry/sentry-dotnet into…
jamescrosswell fec77a2
Format code
getsentry-bot 167f9dc
Update MauiCommunityToolkitMvvmEventsBinder.cs
jamescrosswell 04d35a8
Merge branch 'maui_ctmvvm' of github.com:getsentry/sentry-dotnet into…
jamescrosswell f1565c1
Target older version of CommunityToolkit.Mvvm
jamescrosswell 77cd0cd
Created separate test project for new integration
jamescrosswell cb7b258
Workaround stale Scope.Transaction issue
jamescrosswell 70525e6
Merge branch 'main' into maui_ctmvvm
bruno-garcia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
151 changes: 151 additions & 0 deletions
151
src/Sentry.Maui.CommunityToolkitMvvm/CtMvvmMauiElementEventBinder.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
using System.Windows.Input; | ||
using CommunityToolkit.Mvvm.Input; | ||
|
||
namespace Sentry.Maui.CommunityToolkitMvvm; | ||
|
||
/// <summary> | ||
/// Scans all elements for known commands that are implement | ||
/// </summary> | ||
public class CtMvvmMauiElementEventBinder : IMauiElementEventBinder | ||
{ | ||
/// <summary> | ||
/// Binds to the element | ||
/// </summary> | ||
/// <param name="element"></param> | ||
/// <param name="addBreadcrumb"></param> | ||
public void Bind(VisualElement element, Action<BreadcrumbEvent> addBreadcrumb) => Iterate(element, true); | ||
|
||
/// <summary> | ||
/// Unbinds from the element | ||
/// </summary> | ||
/// <param name="element"></param> | ||
/// <exception cref="NotImplementedException"></exception> | ||
public void UnBind(VisualElement element) => Iterate(element, false); | ||
|
||
|
||
private static void Iterate(VisualElement element, bool bind) | ||
{ | ||
switch (element) | ||
{ | ||
case Button button: | ||
TryBindTo(button.Command, bind); | ||
break; | ||
|
||
case ImageButton imageButton: | ||
TryBindTo(imageButton.Command, bind); | ||
break; | ||
|
||
case CarouselView carousel: | ||
TryBindTo(carousel.CurrentItemChangedCommand, bind); | ||
TryBindTo(carousel.RemainingItemsThresholdReachedCommand, bind); | ||
TryBindTo(carousel.PositionChangedCommand, bind); | ||
break; | ||
|
||
case CollectionView collectionView: | ||
TryBindTo(collectionView.RemainingItemsThresholdReachedCommand, bind); | ||
TryBindTo(collectionView.SelectionChangedCommand, bind); | ||
break; | ||
|
||
case Entry entry: | ||
TryBindTo(entry.ReturnCommand, bind); | ||
break; | ||
|
||
case RefreshView refresh: | ||
TryBindTo(refresh.Command, bind); | ||
break; | ||
|
||
case SearchBar searchBar: | ||
TryBindTo(searchBar.SearchCommand, bind); | ||
break; | ||
|
||
default: | ||
TryGestureBinding(element, bind); | ||
break; | ||
} | ||
} | ||
|
||
private static void TryGestureBinding(VisualElement element, bool bind) | ||
{ | ||
if (element is IGestureRecognizers gestureRecognizers) | ||
{ | ||
foreach (var gestureRecognizer in gestureRecognizers.GestureRecognizers) | ||
{ | ||
TryBindTo(gestureRecognizer, bind); | ||
} | ||
} | ||
} | ||
|
||
private static void TryBindTo(IGestureRecognizer recognizer, bool bind) | ||
{ | ||
switch (recognizer) | ||
{ | ||
case TapGestureRecognizer tap: | ||
TryBindTo(tap.Command, bind); | ||
break; | ||
|
||
case SwipeGestureRecognizer swipe: | ||
TryBindTo(swipe.Command, bind); | ||
break; | ||
|
||
// no commands | ||
//case PinchGestureRecognizer pinch | ||
//case PanGestureRecognizer pan | ||
case DragGestureRecognizer drag: | ||
TryBindTo(drag.DragStartingCommand, bind); // unlikely to ever be async | ||
TryBindTo(drag.DropCompletedCommand, bind); | ||
break; | ||
|
||
case PointerGestureRecognizer pointer: | ||
TryBindTo(pointer.PointerPressedCommand, bind); | ||
TryBindTo(pointer.PointerReleasedCommand, bind); | ||
TryBindTo(pointer.PointerEnteredCommand, bind); // unlikely to ever be async | ||
TryBindTo(pointer.PointerExitedCommand, bind); | ||
break; | ||
} | ||
} | ||
|
||
private static List<IAsyncRelayCommand> _refs = []; | ||
private static void TryBindTo(ICommand? command, bool bind) | ||
{ | ||
if (command is IAsyncRelayCommand relayCommand) | ||
{ | ||
if (bind) | ||
{ | ||
// necessary for collectionview buttons | ||
if (!_refs.Contains(relayCommand)) | ||
{ | ||
_refs.Add(relayCommand); | ||
relayCommand.PropertyChanged += RelayCommandOnPropertyChanged; | ||
} | ||
} | ||
else | ||
{ | ||
_refs.Remove(relayCommand); | ||
relayCommand.PropertyChanged -= RelayCommandOnPropertyChanged; | ||
} | ||
} | ||
} | ||
|
||
private static ConcurrentDictionary<IAsyncRelayCommand, (ITransactionTracer Transaction, ISpan Span)> _contexts = new(); | ||
|
||
private static void RelayCommandOnPropertyChanged(object? sender, PropertyChangedEventArgs e) | ||
{ | ||
if (e.PropertyName == nameof(IAsyncRelayCommand.IsRunning)) | ||
{ | ||
var relay = (IAsyncRelayCommand)sender!; | ||
|
||
if (relay.IsRunning) | ||
{ | ||
var transaction = SentrySdk.StartTransaction("ctmvvm", "asynccommand"); | ||
aritchie marked this conversation as resolved.
Show resolved
Hide resolved
aritchie marked this conversation as resolved.
Show resolved
Hide resolved
|
||
var span = transaction.StartChild("run"); | ||
aritchie marked this conversation as resolved.
Show resolved
Hide resolved
|
||
_contexts.TryAdd(relay, (transaction, span)); | ||
} | ||
else if (_contexts.TryGetValue(relay, out var value)) | ||
{ | ||
value.Span.Finish(); | ||
value.Transaction.Finish(); | ||
_contexts.TryRemove(relay, out _); | ||
} | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/Sentry.Maui.CommunityToolkitMvvm/MauiAppBuilderExtensions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using Sentry.Maui.CommunityToolkitMvvm; | ||
|
||
namespace Sentry.Maui; | ||
|
||
/// <summary> | ||
/// Methods to hook into MAUI & CommunityToolkit.Mvvm | ||
/// </summary> | ||
public static class MauiAppBuilderExtensions | ||
{ | ||
/// <summary> | ||
/// Installs necessary services to auto-instrument CommunityToolkit.Mvvm commands | ||
/// </summary> | ||
/// <param name="builder">The MauiAppBuilder</param> | ||
/// <returns>The MauiAppBuilder</returns> | ||
public static MauiAppBuilder UseSentryCommunityToolkitIntegration(this MauiAppBuilder builder) | ||
{ | ||
builder.Services.UseSentryCommunityToolkitIntegration(); | ||
return builder; | ||
} | ||
|
||
|
||
/// <summary> | ||
/// Installs necessary services to auto-instrument CommunityToolkit.Mvvm commands | ||
/// </summary> | ||
/// <param name="services">The Service Collection</param> | ||
/// <returns>The Service Collection</returns> | ||
public static IServiceCollection UseSentryCommunityToolkitIntegration(this IServiceCollection services) | ||
aritchie marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
services.AddSingleton<IMauiElementEventBinder, CtMvvmMauiElementEventBinder>(); | ||
return services; | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/Sentry.Maui.CommunityToolkitMvvm/Sentry.Maui.CommunityToolkitMvvm.csproj
aritchie marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<Description>MAUI and Community Toolkit integration for Sentry - Open-source error tracking that helps developers monitor and fix crashes in real time.</Description> | ||
<TargetFrameworks>net9.0;net8.0</TargetFrameworks> | ||
|
||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<UseMaui>true</UseMaui> | ||
|
||
<!-- Skip warnings about including Microsoft.Maui.Controls... that should be done from the application --> | ||
<SkipValidateMauiImplicitPackageReferences>true</SkipValidateMauiImplicitPackageReferences> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.4.0"/> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Sentry.Maui\Sentry.Maui.csproj"/> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<InternalsVisibleTo Include="Sentry.Maui.Tests" PublicKey="$(SentryPublicKey)" /> | ||
</ItemGroup> | ||
</Project> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.