<commit_before>﻿using System;
using System.Linq;
#if NETFX_CORE
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
#else
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interop;
#endif

namespace Caliburn.Light
{
    /// <summary>
    /// Locates view and view-model instances.
    /// </summary>
    public class ViewModelLocator : IViewModelLocator
    {
        private static readonly ILogger Log = LogManager.GetLogger(typeof(ViewModelLocator));

        private readonly IViewModelTypeResolver _typeResolver;
        private readonly IServiceLocator _serviceLocator;

        /// <summary>
        /// Creates an instance of <see cref="ViewModelLocator"/>.
        /// </summary>
        /// <param name="typeResolver">The type resolver.</param>
        /// <param name="serviceLocator">The service locator.</param>
        public ViewModelLocator(IViewModelTypeResolver typeResolver, IServiceLocator serviceLocator)
        {
            if (typeResolver == null)
                throw new ArgumentNullException(nameof(typeResolver));
            if (serviceLocator == null)
                throw new ArgumentNullException(nameof(serviceLocator));

            _typeResolver = typeResolver;
            _serviceLocator = serviceLocator;
        }

        /// <summary>
        /// Locates the view for the specified model instance.
        /// </summary>
        /// <param name="model">the model instance.</param>
        /// <param name="context">The context (or null).</param>
        /// <returns>The view.</returns>
        public UIElement LocateForModel(object model, string context)
        {
            if (model == null)
                throw new ArgumentNullException(nameof(model));

            var view = TryGetViewFromViewAware(model, context);
            if (view != null)
            {
                Log.Info("Using cached view for {0}.", model);
                return view;
            }

            var modelType = model.GetType();
            var viewType = _typeResolver.GetViewType(modelType, context);
            if (viewType == null)
            {
                Log.Error("Cannot find view for {0}.", modelType);
                return new TextBlock { Text = string.Format("Cannot find view for {0}.", modelType) };
            }

            view = _serviceLocator.GetAllInstances(viewType).FirstOrDefault() as UIElement;
            if (view == null)
            {
                Log.Error("Cannot create {0}.", viewType);
                return new TextBlock { Text = string.Format("Cannot create {0}.", viewType) };
            }

            return view;
        }

        private UIElement TryGetViewFromViewAware(object model, string context)
        {
            var viewAware = model as IViewAware;
            if (viewAware != null)
            {
                var view = viewAware.GetView(context) as UIElement;
                if (view != null)
                {
#if !SILVERLIGHT && !NETFX_CORE
                    var windowCheck = view as Window;
                    if (windowCheck == null ||
                        (!windowCheck.IsLoaded && !(new WindowInteropHelper(windowCheck).Handle == IntPtr.Zero)))
                    {
                        return view;
                    }
#else
                    return view;
#endif
                }
            }

            return null;
        }

        /// <summary>
        /// Locates the view model for the specified view instance.
        /// </summary>
        /// <param name="view">The view instance.</param>
        /// <returns>The view model.</returns>
        public object LocateForView(UIElement view)
        {
            if (view == null)
                throw new ArgumentNullException(nameof(view));

            var frameworkElement = view as FrameworkElement;
            if (frameworkElement != null && frameworkElement.DataContext != null)
            {
                Log.Info("Using current data context for {0}.", view);
                return frameworkElement.DataContext;
            }

            var viewType = view.GetType();
            var modelType = _typeResolver.GetModelType(viewType);
            if (modelType == null)
            {
                Log.Error("Cannot find model for {0}.", viewType);
                return null;
            }

            var model = _serviceLocator.GetAllInstances(modelType).FirstOrDefault();
            if (model == null)
            {
                Log.Error("Cannot create {0}.", modelType);
                return null;
            }

            return model;
        }
    }
}<commit_msg>refactor ViewModelLocator to better match v2<commit_after>﻿using System;
using System.Linq;
using System.Reflection;
#if NETFX_CORE
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
#else
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interop;
#endif

namespace Caliburn.Light
{
    /// <summary>
    /// Locates view and view-model instances.
    /// </summary>
    public class ViewModelLocator : IViewModelLocator
    {
        private static readonly ILogger Log = LogManager.GetLogger(typeof(ViewModelLocator));

