using LiteDB;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;
using Tango.Core.Commands;
using Tango.Core.IO;
using Tango.Logging;
namespace Tango.Core
{
///
/// Represents an extension to the standard core object with support for property and relay commands changed event.
///
///
[Serializable]
public class ExtendedObject : INotifyPropertyChanged
{
///
/// Occurs when after InvalidateRelayCommands is called.
///
[field: NonSerialized]
public event EventHandler RelayCommandsInvalidated;
///
/// Gets the default log manager.
///
[JsonIgnore]
[NotMapped]
[BsonIgnore]
public LogManager LogManager
{
get { return LogManager.Default; }
}
///
/// Gets the temporary manager.
///
[JsonIgnore]
[NotMapped]
[BsonIgnore]
public TemporaryManager TemporaryManager
{
get { return TemporaryManager.Default; }
}
///
/// Occurs when a property has changed.
///
[field: NonSerialized]
public event PropertyChangedEventHandler PropertyChanged;
///
/// Raises the property changed event.
///
/// Name of the property.
protected virtual void RaisePropertyChanged(String propName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName));
}
///
/// Raises the property changed event.
///
/// Name of the property.
protected virtual void RaisePropertyChangedAuto([CallerMemberName] string caller = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(caller));
}
///
/// Raises all relay commands CanExecute methods in the current instance.
///
protected virtual void InvalidateRelayCommands()
{
InvokeUI(() =>
{
foreach (var prop in this.GetType().GetProperties().Where(x => typeof(RelayCommand).IsAssignableFrom(x.PropertyType)))
{
var value = prop.GetValue(this) as RelayCommand;
if (value != null)
{
value.RaiseCanExecuteChanged();
}
}
RelayCommandsInvalidated?.Invoke(this, new EventArgs());
});
}
///
/// Gets a value indicating whether we are currently in VS design mode.
///
[NotMapped]
[JsonIgnore]
[BsonIgnore]
public bool DesignMode
{
get { return (DesignerProperties.GetIsInDesignMode(new DependencyObject())); }
}
///
/// Invokes the specified action on the UI Thread.
///
/// The action.
protected virtual void InvokeUI(Action action)
{
if (Application.Current != null)
{
Application.Current.Dispatcher.BeginInvoke(action);
}
else
{
action();
}
}
///
/// Invokes the specified action on the UI Thread when context is idle.
///
/// The action.
protected virtual void InvokeUIOnIdle(Action action)
{
Application.Current.Dispatcher.BeginInvoke(action, DispatcherPriority.ContextIdle);
}
///
/// Invokes the specified action on the UI Thread.
///
/// The action.
protected virtual void InvokeUINow(Action action)
{
Application.Current.Dispatcher.Invoke(action);
}
}
}