-
-
Notifications
You must be signed in to change notification settings - Fork 1
Initial implementation of virtual event stream handler for accent colors #31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Arlodotexe
merged 5 commits into
main
from
implementation/eventstreamhandlers/virtual/accentColor
Mar 4, 2025
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| using Ipfs; | ||
| using OwlCore.ComponentModel; | ||
| using OwlCore.Kubo; | ||
| using OwlCore.Nomad; | ||
| using OwlCore.Nomad.Kubo; | ||
| using WindowsAppCommunity.Sdk.Models; | ||
|
|
||
| namespace WindowsAppCommunity.Sdk.Nomad; | ||
|
|
||
| /// <inheritdoc cref="IModifiableAccentColor" /> | ||
| public class ModifiableAccentColor : NomadKuboEventStreamHandler<ValueUpdateEvent>, IModifiableAccentColor, IDelegable<ReadOnlyAccentColor> | ||
| { | ||
| /// <inheritdoc /> | ||
| public required ReadOnlyAccentColor Inner { get; init; } | ||
|
|
||
| /// <summary> | ||
| /// A unique identifier for this instance, persistent across machines and reruns. | ||
| /// </summary> | ||
| public required string Id { get; init; } | ||
|
|
||
| /// <inheritdoc /> | ||
| public string? AccentColor => Inner.AccentColor; | ||
|
|
||
| /// <inheritdoc /> | ||
| public event EventHandler<string?>? AccentColorUpdated; | ||
|
|
||
| /// <inheritdoc /> | ||
| public override async Task ApplyEntryUpdateAsync(ValueUpdateEvent updateEvent, CancellationToken cancellationToken) | ||
| { | ||
| cancellationToken.ThrowIfCancellationRequested(); | ||
|
|
||
| if (updateEvent.TargetId != Id) | ||
| return; | ||
|
|
||
| string? accentColor = null; | ||
|
|
||
| if (updateEvent is { Unset: false, Value: not null }) | ||
| { | ||
| (accentColor, _) = await Client.ResolveDagCidAsync<string>(updateEvent.Value!, nocache: !KuboOptions.UseCache, cancellationToken); | ||
| } | ||
|
|
||
| await ApplyEntryUpdateAsync(updateEvent, accentColor, cancellationToken); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Applies an event stream update event and raises the relevant events. | ||
| /// </summary> | ||
| /// <param name="updateEvent">The update event to apply.</param> | ||
| /// <param name="accentColor">The resolved accent color data for this event.</param> | ||
| /// <param name="cancellationToken">A token that can be used to cancel the ongoing operation.</param> | ||
| public Task ApplyEntryUpdateAsync(ValueUpdateEvent updateEvent, string? accentColor, CancellationToken cancellationToken) | ||
| { | ||
| cancellationToken.ThrowIfCancellationRequested(); | ||
|
|
||
| if (updateEvent.EventId is not nameof(UpdateAccentColorAsync)) | ||
| return Task.CompletedTask; | ||
|
|
||
| Inner.Inner.AccentColor = accentColor; | ||
| AccentColorUpdated?.Invoke(this, accentColor); | ||
|
|
||
| return Task.CompletedTask; | ||
| } | ||
|
|
||
| /// <inheritdoc cref="INomadKuboEventStreamHandler{TEventEntryContent}.AppendNewEntryAsync" /> | ||
| public override async Task<EventStreamEntry<Cid>> AppendNewEntryAsync(ValueUpdateEvent updateEvent, CancellationToken cancellationToken = default) | ||
| { | ||
| // Use extension method for code deduplication (can't use inheritance). | ||
Arlodotexe marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| var localUpdateEventCid = await Client.Dag.PutAsync(updateEvent, pin: KuboOptions.ShouldPin, cancel: cancellationToken); | ||
| var newEntry = await this.AppendEventStreamEntryAsync(localUpdateEventCid, updateEvent.EventId, updateEvent.TargetId, cancellationToken); | ||
| return newEntry; | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| public override Task ResetEventStreamPositionAsync(CancellationToken cancellationToken) | ||
| { | ||
| EventStreamPosition = null; | ||
| Inner.Inner.AccentColor = null; | ||
|
|
||
| return Task.CompletedTask; | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| public async Task UpdateAccentColorAsync(string? accentColor, CancellationToken cancellationToken) | ||
| { | ||
| bool unset = accentColor is null; | ||
|
|
||
| DagCid? valueCid = null; | ||
| if (!unset) | ||
Arlodotexe marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| Cid cid = await Client.Dag.PutAsync(accentColor!, pin: KuboOptions.ShouldPin, cancel: cancellationToken); | ||
| valueCid = (DagCid)cid; | ||
| } | ||
|
|
||
| var updateEvent = new ValueUpdateEvent(Id, nameof(UpdateAccentColorAsync), null, valueCid, unset); | ||
|
|
||
| await ApplyEntryUpdateAsync(updateEvent, accentColor, cancellationToken); | ||
| var appendedEntry = await AppendNewEntryAsync(updateEvent, cancellationToken); | ||
|
|
||
| EventStreamPosition = appendedEntry; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| using Ipfs.CoreApi; | ||
| using OwlCore.ComponentModel; | ||
| using WindowsAppCommunity.Sdk.Models; | ||
|
|
||
| namespace WindowsAppCommunity.Sdk.Nomad; | ||
|
|
||
| /// <inheritdoc cref="IReadOnlyAccentColor" /> | ||
| public class ReadOnlyAccentColor : IReadOnlyAccentColor, IDelegable<IAccentColor> | ||
| { | ||
| /// <summary> | ||
| /// The client to use for communicating with ipfs. | ||
| /// </summary> | ||
| public required ICoreApi Client { get; init; } | ||
|
|
||
| /// <inheritdoc /> | ||
| public required IAccentColor Inner { get; init; } | ||
|
|
||
| /// <inheritdoc /> | ||
| public string? AccentColor => Inner.AccentColor; | ||
|
|
||
| /// <inheritdoc /> | ||
| public event EventHandler<string?>? AccentColorUpdated; | ||
|
Check warning on line 22 in src/Nomad/ReadOnlyAccentColor.cs
|
||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.