Files
jap_learn/src/pages/quiz.rs
T

163 lines
4.4 KiB
Rust
Raw Normal View History

2026-08-25 12:04:07 +03:00
use crate::app::USER_FONT;
use crate::core::kana_set::KanaSet;
2026-08-01 13:04:18 +03:00
use crate::navigation::Page::PreviousPage;
use crate::navigation::{NavigatedPage, Page, RootMessage};
2026-08-24 01:22:00 +03:00
use crate::pages::quiz::QuizMessage::*;
2026-08-01 13:04:18 +03:00
use crate::styling::*;
2026-08-01 19:43:07 +03:00
use iced::widget::*;
2026-08-15 21:32:24 +03:00
use iced::{Element, Fill, Task, alignment};
2026-08-01 19:43:07 +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 {
2026-08-18 12:14:35 +03:00
fn navigate(&mut self, message: &QuizMessage) -> Option<Page> {
2026-08-01 19:36:43 +03:00
if let Back = message {
2026-02-11 16:19:23 +03:00
Some(PreviousPage)
2026-02-10 12:12:16 +03:00
} else {
None
}
}
2026-08-06 23:20:14 +03:00
2026-08-15 21:32:24 +03:00
fn navigated(&mut self) {}
2026-02-10 12:12:16 +03:00
2026-08-13 22:03:47 +03:00
fn update(&mut self, message: QuizMessage) -> Task<RootMessage> {
2026-02-10 12:12:16 +03:00
match message {
2026-08-01 19:36:43 +03:00
ContentChanged(content) => {
2026-02-10 12:12:16 +03:00
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 {
2026-08-15 13:22:59 +03:00
if !self.is_help {
2026-02-10 12:12:16 +03:00
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
}
}
2026-08-19 23:51:41 +03:00
Back => {}
2026-02-10 12:12:16 +03:00
}
2026-02-22 11:07:57 +03:00
Task::none()
2026-02-10 12:12:16 +03:00
}
2026-08-13 22:03:47 +03:00
fn view(&self) -> Element<'_, QuizMessage> {
2026-02-10 12:12:16 +03:00
container(
iced::widget::column![
row![
2026-08-04 14:09:40 +03:00
text!("{}", self.kana.to_lowercase())
2026-08-01 19:43:07 +03:00
.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 01:22:26 +03:00
text_input("Романдзи", &self.current_roman)
2026-02-10 12:12:16 +03:00
.size(28)
.width(150)
2026-08-01 19:36:43 +03:00
.on_input(ContentChanged),
2026-08-15 13:22:59 +03:00
score_display(&self.score),
2026-08-01 19:36:43 +03:00
button("Закончить").style(jl_button).on_press(Back),
2026-02-10 12:12:16 +03:00
]
2026-08-15 21:32:24 +03:00
.spacing(DEFAULT_SPACING)
.align_x(alignment::Horizontal::Center),
2026-02-10 12:12:16 +03:00
)
2026-08-15 21:32:24 +03:00
.center_y(Fill)
.center_x(Fill)
.into()
2026-08-13 22:03:47 +03:00
}
}
2026-08-15 13:22:59 +03:00
fn score_display<'a, T: 'a>(score: &Score) -> Element<'a, T> {
row![
2026-08-15 21:32:24 +03:00
text!("{}", score.total.to_string()).size(25),
text!("{}", score.correct.to_string())
.size(25)
.color(iced::Color::from_rgb8(60, 170, 60)),
text!("{}", score.fail.to_string())
.color(iced::Color::from_rgb8(255, 79, 0))
.size(25),
]
.spacing(DEFAULT_SPACING)
.into()
2026-08-15 13:22:59 +03:00
}
2026-08-13 22:03:47 +03:00
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(),
queue: Vec::new(),
score: Score {
total: 0,
correct: 0,
fail: 0,
},
}
}
}
impl Default for QuizState {
fn default() -> Self {
QuizState::new()
}
}
impl QuizState {
fn update_showed(&mut self) {
self.current_roman = String::new();
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();
self.kana = pair.0;
self.correct_roman = pair.1;
2026-02-10 12:12:16 +03:00
}
}
#[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
}