summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.Android/TapGestureHandler.cs
blob: dcd8d6f7712ba5045734a8832b9f76de77dc6edf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using System;
using System.Collections.Generic;
using System.Linq;

namespace Xamarin.Forms.Platform.Android
{
	internal class TapGestureHandler
	{
		public TapGestureHandler(Func<View> getView)
		{
			GetView = getView;
		}

		Func<View> GetView { get; }

		public void OnSingleClick()
		{
			// only handle click if we don't have double tap registered
			if (TapGestureRecognizers(2).Any())
				return;

			OnTap(1);
		}

		public bool OnTap(int count)
		{
			View view = GetView();

			if (view == null)
				return false;

			IEnumerable<TapGestureRecognizer> gestureRecognizers = TapGestureRecognizers(count);
			var result = false;
			foreach (TapGestureRecognizer gestureRecognizer in gestureRecognizers)
			{
				gestureRecognizer.SendTapped(view);
				result = true;
			}

			return result;
		}

		public IEnumerable<TapGestureRecognizer> TapGestureRecognizers(int count)
		{
			View view = GetView();
			if (view == null)
				return Enumerable.Empty<TapGestureRecognizer>();

			return view.GestureRecognizers.GetGesturesFor<TapGestureRecognizer>(recognizer => recognizer.NumberOfTapsRequired == count);
		}
	}
}