<commit_before>﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ViewStack.cs" company="Catel development team">
//   Copyright (c) 2008 - 2014 Catel development team. All rights reserved.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------


namespace Catel.MVVM.Views
{
    using System;
    using System.Collections.Generic;

#if NETFX_CORE
    using global::Windows.UI.Xaml;
#else
    using System.Windows;
#endif

    /// <summary>
    /// Class containing a view stack and whether the stack is currently loaded.
    /// </summary>
    public class ViewStack
    {
        private readonly ViewStack _parentViewStack;
        private readonly WeakViewInfo _viewInfo;
        private readonly List<ViewStack> _children = new List<ViewStack>();

        private bool _isViewStackLoaded;

        #region Constructors
        /// <summary>
        /// Initializes a new instance of the <see cref="ViewStack" /> class.
        /// </summary>
        /// <param name="view">The view.</param>
        /// <param name="isViewLoaded">if set to <c>true</c>, the view is loaded.</param>
        public ViewStack(IView view, bool isViewLoaded)
            : this(view, isViewLoaded, null) { }

        /// <summary>
        /// Initializes a new instance of the <see cref="ViewStack" /> class.
        /// </summary>
        /// <param name="view">The view.</param>
        /// <param name="isViewLoaded">if set to <c>true</c>, the view is loaded.</param>
        /// <param name="parentViewStack">The parent view stack. Can be <c>null</c> for root view stacks.</param>
        private ViewStack(IView view, bool isViewLoaded, ViewStack parentViewStack)
        {
            Argument.IsNotNull("view", view);

            _viewInfo = new WeakViewInfo(view, isViewLoaded);
            _viewInfo.Loaded += OnViewLoaded;
            _viewInfo.Unloaded += OnViewUnloaded;

            _parentViewStack = parentViewStack;
            if (parentViewStack != null)
            {
                _parentViewStack.ViewStackLoaded += OnParentViewStackLoaded;
            }
        }
        #endregion

        #region Properties
        /// <summary>
        /// Gets or sets a value indicating whether this view stack is outdated, meaning it can be removed.
        /// </summary>
        /// <value><c>true</c> if this instance is outdated; otherwise, <c>false</c>.</value>
        public bool IsOutdated
        {
            get { return !_viewInfo.IsAlive; }
        }

        /// <summary>
        /// Gets a value indicating whether this view stack is loaded.
        /// </summary>
        /// <value><c>true</c> if this instance is view stack loaded; otherwise, <c>false</c>.</value>
        public bool IsViewStackLoaded
        {
            get { return _isViewStackLoaded; }
            private set
            {
                if (_isViewStackLoaded == value)
                {
                    return;
                }

                _isViewStackLoaded = value;

                var eventArgs = new ViewStackPartEventArgs(_viewInfo.View);

                if (_isViewStackLoaded)
                {
                    ViewStackLoaded.SafeInvoke(this, eventArgs);
                }
                else
                {
                    ViewStackUnloaded.SafeInvoke(this, eventArgs);
                }
            }
        }
        #endregion

        #region Events
        /// <summary>
        /// Occurs when the current view has been loaded.
        /// </summary>
        public event EventHandler<ViewStackPartEventArgs> ViewLoaded;

        /// <summary>
        /// Occurs when the current view has been unloaded.
        /// </summary>
        public event EventHandler<ViewStackPartEventArgs> ViewUnloaded;

        /// <summary>
        /// Occurs when one of the child views is loaded.
        /// </summary>
        public event EventHandler<ViewStackPartEventArgs> ViewStackLoaded;

        /// <summary>
        /// Occurs when one of the child views is loaded.
        /// </summary>
        public event EventHandler<ViewStackPartEventArgs> ViewStackUnloaded;
        #endregion

        #region Methods
        /// <summary>
        /// Notifies the that parent is ready to accept loaded messages.
        /// </summary>
        public void NotifyThatParentIsReadyToAcceptLoadedMessages()
        {
            MarkAsLoaded();

            foreach (var child in _children)
            {
                child.NotifyThatParentIsReadyToAcceptLoadedMessages();
            }
        }

