Skip to content

Commit

Permalink
Added LastCloseDateTime property, renamed LastWorkTime to LastWorkTim…
Browse files Browse the repository at this point in the history
…eLeft in (HabiticaSettingsModel).

Little fix in settings: if changed settings-model we read existing settings and rewrite(maybe need think more about it).
Now we remember last work time left, and closing time on closing time.
Added notification of last work time left. Now showing button that if we click LastWorkTimeLeft is loaded into Timer.
  • Loading branch information
Ar6yZuK authored and Ar6yZuK committed Mar 12, 2024
1 parent 552788d commit 7fa987f
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

namespace HabiticaHourUpVSIX.AppSettings.Models;

public record struct HabiticaSettingsModel(TimeSpan LastWorkTime, int TotalTicks);
public record struct HabiticaSettingsModel(TimeSpan LastWorkTimeLeft, DateTime LastCloseDateTime, int TotalTicks);
16 changes: 15 additions & 1 deletion HabiticaHourUpVSIX/AppSettings/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ public override void Save()
internal class SettingsInFile<T>(string fileName, T defaultSettings) : SettingsWithSaving<T>
where T : struct
{
private readonly JsonSerializerSettings _deserializationSettings = new() { MissingMemberHandling = MissingMemberHandling.Error };

public override T Read()
{
if (!File.Exists(fileName))
Expand All @@ -81,7 +83,19 @@ public override T Read()
}

string data = File.ReadAllText(fileName);
var result = JsonConvert.DeserializeObject<T>(data);

T result = defaultSettings;
try
{
result = JsonConvert.DeserializeObject<T>(data, _deserializationSettings);
}
catch (JsonSerializationException)
{
// On member not found we read all existing members and write it
result = JsonConvert.DeserializeObject<T>(data);
Write(result);
return result;
}

return result;
}
Expand Down
54 changes: 54 additions & 0 deletions HabiticaHourUpVSIX/HabiticaHourUpVSIXPackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Threading;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading;
#nullable enable
Expand All @@ -23,6 +24,9 @@ namespace HabiticaHourUpVSIX;
[ProvideToolWindow(typeof(SettingsToolWindow.Pane))]
public sealed class HabiticaHourUpVSIXPackage : ToolkitPackage
{
private readonly TimeSpan OneMinute = TimeSpan.FromMinutes(1d);
private readonly TimeSpan OneDay = TimeSpan.FromDays(1d);

private IAudioPlayer _soundPlayer;

public SettingsWithSaving<HabiticaSettingsModel> HabiticaSettingsReader { get; private set; }
Expand Down Expand Up @@ -74,6 +78,50 @@ protected override async Task InitializeAsync(CancellationToken cancellationToke

TimeSpan tickAfter = vsSettings.Divisor;
Timer.Change(tickAfter, vsSettings.Divisor);

await JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken);
VS.Events.SolutionEvents.OnAfterCloseSolution += OnClose;

await SetLastTimerIfUserAgreeAsync(habiticaSettings, vsSettings.Divisor);
}

