Skip to content

Commit

Permalink
Move ShowError setting to UserSettings.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ar6yZuK authored and Ar6yZuK committed Feb 19, 2024
1 parent de9f290 commit 8fba56b
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 56 deletions.
6 changes: 3 additions & 3 deletions HabiticaHourUpVSIX/AppSettings/Models/SessionSettingsModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace HabiticaHourUpVSIX.AppSettings.Models;

public record struct SessionSettingsModel(int Ticks, int TicksSent, bool ShowError);
public record class SessionSettingsModelClass(int Ticks, int TicksSent, bool ShowError)
public record struct SessionSettingsModel(int Ticks, int TicksSent);
public record class SessionSettingsModelClass(int Ticks, int TicksSent)
{
public static SessionSettingsModelClass Default { get; } = new SessionSettingsModelClass(0, 0, true);
public static SessionSettingsModelClass Default { get; } = new SessionSettingsModelClass(0, 0);
}
2 changes: 1 addition & 1 deletion HabiticaHourUpVSIX/AppSettings/Models/UserSettingsModel.cs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
namespace HabiticaHourUpVSIX.AppSettings.Models;
public record struct UserSettingsModel(TimeSpan Divisor, string TaskIDToScoreUp, bool IsAutoScoreUp);
public record struct UserSettingsModel(TimeSpan Divisor, string TaskIDToScoreUp, bool IsAutoScoreUp, bool ShowErrorOnFailure);
14 changes: 13 additions & 1 deletion HabiticaHourUpVSIX/AppSettings/UserSettings3.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions HabiticaHourUpVSIX/AppSettings/UserSettings3.settings
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,8 @@
<Setting Name="IsAutoScoreUp" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">False</Value>
</Setting>
<Setting Name="ShowErrorOnFailure" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">True</Value>
</Setting>
</Settings>
</SettingsFile>
71 changes: 29 additions & 42 deletions HabiticaHourUpVSIX/HabiticaHourUpVSIXPackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ public sealed class HabiticaHourUpVSIXPackage : ToolkitPackage
public HabiticaClientBase HabiticaClient { get; private set; }
public MyTimer Timer { get; private set; }

public event Action<bool>? ShowErrorChangedEvent;

protected override async Task InitializeAsync(CancellationToken cancellationToken, IProgress<ServiceProgressData> progress)
{
await this.RegisterCommandsAsync();
Expand All @@ -45,7 +43,7 @@ protected override async Task InitializeAsync(CancellationToken cancellationToke
CredentialsSettings = new CredentialsSettings();

UserSettingsReader = new UserSettings();
UserSettingsReader.OnSaving += VSOptionsSettingsReader_OnSaving;
UserSettingsReader.OnSaving += UserSettingsReader_OnSaving;

HabiticaSettingsReader = new HabiticaSettings();
HabiticaSettingsModel habiticaSettings = HabiticaSettingsReader.Read();
Expand All @@ -70,44 +68,39 @@ private void Tick()
AddTicksToAllSettings(1);

var userSettingsModel = UserSettingsReader.Read();
if (userSettingsModel.IsAutoScoreUp)
{
JoinableTaskFactory.RunAsync(
async delegate
{
const string ErrorTitleMessage = "Habitica score up failure\nPress cancel for do not show error again";
if (!userSettingsModel.IsAutoScoreUp)
return;

// Set as no have error
VSConstants.MessageBoxResult? mboxResult = null;
try
{
var scoreUpResult = await HabiticaClient.SendOneTickAsync();
if (scoreUpResult.TryPickT1(out var notSuccess, out _) && SessionSettingsReader.Read().ShowError)
{
mboxResult = await VS.MessageBox.ShowErrorAsync(ErrorTitleMessage, $"{notSuccess.Error}:\n{notSuccess.Message}");
return;
}
}
catch (Exception ex) when(SessionSettingsReader.Read().ShowError)
{
mboxResult = await VS.MessageBox.ShowErrorAsync(ErrorTitleMessage + "\nError will logged in output\nMaybe error with internet connection", ex.ToString());
throw;
}
finally
JoinableTaskFactory.RunAsync(
async delegate
{
const string ErrorTitleMessage = "Habitica score up failure\nPress cancel for do not show error again";

// Set as no have error
VSConstants.MessageBoxResult? mboxResult = null;
try
{
var scoreUpResult = await HabiticaClient.SendOneTickAsync();
if (scoreUpResult.TryPickT1(out var notSuccess, out _) && UserSettingsReader.Read().ShowErrorOnFailure)
{
if(mboxResult is VSConstants.MessageBoxResult.IDCANCEL)
{
SessionSettingsReader.SetShowError(false);
// Use SetShowErrorAndNotify for notify SettingsWindow about changes
// TODO: Maybe change Settings class, adding event about changing property.
SetShowErrorAndNotify(false);
}
mboxResult = await VS.MessageBox.ShowErrorAsync(ErrorTitleMessage, $"{notSuccess.Error}:\n{notSuccess.Message}");
return;
}
}).FireAndForget();
}
}
catch (Exception ex) when (UserSettingsReader.Read().ShowErrorOnFailure)
{
mboxResult = await VS.MessageBox.ShowErrorAsync(ErrorTitleMessage + "\nError will logged in output\nMaybe error with internet connection", ex.ToString());
throw;
}
finally
{
if (mboxResult is VSConstants.MessageBoxResult.IDCANCEL)
UserSettingsReader.SetShowErrorOnFailureWithSave(false);
}
}).FireAndForget();
}

private void VSOptionsSettingsReader_OnSaving(UserSettingsModel userSettingsModel)
private void UserSettingsReader_OnSaving(UserSettingsModel userSettingsModel)
{
Timer.Change(Timer.NextTick, userSettingsModel.Divisor);
}
Expand All @@ -129,10 +122,4 @@ private void AddTicksToAllSettings(int addedTicks)

HabiticaSettingsReader.Save();
}