        /// <summary>
        /// Adds a new child to the stack.
        /// </summary>
        /// <param name="viewStack">The view stack.</param>
        /// <param name="parentViewStack">The parent view stack.</param>
        /// <returns><c>true</c> if added, <c>false</c> otherwise.</returns>
        public bool AddChild(ViewStack viewStack, ViewStack parentViewStack)
        {
            Argument.IsNotNull("viewStack", viewStack);
            Argument.IsNotNull("parentViewStack", parentViewStack);

            if (ReferenceEquals(this, parentViewStack))<commit_msg>Fixed possible null reference exception in the ViewStack for Silverlight<commit_after>﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ViewStack.cs" company="Catel development team">
//   Copyright (c) 2008 - 2014 Catel development team. All rights reserved.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------


namespace Catel.MVVM.Views
{
    using System;
    using System.Collections.Generic;

#if NETFX_CORE
    using global::Windows.UI.Xaml;
#else
    using System.Windows;
#endif

    /// <summary>
    /// Class containing a view stack and whether the stack is currently loaded.
    /// </summary>
    public class ViewStack
    {
        private readonly ViewStack _parentViewStack;
        private readonly WeakViewInfo _viewInfo;
        private readonly List<ViewStack> _children = new List<ViewStack>();

        private bool _isViewStackLoaded;

        #region Constructors
        /// <summary>
        /// Initializes a new instance of the <see cref="ViewStack" /> class.
        /// </summary>
        /// <param name="view">The view.</param>
        /// <param name="isViewLoaded">if set to <c>true</c>, the view is loaded.</param>
        public ViewStack(IView view, bool isViewLoaded)
            : this(view, isViewLoaded, null) { }

        /// <summary>
        /// Initializes a new instance of the <see cref="ViewStack" /> class.
        /// </summary>
        /// <param name="view">The view.</param>
        /// <param name="isViewLoaded">if set to <c>true</c>, the view is loaded.</param>
        /// <param name="parentViewStack">The parent view stack. Can be <c>null</c> for root view stacks.</param>
        private ViewStack(IView view, bool isViewLoaded, ViewStack parentViewStack)
        {
            Argument.IsNotNull("view", view);

            _viewInfo = new WeakViewInfo(view, isViewLoaded);
            _viewInfo.Loaded += OnViewLoaded;
            _viewInfo.Unloaded += OnViewUnloaded;

            _parentViewStack = parentViewStack;
            if (parentViewStack != null)
            {
                _parentViewStack.ViewStackLoaded += OnParentViewStackLoaded;
            }
        }
        #endregion

        #region Properties
        /// <summary>
        /// Gets or sets a value indicating whether this view stack is outdated, meaning it can be removed.
        /// </summary>
        /// <value><c>true</c> if this instance is outdated; otherwise, <c>false</c>.</value>
        public bool IsOutdated
        {
            get { return !_viewInfo.IsAlive; }
        }

        /// <summary>
        /// Gets a value indicating whether this view stack is loaded.
        /// </summary>
        /// <value><c>true</c> if this instance is view stack loaded; otherwise, <c>false</c>.</value>
        public bool IsViewStackLoaded
        {
            get { return _isViewStackLoaded; }
            private set
            {
                if (_isViewStackLoaded == value)
                {
                    return;
                }

                _isViewStackLoaded = value;

                var eventArgs = new ViewStackPartEventArgs(_viewInfo.View);

                if (_isViewStackLoaded)
                {
                    ViewStackLoaded.SafeInvoke(this, eventArgs);
                }
                else
                {
                    ViewStackUnloaded.SafeInvoke(this, eventArgs);
                }
            }
        }
        #endregion

        #region Events
        /// <summary>
        /// Occurs when the current view has been loaded.
        /// </summary>
        public event EventHandler<ViewStackPartEventArgs> ViewLoaded;

        /// <summary>
        /// Occurs when the current view has been unloaded.
        /// </summary>
        public event EventHandler<ViewStackPartEventArgs> ViewUnloaded;

        /// <summary>
        /// Occurs when one of the child views is loaded.
        /// </summary>
        public event EventHandler<ViewStackPartEventArgs> ViewStackLoaded;

        /// <summary>
        /// Occurs when one of the child views is loaded.
        /// </summary>
        public event EventHandler<ViewStackPartEventArgs> ViewStackUnloaded;
        #endregion

        #region Methods
        /// <summary>
        /// Notifies the that parent is ready to accept loaded messages.
        /// </summary>
        public void NotifyThatParentIsReadyToAcceptLoadedMessages()
        {
            MarkAsLoaded();

            CheckForOutdatedChildren();

            foreach (var child in _children)
            {
                if (!child.IsOutdated)
                {
                    child.NotifyThatParentIsReadyToAcceptLoadedMessages();
                }
            }
        }

        /// <summary>
        /// Adds a new child to the stack.
        /// </summary>
        /// <param name="viewStack">The view stack.</param>
        /// <param name="parentViewStack">The parent view stack.</param>
        /// <returns><c>true</c> if added, <c>false</c> otherwise.</returns>
        public bool AddChild(ViewStack viewStack, ViewStack parentViewStack)
        {
            Argument.IsNotNull("viewStack", viewStack);
            Argument.IsNotNull("parentViewStack", parentViewStack);

            if (ReferenceEquals(this, parentViewStack))
{'fraction_non_alphanumeric': 0.14699020065328977, 'fraction_numerical': 0.0014932337844143723, 'mean_word_length': 1.5741052125870767, 'pattern_counts': {'":': 0, '<': 143, '<?xml version=': 0, '>': 143, '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': '2365862', 'n_tokens_mistral': 2650, 'n_tokens_neox': 2549, 'n_words': 735}