Replaced the finished/restart flags with proper enum states and made the restarts functional

master
Wynd 2025-06-18 16:08:46 +03:00
parent fe7f626c01
commit 92983da8fa
2 changed files with 83 additions and 35 deletions

View File

@ -7,8 +7,8 @@ use dioxus::desktop::{Config, LogicalSize, WindowBuilder};
use dioxus::{logger::tracing, prelude::*}; use dioxus::{logger::tracing, prelude::*};
#[cfg(feature = "bundled")] #[cfg(feature = "bundled")]
use include_dir::{Dir, include_dir}; use include_dir::{Dir, include_dir};
use models::{Question, Questions}; use models::{Phase, Question, QuestionState, Questions};
use rand::seq::SliceRandom; use rand::{rngs::ThreadRng, seq::SliceRandom};
#[cfg(feature = "desktop")] #[cfg(feature = "desktop")]
use std::{env, fs}; use std::{env, fs};
#[cfg(not(feature = "web"))] #[cfg(not(feature = "web"))]
@ -78,14 +78,12 @@ fn get_questions() -> Questions {
.clone() .clone()
} }
fn get_rand_questions() -> Vec<Question> { fn get_rand_questions() -> Questions {
let mut questions = get_questions().questions; let mut questions = get_questions();
let mut rng = rand::rng(); let mut rng = rand::rng();
// Randomize the answers // Randomize the answers
questions randomize_answers(&mut rng, &mut questions);
.iter_mut()
.for_each(|q| q.answers.shuffle(&mut rng));
// Randomize the questions list // Randomize the questions list
questions.shuffle(&mut rng); questions.shuffle(&mut rng);
@ -93,6 +91,10 @@ fn get_rand_questions() -> Vec<Question> {
questions questions
} }
fn randomize_answers(rng: &mut ThreadRng, questions: &mut [Question]) {
questions.iter_mut().for_each(|q| q.answers.shuffle(rng));
}
fn main() { fn main() {
#[cfg(feature = "desktop")] #[cfg(feature = "desktop")]
{ {
@ -122,41 +124,59 @@ fn App() -> Element {
let mut correct_questions = use_signal(|| 0); let mut correct_questions = use_signal(|| 0);
let mut wrong_questions = use_signal(|| 0); let mut wrong_questions = use_signal(|| 0);
let mut has_finished = use_signal(|| false); let mut phase = use_signal(|| Phase::Running);
let mut has_restart = use_signal(|| false); let mut next_phase = use_signal(|| Phase::Running);
use_effect(move || { use_effect(move || {
let has_restart = has_restart(); let current_phase = phase();
if has_restart { match current_phase {
let mut questions_lock = questions.write(); Phase::Restart => {
let mut questions_lock = questions.write();
*questions_lock = get_rand_questions(); *questions_lock = get_rand_questions();
correct_questions.set(0); correct_questions.set(0);
wrong_questions.set(0); wrong_questions.set(0);
has_finished.set(false); next_phase.set(Phase::Running);
}
Phase::RestartWrongs => {
let mut questions_lock = questions.write();
let wrongs = questions_lock.wrong_questions.to_vec();
questions_lock.questions = wrongs;
questions_lock.wrong_questions.clear();
let mut rng = rand::rng();
randomize_answers(&mut rng, &mut questions_lock);
correct_questions.set(0);
wrong_questions.set(0);
next_phase.set(Phase::Running);
}
_ => {}
} }
}); });
// I am not a fan of this..._reactivity_
use_effect(move || { use_effect(move || {
let has_finished = has_finished(); let next_phase = next_phase();
if !has_finished { phase.set(next_phase);
has_restart.set(false);
}
}); });
rsx! { rsx! {
document::Link { rel: "stylesheet", href: MAIN_CSS } document::Link { rel: "stylesheet", href: MAIN_CSS }
if !has_finished() { if phase() == Phase::Running {
QuestionForm { questions, correct_questions, wrong_questions, has_finished, has_restart } QuestionForm { questions, correct_questions, wrong_questions, phase }
} }
else if has_finished() { else if phase() != Phase::Running {
Report { Report {
questions: questions(), questions: questions(),
correct_questions: correct_questions(), correct_questions: correct_questions(),
wrong_questions: wrong_questions(), wrong_questions: wrong_questions(),
has_restart, phase,
} }
} }
} }
@ -165,13 +185,12 @@ fn App() -> Element {
#[allow(clippy::redundant_closure)] #[allow(clippy::redundant_closure)]
#[component] #[component]
pub fn QuestionForm( pub fn QuestionForm(
questions: Signal<Vec<Question>>, questions: Signal<Questions>,
correct_questions: Signal<i32>, correct_questions: Signal<i32>,
wrong_questions: Signal<i32>, wrong_questions: Signal<i32>,
has_finished: Signal<bool>, phase: Signal<Phase>,
has_restart: Signal<bool>,
) -> Element { ) -> Element {
let mut current = use_signal(move || questions()[0].clone()); let mut current = use_signal(move || Question::default());
use_effect(move || { use_effect(move || {
let _correct_count = correct_questions(); let _correct_count = correct_questions();
@ -180,7 +199,7 @@ pub fn QuestionForm(
let mut questions_lock = questions.write(); let mut questions_lock = questions.write();
if questions_lock.is_empty() { if questions_lock.is_empty() {
has_finished.set(true); phase.set(Phase::Finished);
} else { } else {
let new_question = questions_lock.remove(0); let new_question = questions_lock.remove(0);
current.set(new_question); current.set(new_question);
@ -223,6 +242,7 @@ pub fn QuestionForm(
sleep(Duration::from_millis(600)).await; sleep(Duration::from_millis(600)).await;
let mut current_lock = current.write(); let mut current_lock = current.write();
let mut questions_lock = questions.write();
let mut correct_questions_lock = correct_questions.write(); let mut correct_questions_lock = correct_questions.write();
let mut wrong_questions_lock = wrong_questions.write(); let mut wrong_questions_lock = wrong_questions.write();
@ -237,6 +257,8 @@ pub fn QuestionForm(
*correct_questions_lock += 1; *correct_questions_lock += 1;
} else if is_wrong { } else if is_wrong {
*wrong_questions_lock += 1; *wrong_questions_lock += 1;
let current_question = current_lock.clone();
questions_lock.wrong_questions.push(current_question);
} }
// Reset animation flags // Reset animation flags
@ -353,7 +375,7 @@ pub fn Result(is_correct: bool, is_wrong: bool) -> Element {
#[allow(clippy::redundant_closure)] #[allow(clippy::redundant_closure)]
#[component] #[component]
pub fn Info( pub fn Info(
questions: Signal<Vec<Question>>, questions: Signal<Questions>,
correct_questions: Signal<i32>, correct_questions: Signal<i32>,
wrong_questions: Signal<i32>, wrong_questions: Signal<i32>,
) -> Element { ) -> Element {
@ -381,10 +403,10 @@ pub fn Info(
#[component] #[component]
pub fn Report( pub fn Report(
questions: Vec<Question>, questions: Questions,
correct_questions: i32, correct_questions: i32,
wrong_questions: i32, wrong_questions: i32,
has_restart: Signal<bool>, phase: Signal<Phase>,
) -> Element { ) -> Element {
let questions_size = questions.len(); let questions_size = questions.len();
@ -404,11 +426,14 @@ pub fn Report(
div { div {
button { button {
onclick: move |_| { onclick: move |_| {
has_restart.set(true); phase.set(Phase::Restart);
}, },
"Restart" "Restart"
}, },
button { button {
onclick: move |_| {
phase.set(Phase::RestartWrongs);
},
"Restart with only wrong questions" "Restart with only wrong questions"
} }
} }

View File

@ -2,9 +2,21 @@ use std::ops::{AddAssign, Deref, DerefMut};
use serde::Deserialize; use serde::Deserialize;
#[derive(Debug, Default, Clone, Deserialize)] #[derive(Debug, Default, Clone, PartialEq)]
pub enum Phase {
#[default]
Running,
Finished,
Restart,
RestartWrongs,
}
#[derive(Debug, Default, Clone, PartialEq, Deserialize)]
pub struct Questions { pub struct Questions {
pub questions: Vec<Question>, pub questions: Vec<Question>,
#[serde(default, skip)]
pub wrong_questions: Vec<Question>,
} }
impl AddAssign for Questions { impl AddAssign for Questions {
@ -27,10 +39,21 @@ impl DerefMut for Questions {
} }
} }
#[derive(Debug, Clone, PartialEq, Deserialize)] #[derive(Debug, Default, Clone, PartialEq)]
pub enum QuestionState {
#[default]
None,
Correct,
Wrong,
}
#[derive(Debug, Default, Clone, PartialEq, Deserialize)]
pub struct Question { pub struct Question {
pub message: String, pub message: String,
pub answers: Vec<Answer>, pub answers: Vec<Answer>,
#[serde(default, skip)]
pub state: QuestionState,
} }
#[derive(Debug, Clone, PartialEq, Deserialize)] #[derive(Debug, Clone, PartialEq, Deserialize)]