private async Task SetLastTimerIfUserAgreeAsync(HabiticaSettingsModel habiticaSettings, TimeSpan divisor)
{
var lastWorkTimeLeft = habiticaSettings.LastWorkTimeLeft;
//if (lastWorkTimeLeft < OneMinute)
// return;

var lastCloseAgo = DateTime.Now - habiticaSettings.LastCloseDateTime;
string lastCloseAgoFormat = @"hh\:mm\:ss";
if (lastCloseAgo >= OneDay)
lastCloseAgoFormat = @"dd\:hh\:mm\:ss";

string setLastNextTickMessage = $"""
On last session next tick was: {lastWorkTimeLeft}.
Do you want to load it?
Last session was on: {habiticaSettings.LastCloseDateTime}({lastCloseAgo.ToString(lastCloseAgoFormat)} ago)
""";

var model = new InfoBarModel(setLastNextTickMessage, new[] { new InfoBarHyperlink("Click here to load") });

var infoBar = await VS.InfoBar.CreateAsync(model);
// if infoBar was null, maybe solution not loaded, then we wait for it.
if (infoBar is null)
{
VS.Events.SolutionEvents.OnAfterOpenSolution += SolutionOpened;
return;
async void SolutionOpened(Solution? obj)
{
VS.Events.SolutionEvents.OnAfterOpenSolution -= SolutionOpened;
// We read because there may have been changes in settings outside(settings file may be changed)
await SetLastTimerIfUserAgreeAsync(HabiticaSettingsReader.Read(), UserSettingsReader.Read().Divisor);
}
}

// Maybe close infoBar on click
infoBar.ActionItemClicked += (s, e) => { Timer.Change(lastWorkTimeLeft, divisor); /*((InfoBar)s).Close();*/ };

await infoBar.TryShowInfoBarUIAsync();
}

internal void PlayBeep()
Expand Down Expand Up @@ -131,6 +179,11 @@ async delegate
}
}).FireAndForget();
}
private void OnClose()
{
HabiticaSettingsReader.SetWithSave(x => x.LastWorkTimeLeft, Timer.NextTick);
HabiticaSettingsReader.SetWithSave(x => x.LastCloseDateTime, DateTime.Now);
}

private void UserSettingsReader_OnSaving(UserSettingsModel userSettingsModel)
{
Expand All @@ -152,6 +205,7 @@ private void AddTicksToAllSettings(int addedTicks)

protected override void Dispose(bool disposing)
{
VS.Events.SolutionEvents.OnAfterCloseSolution -= OnClose;
Timer?.Dispose();
base.Dispose(disposing);
}
Expand Down
8 changes: 0 additions & 8 deletions HabiticaHourUpVSIX/SettingsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,6 @@ namespace HabiticaHourUpVSIX;
// Need read before write, because there may be data that needs to be left untouched
public static partial class SettingsExtensions
{
public static void SetLastTickAfterWithSave(this SettingsWithSaving<HabiticaSettingsModel> obj1, TimeSpan lastTickToSet)
{
HabiticaSettingsModel settingsRead = obj1.Read();
var settingsToWrite = settingsRead with { LastWorkTime = lastTickToSet };

obj1.Write(settingsToWrite);
obj1.Save();
}
public static void SetTotalTicksWithSave(this SettingsWithSaving<HabiticaSettingsModel> obj1, int ticksToSet)
{
HabiticaSettingsModel settingsRead = obj1.Read();
Expand Down
2 changes: 1 addition & 1 deletion HabiticaHourUpVSIX/source.extension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ internal sealed partial class Vsix
public const string Name = "HabiticaHourUpVSIX";
public const string Description = @"Empty VSIX Project.";
public const string Language = "en-US";
public const string Version = "1.5.1";
public const string Version = "1.5.2";
public const string Author = "Ar6";
public const string Tags = "";
}
Expand Down
2 changes: 1 addition & 1 deletion HabiticaHourUpVSIX/source.extension.vsixmanifest
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" ?>
<PackageManifest Version="2.0.0" xmlns="http://schemas.microsoft.com/developer/vsx-schema/2011" xmlns:d="http://schemas.microsoft.com/developer/vsx-schema-design/2011">
<Metadata>
<Identity Id="HabiticaHourUpVSIX.ade8e5f3-585d-49ca-8916-064b2237b928" Version="1.5.1" Language="en-US" Publisher="Ar6" />
<Identity Id="HabiticaHourUpVSIX.ade8e5f3-585d-49ca-8916-064b2237b928" Version="1.5.2" Language="en-US" Publisher="Ar6" />
<DisplayName>HabiticaHourUpVSIX</DisplayName>
<Description xml:space="preserve">Empty VSIX Project.</Description>
<Icon>Resources\Icon.png</Icon>
Expand Down

0 comments on commit 7fa987f

Please sign in to comment.