43 lines
1.3 KiB
C#
43 lines
1.3 KiB
C#
using Blazored.LocalStorage;
|
|||
|
|
|
||
|
|
using decePubClient.Models;
|
||
|
|
|
||
|
|
namespace decePubClient.Services
|
||
|
|
{
|
||
|
|
/// <summary>
|
||
|
|
/// Asks a persona, once, before its first reaction or poll vote, to accept that these are public and visible as that
|
||
|
|
/// persona (an owner decision). PublicNoticeHost shows the question; Confirm returns true at once for a persona that
|
||
|
|
/// already accepted it, and false when the reader declines.
|
||
|
|
/// </summary>
|
||
|
|
public class PublicNoticeService(ILocalStorageService localStorage)
|
||
|
|
{
|
||
|
|
TaskCompletionSource<bool> pending;
|
||
|
|
|
||
|
|
public event Action Changed;
|
||
|
|
|
||
|
|
public bool IsAsking => pending is not null;
|
||
|
|
|
||
|
|
public async Task<bool> Confirm(string personaId, CancellationToken cancellationToken = default)
|
||
|
|
{
|
||
|
|
if (string.IsNullOrEmpty(personaId))
|
||
|
|
return false;
|
||
|
|
var seen = await localStorage.GetItemAsync<PublicActionNotices>(nameof(PublicActionNotices), cancellationToken) ?? new();
|
||
|
|
if (seen.PersonaIds.Contains(personaId))
|
||
|
|
return true;
|
||
|
|
|
||
|
|
pending = new TaskCompletionSource<bool>();
|
||
|
|
Changed?.Invoke();
|
||
|
|
var accepted = await pending.Task;
|
||
|
|
pending = null;
|
||
|
|
Changed?.Invoke();
|
||
|
|
if (!accepted)
|
||
|
|
return false;
|
||
|
|
|
||
|
|
seen.PersonaIds.Add(personaId);
|
||
|
|
await localStorage.SetItemAsync(nameof(PublicActionNotices), seen, cancellationToken);
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
public void Answer(bool accepted) => pending?.TrySetResult(accepted);
|
||
|
|
}
|
||
|
|
}
|