Table of Contents

Class SelectableQuestion

Namespace
Tudormobile.OpenTrivia.UI.ViewModels
Assembly
Tudormobile.OpenTrivia.UI.dll

Represents a trivia question that can be selected by the user, encapsulating the question text, possible answers, and selection state.

public class SelectableQuestion : ObservableObject, INotifyPropertyChanged, INotifyPropertyChanging
Inheritance
SelectableQuestion
Implements
Inherited Members

Remarks

This class provides properties to manage the selection of answers and evaluate correctness, facilitating interactive trivia experiences. It is designed for use in user interfaces where questions and answers are presented, allowing users to select answers and receive feedback on their choices. The class ensures that answer choices are randomized and supports notification mechanisms for UI updates when selection state changes. Instances must be initialized with a valid trivia question and a positive question number.

Constructors

SelectableQuestion(int, TriviaQuestion)

Initializes a new instance of the SelectableQuestion class using the specified question number and associated trivia question.

public SelectableQuestion(int questionNumber, TriviaQuestion triviaQuestion)

Parameters

questionNumber int

The unique identifier for the question. Must be a positive integer.

triviaQuestion TriviaQuestion

The trivia question to associate with this selectable question. Cannot be null.

Remarks

Throws an ArgumentNullException if triviaQuestion is null, or an ArgumentOutOfRangeException if questionNumber is less than or equal to zero. The list of possible answers is initialized lazily when first accessed.

Properties

AllAnswers

Gets all possible answers (both correct and incorrect) in a randomized order.

public IReadOnlyList<string> AllAnswers { get; }

Property Value

IReadOnlyList<string>

Remarks

The answers are randomized once upon first access and cached for the lifetime of the instance to ensure consistency. The randomization is performed in a thread-safe manner using lazy initialization. Subsequent accesses return the same randomized list without re-randomizing.

Category

Gets the category to which the trivia question belongs.

public TriviaCategory Category { get; }

Property Value

TriviaCategory

CorrectAnswer

Gets the correct answer for the associated trivia question.

public string CorrectAnswer { get; }

Property Value

string

Remarks

This property retrieves the correct answer from the underlying trivia question. It is read-only and reflects the answer that is considered correct for the current trivia item.

Difficulty

Gets the difficulty level of the trivia question.

public TriviaQuestionDifficulty Difficulty { get; }

Property Value

TriviaQuestionDifficulty

IncorrectAnswers

Gets the collection of incorrect answer choices for the trivia question.

public IReadOnlyList<string> IncorrectAnswers { get; }

Property Value

IReadOnlyList<string>

IsAnswered

Gets a value indicating whether an answer has been selected.

public bool IsAnswered { get; }

Property Value

bool

Remarks

This property returns true if the SelectedAnswer is not null, indicating that the user has provided an answer. Otherwise, it returns false.

IsCorrect

Gets a value indicating whether the selected answer is correct. Returns null if no answer is selected; otherwise, returns true if the selected answer matches the correct answer, and false if it does not.

public bool? IsCorrect { get; }

Property Value

bool?

Remarks

This property evaluates the user's selected answer against the correct answer, providing a nullable boolean result that reflects the correctness of the selection.

IsSelected

[ObservableProperty]
[ExcludeFromCodeCoverage]
public bool IsSelected { get; set; }

Property Value

bool

Question

Gets the question text to be displayed to the user.

public string Question { get; }

Property Value

string

QuestionNumber

Gets the sequential number assigned to the question within the current set.

public int QuestionNumber { get; init; }

Property Value

int

SelectedAnswer

[ObservableProperty]
[NotifyPropertyChangedFor("IsCorrect")]
[NotifyPropertyChangedFor("IsAnswered")]
[ExcludeFromCodeCoverage]
public string? SelectedAnswer { get; set; }

Property Value

string

Type

Gets the type of the trivia question (e.g., multiple choice, true/false).

public TriviaQuestionType Type { get; }

Property Value

TriviaQuestionType