Last-Modified: Sun, 05 Jul 2026 00:13:04 GMT Expires: Wed, 02 Jul 2036 00:13:04 GMT Several PPC bug fixes. - Tango - Twine softwares
aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Developer/Views/EmbroideryDisplayView.xaml
diff options
context:
space:
mode:
authorRoy Ben Shabat <Roy.mail.net@gmail.com>2020-04-21 00:59:11 +0300
committerRoy Ben Shabat <Roy.mail.net@gmail.com>2020-04-21 00:59:11 +0300
commitd74d21c1ef5c908aa98d7d067a94812a97257cfa (patch)
tree9630f762ba8f14a755952eb7aa62a714989058de /Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Developer/Views/EmbroideryDisplayView.xaml
parentf06785ae3312a82b8f2237b55e8c4eeb174c3519 (diff)
downloadTango-d74d21c1ef5c908aa98d7d067a94812a97257cfa.tar.gz
Tango-d74d21c1ef5c908aa98d7d067a94812a97257cfa.zip
Several PPC bug fixes.
Diffstat (limited to 'Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Developer/Views/EmbroideryDisplayView.xaml')
0 files changed, 0 insertions, 0 deletions
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
using Tango.Core.Commands;
using Tango.Logging;
using Tango.MachineStudio.Common.Notifications;
using Tango.MachineStudio.Logging.Navigation;
using Tango.MachineStudio.Logging.Parsing;
using Tango.MachineStudio.Logging.Views;
using Tango.SharedUI;

namespace Tango.MachineStudio.Logging.ViewModels
{
    public class ApplicationLogsViewVM : ViewModel
    {
        private ApplicationLogFileParser _parser;
        private List<LogFile> _logFiles;
        private INotificationProvider _notification;
        private bool _dialog_shown;
        private bool _is_debug;

        private ControlledObservableCollection<LogItemBase> _realTimeLogs;
        private List<LogItemBase> _pausedLogs;

        private ControlledObservableCollection<LogItemBase> _logs;
        public ControlledObservableCollection<LogItemBase> Logs
        {
            get { return _logs; }
            set { _logs = value; RaisePropertyChangedAuto(); }
        }

        private ICollectionView _logsViewSource;
        public ICollectionView LogsViewSource
        {
            get { return _logsViewSource; }
            set { _logsViewSource = value; RaisePropertyChangedAuto(); }
        }

        private String _filter;
        public String Filter
        {
            get { return _filter; }
            set
            {
                _filter = value;
                RaisePropertyChangedAuto();

                if (RealTimePaused)
                {
                    LogsViewSource.Refresh();
                }
            }
        }

        private bool _displayDebug;
        /// <summary>
        /// Gets or sets a value indicating whether display debug logs.
        /// </summary>
        public bool DisplayDebug
        {
            get { return _displayDebug; }
            set { _displayDebug = value; RaisePropertyChangedAuto(); }
        }

        private LogItemBase _selectedLog;
        public LogItemBase SelectedLog
        {
            get { return _selectedLog; }
            set { _selectedLog = value; RaisePropertyChangedAuto(); OnSelectedLogChanged(); }
        }

        private ObservableCollection<DateTime> _dates;
        public ObservableCollection<DateTime> Dates
        {
            get { return _dates; }
            set { _dates = value; RaisePropertyChangedAuto(); }
        }

        private DateTime _selectedDate;
        public DateTime SelectedDate
        {
            get { return _selectedDate; }
            set { _selectedDate = value; RaisePropertyChangedAuto(); OnSelectedDateChanged(); }
        }

        private DateTime _minDate;
        public DateTime MinDate
        {
            get { return _minDate; }
            set { _minDate = value; RaisePropertyChangedAuto(); }
        }

        private DateTime _maxDate;
        public DateTime MaxDate
        {
            get { return _maxDate; }
            set { _maxDate = value; RaisePropertyChangedAuto(); }
        }


        private bool _isRealTime;
        public bool IsRealTime
        {
            get { return _isRealTime; }
            set { _isRealTime = value; RaisePropertyChangedAuto(); OnSelectedDateChanged(); }
        }

        private bool _realTimePaused;
        public bool RealTimePaused
        {
            get { return _realTimePaused; }
            set
            {
                _realTimePaused = value;
                RaisePropertyChangedAuto();

                if (!_realTimePaused)
                {
                    LogsViewSource.Filter = null;
                    _realTimeLogs.InsertRange(0, _pausedLogs);
                    _pausedLogs.Clear();
                }
                else
                {
                    LogsViewSource.Filter = (x) =>
                    {
                        if (String.IsNullOrWhiteSpace(Filter)) return true;
                        LogItemBase log = x as LogItemBase;
                        return log.Message.ToLower().Contains(Filter.ToLower());
                    };
                }
            }
        }

        public RelayCommand NavigateToHomeCommand { get; set; }

        public RelayCommand ToggleRealTimePaused { get; set; }

        public RelayCommand ClearRealTimeLogsCommand { get; set; }

        public ApplicationLogsViewVM(LoggingNavigationManager navigation, INotificationProvider notification)
        {
            _notification = notification;

            NavigateToHomeCommand = new RelayCommand(() => navigation.NavigateTo(LoggingNavigationView.HomeView));

            _parser = new ApplicationLogFileParser();

            _logFiles = _parser.GetLogFiles();
            Dates = new ObservableCollection<DateTime>(_logFiles.Select(x => x.DateTime).OrderBy(x => x));

            _realTimeLogs = new ControlledObservableCollection<LogItemBase>();
            _pausedLogs = new List<LogItemBase>();

            IsRealTime = true;
            RealTimePaused = true;

            SelectedDate = Dates.LastOrDefault();

            if (Dates.Count > 0)
            {
                MinDate = Dates.Min();
                MaxDate = Dates.Max();
            }

            LogManager.NewLog += LogManager_NewLog;

            ToggleRealTimePaused = new RelayCommand(() => RealTimePaused = !RealTimePaused);

            ClearRealTimeLogsCommand = new RelayCommand(() => { _realTimeLogs.Clear(); });

            _is_debug = LogManager.Categories.Contains(LogCategory.Debug);
        }

        private void LogManager_NewLog(object sender, LogItemBase log)
        {
            if (log.Category == LogCategory.Debug && !DisplayDebug) return;

            if (!RealTimePaused)
            {
                InvokeUI(() =>
                {
                    _realTimeLogs.Insert(0, log);
                });
            }
            else
            {
                _pausedLogs.Add(log);
            }
        }

        private async void OnSelectedDateChanged()
        {
            if (IsRealTime)
            {
                Logs = _realTimeLogs;
            }
            else
            {
                List<LogItemBase> logs = new List<LogItemBase>();

                using (_notification.PushTaskItem("Loading application logs..."))
                {
                    await Task.Factory.StartNew(() =>
                    {
                        foreach (var logFile in _logFiles.Where(x => x.DateTime.Date == SelectedDate.Date))
                        {
                            logs.AddRange(_parser.Parse(logFile));
                        }
                    });
                }

                Logs = new ControlledObservableCollection<LogItemBase>(logs);
            }

            LogsViewSource = CollectionViewSource.GetDefaultView(Logs);
        }

        private void OnSelectedLogChanged()
        {
            if (SelectedLog != null && !_dialog_shown)
            {
                _dialog_shown = true;
                _notification.ShowModalDialog<LogDetailsViewVM, ApplicationLogDetailsView>(new LogDetailsViewVM(SelectedLog), (x) =>
                {

                }, () =>
                {
                    _dialog_shown = false;
                });
            }
        }
    }
}