<commit_before>﻿using NLog;
using Stylet;
using SyncTrayzor.Pages;
using SyncTrayzor.Services.Config;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;
using System.Windows.Threading;

namespace SyncTrayzor.Services.UpdateManagement
{
    public class VersionIgnoredEventArgs : EventArgs
    {
        public Version IgnoredVersion { get; private set; }

        public VersionIgnoredEventArgs(Version ignoredVersion)
        {
            this.IgnoredVersion = ignoredVersion;
        }
    }

    public interface IUpdateManager
    {
        event EventHandler<VersionIgnoredEventArgs> VersionIgnored;
        Version LatestIgnoredVersion { get; set; }
        string UpdateCheckApiUrl { get; set; }
        bool CheckForUpdates { get; set; }
        TimeSpan UpdateCheckInterval { get; set; }

        Task<VersionCheckResults> CheckForAcceptableUpdateAsync();
    }

    public class UpdateManager : IUpdateManager
    {
        private static readonly Logger logger = LogManager.GetCurrentClassLogger();

        private readonly IApplicationState applicationState;
        private readonly IApplicationWindowState applicationWindowState;
        private readonly IUpdateCheckerFactory updateCheckerFactory;
        private readonly IProcessStartProvider processStartProvider;
        private readonly IUpdatePromptProvider updatePromptProvider;
        private readonly Func<IUpdateVariantHandler> updateVariantHandlerFactory;
        private readonly DispatcherTimer promptTimer;

        private readonly SemaphoreSlim versionCheckLock = new SemaphoreSlim(1, 1);

        private DateTime lastCheckedTime;
        private CancellationTokenSource toastCts;

        public event EventHandler<VersionIgnoredEventArgs> VersionIgnored;
        public Version LatestIgnoredVersion { get; set; }
        public string UpdateCheckApiUrl { get; set; }
        public TimeSpan UpdateCheckInterval { get; set; }

        private bool _checkForUpdates;
        public bool CheckForUpdates
        {
            get { return this._checkForUpdates; }
            set
            {
                if (this._checkForUpdates == value)
                    return;
                this._checkForUpdates = value;
                this.UpdateCheckForUpdates(value);
            }
        }

        public UpdateManager(
            IApplicationState applicationState,
            IApplicationWindowState applicationWindowState,
            IUpdateCheckerFactory updateCheckerFactory,
            IProcessStartProvider processStartProvider,
            IUpdatePromptProvider updatePromptProvider,
            Func<IUpdateVariantHandler> updateVariantHandlerFactory)
        {
            this.applicationState = applicationState;
            this.applicationWindowState = applicationWindowState;
            this.updateCheckerFactory = updateCheckerFactory;
            this.processStartProvider = processStartProvider;
            this.updatePromptProvider = updatePromptProvider;
            this.updateVariantHandlerFactory = updateVariantHandlerFactory;

            this.promptTimer = new DispatcherTimer();
            this.promptTimer.Tick += this.PromptTimerElapsed;

            // Strategy time:
            // We'll prompt the user a fixed period after the computer starts up / resumes from sleep
            // We'll also check on a fixed interval since this point
            // We'll also check when the application is restored from tray

            this.applicationState.Startup += this.ApplicationStartup;
            this.applicationWindowState.RootWindowActivated += this.RootWindowActivated;
        }

        private async void UpdateCheckForUpdates(bool checkForUpdates)
        {
            if (checkForUpdates)
            {
                this.RestartTimer();
                // Give them a minute to catch their breath
                await Task.Delay(TimeSpan.FromSeconds(30));
                if (this.UpdateCheckDue())
                    await this.CheckForUpdatesAsync();
            }
            else
            {
                this.promptTimer.IsEnabled = false;
            }
        }

        private async void ApplicationStartup(object sender, EventArgs e)
        {
            await this.CheckForUpdatesAsync();
        }

        private async void RootWindowActivated(object sender, ActivationEventArgs e)
        {
            if (this.toastCts != null)
                this.toastCts.Cancel();

            if (this.UpdateCheckDue())
                await this.CheckForUpdatesAsync();
        }

        private async void PromptTimerElapsed(object sender, EventArgs e)
        {
            if (this.UpdateCheckDue())
                await this.CheckForUpdatesAsync();
        }

        private void OnVersionIgnored(Version ignoredVersion)
        {
            var handler = this.VersionIgnored;
            if (handler != null)
                handler(this, new VersionIgnoredEventArgs(ignoredVersion));
        }

        private bool UpdateCheckDue()<commit_msg>Check for updates on resume from sleep<commit_after>﻿using NLog;
using Stylet;
using SyncTrayzor.Pages;
using SyncTrayzor.Services.Config;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;
using System.Windows.Threading;

namespace SyncTrayzor.Services.UpdateManagement
{
    public class VersionIgnoredEventArgs : EventArgs
    {
        public Version IgnoredVersion { get; private set; }

