From 710fd613134d016a33f244b9576079c824c7addc Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tobias=20Sch=C3=B6ner?= <tobias.stonehead@gmail.com>
Date: Thu, 16 Feb 2023 13:14:38 +0100
Subject: [PATCH] feat: Android: double-tap fact to favorise it

---
 .../UI/FactExplorer/FactFavorisation.cs       | 54 +++++++++++++++----
 1 file changed, 44 insertions(+), 10 deletions(-)

diff --git a/Assets/Scripts/UI/FactExplorer/FactFavorisation.cs b/Assets/Scripts/UI/FactExplorer/FactFavorisation.cs
index b439daf1..662cf3bf 100644
--- a/Assets/Scripts/UI/FactExplorer/FactFavorisation.cs
+++ b/Assets/Scripts/UI/FactExplorer/FactFavorisation.cs
@@ -4,7 +4,7 @@
 using UnityEngine.Events;
 using UnityEngine.EventSystems;
 
-[RequireComponent(typeof(FactWrapper))]
+[RequireComponent(typeof(FactWrapper), typeof(RectTransform))]
 public class FactFavorisation : MonoBehaviour, IPointerClickHandler
 {
     #region InspectorVariables
@@ -20,6 +20,8 @@ public class FactFavorisation : MonoBehaviour, IPointerClickHandler
     #region Variables
     private GameObject favoriteDisplay;
     private Fact fact;
+    private const float COOLDOWN_DURATION = 0.15f; // cooldown of the double touch
+    private bool touchOnCooldown = false;
     #endregion Variables
 
     #region Properties
@@ -34,20 +36,18 @@ public bool IsFavorite
     #region UnityMethods
     public void OnPointerClick(PointerEventData eventData)
     {
-        // TODO: add support for other input systems
         if (eventData.button == PointerEventData.InputButton.Middle)
         {
-            // write to property to invoke event
-            IsFavorite = !IsFavorite;
-
-            // update favorites list
-            if (isFavorite)
-                favorites.Add(fact);
-            else
-                favorites.Remove(fact);
+            ToggleFavorite();
         }
     }
 
+    private void Update()
+    {
+        if (!touchOnCooldown)
+            HandleTouches();
+    }
+
     private void Start()
     {
         fact = transform.GetComponent<FactWrapper>().fact;
@@ -68,6 +68,28 @@ private void Start()
     }
     #endregion UnityMethods
 
+    #region TouchControls
+    private void HandleTouches()
+    {
+        if (Input.touchCount != 1)
+            return;
+
+        var touch = Input.touches[0];
+        if (RectTransformUtility.RectangleContainsScreenPoint(transform.GetComponent<RectTransform>(), touch.position) && touch.tapCount == 2)
+        {
+            StartCoroutine(Cooldown());
+            ToggleFavorite();
+        }
+    }
+
+    private IEnumerator Cooldown()
+    {
+        touchOnCooldown = true;
+        yield return new WaitForSeconds(COOLDOWN_DURATION);
+        touchOnCooldown = false;
+    }
+    #endregion TouchControls
+
     #region Implementation
     private void OnFavoriteChange(Fact changedFact, bool isFavorite)
     {
@@ -82,5 +104,17 @@ private void UpdateDisplay()
     {
         favoriteDisplay.SetActive(isFavorite);
     }
+
+    private void ToggleFavorite()
+    {
+        // write to property to invoke event
+        IsFavorite = !IsFavorite;
+
+        // update favorites list
+        if (isFavorite)
+            favorites.Add(fact);
+        else
+            favorites.Remove(fact);
+    }
     #endregion Implementation
 }
-- 
GitLab