Skip to content

Commit

Permalink
chore: Refactor Some Native Platform Classes
Browse files Browse the repository at this point in the history
  • Loading branch information
Soap-141 committed Jan 23, 2025
1 parent af371d8 commit eb0691d
Show file tree
Hide file tree
Showing 37 changed files with 328 additions and 239 deletions.
2 changes: 2 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,8 @@ dotnet_diagnostic.CA1065.severity = none
dotnet_diagnostic.CA1303.severity = none
# IDE0047:Remove unnecessary parentheses
dotnet_diagnostic.IDE0047.severity = suggestion
# IDE0130: Namespace does not match folder structure
dotnet_diagnostic.IDE0130.severity = none
# CA1056:Uri properties should not be strings
dotnet_diagnostic.CA1056.severity = suggestion
# CA1819:Properties should not return arrays
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Prefix your items with `(Template)` if the change is about the template and not

## 3.6.X
- Added conventional commit validation stage `stage-commit-validation.yml`
- Refactored the email and connectivity services to be in the access layer.

## 3.5.X
- Bump Uno packages to 5.2.121 to fix a crash on iOS.
Expand Down
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
<!-- Microsoft.VisualStudio.Threading.Analyzers has the async void analyzers. -->
<PackageReference Include="Microsoft.VisualStudio.Threading.Analyzers" Version="17.5.22" PrivateAssets="all" />
<PackageReference Include="Microsoft.VisualStudio.Threading.Analyzers" Version="17.12.19" PrivateAssets="all" />
<PackageReference Include="GooseAnalyzers" Version="1.0.0" PrivateAssets="all" />
</ItemGroup>

Expand Down
2 changes: 1 addition & 1 deletion doc/Architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ The application solution is divided in 3 main areas.
The other layers are `.Net Standard 2.0` libraries that are platform agnostic.

### Access (DAL)
The _data access layer_ is where you would put external dependencies such as API clients and local storage.
The _data access layer_ is where you would put external dependencies such as API clients, local storage and native platform access.
Classes providing data should be suffixed with `Repository`.
This is where you put serializable entities.
The associated `csproj` is named `Access` (and not `DataAccess`) so that it shows as the first element alphabetically.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<PackageReference Include="Microsoft.Extensions.Http" Version="3.1.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.1" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="3.1.0" />
<PackageReference Include="Microsoft.VisualStudio.Threading" Version="17.12.19" />
<PackageReference Include="Reactive.Annex" Version="2.0.0" />
<PackageReference Include="Refit" Version="8.0.0" />
<PackageReference Include="ReviewService.Abstractions" Version="1.0.0" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Microsoft.Extensions.DependencyInjection;

namespace ApplicationTemplate.DataAccess;

