<commit_before>﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Interactivity;
using Syncfusion.Windows.ComponentModel;
using Syncfusion.Windows.Controls.Grid;

namespace XComponent.Common.UI.Grid.Behavior
{
    public class AutoScrollOnNewRowBehavior : Behavior<GridDataControl>
    {
        protected override void OnAttached()
        {
            AssociatedObject.Loaded += AssociatedObjectOnLoaded;

            base.OnAttached();
        }

        private void AssociatedObjectOnLoaded(object sender, RoutedEventArgs routedEventArgs)
        {
            SubscribeToCollectionChanged();

            // We need to re-subscribe to the event in case the source collection changed..
            AssociatedObject.ItemsSourceChanged += OnItemsSourceChanged;
        }

        private void ViewOnCollectionChanged(object sender, NotifyCollectionChangedEventArgs notifyCollectionChangedEventArgs)
        {
            if (notifyCollectionChangedEventArgs.Action == NotifyCollectionChangedAction.Add)
            {
                AssociatedObject.Model.View.Refresh();

                foreach (var gridControl in AssociatedObject.Model.Views)
                {
                    gridControl.ScrollToBottom();
                }

                // if no element is selected then select the newly added one..
                if (AssociatedObject.SelectedItem == null
                    && notifyCollectionChangedEventArgs.NewItems != null
                    && notifyCollectionChangedEventArgs.NewItems.Count != 0)
                {
                    AssociatedObject.SelectedItem = notifyCollectionChangedEventArgs.NewItems[0];
                }
            }
        }

        private void OnItemsSourceChanged(object sender, SyncfusionRoutedEventArgs args)
        {
            SubscribeToCollectionChanged();
        }

        private void SubscribeToCollectionChanged()
        {
            AssociatedObject.Model.View.CollectionChanged += ViewOnCollectionChanged;
        }

        private void UnSubscribeFromCollectionChanged()
        {
            AssociatedObject.Model.View.CollectionChanged -= ViewOnCollectionChanged;
        }

        protected override void OnDetaching()
        {
            UnSubscribeFromCollectionChanged();
            AssociatedObject.ItemsSourceChanged -= OnItemsSourceChanged;
            AssociatedObject.Loaded -= AssociatedObjectOnLoaded;

            base.OnDetaching();
        }
    }
}<commit_msg>Fix AutoScrollOnNewRowBehavior crash on model loaded<commit_after>﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Interactivity;
using Syncfusion.Windows.ComponentModel;
using Syncfusion.Windows.Controls.Grid;

namespace XComponent.Common.UI.Grid.Behavior
{
    public class AutoScrollOnNewRowBehavior : Behavior<GridDataControl>
    {
        protected override void OnAttached()
        {
            AssociatedObject.ModelLoaded += AssociatedObjectOnLoaded;

            base.OnAttached();
        }

        private void AssociatedObjectOnLoaded(object sender, EventArgs eventArgs)
        {
            AssociatedObject.Model.Initialized += ModelOnInitialized;
        }

        private void ModelOnInitialized(object sender, EventArgs eventArgs)
        {
            SubscribeToCollectionChanged();

            // We need to re-subscribe to the event in case the source collection changed..
            AssociatedObject.ItemsSourceChanged += OnItemsSourceChanged;
        }

        private void ViewOnCollectionChanged(object sender, NotifyCollectionChangedEventArgs notifyCollectionChangedEventArgs)
        {
            if (notifyCollectionChangedEventArgs.Action == NotifyCollectionChangedAction.Add)
            {
                AssociatedObject.Model.View.Refresh();

                foreach (var gridControl in AssociatedObject.Model.Views)
                {
                    gridControl.ScrollToBottom();
                }

                // if no element is selected then select the newly added one..
                if (AssociatedObject.SelectedItem == null
                    && notifyCollectionChangedEventArgs.NewItems != null
                    && notifyCollectionChangedEventArgs.NewItems.Count != 0)
                {
                    AssociatedObject.SelectedItem = notifyCollectionChangedEventArgs.NewItems[0];
                }
            }
        }

        private void OnItemsSourceChanged(object sender, SyncfusionRoutedEventArgs args)
        {
            SubscribeToCollectionChanged();
        }

        private void SubscribeToCollectionChanged()
        {
            AssociatedObject.Model.View.CollectionChanged += ViewOnCollectionChanged;
        }

        private void UnSubscribeFromCollectionChanged()
        {
            AssociatedObject.Model.View.CollectionChanged -= ViewOnCollectionChanged;
        }

        protected override void OnDetaching()
        {
            UnSubscribeFromCollectionChanged();
            AssociatedObject.ItemsSourceChanged -= OnItemsSourceChanged;
            AssociatedObject.Model.Initialized -= ModelOnInitialized;
            AssociatedObject.ModelLoaded -= AssociatedObjectOnLoaded;

            base.OnDetaching();
        }
    }
}
{'fraction_non_alphanumeric': 0.06604803493449782, 'fraction_numerical': 0.000727802037845706, 'mean_word_length': 2.303485576923077, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '1780815', 'n_tokens_mistral': 1261, 'n_tokens_neox': 1193, 'n_words': 251}