Skip to content

Commit

Permalink
Support stacked content and answer options in question play
Browse files Browse the repository at this point in the history
  • Loading branch information
VladimirKhil committed Nov 19, 2023
1 parent bc8f27e commit 1e40455
Show file tree
Hide file tree
Showing 6 changed files with 212 additions and 92 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using SIEngine.Core;
using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace SIQuester.ViewModel.Workspaces.Dialogs.Play;

/// <summary>
/// Defines answer option view model.
/// </summary>
/// <param name="Label">Option label.</param>
/// <param name="Content">Option content.</param>
public sealed record AnswerOptionViewModel(string Label, ContentInfo Content) : INotifyPropertyChanged
{
private bool _isSelected;

/// <summary>
/// Is option selected.
/// </summary>
public bool IsSelected
{
get => _isSelected;
set
{
if (_isSelected != value)
{
_isSelected = value;
OnPropertyChanged();
}
}
}

public event PropertyChangedEventHandler? PropertyChanged;

private void OnPropertyChanged([CallerMemberName] string? propertyName = null) =>
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace SIQuester.ViewModel.Workspaces.Dialogs.Play;

/// <summary>
/// Defines content info.
/// </summary>
/// <param name="Type">Content type.</param>
/// <param name="Value">Content value.</param>
public sealed record ContentInfo(ContentType Type, string Value);
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/// <summary>
/// Defines question table content types.
/// </summary>
public enum ContentTypes
public enum ContentType
{
/// <summary>
/// Empty content.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using SIEngine.Core;
using SIPackages;
using SIPackages.Core;
using SIQuester.ViewModel.Workspaces.Dialogs.Play;
using Utils.Commands;

namespace SIQuester.ViewModel.Workspaces.Dialogs;
Expand All @@ -17,30 +18,12 @@ public sealed class QuestionPlayViewModel : WorkspaceViewModel, IQuestionEngineP

public override string Header => Properties.Resources.QuestionPlay;

private Play.ContentTypes _contentType = Dialogs.Play.ContentTypes.None;

/// <summary>
/// Current question content type.
/// </summary>
public Play.ContentTypes ContentType
{
get => _contentType;
set
{
if (_contentType != value)
{
_contentType = value;
OnPropertyChanged();
}
}
}

private string? _content;
private ContentInfo[] _content = Array.Empty<ContentInfo>();

/// <summary>
/// Current question content.
/// </summary>
public string? Content
public ContentInfo[] Content
{
get => _content;
set
Expand Down Expand Up @@ -97,6 +80,24 @@ public string? Oral
}
}

private AnswerOptionViewModel[] _answerOptions = Array.Empty<AnswerOptionViewModel>();

/// <summary>
/// Answer options.
/// </summary>
public AnswerOptionViewModel[] AnswerOptions
{
get => _answerOptions;
set
{
if (_answerOptions != value)
{
_answerOptions = value;
OnPropertyChanged();
}
}
}

private bool _isAnswer;

/// <summary>
Expand Down Expand Up @@ -157,7 +158,8 @@ public void Play_Executed(object? arg)
_isFinished = true;
Play.CanBeExecuted = false;
Sound = null;
ContentType = Dialogs.Play.ContentTypes.None;
Content = Array.Empty<ContentInfo>();
AnswerOptions = Array.Empty<AnswerOptionViewModel>();
return;
}

Expand All @@ -167,59 +169,52 @@ public void Play_Executed(object? arg)

public void OnQuestionContent(IReadOnlyCollection<ContentItem> content)
{
var screenContent = new List<ContentInfo>();

foreach (var contentItem in content)
{
OnQuestionContentItem(contentItem);
switch (contentItem.Placement)
{
case ContentPlacements.Replic:
Oral = contentItem.Value;
break;

case ContentPlacements.Screen:
switch (contentItem.Type)
{
case ContentTypes.Text:
screenContent.Add(new ContentInfo(ContentType.Text, contentItem.Value));
break;

case ContentTypes.Image:
screenContent.Add(new ContentInfo(ContentType.Image, contentItem.IsRef ? _qDocument.Images.Wrap(contentItem.Value).Uri : contentItem.Value));
break;

case ContentTypes.Video:
screenContent.Add(new ContentInfo(ContentType.Video, contentItem.IsRef ? _qDocument.Video.Wrap(contentItem.Value).Uri : contentItem.Value));
break;

case ContentTypes.Html:
screenContent.Add(new ContentInfo(ContentType.Html, contentItem.IsRef ? _qDocument.Html.Wrap(contentItem.Value).Uri : contentItem.Value));
break;

default:
break;
}
break;

case ContentPlacements.Background:
Sound = contentItem.IsRef ? _qDocument.Audio.Wrap(contentItem.Value).Uri : contentItem.Value;
break;

default:
break;
}
}
}

public void OnQuestionContentItem(ContentItem contentItem)
{
switch (contentItem.Placement)
if (screenContent.Count > 0)
{
case ContentPlacements.Replic:
Oral = contentItem.Value;
break;

case ContentPlacements.Screen:
switch (contentItem.Type)
{
case ContentTypes.Text:
Content = contentItem.Value;
ContentType = Dialogs.Play.ContentTypes.Text;
break;

case ContentTypes.Image:
Content = contentItem.IsRef ? _qDocument.Images.Wrap(contentItem.Value).Uri : contentItem.Value;
ContentType = Dialogs.Play.ContentTypes.Image;
break;

case ContentTypes.Video:
Content = contentItem.IsRef ? _qDocument.Video.Wrap(contentItem.Value).Uri : contentItem.Value;
ContentType = Dialogs.Play.ContentTypes.Video;
break;

case ContentTypes.Html:
Content = contentItem.IsRef ? _qDocument.Html.Wrap(contentItem.Value).Uri : contentItem.Value;
ContentType = Dialogs.Play.ContentTypes.Html;
break;

default:
break;
}
break;

case ContentPlacements.Background:
Sound = contentItem.IsRef ? _qDocument.Audio.Wrap(contentItem.Value).Uri : contentItem.Value;

if (ContentType == Dialogs.Play.ContentTypes.None)
{
ContentType = Dialogs.Play.ContentTypes.Audio;
}
break;

default:
break;
Content = screenContent.ToArray();
}
}

Expand Down Expand Up @@ -264,7 +259,42 @@ public void OnAskAnswerStop()

public bool OnAnnouncePrice(NumberSet? availableRange) => false;

public bool OnAnswerOptions(AnswerOption[] answerOptions) => false;
public bool OnAnswerOptions(AnswerOption[] answerOptions)
{
var options = new List<AnswerOptionViewModel>();

foreach (var option in answerOptions)
{
switch (option.Content.Type)
{
case ContentTypes.Text:
options.Add(new AnswerOptionViewModel(option.Label, new ContentInfo(ContentType.Text, option.Content.Value)));
break;

public bool OnRightAnswerOption(string rightOptionLabel) => false;
case ContentTypes.Image:
options.Add(new AnswerOptionViewModel(option.Label, new ContentInfo(ContentType.Image, option.Content.IsRef ? _qDocument.Images.Wrap(option.Content.Value).Uri : option.Content.Value)));
break;

default:
break;
}
}

AnswerOptions = options.ToArray();
return false;
}

public bool OnRightAnswerOption(string rightOptionLabel)
{
foreach (var option in AnswerOptions)
{
if (option.Label == rightOptionLabel)
{
option.IsSelected = true;
break;
}
}

return true;
}
}
Loading

0 comments on commit 1e40455

Please sign in to comment.