        public VersionIgnoredEventArgs(Version ignoredVersion)
        {
            this.IgnoredVersion = ignoredVersion;
        }
    }

    public interface IUpdateManager
    {
        event EventHandler<VersionIgnoredEventArgs> VersionIgnored;
        Version LatestIgnoredVersion { get; set; }
        string UpdateCheckApiUrl { get; set; }
        bool CheckForUpdates { get; set; }
        TimeSpan UpdateCheckInterval { get; set; }

        Task<VersionCheckResults> CheckForAcceptableUpdateAsync();
    }

    public class UpdateManager : IUpdateManager
    {
        private static readonly Logger logger = LogManager.GetCurrentClassLogger();

        private readonly IApplicationState applicationState;
        private readonly IApplicationWindowState applicationWindowState;
        private readonly IUpdateCheckerFactory updateCheckerFactory;
        private readonly IProcessStartProvider processStartProvider;
        private readonly IUpdatePromptProvider updatePromptProvider;
        private readonly Func<IUpdateVariantHandler> updateVariantHandlerFactory;
        private readonly DispatcherTimer promptTimer;

        private readonly SemaphoreSlim versionCheckLock = new SemaphoreSlim(1, 1);

        private DateTime lastCheckedTime;
        private CancellationTokenSource toastCts;

        public event EventHandler<VersionIgnoredEventArgs> VersionIgnored;
        public Version LatestIgnoredVersion { get; set; }
        public string UpdateCheckApiUrl { get; set; }
        public TimeSpan UpdateCheckInterval { get; set; }

        private bool _checkForUpdates;
        public bool CheckForUpdates
        {
            get { return this._checkForUpdates; }
            set
            {
                if (this._checkForUpdates == value)
                    return;
                this._checkForUpdates = value;
                this.UpdateCheckForUpdates(value);
            }
        }

        public UpdateManager(
            IApplicationState applicationState,
            IApplicationWindowState applicationWindowState,
            IUpdateCheckerFactory updateCheckerFactory,
            IProcessStartProvider processStartProvider,
            IUpdatePromptProvider updatePromptProvider,
            Func<IUpdateVariantHandler> updateVariantHandlerFactory)
        {
            this.applicationState = applicationState;
            this.applicationWindowState = applicationWindowState;
            this.updateCheckerFactory = updateCheckerFactory;
            this.processStartProvider = processStartProvider;
            this.updatePromptProvider = updatePromptProvider;
            this.updateVariantHandlerFactory = updateVariantHandlerFactory;

            this.promptTimer = new DispatcherTimer();
            this.promptTimer.Tick += this.PromptTimerElapsed;

            // Strategy time:
            // We'll prompt the user a fixed period after the computer starts up / resumes from sleep
            // We'll also check on a fixed interval since this point
            // We'll also check when the application is restored from tray

            this.applicationState.Startup += this.ApplicationStartup;
            this.applicationState.ResumeFromSleep += this.ResumeFromSleep;
            this.applicationWindowState.RootWindowActivated += this.RootWindowActivated;
        }

        private async void UpdateCheckForUpdates(bool checkForUpdates)
        {
            if (checkForUpdates)
            {
                this.RestartTimer();
                // Give them a minute to catch their breath
                await Task.Delay(TimeSpan.FromSeconds(30));
                if (this.UpdateCheckDue())
                    await this.CheckForUpdatesAsync();
            }
            else
            {
                this.promptTimer.IsEnabled = false;
            }
        }

        private async void ApplicationStartup(object sender, EventArgs e)
        {
            await this.CheckForUpdatesAsync();
        }

        private async void ResumeFromSleep(object sender, EventArgs e)
        {
            if (this.UpdateCheckDue())
                await this.CheckForUpdatesAsync();
        }

        private async void RootWindowActivated(object sender, ActivationEventArgs e)
        {
            if (this.toastCts != null)
                this.toastCts.Cancel();

            if (this.UpdateCheckDue())
                await this.CheckForUpdatesAsync();
        }

        private async void PromptTimerElapsed(object sender, EventArgs e)
        {
            if (this.UpdateCheckDue())
                await this.CheckForUpdatesAsync();
        }

        private void OnVersionIgnored(Version ignoredVersion)
        {
            var handler = this.VersionIgnored;
            if (handler != null)
                handler(this, new VersionIgnoredEventArgs(ignoredVersion));
        }

        private bool UpdateCheckDue()
{'fraction_non_alphanumeric': 0.05883465758607643, 'fraction_numerical': 0.0007567158531971245, 'mean_word_length': 2.30716296527995, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '5524751', 'n_tokens_mistral': 2516, 'n_tokens_neox': 2442, 'n_words': 622}