<commit_before>﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace WpfUtils
{
    /// <summary>
    /// The view model base class
    /// </summary>
    public class BaseVM : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// Provides a convenient way to send notifications.  When called inside a property,
        /// it can be called safely with the default paramater
        /// </summary>
        /// <param name="property"></param>
        protected void NotifyPropertyChanged([CallerMemberName] string property = "no property")
        {
            if (this.PropertyChanged != null)
                this.PropertyChanged(this, new PropertyChangedEventArgs(property));
        }
    
    }
}<commit_msg>Added convenience functions to the BaseVM to remove much of the repetitive code in the view model properties<commit_after>﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace WpfUtils
{
    /// <summary>
    /// The view model base class
    /// </summary>
    public class BaseVM : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// Provides a convenient way to send notifications.  When called inside a property,
        /// it can be called safely with the default paramater
        /// </summary>
        /// <param name="property">The name of the property that is changing. This defaults to [CallerMemberName]</param>
        protected void NotifyPropertyChanged([CallerMemberName] string property = "no property")
        {
            if (this.PropertyChanged != null)
                this.PropertyChanged(this, new PropertyChangedEventArgs(property));
        }
    
        /// <summary>
        /// A convenience function for simplifying setters in derived classes.  Uses reflection to 
        /// determine appropriate properties to set
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="target">The model that contains the data backing the property</param>
        /// <param name="newValue">The value parameter of the setter</param>
        /// <param name="allowNull">Whether the property can be set to null or not</param>
        /// <param name="secondaryAction">Any action that should be performed after the property value is changed</param>
        /// <param name="targetProperty">The name of the property on the target object.  This must be filled out if the
        /// property on the model is not the same as on the view model</param>
        /// <param name="vmProperty">The name of the property that should be passed to NotifyPropertyChanged.  This must be
        /// filled out if this function is not called from the property itself</param>
        protected void PropertySet<T>(object target, T newValue, 
            bool allowNull = false, Action secondaryAction = null,
            [CallerMemberName] string targetProperty = "default property", 
            [CallerMemberName] string vmProperty = "view model property")
        {
            var tType = target.GetType();
            // get our target property's value to work with
            var tProp = tType.GetProperty(targetProperty);
            var tValue = tProp.GetValue(target);
            if (tValue.GetType() != typeof(T))
                throw new ArgumentException("Referenced property is not of type: " + typeof(T).FullName);
            if (!allowNull && newValue == null)
                return;
            // prefer using the comparable interface
            var tValComp = tValue as IComparable<T>;
            if (tValComp != null)
            {
                if (tValComp.CompareTo(newValue) != 0)
                {
                    tProp.SetValue(target, newValue);
                    this.NotifyPropertyChanged(vmProperty);
                }
            }
            // fall back on object equality
            else if (!tValue.Equals(newValue))
            {
                tProp.SetValue(target, newValue);
                this.NotifyPropertyChanged(vmProperty);
            }
            else
                return; // do nothing since we did not have to change the value
            
            // perform any other actions requested of the setter
            if (secondaryAction != null)
                secondaryAction();
        }
    }
}
{'fraction_non_alphanumeric': 0.08950418544752092, 'fraction_numerical': 0.000214638334406525, 'mean_word_length': 2.1722260040844112, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, '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': '6018203', 'n_tokens_mistral': 1106, 'n_tokens_neox': 1089, 'n_words': 397}