Skip to content

Commit

Permalink
boost(Update::Postpone): The more the user postpone, the longer it wa…
Browse files Browse the repository at this point in the history
…its before asking to update.

Start with 3 days, then 7 days, then 12 days, then 19 days then 28 days.

28 days is the maximum.

Fixes #528
  • Loading branch information
Belphemur committed May 18, 2021
1 parent a2b507e commit 355dd69
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 41 deletions.
65 changes: 65 additions & 0 deletions SoundSwitch/Framework/Updater/Remind/PostponeService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using System;
using Serilog;
using SoundSwitch.Framework.Configuration;

namespace SoundSwitch.Framework.Updater.Remind
{
public class PostponeService
{
/// <summary>
/// Set the release as postponed
/// </summary>
/// <param name="release"></param>
public void PostponeRelease(Release release)
{
var postponed = AppConfigs.Configuration.Postponed;
try
{
if (postponed == null || postponed.Version < release.ReleaseVersion)
{
AppConfigs.Configuration.Postponed = new ReleasePostponed(release.ReleaseVersion, DateTime.UtcNow + 3 * TimeSpan.FromSeconds(AppConfigs.Configuration.UpdateCheckInterval), 1);
return;
}

if (postponed.Version == release.ReleaseVersion)
{
var postponedCount = postponed.Count + 1;
if (postponedCount > 5)
{
postponedCount = 5;
}
AppConfigs.Configuration.Postponed = postponed with
{
Count = postponedCount, Until = DateTime.UtcNow + (3 + (postponedCount ^ 2)) * TimeSpan.FromSeconds(AppConfigs.Configuration.UpdateCheckInterval)
};
}
}
finally
{
Log.Information($"Release {release} set as {AppConfigs.Configuration.Postponed}");
AppConfigs.Configuration.Save();
}
}

/// <summary>
/// Return true if the release should be postponed, false if it can be done now.
/// </summary>
/// <param name="release"></param>
/// <returns></returns>
public bool ShouldPostpone(Release release)
{
var postponed = AppConfigs.Configuration.Postponed;
if (postponed == null)
{
return false;
}

if (release.ReleaseVersion > postponed.Version)
{
return false;
}

return postponed.Until > DateTime.UtcNow;
}
}
}
18 changes: 1 addition & 17 deletions SoundSwitch/Framework/Updater/Remind/ReleasePostponed.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,5 @@

namespace SoundSwitch.Framework.Updater.Remind
{
public record ReleasePostponed(Version Version, DateTime Until)
{
/// <summary>
/// Return true if the release should be postponed, false if it can be done now.
/// </summary>
/// <param name="release"></param>
/// <returns></returns>
public bool ShouldPostpone(Release release)
{
if (release.ReleaseVersion > Version)
{
return false;
}

return Until > DateTime.UtcNow;
}
}
public record ReleasePostponed(Version Version, DateTime Until, uint Count);
}
11 changes: 0 additions & 11 deletions SoundSwitch/Model/AppModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
using SoundSwitch.Framework.Profile.Trigger;
using SoundSwitch.Framework.Updater;
using SoundSwitch.Framework.Updater.Job;
using SoundSwitch.Framework.Updater.Remind;
using SoundSwitch.Framework.WinApi;
using SoundSwitch.Framework.WinApi.Keyboard;
using SoundSwitch.Localization;
Expand Down Expand Up @@ -161,16 +160,6 @@ public UpdateMode UpdateMode
}
}

public ReleasePostponed ReleasePostponed
{
get => AppConfigs.Configuration.Postponed;
set
{
AppConfigs.Configuration.Postponed = value;
AppConfigs.Configuration.Save();
}
}

