Skip to content

Commit

Permalink
Merge pull request #71 from goodtrailer/simplify-api
Browse files Browse the repository at this point in the history
Core: Remove sync duplicate methods
  • Loading branch information
goodtrailer authored Aug 26, 2023
2 parents c04810b + 24e04ed commit a82f599
Show file tree
Hide file tree
Showing 15 changed files with 41 additions and 526 deletions.
111 changes: 2 additions & 109 deletions DailyDesktop.Core/Configuration/AbstractConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,41 +36,12 @@ public AbstractConfiguration(string jsonPath = "")
[JsonIgnore]
public virtual bool IsAutoSerializing { get; set; }

/// <inheritdoc/>
public event EventHandler? OnUpdate;

/// <inheritdoc/>
public event EventHandler? OnSerialize;

/// <inheritdoc/>
public event AsyncEventHandler? OnUpdateAsync;

/// <inheritdoc/>
public event AsyncEventHandler? OnSerializeAsync;

/// <summary>
/// Deserialize a JSON file (located at <see cref="JsonPath"/>) to a configuration.
/// </summary>
public void Deserialize()
{
if (string.IsNullOrWhiteSpace(JsonPath))
throw new InvalidOperationException($"{JsonPath} is null or whitespace.");

var options = new JsonSerializerOptions();
ConfigureDeserializer(options);

T newConfig;
using (var jsonStream = File.OpenRead(JsonPath))
newConfig = JsonSerializer.Deserialize<T>(jsonStream, options) ?? throw new NullReferenceException("Deserialized config was null.");

bool temp = IsAutoSerializing;
IsAutoSerializing = false;

Load(newConfig);

IsAutoSerializing = temp;
}

/// <summary>
/// Asynchronously deserialize a JSON file (located at <see cref="JsonPath"/>) to a configuration.
/// </summary>
Expand All @@ -94,29 +65,6 @@ public async Task DeserializeAsync(CancellationToken cancellationToken)
IsAutoSerializing = temp;
}

/// <summary>
/// Try to deserialize a JSON file (located at <see cref="JsonPath"/>) to a configuration.
/// </summary>
/// <returns>
/// Whether or not the deserialiazation was successful.
/// </returns>
public bool TryDeserialize()
{
try
{
Deserialize();
return true;
}
catch (Exception ex) when (ex is JsonException
|| ex is IOException
|| ex is SystemException
|| ex is InvalidOperationException
|| ex is NullReferenceException)
{
return false;
}
}

/// <summary>
/// Try to asynchronously deserialize a JSON file (located at <see cref="JsonPath"/>) to a configuration.
/// </summary>
Expand All @@ -140,15 +88,6 @@ public async Task<bool> TryDeserializeAsync(CancellationToken cancellationToken)
}
}

/// <inheritdoc/>
public void Update()
{
OnUpdate?.Invoke(this, EventArgs.Empty);

if (IsAutoSerializing)
Serialize();
}

/// <inheritdoc/>
public async Task UpdateAsync(CancellationToken cancellationToken)
{
Expand All @@ -158,24 +97,6 @@ public async Task UpdateAsync(CancellationToken cancellationToken)
await SerializeAsync(cancellationToken);
}

/// <inheritdoc/>
public void Serialize()
{
if (!(this is T @this))
throw new InvalidCastException($"this is not of type {nameof(T)}: {typeof(T).FullName}.");

if (string.IsNullOrWhiteSpace(JsonPath))
throw new InvalidOperationException($"{JsonPath} is null or whitespace.");

var options = new JsonSerializerOptions();
ConfigureSerializer(options);

using (var jsonStream = File.Create(JsonPath))
JsonSerializer.Serialize(jsonStream, @this, options);

OnSerialize?.Invoke(this, EventArgs.Empty);
}

/// <inheritdoc/>
public async Task SerializeAsync(CancellationToken cancellationToken)
{
Expand All @@ -194,23 +115,6 @@ public async Task SerializeAsync(CancellationToken cancellationToken)
await OnSerializeAsync.InvokeAsync(this, EventArgs.Empty, cancellationToken);
}

/// <inheritdoc/>
public bool TrySerialize()
{
try
{
Serialize();
return true;
}
catch (Exception ex) when (ex is JsonException
|| ex is IOException
|| ex is SystemException
|| ex is InvalidOperationException)
{
return false;
}
}

/// <inheritdoc/>
public async Task<bool> TrySerializeAsync(CancellationToken cancellationToken)
{
Expand All @@ -228,17 +132,6 @@ public async Task<bool> TrySerializeAsync(CancellationToken cancellationToken)
}
}

