<commit_before>using Android.Views;
using System;
using System.Collections.Generic;
using System.Linq;
using Xamarin.Forms;
using Xamarin.Forms.Internals;
using Xamarin.Forms.Platform.Android;

[assembly: ResolutionGroupName("BuildIt")]
[assembly: ExportEffect(typeof(BuildIt.Forms.Controls.Droid.TouchEffect), "TouchEffect")]

// ReSharper disable once CheckNamespace - keeping platform specific namespace
namespace BuildIt.Forms.Controls.Droid
{
    /// <summary>
    /// Touch effect implementation for Android
    /// </summary>
    [Preserve]
    public class TouchEffect : PlatformEffect
    {
        private static Dictionary<Android.Views.View, TouchEffect> viewDictionary =
            new Dictionary<Android.Views.View, TouchEffect>();

        private static Dictionary<int, TouchEffect> idToEffectDictionary =
            new Dictionary<int, TouchEffect>();

        private Android.Views.View view;
        private Element formsElement;
        private Forms.TouchEffect pclTouchEffect;
        private bool capture;
        private Func<double, double> fromPixels;
        private int[] twoIntArray = new int[2];

        /// <inheritdoc />
        /// <summary>
        /// Attach the effect
        /// </summary>
        protected override void OnAttached()
        {
            // Get the Android View corresponding to the Element that the effect is attached to
            view = Control ?? Container;

            // Get access to the TouchEffect class in the PCL
            Forms.TouchEffect touchEffect =
                (Forms.TouchEffect)Element.Effects.
                    FirstOrDefault(e => e is Forms.TouchEffect);

            if (touchEffect == null || view == null)
            {
                return;
            }

            viewDictionary[view] = this;

            formsElement = Element;

            if (formsElement is Xamarin.Forms.View fview)
            {
                ElementHelper.ApplyToAllNested<Xamarin.Forms.View>(fview, e => e.InputTransparent = true, false);
            }

            pclTouchEffect = touchEffect;

            // Save fromPixels function
            fromPixels = view.Context.FromPixels;

            // Set event handler on View
            view.Touch += OnTouch;
            view.ViewDetachedFromWindow += ViewDetachedFromWindow;
        }

        /// <inheritdoc/>
        /// <summary>
        /// Detach the effect
        /// </summary>
        protected override void OnDetached()
        {
            // Method must be overridden
        }

        private void ViewDetachedFromWindow(object sender, Android.Views.View.ViewDetachedFromWindowEventArgs e)
        {
            try
            {
                if (!viewDictionary.ContainsKey(e.DetachedView))
                {
                    return;
                }

                viewDictionary.Remove(e.DetachedView);
                e.DetachedView.Touch -= OnTouch;
                e.DetachedView.ViewDetachedFromWindow -= ViewDetachedFromWindow;
            }
            catch (Exception ex)
            {
                ex.LogError();
            }
        }

        private void OnTouch(object sender, Android.Views.View.TouchEventArgs args)
        {
            try
            {
                // Two object common to all the events<commit_msg>Properly cleaning up event handlers in cases of duplicate effects<commit_after>using Android.Views;
using System;
using System.Collections.Generic;
using System.Linq;
using Xamarin.Forms;
using Xamarin.Forms.Internals;
using Xamarin.Forms.Platform.Android;

[assembly: ResolutionGroupName("BuildIt")]
[assembly: ExportEffect(typeof(BuildIt.Forms.Controls.Droid.TouchEffect), "TouchEffect")]

// ReSharper disable once CheckNamespace - keeping platform specific namespace
namespace BuildIt.Forms.Controls.Droid
{
    /// <summary>
    /// Touch effect implementation for Android
    /// </summary>
    [Preserve]
    public class TouchEffect : PlatformEffect
    {
        private static Dictionary<Android.Views.View, TouchEffect> viewDictionary =
            new Dictionary<Android.Views.View, TouchEffect>();

        private static Dictionary<int, TouchEffect> idToEffectDictionary =
            new Dictionary<int, TouchEffect>();

        private Android.Views.View view;
        private Element formsElement;
        private Forms.TouchEffect pclTouchEffect;
        private bool capture;
        private Func<double, double> fromPixels;
        private int[] twoIntArray = new int[2];

        /// <inheritdoc />
        /// <summary>
        /// Attach the effect
        /// </summary>
        protected override void OnAttached()
        {
            // Get the Android View corresponding to the Element that the effect is attached to
            view = Control ?? Container;

            // Get access to the TouchEffect class in the PCL
            Forms.TouchEffect touchEffect =
                (Forms.TouchEffect)Element.Effects.
                    FirstOrDefault(e => e is Forms.TouchEffect);

            if (touchEffect == null || view == null)
            {
                return;
            }

            if (viewDictionary.ContainsKey(view))
            {
                // This effect may have been added twice due to being used in a ListView that has updated it's collection.
                // cleanup the old effect handlers -RR
                CleanupHandlers(view);
            }

            viewDictionary.Add(view, this);

            formsElement = Element;

            if (formsElement is Xamarin.Forms.View fview)
            {
                ElementHelper.ApplyToAllNested<Xamarin.Forms.View>(fview, e => e.InputTransparent = true, false);
            }

            pclTouchEffect = touchEffect;

            // Save fromPixels function
            fromPixels = view.Context.FromPixels;

            // Set event handler on View
            view.Touch += OnTouch;
            view.ViewDetachedFromWindow += ViewDetachedFromWindow;
        }

        /// <inheritdoc/>
        /// <summary>
        /// Detach the effect
        /// </summary>
        protected override void OnDetached()
        {
            // Method must be overridden
        }

        private void CleanupHandlers(Android.Views.View viewToCleanup)
        {
            try
            {
                if (!viewDictionary.ContainsKey(viewToCleanup))
                {
                    return;
                }

                viewDictionary.Remove(viewToCleanup);
                viewToCleanup.Touch -= OnTouch;
                viewToCleanup.ViewDetachedFromWindow -= ViewDetachedFromWindow;
            }
            catch (Exception ex)
            {
                ex.LogError();
            }
        }

        private void ViewDetachedFromWindow(object sender, Android.Views.View.ViewDetachedFromWindowEventArgs e)
        {
            CleanupHandlers(e.DetachedView);
        }

        private void OnTouch(object sender, Android.Views.View.TouchEventArgs args)
        {
            try
            {
                // Two object common to all the events
{'fraction_non_alphanumeric': 0.08401466027628983, 'fraction_numerical': 0.000281928390188892, 'mean_word_length': 1.7660818713450293, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3317762', 'n_tokens_mistral': 1743, 'n_tokens_neox': 1684, 'n_words': 449}