Skip to content
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

[Android] Avoid double event subscribes in gesture manager #23242

Merged
merged 2 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -207,11 +207,10 @@ void SetupGestures()
shouldAddTouchEvent = true;
}

if (!shouldAddTouchEvent)
{
platformView.Touch -= OnPlatformViewTouched;
}
else
// Always unsubscribe first to avoid duplicates
platformView.Touch -= OnPlatformViewTouched;

if (shouldAddTouchEvent)
{
platformView.Touch += OnPlatformViewTouched;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
namespace Maui.Controls.Sample
{
public class DynamicTapGestureGallery : Microsoft.Maui.Controls.ContentView
{
public DynamicTapGestureGallery()
{
AutomationId = nameof(DynamicTapGestureGallery);

var layout = new VerticalStackLayout { Margin = 10, Spacing = 10 };

var singleTapResults = new Label { AutomationId = "DynamicTapGestureResults", Text = "0" };
var singleTapSurface = new Grid()
{
HeightRequest = 100,
WidthRequest = 200,
BackgroundColor = Microsoft.Maui.Graphics.Colors.AliceBlue,
Children = { new Label { Text = "DynamicTapSurface", AutomationId = "DynamicTapSurface" } }
};

var tapCount = 0;

var singleTapRecognizer = new TapGestureRecognizer();
singleTapRecognizer.Tapped += (sender, args) => {
tapCount++;
singleTapResults.Text = tapCount.ToString();

var dynamicTapRecognizer = new TapGestureRecognizer();
dynamicTapRecognizer.Tapped += (sender, args) =>
{
Console.WriteLine("DynamicTap");
};
singleTapSurface.GestureRecognizers.Add(dynamicTapRecognizer);
};
singleTapSurface.GestureRecognizers.Add(singleTapRecognizer);

layout.Add(singleTapSurface);
layout.Add(singleTapResults);

Content = layout;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public GestureRecognizerGallery()
Add(new PointerGestureRecognizerEvents());
Add(new DoubleTapGallery());
Add(new SingleTapGallery());
Add(new DynamicTapGestureGallery());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,30 @@ public void DisabledSingleTap()
var result = App.FindElement("DisabledTapGestureResults").GetText();
ClassicAssert.AreNotEqual("Failed", result);
}

[Test]
[Category(UITestCategories.Gestures)]
public void DynamicallyAddedTapGesturesDontCauseMultipleTapEvents()
{
App.WaitForElement("TargetView");
App.EnterText("TargetView", "DynamicTapGestureGallery");
App.Tap("GoButton");

App.WaitForElement("DynamicTapSurface");
App.Tap("DynamicTapSurface");
App.Tap("DynamicTapSurface");
App.Tap("DynamicTapSurface");

var result = App.FindElement("DynamicTapGestureResults").GetText();

if (int.TryParse(result, out var resultInt))
{
ClassicAssert.AreEqual(3, resultInt);
}
else
{
ClassicAssert.Fail("Failed to parse result as int");
}
}
}
}
Loading