Skip to content

Commit ce6821c

Browse files
committed
Complete first-pass implementation
1 parent ceeeee5 commit ce6821c

File tree

1 file changed

+54
-13
lines changed

1 file changed

+54
-13
lines changed

src/Nomad/ModifiableAccentColor.cs

Lines changed: 54 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
1-
using System.Collections.Generic;
2-
using System.Linq;
3-
using CommunityToolkit.Diagnostics;
4-
using Ipfs;
5-
using Ipfs.CoreApi;
1+
using Ipfs;
62
using OwlCore.ComponentModel;
73
using OwlCore.Kubo;
84
using OwlCore.Nomad;
95
using OwlCore.Nomad.Kubo;
10-
using OwlCore.Storage;
116
using WindowsAppCommunity.Sdk.Models;
127

138
namespace WindowsAppCommunity.Sdk.Nomad;
@@ -37,24 +32,70 @@ public override async Task ApplyEntryUpdateAsync(ValueUpdateEvent updateEvent, C
3732
if (updateEvent.TargetId != Id)
3833
return;
3934

40-
Inner.AccentColor = updateEvent.Value;
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);
4143
}
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();
4254

43-
/// <inheritdoc />
44-
public override Task<EventStreamEntry<Cid>> AppendNewEntryAsync(ValueUpdateEvent updateEvent, CancellationToken cancellationToken = new CancellationToken())
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)
4566
{
46-
throw new NotImplementedException();
67+
// Use extension method for code deduplication (can't use inheritance).
68+
var localUpdateEventCid = await Client.Dag.PutAsync(updateEvent, pin: KuboOptions.ShouldPin, cancel: cancellationToken);
69+
var newEntry = await this.AppendEventStreamEntryAsync(localUpdateEventCid, updateEvent.EventId, updateEvent.TargetId, cancellationToken);
70+
return newEntry;
4771
}
4872

4973
/// <inheritdoc />
5074
public override Task ResetEventStreamPositionAsync(CancellationToken cancellationToken)
5175
{
52-
throw new NotImplementedException();
76+
EventStreamPosition = null;
77+
Inner.Inner.AccentColor = null;
78+
79+
return Task.CompletedTask;
5380
}
5481

5582
/// <inheritdoc />
56-
public Task UpdateAccentColorAsync(string? accentColor, CancellationToken cancellationToken)
83+
public async Task UpdateAccentColorAsync(string? accentColor, CancellationToken cancellationToken)
5784
{
58-
throw new NotImplementedException();
85+
bool unset = accentColor is null;
86+
87+
DagCid? valueCid = null;
88+
if (!unset)
89+
{
90+
Cid cid = await Client.Dag.PutAsync(accentColor!, pin: KuboOptions.ShouldPin, cancel: cancellationToken);
91+
valueCid = (DagCid)cid;
92+
}
93+
94+
var updateEvent = new ValueUpdateEvent(Id, nameof(UpdateAccentColorAsync), null, valueCid, unset);
95+
96+
await ApplyEntryUpdateAsync(updateEvent, accentColor, cancellationToken);
97+
var appendedEntry = await AppendNewEntryAsync(updateEvent, cancellationToken);
98+
99+
EventStreamPosition = appendedEntry;
59100
}
60101
}

0 commit comments

Comments
 (0)