Skip to content

Commit f2d6873

Browse files
committed
2 parents f4cfa8c + d420285 commit f2d6873

File tree

2 files changed

+121
-0
lines changed

2 files changed

+121
-0
lines changed

src/Nomad/ModifiableAccentColor.cs

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
using Ipfs;
2+
using OwlCore.ComponentModel;
3+
using OwlCore.Kubo;
4+
using OwlCore.Nomad;
5+
using OwlCore.Nomad.Kubo;
6+
using WindowsAppCommunity.Sdk.Models;
7+
8+
namespace WindowsAppCommunity.Sdk.Nomad;
9+
10+
/// <inheritdoc cref="IModifiableAccentColor" />
11+
public class ModifiableAccentColor : NomadKuboEventStreamHandler<ValueUpdateEvent>, IModifiableAccentColor, IDelegable<ReadOnlyAccentColor>
12+
{
13+
/// <inheritdoc />
14+
public required ReadOnlyAccentColor Inner { get; init; }
15+
16+
/// <summary>
17+
/// A unique identifier for this instance, persistent across machines and reruns.
18+
/// </summary>
19+
public required string Id { get; init; }
20+
21+
/// <inheritdoc />
22+
public string? AccentColor => Inner.AccentColor;
23+
24+
/// <inheritdoc />
25+
public event EventHandler<string?>? AccentColorUpdated;
26+
27+
/// <inheritdoc />
28+
public override async Task ApplyEntryUpdateAsync(ValueUpdateEvent updateEvent, CancellationToken cancellationToken)
29+
{
30+
cancellationToken.ThrowIfCancellationRequested();
31+
32+
if (updateEvent.TargetId != Id)
33+
return;
34+
35+
string? accentColor = null;
36+
37+
if (updateEvent is { Unset: false, Value: not null })
38+
{
39+
(accentColor, _) = await Client.ResolveDagCidAsync<string>(updateEvent.Value, nocache: !KuboOptions.UseCache, cancellationToken);
40+
}
41+
42+
await ApplyEntryUpdateAsync(updateEvent, accentColor, cancellationToken);
43+
}
44+
45+
/// <summary>
46+
/// Applies an event stream update event and raises the relevant events.
47+
/// </summary>
48+
/// <param name="updateEvent">The update event to apply.</param>
49+
/// <param name="accentColor">The resolved accent color data for this event.</param>
50+
/// <param name="cancellationToken">A token that can be used to cancel the ongoing operation.</param>
51+
public Task ApplyEntryUpdateAsync(ValueUpdateEvent updateEvent, string? accentColor, CancellationToken cancellationToken)
52+
{
53+
cancellationToken.ThrowIfCancellationRequested();
54+
55+
if (updateEvent.EventId is not nameof(UpdateAccentColorAsync))
56+
return Task.CompletedTask;
57+
58+
Inner.Inner.AccentColor = accentColor;
59+
AccentColorUpdated?.Invoke(this, accentColor);
60+
61+
return Task.CompletedTask;
62+
}
63+
64+
/// <inheritdoc cref="INomadKuboEventStreamHandler{TEventEntryContent}.AppendNewEntryAsync" />
65+
public override async Task<EventStreamEntry<Cid>> AppendNewEntryAsync(ValueUpdateEvent updateEvent, CancellationToken cancellationToken = default)
66+
{
67+
var localUpdateEventCid = await Client.Dag.PutAsync(updateEvent, pin: KuboOptions.ShouldPin, cancel: cancellationToken);
68+
var newEntry = await this.AppendEventStreamEntryAsync(localUpdateEventCid, updateEvent.EventId, updateEvent.TargetId, cancellationToken);
69+
return newEntry;
70+
}
71+
72+
/// <inheritdoc />
73+
public override Task ResetEventStreamPositionAsync(CancellationToken cancellationToken)
74+
{
75+
EventStreamPosition = null;
76+
Inner.Inner.AccentColor = null;
77+
78+
return Task.CompletedTask;
79+
}
80+
81+
/// <inheritdoc />
82+
public async Task UpdateAccentColorAsync(string? accentColor, CancellationToken cancellationToken)
83+
{
84+
DagCid? valueCid = null;
85+
if (accentColor is not null)
86+
{
87+
Cid cid = await Client.Dag.PutAsync(accentColor, pin: KuboOptions.ShouldPin, cancel: cancellationToken);
88+
valueCid = (DagCid)cid;
89+
}
90+
91+
var updateEvent = new ValueUpdateEvent(Id, nameof(UpdateAccentColorAsync), null, valueCid, accentColor is null);
92+
93+
await ApplyEntryUpdateAsync(updateEvent, accentColor, cancellationToken);
94+
var appendedEntry = await AppendNewEntryAsync(updateEvent, cancellationToken);
95+
96+
EventStreamPosition = appendedEntry;
97+
}
98+
}

src/Nomad/ReadOnlyAccentColor.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using Ipfs.CoreApi;
2+
using OwlCore.ComponentModel;
3+
using WindowsAppCommunity.Sdk.Models;
4+
5+
namespace WindowsAppCommunity.Sdk.Nomad;
6+
7+
/// <inheritdoc cref="IReadOnlyAccentColor" />
8+
public class ReadOnlyAccentColor : IReadOnlyAccentColor, IDelegable<IAccentColor>
9+
{
10+
/// <summary>
11+
/// The client to use for communicating with ipfs.
12+
/// </summary>
13+
public required ICoreApi Client { get; init; }
14+
15+
/// <inheritdoc />
16+
public required IAccentColor Inner { get; init; }
17+
18+
/// <inheritdoc />
19+
public string? AccentColor => Inner.AccentColor;
20+
21+
/// <inheritdoc />
22+
public event EventHandler<string?>? AccentColorUpdated;
23+
}

0 commit comments

Comments
 (0)