Skip to content

Commit

Permalink
📖 update code sample for interactive element
Browse files Browse the repository at this point in the history
  • Loading branch information
MorganKryze committed Mar 23, 2024
1 parent 5e3e5cb commit 0cb694b
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 11 deletions.
2 changes: 1 addition & 1 deletion docs/articles/create_element.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ The method that you can override are the same as the `Element` class at some exc

The callable attributes and methods are **highlighted in yellow** here:

[!code-csharp[](../assets/code/InteractiveElement.cs?highlight=23,28,62)]
[!code-csharp[](../assets/code/InteractiveElement.cs?highlight=23,28,59)]

Two new methods are available and cannot be modified:

Expand Down
58 changes: 48 additions & 10 deletions docs/assets/code/InteractiveElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
GNU GPL License 2024 MorganKryze(Yann Vidamment)
For full license information, please visit: https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/LICENSE
*/
namespace ConsoleAppVisuals;
namespace ConsoleAppVisuals.Models;

/// <summary>
/// Defines the basic properties of an console element.
Expand All @@ -11,7 +11,7 @@ namespace ConsoleAppVisuals;
/// For more information, refer to the following resources:
/// <list type="bullet">
/// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
/// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/Program.cs">Example Project</a></description></item>
/// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
/// </list>
/// </remarks>
public abstract class InteractiveElement<TResponse> : Element
Expand All @@ -28,19 +28,14 @@ public abstract class InteractiveElement<TResponse> : Element
public sealed override int MaxNumberOfThisElement { get; } = 1;

/// <summary>
/// The event that is triggered when the user has made a choice.
/// The event that is triggered when the user has interacted with the element.
/// </summary>
public event EventHandler<InteractionEventArgs<TResponse>>? EndOfInteractionEvent;

/// <summary>
/// The response of the user.
/// </summary>
protected InteractionEventArgs<TResponse>? _interactionResponse;

/// <summary>
/// Returns the response of the user.
/// </summary>
public InteractionEventArgs<TResponse>? GetInteractionResponse => _interactionResponse;
protected List<InteractionEventArgs<TResponse>?> _interactionResponse = new();
#endregion

#region Methods
Expand All @@ -49,24 +44,65 @@ public abstract class InteractiveElement<TResponse> : Element
/// </summary>
/// <param name="sender">The sender of the event.</param>
/// <param name="e">The response of the user.</param>
[Visual]
protected void SetInteractionResponse(object? sender, InteractionEventArgs<TResponse> e)
{
_interactionResponse = e;
_interactionResponse.Add(e);
}

/// <summary>
/// Triggers the EndOfInteractionEvent event.
/// </summary>
/// <param name="sender">The sender of the event.</param>
/// <param name="e">The response of the user.</param>
[Visual]
protected void SendResponse(object? sender, InteractionEventArgs<TResponse> e)
{
EndOfInteractionEvent?.Invoke(sender, e);
}

/// <summary>
/// Returns the response of the user after an interaction.
/// </summary>
/// <returns>Null if the user has not interacted with the element, otherwise the response of the user.</returns>
/// <remarks>
/// This sample shows how to use the <see cref="GetResponse"/> method using the var keyword:
/// <code>
/// var response = element.GetResponse();
/// </code>
/// For more information, refer to the following resources:
/// <list type="bullet">
/// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
/// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
/// </list>
/// </remarks>
[Visual]
public InteractionEventArgs<TResponse>? GetResponse()
{
return _interactionResponse.LastOrDefault();
}

/// <summary>
/// Returns the history of the responses of the user after interactions.
/// </summary>
/// <returns>The history of the responses of the user after interactions.</returns>
/// <remarks>
/// For more information, refer to the following resources:
/// <list type="bullet">
/// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
/// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
/// </list>
/// </remarks>
[Visual]
public List<InteractionEventArgs<TResponse>?> GetResponseHistory()
{
return _interactionResponse;
}

/// <summary>
/// This method is used to set options before drawing the element on the console.
/// </summary>
[Visual]
protected sealed override void RenderOptionsBeforeHand()
{
EndOfInteractionEvent += SetInteractionResponse;
Expand All @@ -75,9 +111,11 @@ protected sealed override void RenderOptionsBeforeHand()
/// <summary>
/// This method is used to set options after drawing the element on the console.
/// </summary>
[Visual]
protected sealed override void RenderOptionsAfterHand()
{
EndOfInteractionEvent -= SetInteractionResponse;
Window.DeactivateElement(this);
}
#endregion
}

0 comments on commit 0cb694b

Please sign in to comment.