/// <summary>
/// Loads options from another configuration instance. Basically like
/// a copy constructor/method.
/// </summary>
/// <param name="other">The other configuration instance to copy options from.</param>
public void Load(T other)
{
LoadImpl(other);
Update();
}

/// <summary>
/// Asynchronously loads options from another configuration instance. Basically like
/// a copy constructor/method.
Expand All @@ -252,8 +145,8 @@ public async Task LoadAsync(T other, CancellationToken cancellationToken)
}

/// <summary>
/// Implementation of <see cref="Load(T)"/>/<see cref="LoadAsync(T, CancellationToken)"/>.
/// Should do the actual value copying.
/// Implementation of <see cref="LoadAsync(T, CancellationToken)"/>. Should do
/// the actual value copying.
/// </summary>
/// <param name="other">The other configuration instance to copy options from.</param>
protected abstract void LoadImpl(T other);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,6 @@ namespace DailyDesktop.Core.Configuration
/// </summary>
public interface IPublicNullableConfiguration
{
/// <summary>
/// Nullifies all <see cref="string"/> properties that satisfy
/// <see cref="string.IsNullOrWhiteSpace(string?)"/>.
/// </summary>
/// <returns>The number of properties that were nullified.</returns>
int NullifyWhitespace();

/// <summary>
/// Asynchronously nullifies all <see cref="string"/> properties that satisfy
/// <see cref="string.IsNullOrWhiteSpace(string?)"/>.
Expand Down
15 changes: 0 additions & 15 deletions DailyDesktop.Core/Configuration/IPublicPathConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,6 @@ namespace DailyDesktop.Core.Configuration
/// </summary>
public interface IPublicPathConfiguration : IReadOnlyPathConfiguration
{
/// <summary>
/// The assembly directory (e.g. for the task executable).
/// </summary>
new string AssemblyDir { get; set; }

/// <summary>
/// The providers directory (e.g. for <see cref="IProvider"/> DLL modules).
/// </summary>
new string ProvidersDir { get; set; }

/// <summary>
/// The serialization directory (e.g. for the <see cref="TaskConfiguration"/> JSON).
/// </summary>
new string SerializationDir { get; set; }

/// <summary>
/// Asynchronously sets the assembly directory (e.g. for the task executable).
/// </summary>
Expand Down
37 changes: 2 additions & 35 deletions DailyDesktop.Core/Configuration/IPublicTaskConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,39 +13,6 @@ namespace DailyDesktop.Core.Configuration
/// </summary>
public interface IPublicTaskConfiguration : IReadOnlyTaskConfiguration
{
/// <summary>
/// The path of the <see cref="IProvider"/> DLL module.
/// </summary>
new string Dll { get; set; }

/// <summary>
/// Whether wallpaper update <see cref="Microsoft.Win32.TaskScheduler.Task"/> triggers are
/// enabled or disabled.
/// </summary>
new bool IsEnabled { get; set; }

/// <summary>
/// The time at which the daily wallpaper update trigger executes. Only applies if
/// <see cref="IsEnabled"/> is set to <c>true</c>.
/// </summary>
new DateTime UpdateTime { get; set; }

/// <summary>
/// Whether or not to apply resize to wallpaper images to screen resolution, if larger.
/// </summary>
new bool DoResize { get; set; }

/// <summary>
/// Whether or not to apply blurred-fit to wallpaper images.
/// </summary>
new bool DoBlurredFit { get; set; }

/// <summary>
/// The blur strength for wallpaper images. Only applies if
/// <see cref="DoBlurredFit"/> is set to <c>true</c>.
/// </summary>
new int BlurStrength { get; set; }

/// <summary>
/// Sets the path of the <see cref="IProvider"/> DLL module.
/// </summary>
Expand All @@ -63,7 +30,7 @@ public interface IPublicTaskConfiguration : IReadOnlyTaskConfiguration

/// <summary>
/// Sets the time at which the daily wallpaper update trigger executes. Only applies if
/// <see cref="IsEnabled"/> is set to <c>true</c>.
/// <see cref="IReadOnlyTaskConfiguration.IsEnabled"/> is <c>true</c>.
/// </summary>
/// <param name="updateTime">The value to set as.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
Expand All @@ -85,7 +52,7 @@ public interface IPublicTaskConfiguration : IReadOnlyTaskConfiguration

/// <summary>
/// Sets the blur strength for wallpaper images. Only applies if
/// <see cref="DoBlurredFit"/> is set to <c>true</c>.
/// <see cref="IReadOnlyTaskConfiguration.DoBlurredFit"/> is set to <c>true</c>.
/// </summary>
/// <param name="blurStrength">The value to set as.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
Expand Down
32 changes: 1 addition & 31 deletions DailyDesktop.Core/Configuration/IPublicWallpaperConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,36 +11,6 @@ namespace DailyDesktop.Core.Configuration
/// </summary>
public interface IPublicWallpaperConfiguration : IReadOnlyWallpaperConfiguration, IPublicNullableConfiguration
{
/// <summary>
/// The URI of the image file. Cannot be null because there must be an image is necessary.
/// </summary>
new string ImageUri { get; set; }

/// <summary>
/// The author of the work (i.e. the illustrator, photographer, painter, etc.)
/// </summary>
new string? Author { get; set; }

/// <summary>
/// A URI to the <see cref="Author"/>. Usually a URL to the author's website or profile page.
/// </summary>
new string? AuthorUri { get; set; }

/// <summary>
/// The title of the work.
/// </summary>
new string? Title { get; set; }

/// <summary>
/// A URI to the work. Usually a URL to the image's page on the source website where it was downloaded from.
/// </summary>
new string? TitleUri { get; set; }

/// <summary>
/// A description for the work.
/// </summary>
new string? Description { get; set; }

/// <summary>
/// Asynchronously sets the URI of the image file. Cannot be null because there must be an image is necessary.
/// </summary>
Expand All @@ -56,7 +26,7 @@ public interface IPublicWallpaperConfiguration : IReadOnlyWallpaperConfiguration
Task SetAuthorAsync(string? author, CancellationToken cancellationToken);

/// <summary>
/// Asynchronously sets the URI to the <see cref="Author"/>. Usually a URL to the author's website or profile page.
/// Asynchronously sets the URI to the <see cref="IReadOnlyWallpaperConfiguration.Author"/>. Usually a URL to the author's website or profile page.
/// </summary>
/// <param name="authorUri">The value to set as.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
Expand Down
30 changes: 0 additions & 30 deletions DailyDesktop.Core/Configuration/IReadOnlyConfiguration.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) Alden Wu <aldenwu0@gmail.com>. Licensed under the MIT Licence.
// See the LICENSE file in the repository root for full licence text.

using System;
using System.Threading;
using System.Threading.Tasks;
using DailyDesktop.Core.Util;
Expand All @@ -23,56 +22,27 @@ public interface IReadOnlyConfiguration
/// </summary>
bool IsAutoSerializing { get; }

/// <summary>
/// Event published on calls to <see cref="Update"/>.
/// </summary>
event EventHandler OnUpdate;

/// <summary>
/// Asynchronous event published on calls to <see cref="UpdateAsync"/>.
/// </summary>
event AsyncEventHandler OnUpdateAsync;

/// <summary>
/// Event published on successful calls to <see cref="Serialize"/>.
/// </summary>
event EventHandler OnSerialize;

/// <summary>
/// Asynchronous event published on successful calls to <see cref="SerializeAsync"/>.
/// </summary>
event AsyncEventHandler OnSerializeAsync;

/// <summary>
/// Automatically called upon setting properties. Can be called manually to
/// publish to <see cref="OnUpdateAsync"/> and serialize (in case <see cref="IsAutoSerializing"/> is true).
/// </summary>
void Update();

/// <summary>
/// Automatically called upon setting properties. Can be called manually to asynchronously
/// publish to <see cref="OnUpdateAsync"/> and serialize (in case <see cref="IsAutoSerializing"/> is true).
/// </summary>
Task UpdateAsync(CancellationToken cancellationToken);

/// <summary>
/// Serialize configuration to a JSON file (located at <see cref="JsonPath"/>).
/// </summary>
void Serialize();

/// <summary>
/// Asynchronously serialize configuration to a JSON file (located at <see cref="JsonPath"/>).
/// </summary>
Task SerializeAsync(CancellationToken cancellationToken);

/// <summary>
/// Try to serialize configuration to a JSON file (located at <see cref="JsonPath"/>).
/// </summary>
/// <returns>
/// Whether or not the serialiazation was successful.
/// </returns>
bool TrySerialize();

/// <summary>
/// Try to asynchronously serialize configuration to a JSON file (located at <see cref="JsonPath"/>).
/// </summary>
Expand Down
Loading

0 comments on commit a82f599

Please sign in to comment.