Skip to content

Commit

Permalink
Implement services
Browse files Browse the repository at this point in the history
  • Loading branch information
WilStead committed Nov 25, 2024
1 parent ad364a3 commit 88d9b27
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,6 @@ public static class ExampleWikiOptions
{
AppBar = typeof(TopAppBar),
AppBarRenderMode = RenderMode.InteractiveWebAssembly,
ArticleFrontMatter = page => string.IsNullOrEmpty(page.Title.Namespace)
&& string.IsNullOrEmpty(page.Title.Title)
? typeof(MainFrontMatter)
: null,
ArticleFrontMatterRenderMode = page => string.IsNullOrEmpty(page.Title.Namespace)
&& string.IsNullOrEmpty(page.Title.Title)
? RenderMode.InteractiveWebAssembly
: null,
CanEditOffline = (_, _, _) => ValueTask.FromResult(true),
ContactPageTitle = null,
ContentsPageTitle = null,
CopyrightPageTitle = null,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using Tavenem.Wiki.Blazor.Example;
using Tavenem.Wiki.Blazor.Example.Client.Services;
using Tavenem.Wiki.Blazor.Example.Services;

var builder = WebAssemblyHostBuilder.CreateDefault(args);
Expand All @@ -14,6 +15,12 @@
builder.Services.AddScoped<AuthenticationStateProvider>(provider =>
provider.GetRequiredService<CustomAuthenticationStateProvider>());

builder.Services.AddWikiClient(ExampleWikiOptions.Instance);
builder.Services.AddWikiClient(
ExampleWikiOptions.Instance,
config =>
{
config.ConfigureArticleRenderManager(typeof(CustomArticleRenderManager));
config.ConfigureOfflineManager(typeof(CustomOfflineManager));
});

await builder.Build().RunAsync();
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
using System.Diagnostics.CodeAnalysis;
using Tavenem.Wiki.Blazor.Client.Services;

namespace Tavenem.Wiki.Blazor.Example.Client.Services;

public class CustomArticleRenderManager : IArticleRenderManager
{
/// <inheritdoc />
[return: DynamicallyAccessedMembers((DynamicallyAccessedMemberTypes)(-1))]
public Type? GetArticleEndMatter(Page page) => null;

/// <inheritdoc />
public IComponentRenderMode? GetArticleEndMatterRenderMode(Page page) => null;

/// <inheritdoc />
[return: DynamicallyAccessedMembers((DynamicallyAccessedMemberTypes)(-1))]
public Type? GetArticleFrontMatter(Page page) => string.IsNullOrEmpty(page.Title.Namespace)
&& string.IsNullOrEmpty(page.Title.Title)
? typeof(MainFrontMatter)
: null;

/// <inheritdoc />
public IComponentRenderMode? GetArticleFrontMatterRenderMode(Page page) => string.IsNullOrEmpty(page.Title.Namespace)
&& string.IsNullOrEmpty(page.Title.Title)
? RenderMode.InteractiveWebAssembly
: null;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Tavenem.Wiki.Blazor.Client.Services;

namespace Tavenem.Wiki.Blazor.Example.Client.Services;

public class CustomOfflineManager : IOfflineManager
{
/// <inheritdoc />
public ValueTask<bool> CanEditOfflineAsync(string title, string wikiNamespace, string? domain)
=> ValueTask.FromResult(true);

/// <inheritdoc />
public ValueTask<bool> IsOfflineDomainAsync(string domain)
=> ValueTask.FromResult(false);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Tavenem.DataStorage;
using Tavenem.Wiki;
using Tavenem.Wiki.Blazor.Example;
using Tavenem.Wiki.Blazor.Example.Client.Services;
using Tavenem.Wiki.Blazor.Example.Components;
using Tavenem.Wiki.Blazor.Example.Services;

Expand All @@ -26,7 +27,12 @@

builder.Services.AddWikiServer(
ExampleWikiOptions.Instance,
options => options.ConfigureUserManager(typeof(DefaultUserManager)));
config =>
{
config.ConfigureArticleRenderManager(typeof(CustomArticleRenderManager));
config.ConfigureOfflineManager(typeof(CustomOfflineManager));
config.ConfigureUserManager(typeof(DefaultUserManager));
});

var archiveText = File.ReadAllText(Path.Combine(builder.Environment.WebRootPath, "archive.json"));
var archive = JsonSerializer.Deserialize<Archive>(archiveText, WikiArchiveJsonSerializerOptions.Instance);
Expand Down
5 changes: 3 additions & 2 deletions sample/WebAssembly Example/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
var wikiOptions = new WikiBlazorOptions()
{
AppBar = typeof(TopAppBar),
CanEditOffline = (_, _, _) => ValueTask.FromResult(true),
ContactPageTitle = null,
ContentsPageTitle = null,
CopyrightPageTitle = null,
Expand All @@ -61,6 +60,8 @@
}
}

builder.Services.AddWikiClient(wikiOptions);
builder.Services.AddWikiClient(
wikiOptions,
config => config.ConfigureOfflineManager(typeof(CustomOfflineManager)));

await builder.Build().RunAsync();
14 changes: 14 additions & 0 deletions sample/WebAssembly Example/Services/CustomOfflineManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Tavenem.Wiki.Blazor.Client.Services;

namespace Tavenem.Wiki.Blazor.Sample.Services;

public class CustomOfflineManager : IOfflineManager
{
/// <inheritdoc />
public ValueTask<bool> CanEditOfflineAsync(string title, string wikiNamespace, string? domain)
=> ValueTask.FromResult(true);

/// <inheritdoc />
public ValueTask<bool> IsOfflineDomainAsync(string domain)
=> ValueTask.FromResult(false);
}
11 changes: 6 additions & 5 deletions src/Client/Pages/ArticleView.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Rendering;
using System.Diagnostics.CodeAnalysis;
using Tavenem.Wiki.Blazor.Client.Services;

namespace Tavenem.Wiki.Blazor.Client.Pages;

Expand Down Expand Up @@ -34,7 +35,7 @@ public class ArticleView : ComponentBase
/// </summary>
[Parameter] public IWikiUser? User { get; set; }

[Inject, NotNull] private WikiBlazorOptions? WikiBlazorClientOptions { get; set; }
[Inject, NotNull] private IArticleRenderManager? ArticleRenderManager { get; set; }

[Inject, NotNull] private WikiOptions? WikiOptions { get; set; }

Expand All @@ -53,10 +54,10 @@ protected override void BuildRenderTree(RenderTreeBuilder builder)

if (!IsDiff && Page is not null)
{
var frontMatterType = WikiBlazorClientOptions.GetArticleFrontMatter(Page);
var frontMatterType = ArticleRenderManager.GetArticleFrontMatter(Page);
if (frontMatterType is not null)
{
var frontMatterRenderMode = WikiBlazorClientOptions.GetArticleFrontMatterRenderMode(Page);
var frontMatterRenderMode = ArticleRenderManager.GetArticleFrontMatterRenderMode(Page);
builder.OpenComponent(4, frontMatterType);
builder.AddAttribute(5, nameof(WikiComponent.Page), Page);
builder.AddAttribute(6, nameof(WikiComponent.CanEdit), CanEdit);
Expand All @@ -76,10 +77,10 @@ protected override void BuildRenderTree(RenderTreeBuilder builder)

if (!IsDiff && Page is not null)
{
var endMatterType = WikiBlazorClientOptions.GetArticleFrontMatter(Page);
var endMatterType = ArticleRenderManager.GetArticleEndMatter(Page);
if (endMatterType is not null)
{
var endMatterRenderMode = WikiBlazorClientOptions.GetArticleFrontMatterRenderMode(Page);
var endMatterRenderMode = ArticleRenderManager.GetArticleEndMatterRenderMode(Page);
builder.OpenComponent(11, endMatterType);
builder.AddAttribute(12, nameof(WikiComponent.Page), Page);
builder.AddAttribute(13, nameof(WikiComponent.CanEdit), CanEdit);
Expand Down
7 changes: 4 additions & 3 deletions src/Client/Pages/Upload.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public partial class Upload

private bool InsufficientSpace { get; set; }

[Inject, NotNull] private IOfflineManager? OfflineManager { get; set; }

private bool IsInteractive { get; set; }

[Inject, NotNull] private NavigationManager? NavigationManager { get; set; }
Expand Down Expand Up @@ -222,10 +224,9 @@ private async Task UploadAsync(bool confirmOverwrite = false)

var isLocal = string.IsNullOrEmpty(WikiBlazorClientOptions.WikiServerApiRoute);
if (!isLocal
&& !string.IsNullOrEmpty(WikiState.WikiDomain)
&& WikiBlazorClientOptions.IsOfflineDomain is not null)
&& !string.IsNullOrEmpty(WikiState.WikiDomain))
{
isLocal = await WikiBlazorClientOptions.IsOfflineDomain.Invoke(WikiState.WikiDomain);
isLocal = await OfflineManager.IsOfflineDomainAsync(WikiState.WikiDomain);
}
if (!isLocal && HttpClient is not null)
{
Expand Down
31 changes: 13 additions & 18 deletions src/Client/Services/ClientWikiDataService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class ClientWikiDataService(
WikiDataService wikiDataService,
ILoggerFactory loggerFactory,
NavigationManager navigationManager,
IOfflineManager offlineManager,
IServiceProvider serviceProvider,
SnackbarService snackbarService,
WikiBlazorOptions wikiBlazorClientOptions,
Expand Down Expand Up @@ -539,10 +540,9 @@ public Task RestoreArchiveAsync(Archive archive) => PostAsync(

var isLocal = string.IsNullOrEmpty(wikiBlazorClientOptions.WikiServerApiRoute);
if (!isLocal
&& !string.IsNullOrEmpty(wikiState.WikiDomain)
&& wikiBlazorClientOptions.IsOfflineDomain is not null)
&& !string.IsNullOrEmpty(wikiState.WikiDomain))
{
isLocal = await wikiBlazorClientOptions.IsOfflineDomain.Invoke(wikiState.WikiDomain);
isLocal = await offlineManager.IsOfflineDomainAsync(wikiState.WikiDomain);
}
ClaimsPrincipal? user = null;
AuthenticationState? state = null;
Expand Down Expand Up @@ -635,10 +635,9 @@ private async Task<int> FetchIntAsync(
{
var isLocal = string.IsNullOrEmpty(wikiBlazorClientOptions.WikiServerApiRoute);
if (!isLocal
&& !string.IsNullOrEmpty(wikiState.WikiDomain)
&& wikiBlazorClientOptions.IsOfflineDomain is not null)
&& !string.IsNullOrEmpty(wikiState.WikiDomain))
{
isLocal = await wikiBlazorClientOptions.IsOfflineDomain.Invoke(wikiState.WikiDomain);
isLocal = await offlineManager.IsOfflineDomainAsync(wikiState.WikiDomain);
}
ClaimsPrincipal? user = null;
AuthenticationState? state = null;
Expand Down Expand Up @@ -732,10 +731,9 @@ private async Task<int> FetchIntAsync(
{
var isLocal = string.IsNullOrEmpty(wikiBlazorClientOptions.WikiServerApiRoute);
if (!isLocal
&& !string.IsNullOrEmpty(wikiState.WikiDomain)
&& wikiBlazorClientOptions.IsOfflineDomain is not null)
&& !string.IsNullOrEmpty(wikiState.WikiDomain))
{
isLocal = await wikiBlazorClientOptions.IsOfflineDomain.Invoke(wikiState.WikiDomain);
isLocal = await offlineManager.IsOfflineDomainAsync(wikiState.WikiDomain);
}
ClaimsPrincipal? user = null;
AuthenticationState? state = null;
Expand Down Expand Up @@ -839,10 +837,9 @@ private async Task<int> FetchIntAsync(

var isLocal = string.IsNullOrEmpty(wikiBlazorClientOptions.WikiServerApiRoute);
if (!isLocal
&& !string.IsNullOrEmpty(wikiState.WikiDomain)
&& wikiBlazorClientOptions.IsOfflineDomain is not null)
&& !string.IsNullOrEmpty(wikiState.WikiDomain))
{
isLocal = await wikiBlazorClientOptions.IsOfflineDomain.Invoke(wikiState.WikiDomain);
isLocal = await offlineManager.IsOfflineDomainAsync(wikiState.WikiDomain);
}
ClaimsPrincipal? user = null;
AuthenticationState? state = null;
Expand Down Expand Up @@ -954,10 +951,9 @@ private async Task<bool> PostAsync<T>(
{
var isLocal = string.IsNullOrEmpty(wikiBlazorClientOptions.WikiServerApiRoute);
if (!isLocal
&& !string.IsNullOrEmpty(wikiState.WikiDomain)
&& wikiBlazorClientOptions.IsOfflineDomain is not null)
&& !string.IsNullOrEmpty(wikiState.WikiDomain))
{
isLocal = await wikiBlazorClientOptions.IsOfflineDomain.Invoke(wikiState.WikiDomain);
isLocal = await offlineManager.IsOfflineDomainAsync(wikiState.WikiDomain);
}
ClaimsPrincipal? user = null;
AuthenticationState? state = null;
Expand Down Expand Up @@ -1059,10 +1055,9 @@ private async Task<bool> PostAsync<T>(

var isLocal = string.IsNullOrEmpty(wikiBlazorClientOptions.WikiServerApiRoute);
if (!isLocal
&& !string.IsNullOrEmpty(wikiState.WikiDomain)
&& wikiBlazorClientOptions.IsOfflineDomain is not null)
&& !string.IsNullOrEmpty(wikiState.WikiDomain))
{
isLocal = await wikiBlazorClientOptions.IsOfflineDomain.Invoke(wikiState.WikiDomain);
isLocal = await offlineManager.IsOfflineDomainAsync(wikiState.WikiDomain);
}
ClaimsPrincipal? user = null;
AuthenticationState? state = null;
Expand Down

0 comments on commit 88d9b27

Please sign in to comment.