Skip to content

Commit

Permalink
offline tested and working
Browse files Browse the repository at this point in the history
  • Loading branch information
aritchie committed Sep 22, 2024
1 parent feec966 commit 4cac265
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 25 deletions.
2 changes: 1 addition & 1 deletion samples/Sample/Handlers/OfflineRequestHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Sample.Handlers;
[SingletonHandler]
public class OfflineRequestHandler : IRequestHandler<OfflineRequest, string>
{
[OfflineAvailable(true)]
// [OfflineAvailable(true)]
public Task<string> Handle(OfflineRequest request, CancellationToken cancellationToken)
{
var r = DateTimeOffset.Now.ToString("h:mm:ss tt");
Expand Down
12 changes: 4 additions & 8 deletions samples/Sample/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
},
"Offline": {
// * probably a bad idea - block?
"*": {
"Sample.Handlers.OfflineRequestHandler": {
"AvailableAcrossSessions": true
}
},
Expand Down Expand Up @@ -46,16 +46,12 @@
"SlidingExpirationSeconds": 60
}
},
"UserNotify": {
"UserErrorNotifications": {
// this works
"*": {
"en-US": {
"*": {
"Title": "ERROR",
"ErrorMessage" : "Failed to do something"
},
"fr-CA": {
"Title": "ERREUR",
"ErrorMessage" : "Échec de faire quelque chose"
"Message" : "Failed to do something"
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/Shiny.Mediator.AppSupport/AppSupportExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,22 @@ public static class AppSupportExtensions
/// <returns></returns>
public static ShinyConfigurator AddStandardAppSupportMiddleware(this ShinyConfigurator cfg)
{
cfg.AddUserNotificationExceptionMiddleware();
cfg.AddUserErrorNotificationsRequestMiddleware();
cfg.AddOfflineAvailabilityMiddleware();
cfg.AddReplayStreamMiddleware();
return cfg;
}


/// <summary>
/// Allows you to mark [UserNotify] on your request handlers which logs an error & displays an alert to the user
/// Allows you to configure error handling on your request handlers which logs an error & displays an alert to the user
/// to show a customized message
/// </summary>
/// <param name="cfg"></param>
/// <returns></returns>
public static ShinyConfigurator AddUserNotificationExceptionMiddleware(this ShinyConfigurator cfg)
public static ShinyConfigurator AddUserErrorNotificationsRequestMiddleware(this ShinyConfigurator cfg)
{
cfg.AddOpenRequestMiddleware(typeof(UserExceptionRequestMiddleware<,>));
cfg.AddOpenRequestMiddleware(typeof(UserErrorNotificationsRequestMiddleware<,>));
return cfg;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace Shiny.Mediator.Infrastructure;


// TODO: this needs a "clear by type - replay, offline, etc"
public interface IStorageService
{
Task Store(object request, object result, bool isPeristent);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
using System.Globalization;
using System.Reflection;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Shiny.Mediator.Infrastructure;

namespace Shiny.Mediator.Middleware;


public class UserExceptionRequestMiddleware<TRequest, TResult>(
public class UserErrorNotificationsRequestMiddleware<TRequest, TResult>(
ILogger<TRequest> logger,
IAlertDialogService alerts,
IConfiguration configuration
Expand All @@ -20,19 +19,10 @@ public async Task<TResult> Process(
CancellationToken cancellationToken
)
{
var section = configuration.GetHandlerSection("UserNotify", request!, requestHandler);
var section = configuration.GetHandlerSection("UserErrorNotifications", request!, requestHandler);
if (section == null)
return await next().ConfigureAwait(false);

var key = CultureInfo.CurrentUICulture.Name;
var locale = section.GetSection(key);
if (!locale.Exists())
{
locale = section.GetSection("*");
if (!locale.Exists())
logger.LogError("No locale found for {RequestType}", typeof(TRequest).FullName);
}

var result = default(TResult);
try
{
Expand All @@ -41,6 +31,16 @@ CancellationToken cancellationToken
catch (Exception ex)
{
logger.LogError(ex, "Error executing pipeline for {Error}", typeof(TRequest).FullName);

var key = CultureInfo.CurrentUICulture.Name.ToLower();
var locale = section.GetSection(key);
if (!locale.Exists())
{
locale = section.GetSection("*");
if (!locale.Exists())
logger.LogError("No locale found for {RequestType}", typeof(TRequest).FullName);
}

if (locale.Exists())
{
var title = locale.GetValue<string>("Title", "ERROR");
Expand Down

0 comments on commit 4cac265

Please sign in to comment.