/// <summary>
/// This class is used for native platform configuration.
/// - Configures native platform repositories.
/// </summary>
public static class NativePlatformConfiguration
{
/// <summary>
/// Adds the native platform repositories to the <see cref="IServiceCollection"/>.
/// </summary>
/// <param name="services">Service collection.</param>
/// <returns><see cref="IServiceCollection"/>.</returns>
public static IServiceCollection AddNativePlaformRepositories(this IServiceCollection services)
{
return services
.AddSingleton<IConnectivityRepository, MockedConnectivityRepository>()
.AddSingleton<IEmailRepository, MockedEmailRepository>();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;

namespace ApplicationTemplate;
namespace ApplicationTemplate.DataAccess;

public sealed class ConnectivityChangedEventArgs : EventArgs
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;

namespace ApplicationTemplate.DataAccess;

/// <summary>
/// Provides access to the current network state and detects changes.
/// </summary>
public interface IConnectivityRepository
{
/// <summary>
/// Occurs when network state changes.
/// </summary>
event EventHandler<ConnectivityChangedEventArgs> ConnectivityChanged;

/// <summary>
/// Gets the current network state.
/// </summary>
NetworkAccess State { get; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;

namespace ApplicationTemplate.DataAccess;

public sealed class MockedConnectivityRepository : IConnectivityRepository
{
private NetworkAccess _networkAccess;

public MockedConnectivityRepository()
{
State = NetworkAccess.Internet;
}

public MockedConnectivityRepository(NetworkAccess networkAccess)
{
State = networkAccess;
}

public NetworkAccess State
{
get => _networkAccess;
set
{
_networkAccess = value;
ConnectivityChanged?.Invoke(this, new ConnectivityChangedEventArgs(value));
}
}

public event EventHandler<ConnectivityChangedEventArgs> ConnectivityChanged;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace ApplicationTemplate;
namespace ApplicationTemplate.DataAccess;

/// <summary>
/// Various states of the connection to the internet.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Collections.Generic;
using System.Linq;

namespace ApplicationTemplate;
namespace ApplicationTemplate.DataAccess;

public sealed class Email
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.IO;

namespace ApplicationTemplate;
namespace ApplicationTemplate.DataAccess;

public sealed class EmailAttachment
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace ApplicationTemplate;
namespace ApplicationTemplate.DataAccess;

public enum EmailBodyFormat
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using System.Threading.Tasks;

namespace ApplicationTemplate;
namespace ApplicationTemplate.DataAccess;

/// <summary>
/// Provides methods to send emails.
/// Provides access to native methods to send emails.
/// </summary>
public interface IEmailService
public interface IEmailRepository
{
/// <summary>
/// Launches the email application with a new <see cref="Email"/> displayed.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;

namespace ApplicationTemplate.DataAccess;

public sealed class MockedEmailRepository : IEmailRepository
{
private readonly ILogger<MockedEmailRepository> _logger;

public MockedEmailRepository(ILogger<MockedEmailRepository> logger)
{
_logger = logger;
}

public Task Compose(Email email)
{
_logger.LogInformation("Email composed: {Email}", email);
return Task.CompletedTask;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<TreatWarningsAsErrors Condition="'$(Configuration)'=='Release'">true</TreatWarningsAsErrors>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.VisualStudio.Threading" Version="17.12.19" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\ApplicationTemplate.Access\ApplicationTemplate.Access.csproj" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<PackageReference Include="ExtendedSplashScreen.Uno.WinUI" Version="2.0.0" />
<PackageReference Include="MessageDialogService.Uno.WinUI" Version="2.0.0" />
<PackageReference Include="Microsoft.Maui.Essentials" Version="8.0.3" />
<PackageReference Include="Microsoft.VisualStudio.Threading" Version="17.12.19" />
<PackageReference Include="Nventive.Persistence.Uno.WinUI" Version="0.5.0" />
<PackageReference Include="Nventive.View.Uno.WinUI" Version="0.6.0" />
<PackageReference Include="Reactive.Annex.Uno.WinUI" Version="2.0.0" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Localization.Abstractions" Version="6.0.2" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="7.0.0" />
<PackageReference Include="Microsoft.VisualStudio.Threading" Version="17.12.19" />
<PackageReference Include="ReviewService" Version="1.0.0" />
<PackageReference Include="Uno.SourceGenerationTasks" Version="4.2.0" />
<PackageReference Include="Serilog.Extensions.Hosting" Version="3.0.0" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ private static IServiceCollection AddNetworkExceptionHandler(this IServiceCollec
{
return services
.AddSingleton<INetworkAvailabilityChecker>(s =>
new NetworkAvailabilityChecker(ct => Task.FromResult(s.GetRequiredService<IConnectivityProvider>().NetworkAccess is NetworkAccess.Internet))
new NetworkAvailabilityChecker(ct => Task.FromResult(s.GetRequiredService<IConnectivityRepository>().State is NetworkAccess.Internet))
)
.AddTransient<NetworkExceptionHandler>();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using ApplicationTemplate.Business;
using ApplicationTemplate.DataAccess;
using ApplicationTemplate.Presentation;
using ApplicationTemplate.Presentation.Framework.Connectivity;
using MessageDialogService;
using Microsoft.Extensions.DependencyInjection;

Expand All @@ -24,7 +23,7 @@ public static IServiceCollection AddAppServices(this IServiceCollection services
{
return services
.AddSingleton<IMessageDialogService, AcceptOrDefaultMessageDialogService>()
.AddSingleton<IConnectivityProvider, MockedConnectivityProvider>()
.AddSingleton<IConnectivityRepository, MockedConnectivityRepository>()
.AddSingleton<IBackgroundScheduler>(s => TaskPoolScheduler.Default.ToBackgroundScheduler())
.AddSingleton<IApplicationSettingsRepository, ApplicationSettingsRepository>()
.AddSingleton<IPostService, PostService>()
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using ApplicationTemplate.DataAccess;
using Chinook.DataLoader;
using MallardMessageHandlers;

Expand All @@ -12,9 +13,9 @@ namespace ApplicationTemplate;
public sealed class NetworkReconnectionDataLoaderTrigger : DataLoaderTriggerBase
{
private readonly IDataLoader _dataLoader;
private readonly IConnectivityProvider _connectivity;
private readonly IConnectivityRepository _connectivity;

public NetworkReconnectionDataLoaderTrigger(IDataLoader dataLoader, IConnectivityProvider connectivity)
public NetworkReconnectionDataLoaderTrigger(IDataLoader dataLoader, IConnectivityRepository connectivity)
: base("NetworkReconnection")
{
_dataLoader = dataLoader ?? throw new ArgumentNullException(nameof(dataLoader));
Expand Down Expand Up @@ -44,7 +45,7 @@ public override void Dispose()

public static class NetworkReconnectionDataLoaderExtensions
{
public static TBuilder TriggerOnNetworkReconnection<TBuilder>(this TBuilder dataLoaderBuilder, IConnectivityProvider connectivity)
public static TBuilder TriggerOnNetworkReconnection<TBuilder>(this TBuilder dataLoaderBuilder, IConnectivityRepository connectivity)
where TBuilder : IDataLoaderBuilder
=> (TBuilder)dataLoaderBuilder.WithTrigger(dataLoader => new NetworkReconnectionDataLoaderTrigger(dataLoader, connectivity));
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,16 @@ public interface IDiagnosticsService
/// This value is based on the platform.
/// </summary>
bool CanOpenSettingsFolder { get; }

/// <summary>
/// Sends the diagnostics summary by email.
/// </summary>
/// <param name="ct">The cancellation token.</param>
Task SendSummary(CancellationToken ct);

/// <summary>
/// Gets the diagnostics summary.
/// </summary>
/// <returns>The diagnostics summary.</returns>
string GetSummary();
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Threading;
using System.Threading.Tasks;
using ApplicationTemplate.Business;
using ApplicationTemplate.DataAccess;
using Chinook.DataLoader;
using Chinook.DynamicMvvm;
using Chinook.SectionsNavigation;
Expand All @@ -27,7 +28,7 @@ public sealed partial class DadJokesPageViewModel : ViewModel
public IDataLoader<DadJokesItemViewModel[]> Jokes => this.GetDataLoader(LoadJokes, b => b
// Dispose the previous ItemViewModels when Quotes produces new values.
.DisposePreviousData()
.TriggerOnNetworkReconnection(this.GetService<IConnectivityProvider>())
.TriggerOnNetworkReconnection(this.GetService<IConnectivityRepository>())
.TriggerFromObservable(this.GetService<IDadJokesService>().GetAndObservePostTypeFilter().Skip(1))
);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Chinook.DynamicMvvm;
using Chinook.DynamicMvvm;
using Chinook.SectionsNavigation;
using Chinook.StackNavigation;
using Uno;
Expand Down
Loading

0 comments on commit eb0691d

Please sign in to comment.