using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using MaterialDesignThemes.Wpf; using Tango.MachineStudio.Common.Notifications; using Tango.MachineStudio.UI.Windows; using System.Windows.Media; using Tango.Core; using System.Collections.ObjectModel; namespace Tango.MachineStudio.UI.Notifications { public class DefaultNotificationProvider : ExtendedObject, INotificationProvider { private static List viewTypes; public ObservableCollection TaskItems { get; private set; } public bool HasTaskItems { get { return TaskItems.Count > 0; } } private TaskItem _currentTaskItem; public TaskItem CurrentTaskItem { get { return _currentTaskItem; } set { _currentTaskItem = value; RaisePropertyChangedAuto(); RaisePropertyChanged(nameof(HasTaskItems)); } } public DefaultNotificationProvider() { TaskItems = new ObservableCollection(); } public bool? ShowDialog(PackIconKind icon, Brush iconColor, string message, bool hasCancel) { MainWindow.Instance.shadowGrid.Visibility = Visibility.Visible; var result = new MessageBoxWindow() { Owner = Application.Current.MainWindow, Message = message, IconKind = icon, IconColor = iconColor, HasCancel = hasCancel }.ShowDialog(); MainWindow.Instance.shadowGrid.Visibility = Visibility.Hidden; return result; } public void ShowModalDialog(Action onAccept, Action onCancel) where View : FrameworkElement where VM : DialogViewVM { var view = Activator.CreateInstance(); DialogWindow dialog = new DialogWindow(); dialog.Owner = Application.Current.MainWindow; dialog.InnerContent = view; MainWindow.Instance.shadowGrid.Visibility = Visibility.Visible; view.Loaded += (x, y) => { VM context = view.DataContext as VM; dialog.DataContext = context; Action onAcceptAction = null; onAcceptAction = new Action(() => { dialog.Close(); onAccept(context); context.Accepted -= onAcceptAction; }); Action onCancelAction = null; onCancelAction = new Action(() => { dialog.Close(); if (onCancel != null) { onCancel(); } context.Canceled -= onCancelAction; }); context.Accepted += onAcceptAction; context.Canceled += onCancelAction; context.OnShow(); }; dialog.ShowDialog(); MainWindow.Instance.shadowGrid.Visibility = Visibility.Hidden; } public void ShowModalDialog(Action onAccept, Action onCancel) where VM : DialogViewVM { String viewName = typeof(VM).Name.Replace("VM", ""); if (viewTypes == null) { viewTypes = AppDomain.CurrentDomain.GetAssemblies().SelectMany(x => x.GetTypes()).ToList(); } var viewType = viewTypes.SingleOrDefault(x => x.Name == viewName); if (viewType == null) { throw new NullReferenceException("Could not locate view " + viewName); } var view = Activator.CreateInstance(viewType) as FrameworkElement; DialogWindow dialog = new DialogWindow(); dialog.Owner = Application.Current.MainWindow; dialog.InnerContent = view; MainWindow.Instance.shadowGrid.Visibility = Visibility.Visible; view.Loaded += (x, y) => { VM context = view.DataContext as VM; dialog.DataContext = context; Action onAcceptAction = null; onAcceptAction = new Action(() => { dialog.Close(); onAccept(context); context.Accepted -= onAcceptAction; }); Action onCancelAction = null; onCancelAction = new Action(() => { dialog.Close(); if (onCancel != null) { onCancel(); } context.Canceled -= onCancelAction; }); context.Accepted += onAcceptAction; context.Canceled += onCancelAction; context.OnShow(); }; dialog.ShowDialog(); MainWindow.Instance.shadowGrid.Visibility = Visibility.Hidden; } public void ShowModalDialog(Action onAccept) where VM : DialogViewVM { ShowModalDialog(onAccept, null); } public void ShowError(string message) { ShowDialog(PackIconKind.Exclamation, Brushes.Red, message, false); } public void ShowInfo(string message) { ShowDialog(PackIconKind.Information, Brushes.Black, message, false); } public bool ShowQuestion(string message) { return ShowDialog(PackIconKind.CommentQuestionOutline, Brushes.Black, message, true).Value; } public void ShowWarnning(string message) { ShowDialog(PackIconKind.Exclamation, Brushes.DarkOrange, message, false); } public void PushTaskItem(TaskItem taskItem) { TaskItems.Add(taskItem); CurrentTaskItem = taskItem; } public TaskItem PushTaskItem(string message) { TaskItem item = new TaskItem(this); item.Message = message; PushTaskItem(item); return item; } public void PopTaskItem(TaskItem taskItem) { TaskItems.Remove(taskItem); if (TaskItems.Count > 0) { CurrentTaskItem = TaskItems.Last(); } RaisePropertyChanged(nameof(HasTaskItems)); } } }