public Language Language
{
get => AppConfigs.Configuration.Language;
Expand Down
6 changes: 0 additions & 6 deletions SoundSwitch/Model/IAppModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
using SoundSwitch.Framework.NotificationManager;
using SoundSwitch.Framework.Profile;
using SoundSwitch.Framework.Updater;
using SoundSwitch.Framework.Updater.Remind;
using SoundSwitch.Framework.WinApi.Keyboard;
using SoundSwitch.Localization.Factory;
using SoundSwitch.UI.Component;
Expand Down Expand Up @@ -115,11 +114,6 @@ public interface IAppModel : IDisposable
/// </summary>
IAudioDeviceLister ActiveUnpluggedAudioLister { get; set; }

/// <summary>
/// Do we have a postponement for a specific release
/// </summary>
ReleasePostponed ReleasePostponed { get; set; }

#endregion

#region Events
Expand Down
9 changes: 5 additions & 4 deletions SoundSwitch/UI/Component/TrayIcon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
using SoundSwitch.Framework.TrayIcon.Icon;
using SoundSwitch.Framework.TrayIcon.TooltipInfoManager;
using SoundSwitch.Framework.Updater;
using SoundSwitch.Framework.Updater.Remind;
using SoundSwitch.Localization;
using SoundSwitch.Model;
using SoundSwitch.Properties;
Expand All @@ -58,6 +59,7 @@ public sealed class TrayIcon : IDisposable

private readonly ContextMenuStrip _selectionMenu = new();
private readonly ContextMenuStrip _settingsMenu = new();
private readonly PostponeService _postponeService = new();

private readonly SynchronizationContext _context =
SynchronizationContext.Current ?? new SynchronizationContext();
Expand Down Expand Up @@ -241,15 +243,14 @@ private void SetEventHandlers()

private void NewReleaseAvailable(object sender, UpdateChecker.NewReleaseEvent newReleaseEvent)
{

_updateMenuItem.Tag = newReleaseEvent.Release;
_updateMenuItem.Text = string.Format(TrayIconStrings.updateAvailable, newReleaseEvent.Release.ReleaseVersion);
var configurationPostponed = AppConfigs.Configuration.Postponed;
if (configurationPostponed?.ShouldPostpone(newReleaseEvent.Release) ?? false)
if (_postponeService.ShouldPostpone(newReleaseEvent.Release))
{
Log.Information("Release {release} has been postponed to {date:yyyy-MM-dd hh:mm}", newReleaseEvent.Release, configurationPostponed.Until);
Log.Information("Release {release} has been postponed", newReleaseEvent.Release);
return;
}

StartAnimationIconUpdate();
NotifyIcon.BalloonTipClicked += OnUpdateClick;
NotifyIcon.ShowBalloonTip(3000, string.Format(TrayIconStrings.versionAvailable, newReleaseEvent.Release.ReleaseVersion), newReleaseEvent.Release.Name + '\n' + TrayIconStrings.clickToUpdate, ToolTipIcon.Info);
Expand Down
5 changes: 2 additions & 3 deletions SoundSwitch/UI/Forms/UpdateDownloadForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@
using System;
using System.Windows.Forms;
using Serilog;
using SoundSwitch.Framework.Configuration;
using SoundSwitch.Framework.Updater;
using SoundSwitch.Framework.Updater.Installer;
using SoundSwitch.Framework.Updater.Remind;
using SoundSwitch.Localization;
using SoundSwitch.Model;
using SoundSwitch.Properties;
using SoundSwitch.UI.Component;

Expand All @@ -30,6 +28,7 @@ public sealed partial class UpdateDownloadForm : Form
{
private WebFile _releaseFile;
private Release _releaseInfo;
private readonly PostponeService _postponeService = new();

public UpdateDownloadForm()
{
Expand Down Expand Up @@ -116,7 +115,7 @@ private void cancelButton_Click(object sender, EventArgs e)
}
else
{
AppModel.Instance.ReleasePostponed = new ReleasePostponed(_releaseInfo.ReleaseVersion, DateTime.UtcNow + 3 * TimeSpan.FromSeconds(AppConfigs.Configuration.UpdateCheckInterval));
_postponeService.PostponeRelease(_releaseInfo);
}
Close();
}
Expand Down

0 comments on commit 355dd69

Please sign in to comment.