<commit_before>﻿using System;
using System.Linq;
using UnityEngine;
using UnityUI.Internal;

namespace UnityUI.Binding
{
    /// <summary>
    /// Base class for binders to Unity MonoBehaviours.
    /// </summary>
    public abstract class AbstractMemberBinding : MonoBehaviour, IMemberBinding
    {
        /// <summary>
        /// Name of the view model to bind.
        /// </summary>
        public string viewModelName;

        /// <summary>
        /// Initialise this binding. Used when we first start the scene.
        /// Detaches any attached view models, finds available view models afresh and then connects the binding.
        /// </summary>
        public virtual void Init()
        {
            Disconnect();

            Connect();
        }

        /// <summary>
        /// Helper method to get the voiw model object from the connected ViewModelBinding.
        /// </summary>
        protected object GetViewModel()
        {
            return GetViewModelBinding().BoundViewModel;
        }

        /// <summary>
        /// Scan up the hierarchy to get the view model corrosponding to the name set in viewModelName.
        /// </summary>
        protected IViewModelBinding GetViewModelBinding()
        {
            var trans = transform;
            while (trans != null)
            {
                var components = trans.GetComponents<MonoBehaviour>();
                var boundMonoBehaviour = components.Where(component => component.GetType().Name == viewModelName)
                    .FirstOrDefault();
                if (boundMonoBehaviour != null)
                {
                    return new MonoBehaviourBinding(boundMonoBehaviour);
                }

                var newViewModelBinding = components                    
                    .Select(component => component as IViewModelBinding)
                    .Where(component => component != null)
                    .Where(viewModelBinding => viewModelBinding.ViewModelTypeName == viewModelName && (object)viewModelBinding != this)
                    .FirstOrDefault();
                if (newViewModelBinding != null)
                {
                    return newViewModelBinding;
                }

                trans = trans.parent;
            }

            throw new ApplicationException(string.Format("Tried to get view {0} but it could not be found on "
                + "object {1}. Check that a ViewModelBinding for that view exists further up in "
                + "the scene hierarchy. ", viewModelName, gameObject.name)
            );
        }

        /// <summary>
        /// Find the type of the adapter with the specified name and create it.
        /// </summary>
        protected IAdapter CreateAdapter(string adapterTypeName)
        {
            if (string.IsNullOrEmpty(adapterTypeName))
            {
                return null;
            }

            //todo: Need to cache adapter types.
            var adapterType = TypeResolver.FindAdapterType(adapterTypeName);
            if (adapterType == null)
            {
                throw new ApplicationException("Could not find adapter type '" + adapterTypeName + "'.");
            }

            if (!typeof(IAdapter).IsAssignableFrom(adapterType))
            {
                throw new ApplicationException("Type '" + adapterTypeName + "' does not implement IAdapter and " +
                    "cannot be used as an adapter.");
            }

            return (IAdapter)Activator.CreateInstance(adapterType);
        }

        /// <summary>
        /// Connect to all the attached view models
        /// </summary>
        public abstract void Connect();

        /// <summary>
        /// Disconnect from all attached view models.
        /// </summary>
        public abstract void Disconnect();

        /// <summary>
        /// Clean up when the game object is destroyed.
        /// </summary>
        public void OnDestroy()
        {<commit_msg>Each component now initializes itself.  BindingRoot use to do this but it has been removed.<commit_after>﻿using System;
using System.Linq;
using UnityEngine;
using UnityUI.Internal;

namespace UnityUI.Binding
{
    /// <summary>
    /// Base class for binders to Unity MonoBehaviours.
    /// </summary>
    public abstract class AbstractMemberBinding : MonoBehaviour, IMemberBinding
    {
        /// <summary>
        /// Name of the view model to bind.
        /// </summary>
        public string viewModelName;

        /// <summary>
        /// Initialise this binding. Used when we first start the scene.
        /// Detaches any attached view models, finds available view models afresh and then connects the binding.
        /// </summary>
        public virtual void Init()
        {
            Disconnect();

            Connect();
        }

        /// <summary>
        /// Helper method to get the voiw model object from the connected ViewModelBinding.
        /// </summary>
        protected object GetViewModel()
        {
            return GetViewModelBinding().BoundViewModel;
        }

        /// <summary>
        /// Scan up the hierarchy to get the view model corrosponding to the name set in viewModelName.
        /// </summary>
        protected IViewModelBinding GetViewModelBinding()
        {
            var trans = transform;
            while (trans != null)
            {
                var components = trans.GetComponents<MonoBehaviour>();
                var boundMonoBehaviour = components.Where(component => component.GetType().Name == viewModelName)
                    .FirstOrDefault();
                if (boundMonoBehaviour != null)
                {
                    return new MonoBehaviourBinding(boundMonoBehaviour);
                }

                var newViewModelBinding = components                    
                    .Select(component => component as IViewModelBinding)
                    .Where(component => component != null)
                    .Where(viewModelBinding => viewModelBinding.ViewModelTypeName == viewModelName && (object)viewModelBinding != this)
                    .FirstOrDefault();
                if (newViewModelBinding != null)
                {
                    return newViewModelBinding;
                }

                trans = trans.parent;
            }

            throw new ApplicationException(string.Format("Tried to get view {0} but it could not be found on "
                + "object {1}. Check that a ViewModelBinding for that view exists further up in "
                + "the scene hierarchy. ", viewModelName, gameObject.name)
            );
        }

        /// <summary>
        /// Find the type of the adapter with the specified name and create it.
        /// </summary>
        protected IAdapter CreateAdapter(string adapterTypeName)
        {
            if (string.IsNullOrEmpty(adapterTypeName))
            {
                return null;
            }

            //todo: Need to cache adapter types.
            var adapterType = TypeResolver.FindAdapterType(adapterTypeName);
            if (adapterType == null)
            {
                throw new ApplicationException("Could not find adapter type '" + adapterTypeName + "'.");
            }

            if (!typeof(IAdapter).IsAssignableFrom(adapterType))
            {
                throw new ApplicationException("Type '" + adapterTypeName + "' does not implement IAdapter and " +
                    "cannot be used as an adapter.");
            }

            return (IAdapter)Activator.CreateInstance(adapterType);
        }

        /// <summary>
        /// Connect to all the attached view models
        /// </summary>
        public abstract void Connect();

        /// <summary>
        /// Disconnect from all attached view models.
        /// </summary>
        public abstract void Disconnect();

        public void Awake()
        {
            Connect();
        }

        /// <summary>
        /// Clean up when the game object is destroyed.
        /// </summary>
        public void OnDestroy()
        {
{'fraction_non_alphanumeric': 0.09044790893343232, 'fraction_numerical': 0.0004949269982677555, 'mean_word_length': 1.5635902315255312, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 49, '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': '5311591', 'n_tokens_mistral': 1902, 'n_tokens_neox': 1785, 'n_words': 592}