Esempio n. 1
0
        public ManagerForm(LogCache logCache, ApiData apiData)
        {
            LogCache = logCache ?? throw new ArgumentNullException(nameof(logCache));
            ApiData  = apiData ?? throw new ArgumentNullException(nameof(apiData));

            // Background processors
            var dpsReportUploader = new DpsReportUploader();

            UploadProcessor   = new UploadProcessor(dpsReportUploader, LogCache);
            ApiProcessor      = new ApiProcessor(ApiData, new Gw2Client());
            LogDataProcessor  = new LogDataProcessor(LogCache, ApiProcessor, LogAnalytics);
            LogNameProvider   = new TranslatedLogNameProvider(GameLanguage.English);
            LogCacheAutoSaver = LogCacheAutoSaver.StartNew(logCache, LogCacheAutoSavePeriod);

            LogDataProcessor.StoppingWithError += (sender, args) =>
            {
                Application.Instance.InvokeAsync(() => MessageBox.Show(this,
                                                                       $"The background processor for logs has failed critically. " +
                                                                       $"Please report the following error:\n\nException: {args.Exception}", "Error", MessageBoxType.Error));
            };

            ApiProcessor.StoppingWithError += (sender, args) =>
            {
                Application.Instance.InvokeAsync(() => MessageBox.Show(this,
                                                                       $"The background processor for API requests has failed critically. " +
                                                                       $"Please report the following error:\n\nException: {args.Exception}", "Error", MessageBoxType.Error));
            };

            UploadProcessor.StoppingWithError += (sender, args) =>
            {
                Application.Instance.InvokeAsync(() => MessageBox.Show(this,
                                                                       $"The background processor for log uploads has failed critically. " +
                                                                       $"Please report the following error:\n\nException: {args.Exception}", "Error", MessageBoxType.Error));
            };

            Filters = new LogFilters(new SettingsFilters());
            Filters.PropertyChanged += (sender, args) => logsFiltered.Refresh();

            if (Settings.UseGW2Api)
            {
                ApiProcessor.StartBackgroundTask();
            }

            Settings.UseGW2ApiChanged += (sender, args) =>
            {
                if (Settings.UseGW2Api)
                {
                    ApiProcessor.StartBackgroundTask();
                }
                else
                {
                    ApiProcessor.StopBackgroundTask();
                }
            };

            Settings.DpsReportDomainChanged += (sender, args) => { dpsReportUploader.Domain = Settings.DpsReportDomain; };

            // Form layout
            Icon       = Resources.GetProgramIcon();
            Title      = "arcdps Log Manager";
            ClientSize = new Size(1300, 768);
            var formLayout = new DynamicLayout();

            Content = formLayout;

            Menu = ConstructMenuBar();

            formLayout.BeginVertical(new Padding(5), yscale: false);
            {
                formLayout.Add(ConstructMainSplitter(), yscale: true);
                formLayout.Add(ConstructStatusPanel());
            }
            formLayout.EndVertical();

            // Event handlers
            ApiProcessor.Processed += (sender, args) =>
            {
                bool last = args.CurrentScheduledItems == 0;

                if (last)
                {
                    ApiData.SaveDataToFile();
                }
            };

            Settings.LogRootPathChanged += (sender, args) => Application.Instance.Invoke(ReloadLogs);

            Closing += (sender, args) =>
            {
                if (LogCache?.ChangedSinceLastSave ?? false)
                {
                    LogCache?.SaveToFile();
                }

                ApiData?.SaveDataToFile();
            };
            LogSearchFinished += (sender, args) =>
            {
                var updates = LogDataUpdater.GetUpdates(logs).ToList();
                if (updates.Count > 0)
                {
                    new ProcessingUpdateDialog(LogDataProcessor, updates).ShowModal(this);
                }
            };

            // Collection initialization
            logsFiltered = new FilterCollection <LogData>(logs);
            logsFiltered.CollectionChanged += (sender, args) => FilteredLogsUpdated?.Invoke(this, EventArgs.Empty);
            logsFiltered.Filter             = Filters.FilterLog;
            LogCollectionsInitialized?.Invoke(this, EventArgs.Empty);
            LogDataProcessor.Processed += (sender, args) =>
            {
                bool last = args.CurrentScheduledItems == 0;
                if (last)
                {
                    Application.Instance.AsyncInvoke(logsFiltered.Refresh);
                }
            };

            Shown += (sender, args) => ReloadLogs();
            Shown += (sender, args) => CheckUpdates();
        }