Files
jap_learn/src/quiz.rs
T

151 lines
4.3 KiB
Rust
Raw Normal View History

2026-02-10 12:12:16 +03:00
use crate::lang::KanaSet;
2026-02-11 16:19:23 +03:00
use crate::Page::PreviousPage;
2026-08-01 00:15:28 +03:00
use crate::{jl_text_input, NavigatedPage, Page, RootMessage, DEFAULT_SPACING, USER_FONT};
2026-02-10 12:12:16 +03:00
use iced::widget::*;
2026-02-22 11:07:57 +03:00
use iced::{alignment, Element, Fill, Task};
2026-02-10 17:25:39 +03:00
use rand::seq::SliceRandom;
2026-02-10 12:12:16 +03:00
#[derive(Clone, Debug)]
pub struct QuizState {
kana: String,
current_roman: String,
correct_roman: String,
pub(crate) set: KanaSet,
2026-02-10 17:25:39 +03:00
queue: Vec<(String, String)>,
2026-02-10 12:12:16 +03:00
score: Score,
is_help: bool,
}
impl NavigatedPage<QuizMessage> for QuizState {
fn navigate(&self, message: &QuizMessage) -> Option<Page> {
if let QuizMessage::Back = message {
2026-02-11 16:19:23 +03:00
Some(PreviousPage)
2026-02-10 12:12:16 +03:00
} else {
None
}
}
}
impl QuizState {
pub(crate) fn new() -> QuizState {
QuizState {
kana: "ぁ".to_string(),
correct_roman: "a".to_string(),
is_help: false,
current_roman: "".to_string(),
set: KanaSet::hiragana(),
2026-02-10 17:25:39 +03:00
queue: Vec::new(),
2026-02-10 12:12:16 +03:00
score: Score {
total: 0,
correct: 0,
fail: 0,
},
}
}
}
impl Default for QuizState {
fn default() -> Self {
QuizState::new()
}
}
impl QuizState {
2026-02-22 11:07:57 +03:00
pub fn update(&mut self, message: QuizMessage) -> Task<RootMessage> {
2026-02-10 12:12:16 +03:00
match message {
QuizMessage::ContentChanged(content) => {
if content.contains("`") {
self.is_help = true;
self.score.fail += 1;
2026-02-22 11:07:57 +03:00
return Task::none();
2026-02-10 12:12:16 +03:00
}
self.current_roman = content;
if self.correct_roman == self.current_roman {
if self.is_help == false {
self.score.correct += 1;
}
self.is_help = false;
self.score.total += 1;
self.update_showed()
} else if self.current_roman.len() == self.correct_roman.len() {
self.score.total += 1;
self.score.fail += 1;
self.update_showed()
2026-02-10 12:12:16 +03:00
}
}
QuizMessage::Back => todo!(),
}
2026-02-22 11:07:57 +03:00
Task::none()
2026-02-10 12:12:16 +03:00
}
fn update_showed(&mut self) {
self.current_roman = String::new();
2026-02-10 17:25:39 +03:00
let queue = &mut self.queue;
if queue.is_empty() {
for pair in self.set.list() {
queue.push(pair);
}
queue.shuffle(&mut rand::rng());
}
let pair = self.queue.pop().unwrap();
2026-02-10 12:12:16 +03:00
self.kana = pair.0;
self.correct_roman = pair.1;
}
pub fn view(&self) -> Element<'_, QuizMessage> {
container(
iced::widget::column![
row![
2026-07-31 23:03:40 +03:00
text!("{}", self.kana.to_uppercase()).size(54).font(USER_FONT),
2026-02-10 12:12:16 +03:00
text!(
"{}",
if self.is_help {
self.correct_roman.clone()
} else {
String::new()
}
),
]
.align_y(alignment::Vertical::Center)
.spacing(20),
2026-08-01 00:15:28 +03:00
jl_text_input("Романдзи", &self.current_roman)
2026-02-10 12:12:16 +03:00
.size(28)
.width(150)
.on_input(QuizMessage::ContentChanged),
row![
text!("{}", self.score.total.to_string()).size(25),
text!("{}", self.score.correct.to_string())
.size(25)
.color(iced::Color::from_rgb8(60, 170, 60)),
text!("{}", self.score.fail.to_string())
.color(iced::Color::from_rgb8(255, 79, 0))
.size(25),
]
2026-04-16 09:56:57 +03:00
.spacing(DEFAULT_SPACING),
2026-02-10 12:12:16 +03:00
button("Закончить").on_press(QuizMessage::Back),
]
2026-04-16 09:56:57 +03:00
.spacing(DEFAULT_SPACING)
2026-02-10 12:12:16 +03:00
.align_x(alignment::Horizontal::Center),
)
.center_y(Fill)
2026-02-10 12:12:16 +03:00
.center_x(Fill)
.into()
}
}
#[derive(Debug, Clone)]
pub enum QuizMessage {
ContentChanged(String),
Back,
}
#[derive(Default, Debug, Clone)]
pub struct Score {
2026-02-22 11:07:57 +03:00
pub(crate) total: i32,
pub(crate) correct: i32,
pub(crate) fail: i32,
2026-02-10 12:12:16 +03:00
}