-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
302 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using Microsoft.Extensions.Logging; | ||
using Shiny.Mediator.Infrastructure; | ||
using Uno.Extensions.Hosting; | ||
|
||
namespace Shiny.Mediator; | ||
|
||
|
||
public class ConnectivityBroadcaster( | ||
ILogger<ConnectivityBroadcaster> logger, | ||
IMediator mediator, | ||
IInternetService internetService | ||
) : IServiceInitialize | ||
{ | ||
public void Initialize() | ||
{ | ||
internetService.StateChanged += async (_, connected) => | ||
{ | ||
try | ||
{ | ||
await mediator.Publish(new ConnectivityChanged(connected)); | ||
} | ||
catch (Exception ex) | ||
{ | ||
logger.LogError(ex, "Error occured while connectivity Sprayer"); | ||
} | ||
}; | ||
} | ||
} | ||
|
||
public record ConnectivityChanged(bool Connected) : IEvent; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
global using System; | ||
global using System.Collections.Generic; | ||
global using System.Linq; | ||
global using System.Threading; | ||
global using System.Threading.Tasks; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using System.Globalization; | ||
using System.Net.Http; | ||
using System.Net.Http.Headers; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Shiny.Mediator.Http; | ||
|
||
|
||
public class UnoHttpRequestDecorator<TRequest, TResult>( | ||
ILogger<UnoHttpRequestDecorator<TRequest, TResult>> logger | ||
) : IHttpRequestDecorator<TRequest, TResult> where TRequest : IHttpRequest<TResult> | ||
{ | ||
public async Task Decorate(HttpRequestMessage httpMessage, TRequest request) | ||
{ | ||
var pkg = Windows.ApplicationModel.Package.Current; | ||
|
||
httpMessage.Headers.Add("AppId", pkg.Id.FullName); | ||
httpMessage.Headers.Add("AppVersion", pkg.Id.Version.ToString() ?? "1.0.0"); | ||
// httpMessage.Headers.Add("DeviceManufacturer", deviceInfo.Manufacturer); | ||
// httpMessage.Headers.Add("DeviceModel", deviceInfo.Model); | ||
// httpMessage.Headers.Add("DevicePlatform", deviceInfo.Platform.ToString()); | ||
// httpMessage.Headers.Add("DeviceVersion", deviceInfo.Version.ToString()); | ||
httpMessage.Headers.AcceptLanguage.Add(new StringWithQualityHeaderValue(CultureInfo.CurrentCulture.Name)); | ||
|
||
// try | ||
// { | ||
// var result = await Permissions.CheckStatusAsync<Permissions.LocationWhenInUse>(); | ||
// if (result == PermissionStatus.Granted) | ||
// { | ||
// var gps = await geolocation.GetLastKnownLocationAsync(); | ||
// if (gps != null) | ||
// httpMessage.Headers.Add("GpsCoords", $"{gps.Latitude},{gps.Longitude}"); | ||
// } | ||
// } | ||
// catch (Exception ex) | ||
// { | ||
// logger.LogInformation(ex, "Failed to get GPS"); | ||
// } | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/Shiny.Mediator.Uno/Infrastructure/AlertDialogService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.UI.Xaml.Controls; | ||
|
||
namespace Shiny.Mediator.Infrastructure; | ||
|
||
|
||
public class AlertDialogService(ILogger<AlertDialogService> logger) : IAlertDialogService | ||
{ | ||
public void Display(string title, string message) | ||
{ | ||
// TODO: could use acr userdialogs | ||
// ContentDialog deleteFileDialog = new ContentDialog | ||
// { | ||
// Title = "Delete file permanently?", | ||
// Content = "If you delete this file, you won't be able to recover it. Do you want to delete it?", | ||
// PrimaryButtonText = "Delete", | ||
// CloseButtonText = "Cancel" | ||
// }; | ||
// | ||
// deleteFileDialog.XamlRoot = anyLoadedControl.XamlRoot; | ||
// | ||
// ContentDialogResult result = await deleteFileDialog.ShowAsync(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
using Windows.Networking.Connectivity; | ||
|
||
namespace Shiny.Mediator.Infrastructure; | ||
|
||
|
||
// https://platform.uno/docs/articles/features/windows-networking.html | ||
|
||
public class InternetService : IInternetService | ||
{ | ||
EventHandler<bool>? handler; | ||
public event EventHandler<bool>? StateChanged | ||
{ | ||
add | ||
{ | ||
if (this.handler == null) | ||
{ | ||
//NetworkInformation.NetworkStatusChanged += handler; | ||
} | ||
this.handler += value; | ||
} | ||
remove | ||
{ | ||
//NetworkInformation.NetworkStatusChanged -= handler; | ||
} | ||
} | ||
|
||
public bool IsAvailable | ||
{ | ||
get | ||
{ | ||
var profile = NetworkInformation.GetInternetConnectionProfile(); | ||
if (profile == null) | ||
return false; | ||
|
||
var level = profile.GetNetworkConnectivityLevel(); | ||
return level == NetworkConnectivityLevel.InternetAccess; | ||
} | ||
} | ||
|
||
|
||
public async Task WaitForAvailable(CancellationToken cancelToken = default) | ||
{ | ||
if (this.IsAvailable) | ||
return; | ||
|
||
var tcs = new TaskCompletionSource(); | ||
var handler = new NetworkStatusChangedEventHandler(_ => | ||
{ | ||
if (this.IsAvailable) | ||
tcs.TrySetResult(); | ||
}); | ||
try | ||
{ | ||
using var _ = cancelToken.Register(() => tcs.TrySetCanceled()); | ||
NetworkInformation.NetworkStatusChanged += handler; | ||
await tcs.Task.ConfigureAwait(false); | ||
} | ||
finally | ||
{ | ||
NetworkInformation.NetworkStatusChanged -= handler; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using Uno.Extensions.Storage; | ||
|
||
namespace Shiny.Mediator.Infrastructure; | ||
|
||
|
||
public class StorageService(IStorage storage, ISerializerService serializer) : IStorageService | ||
{ | ||
public Task Set<T>(string key, T value) | ||
{ | ||
// var path = this.GetFilePath(key); | ||
// var json = serializer.Serialize(value); | ||
// File.WriteAllText(path, json); | ||
|
||
return Task.CompletedTask; | ||
} | ||
|
||
|
||
public Task<T?> Get<T>(string key) | ||
{ | ||
// T? returnValue = default; | ||
// var path = this.GetFilePath(key); | ||
// if (File.Exists(path)) | ||
// { | ||
// var json = File.ReadAllText(path); | ||
// returnValue = serializer.Deserialize<T>(json)!; | ||
// } | ||
|
||
//return Task.FromResult(returnValue); | ||
return Task.FromResult((T?)default); | ||
} | ||
|
||
|
||
|
||
public Task Remove(string key) | ||
{ | ||
// var fn = this.GetFilePath(key); | ||
// if (File.Exists(fn)) | ||
// File.Delete(fn); | ||
// | ||
// return Task.CompletedTask; | ||
return null; | ||
} | ||
|
||
public Task RemoveByPrefix(string prefix) => this.DeleteBy(prefix + "*.mediator"); | ||
public Task Clear() => this.DeleteBy("*.mediator"); | ||
|
||
|
||
Task DeleteBy(string pattern) | ||
{ | ||
// var dir = new DirectoryInfo(fileSystem.CacheDirectory); | ||
// var files = dir.GetFiles(pattern); | ||
// foreach (var file in files) | ||
// file.Delete(); | ||
// | ||
return Task.CompletedTask; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<Project Sdk="Uno.Sdk/5.6.22"> | ||
<PropertyGroup> | ||
<TargetFramework>net9.0</TargetFramework> | ||
<UnoSingleProject>true</UnoSingleProject> | ||
<OutputType>Library</OutputType> | ||
<Nullable>enable</Nullable> | ||
|
||
<UnoFeatures>Hosting;Storage</UnoFeatures> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Shiny.Mediator.AppSupport\Shiny.Mediator.AppSupport.csproj"/> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.DependencyInjection.Extensions; | ||
using Microsoft.Extensions.Hosting; | ||
using Shiny.Mediator.Infrastructure; | ||
using Uno.Extensions.Hosting; | ||
|
||
namespace Shiny.Mediator; | ||
|
||
|
||
public static class UnoExtensions | ||
{ | ||
/// <summary> | ||
/// Add shiny mediator to Uno | ||
/// </summary> | ||
/// <param name="builder"></param> | ||
/// <param name="configure"></param> | ||
/// <param name="includeStandardMiddleware"></param> | ||
/// <returns></returns> | ||
public static IHostBuilder AddShinyMediator(this IHostBuilder builder, Action<ShinyConfigurator> configure, bool includeStandardMiddleware = true) | ||
{ | ||
// TODO: Uno Event Collector... How? | ||
builder.ConfigureServices(x => x.AddShinyMediator( | ||
cfg => | ||
{ | ||
if (includeStandardMiddleware) | ||
{ | ||
cfg.AddStandardAppSupportMiddleware(); | ||
cfg.AddUnoInfrastructure(); | ||
} | ||
}, | ||
includeStandardMiddleware | ||
)); | ||
return builder; | ||
} | ||
|
||
|
||
/// <summary> | ||
/// Adds necessary infrastructure for standard app middleware | ||
/// </summary> | ||
/// <param name="cfg"></param> | ||
/// <returns></returns> | ||
public static ShinyConfigurator AddUnoInfrastructure(this ShinyConfigurator cfg) | ||
{ | ||
cfg.Services.TryAddSingleton<IInternetService, InternetService>(); | ||
cfg.Services.TryAddSingleton<IAlertDialogService, AlertDialogService>(); | ||
cfg.Services.TryAddSingleton<IStorageService, StorageService>(); | ||
return cfg; | ||
} | ||
|
||
|
||
/// <summary> | ||
/// Adds connectivity broadcaster | ||
/// </summary> | ||
/// <param name="configurator"></param> | ||
/// <returns></returns> | ||
public static ShinyConfigurator AddConnectivityBroadcaster(this ShinyConfigurator configurator) | ||
{ | ||
configurator.AddUnoInfrastructure(); | ||
configurator.Services.AddSingleton<IServiceInitialize, ConnectivityBroadcaster>(); | ||
return configurator; | ||
} | ||
} | ||
|