        private readonly IViewModelTypeResolver _typeResolver;
        private readonly IServiceLocator _serviceLocator;

        /// <summary>
        /// Creates an instance of <see cref="ViewModelLocator"/>.
        /// </summary>
        /// <param name="typeResolver">The type resolver.</param>
        /// <param name="serviceLocator">The service locator.</param>
        public ViewModelLocator(IViewModelTypeResolver typeResolver, IServiceLocator serviceLocator)
        {
            if (typeResolver == null)
                throw new ArgumentNullException(nameof(typeResolver));
            if (serviceLocator == null)
                throw new ArgumentNullException(nameof(serviceLocator));

            _typeResolver = typeResolver;
            _serviceLocator = serviceLocator;
        }

        /// <summary>
        /// Locates the view for the specified model instance.
        /// </summary>
        /// <param name="model">the model instance.</param>
        /// <param name="context">The context (or null).</param>
        /// <returns>The view.</returns>
        public UIElement LocateForModel(object model, string context)
        {
            if (model == null)
                throw new ArgumentNullException(nameof(model));

            var view = TryGetViewFromViewAware(model, context);
            if (view != null)
            {
                Log.Info("Using cached view for {0}.", model);
                return view;
            }

            var modelType = model.GetType();
            var viewType = _typeResolver.GetViewType(modelType, context);
            if (viewType == null)
            {
                Log.Error("Cannot find view for {0}.", modelType);
                return new TextBlock { Text = string.Format("Cannot find view for {0}.", modelType) };
            }

            view = _serviceLocator.GetAllInstances(viewType).FirstOrDefault() as UIElement;
            if (view != null)
            {
                return view;
            }

            var viewTypeInfo = viewType.GetTypeInfo();
            var uiElementInfo = typeof(UIElement).GetTypeInfo();
            if (viewTypeInfo.IsInterface || viewTypeInfo.IsAbstract || !uiElementInfo.IsAssignableFrom(viewTypeInfo))
            {
                Log.Error("Cannot create {0}.", viewType);
                return new TextBlock { Text = string.Format("Cannot create {0}.", viewType) };
            }

            view = (UIElement)Activator.CreateInstance(viewType);
            return view;
        }

        private UIElement TryGetViewFromViewAware(object model, string context)
        {
            var viewAware = model as IViewAware;
            if (viewAware != null)
            {
                var view = viewAware.GetView(context) as UIElement;
                if (view != null)
                {
#if !SILVERLIGHT && !NETFX_CORE
                    var windowCheck = view as Window;
                    if (windowCheck == null ||
                        (!windowCheck.IsLoaded && !(new WindowInteropHelper(windowCheck).Handle == IntPtr.Zero)))
                    {
                        return view;
                    }
#else
                    return view;
#endif
                }
            }

            return null;
        }

        /// <summary>
        /// Locates the view model for the specified view instance.
        /// </summary>
        /// <param name="view">The view instance.</param>
        /// <returns>The view model.</returns>
        public object LocateForView(UIElement view)
        {
            if (view == null)
                throw new ArgumentNullException(nameof(view));

            var frameworkElement = view as FrameworkElement;
            if (frameworkElement != null && frameworkElement.DataContext != null)
            {
                Log.Info("Using current data context for {0}.", view);
                return frameworkElement.DataContext;
            }

            var viewType = view.GetType();
            var modelType = _typeResolver.GetModelType(viewType);
            if (modelType == null)
            {
                Log.Error("Cannot find model for {0}.", viewType);
                return null;
            }

            var model = _serviceLocator.GetInstance(modelType);
            if (model == null)
            {
                Log.Error("Cannot locate {0}.", modelType);
                return null;
            }

            return model;
        }
    }
}
{'fraction_non_alphanumeric': 0.10258271963489264, 'fraction_numerical': 0.001763302561974899, 'mean_word_length': 1.6813125695216908, 'pattern_counts': {'":': 0, '<': 49, '<?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': '4723619', 'n_tokens_mistral': 2435, 'n_tokens_neox': 2347, 'n_words': 621}