public void SetShowErrorAndNotify(bool newValue)
{
SessionSettingsReader.SetShowError(newValue);
ShowErrorChangedEvent?.Invoke(newValue);
}
}
7 changes: 4 additions & 3 deletions HabiticaHourUpVSIX/SettingsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,13 @@ public static void AddSessionTicksSent(this Settings<SessionSettingsModel> obj1,

obj1.Write(settingsToWrite);
}
public static void SetShowError(this Settings<SessionSettingsModel> obj1, bool showError)
public static void SetShowErrorOnFailureWithSave(this SettingsWithSaving<UserSettingsModel> obj1, bool showError)
{
SessionSettingsModel settingsRead = obj1.Read();
var settingsToWrite = settingsRead with { ShowError = showError };
var settingsRead = obj1.Read();
var settingsToWrite = settingsRead with { ShowErrorOnFailure = showError };

obj1.Write(settingsToWrite);
obj1.Save();
}

public static void SetUserIDWithSave(this SettingsWithSaving<HabiticaCredentials> obj1, string userId)
Expand Down
4 changes: 2 additions & 2 deletions HabiticaHourUpVSIX/ToolWindows/SettingsWindowControl.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
xmlns:toolkit="clr-namespace:Community.VisualStudio.Toolkit;assembly=Community.VisualStudio.Toolkit"
toolkit:Themes.UseVsTheme="True"
mc:Ignorable="d"
d:DesignHeight="330"
d:DesignHeight="370"
d:DesignWidth="327"
Name="MyToolWindow"
xmlns:local="clr-namespace:HabiticaHourUpVSIX"
Expand Down Expand Up @@ -72,7 +72,7 @@
<CheckBox IsChecked="{Binding IsAutoScoreUp}" Grid.Row="8" Grid.Column="1"/>

<Label Content="Show error on habitica score up failure" Grid.Row="9"/>
<CheckBox IsChecked="{Binding SessionShowError}" Grid.Row="9" Grid.Column="1"/>
<CheckBox IsChecked="{Binding ShowErrorOnFailure}" Grid.Row="9" Grid.Column="1"/>
</Grid>
</ScrollViewer>
</UserControl>
9 changes: 5 additions & 4 deletions HabiticaHourUpVSIX/ToolWindows/SettingsWindowControl.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@ public int SessionTicksSent
get => _package.SessionSettingsReader.Read().TicksSent;
set => _package.SessionSettingsReader.SetSessionTicksSent(value);
}
public bool SessionShowError
public bool ShowErrorOnFailure
{
get => _package.SessionSettingsReader.Read().ShowError;
set => _package.SetShowErrorAndNotify(value);
get => _package.UserSettingsReader.Read().ShowErrorOnFailure;
// On notify it reads on get
set => _package.UserSettingsReader.SetShowErrorOnFailureWithSave(value);
}

public TimeSpan TimeToTick
Expand Down Expand Up @@ -90,7 +91,7 @@ public SettingsWindow(HabiticaHourUpVSIXPackage habiticaHourUpVSIXPackage)

_package.HabiticaClient.OnSuccessfullySend += delegate { OnPropertyChanged(nameof(SessionTicksSent)); };

_package.ShowErrorChangedEvent += x => OnPropertyChanged(nameof(SessionShowError));
_package.UserSettingsReader.OnSaving += delegate { OnPropertyChanged(nameof(ShowErrorOnFailure)); };

var maxTimeToTickValue = TimeSpan.FromMilliseconds(uint.MaxValue - 1);
_timeToTickValidator = new(TimeToTick, minValue: TimeSpan.Zero, maxTimeToTickValue);
Expand Down
3 changes: 3 additions & 0 deletions HabiticaHourUpVSIX/app.config
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
<setting name="IsAutoScoreUp" serializeAs="String">
<value>False</value>
</setting>
<setting name="ShowErrorOnFailure" serializeAs="String">
<value>True</value>
</setting>
</HabiticaHourUpVSIX.AppSettings.UserSettings3>
<HabiticaHourUpVSIX.AppSettings.CredentialsSettings1>
<setting name="UserId" serializeAs="String">
Expand Down

0 comments on commit 8fba56b

Please sign in to comment.